Private
Public Access
2
0

java: Added Java SWIG binding creation

%init directive is not supported in java so move %init to js/py interface files

Signed-off-by: Alexander Komarov <alexander.komarov@intel.com>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
Alexander Komarov
2015-04-13 13:23:13 +00:00
committed by Brendan Le Foll
parent c5780dceaa
commit 4302f0d0c3
12 changed files with 303 additions and 5 deletions

View File

@@ -53,6 +53,7 @@ option (BUILDDOC "Build all doc." OFF)
option (BUILDSWIG "Build swig modules." ON)
option (BUILDSWIGPYTHON "Build swig python modules." ON)
option (BUILDSWIGNODE "Build swig node modules." ON)
option (BUILDSWIGJAVA "Build Java API." OFF)
option (IPK "Generate IPK using CPack" OFF)
option (BUILDPYTHON3 "Use python3 for building/installing" OFF)
option (INSTALLGPIOTOOL "Install gpio tool" OFF)

View File

@@ -68,6 +68,30 @@ typedef enum {
EDGE_FALLING = 3 /**< Interupt on falling only */
} Edge;
#if defined(SWIGJAVA)
class IsrCallback
{
public:
virtual ~IsrCallback()
{
}
virtual void
run()
{ /* empty, overloaded in Java*/
}
private:
};
void
generic_isr_callback(void* data)
{
IsrCallback* callback = (IsrCallback*) data;
callback->run();
}
#endif
/**
* @brief API to General Purpose IO
*
@@ -171,6 +195,12 @@ class Gpio
#endif
return mraa_gpio_isr(m_gpio, (gpio_edge_t) mode, &uvwork, this);
}
#elif defined(SWIGJAVA)
mraa_result_t
isr(Edge mode, IsrCallback* cb, void* args)
{
return mraa_gpio_isr(m_gpio, (gpio_edge_t) mode, generic_isr_callback, cb);
}
#else
/**
* Sets a callback to be called when pin value changes

View File

@@ -115,3 +115,15 @@ cmake -DBUILDDOC=OFF -DBUILDSWIG=OFF ..
cov-build --dir cov-int make
tar caf mraa.tar.bz2 cov-int
~~~~~~~~~~~~~
## Building Java bindings
Have JAVA_HOME set to JDK install directory. Then use the cmake configuration flag:
-DBUILDSWIGJAVA=ON
To compile Example.java
~~~~~~~~~~~~~{.sh}
javac -cp $DIR_WHERE_YOU_INSTALLED_MRAA/mraa.jar:. Example.java
~~~~~~~~~~~~~
To run, make sure libmraajava.so is in LD_LIBRARY_PATH
~~~~~~~~~~~~~{.sh}
jave -cp $DIR_WHERE_YOU_INSTALLED_MRAA/mraa.jar:. Example
~~~~~~~~~~~~~

75
examples/java/bmp85.java Normal file
View File

@@ -0,0 +1,75 @@
/*
* Author: Alexander Komarov <alexander.komarov@intel.com>
* Copyright (c) 2014 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
public class bmp85 {
static {
try {
System.loadLibrary("mraajava");
} catch (UnsatisfiedLinkError e) {
System.err.println(
"Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" +
e);
System.exit(1);
}
}
public static void main(String argv[]) {
mraa.mraa.init();
System.out.println(mraa.mraa.getVersion());
// helper function to go from hex val to dec
// function char(x) { return parseInt(x, 16); }
mraa.I2c i2c = new mraa.I2c(0);
i2c.address((byte)0x77);
i2c.writeByte((byte)0xd0);
/*
SWIGTYPE_p_unsigned_char data0 = new SWIGTYPE_p_unsigned_char();*/
byte[] data = new byte[1];
i2c.read(data);
System.out.println((new Integer(data[0])).toString());
i2c.writeReg((byte)0xf4, (byte)0x2e);
// initialise device
if (i2c.readReg((byte)0xd0) != 0x55) {
System.out.println("error");
}
// we want to read temperature so write 0x2e into control reg
i2c.writeReg((byte)0xf4, (byte)0x2e);
// read a 16bit reg, obviously it's uncalibrated so mostly a useless value
// :)
System.out.println(i2c.readWordReg((byte)0xf6));
byte[] buf = new byte[2];
buf[0] = (byte)0xf4;
buf[1] = (byte)0x2e;
i2c.write(buf);
i2c.writeByte((byte)0xf6);
int d = i2c.readReg((byte)2);
System.out.println((new Integer(d)).toString());
};
}
;

View File

@@ -0,0 +1,41 @@
/*
* Author: Alexander Komarov <alexander.komarov@intel.com>
* Copyright (c) 2014 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
public class Example {
static {
try {
System.loadLibrary("mraajava");
} catch (UnsatisfiedLinkError e) {
System.err.println(
"Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" +
e);
System.exit(1);
}
}
public static void main(String argv[]) {
mraa.mraa.init();
System.out.println(mraa.mraa.getVersion());
};
}
;

54
examples/java/isr.java Normal file
View File

@@ -0,0 +1,54 @@
/*
* Author: Alexander Komarov <alexander.komarov@intel.com>
* Copyright (c) 2014 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
public class Isr {
static {
try {
System.loadLibrary("mraajava");
} catch (UnsatisfiedLinkError e) {
System.err.println(
"Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" +
e);
System.exit(1);
}
}
public static void main(String argv[]) {
mraa.mraa.init();
mraa.Gpio gpio = new mraa.Gpio(7);
mraa.IsrCallback callback = new JavaCallback();
gpio.isr(mraa.Edge.EDGE_RISING, callback, null);
while (true)
;
};
}
;
class JavaCallback extends mraa.IsrCallback {
public JavaCallback() { super(); }
public void run() { System.out.println("JavaCallback.run()"); }
}

View File

@@ -105,6 +105,9 @@ if (BUILDSWIG)
if (BUILDSWIGPYTHON)
add_subdirectory (python)
endif ()
if (BUILDSWIGJAVA)
add_subdirectory (java)
endif ()
if (BUILDSWIGNODE)
if (SWIG_VERSION VERSION_GREATER 3.0.4)
add_subdirectory (javascript)

39
src/java/CMakeLists.txt Normal file
View File

@@ -0,0 +1,39 @@
FIND_PACKAGE (JNI REQUIRED)
include_directories (
${JAVA_INCLUDE_PATH}
${JAVA_INCLUDE_PATH2}
${CMAKE_CURRENT_SOURCE_DIR}/..
)
# SWIG treats SWIG_FLAGS as a list and not a string so semicolon seperation is required
set_source_files_properties (mraajava.i PROPERTIES SWIG_FLAGS ";-package;mraa;-I${CMAKE_BINARY_DIR}/src")
set_source_files_properties (mraajava.i PROPERTIES CPLUSPLUS ON)
set (CMAKE_CXX_FLAGS "-fpermissive")
set (JAVAC $ENV{JAVA_HOME}/bin/javac)
set (JAR $ENV{JAVA_HOME}/bin/jar)
swig_add_module (mraajava java mraajava.i ${mraa_LIB_SRCS})
swig_link_libraries (mraajava ${JAVA_LIBRARIES})
add_custom_command (TARGET mraajava
POST_BUILD
COMMAND cmake -E echo "Compiling java.."
COMMAND cmake -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/mraa
COMMAND ${JAVAC} *.java -d ${CMAKE_CURRENT_BINARY_DIR}
COMMAND cmake -E echo "Creating jar"
COMMAND ${JAR} cvf mraa.jar mraa
)
if (DOXYGEN_FOUND)
foreach (_file ${DOCFILES})
add_dependencies (${SWIG_MODULE_mraajava_REAL_NAME} ${_file}doc_i)
endforeach ()
endif ()
install (FILES ${CMAKE_CURRENT_BINARY_DIR}/mraa.jar
${CMAKE_CURRENT_BINARY_DIR}/libmraajava.so
DESTINATION lib
)

39
src/java/mraajava.i Normal file
View File

@@ -0,0 +1,39 @@
%module (directors="1",docstring="Java interface to libmraa") mraa
%feature("autodoc", "3");
%typemap(jtype) (uint8_t *txBuf, int length) "byte[]"
%typemap(jstype) (uint8_t *txBuf, int length) "byte[]"
%typemap(jni) (uint8_t *txBuf, int length) "jbyteArray"
%typemap(javain) (uint8_t *txBuf, int length) "$javainput"
%typemap(in,numinputs=1) (uint8_t *txBuf, int length) {
$1 = JCALL2(GetByteArrayElements, jenv, $input, NULL);
$2 = JCALL1(GetArrayLength, jenv, $input);
}
%typemap(jtype) (uint8_t *data, int length) "byte[]"
%typemap(jstype) (uint8_t *data, int length) "byte[]"
%typemap(jni) (uint8_t *data, int length) "jbyteArray"
%typemap(javain) (uint8_t *data, int length) "$javainput"
%typemap(in,numinputs=1) (uint8_t *data, int length) {
$1 = JCALL2(GetByteArrayElements, jenv, $input, NULL);
$2 = JCALL1(GetArrayLength, jenv, $input);
}
%typemap(argout) (uint8_t *data, int length) {
JCALL3(ReleaseByteArrayElements, jenv, $input, $1, JNI_COMMIT);
}
%typemap(jtype) (const uint8_t *data, int length) "byte[]"
%typemap(jstype) (const uint8_t *data, int length) "byte[]"
%typemap(jni) (const uint8_t *data, int length) "jbyteArray"
%typemap(javain) (const uint8_t *data, int length) "$javainput"
%typemap(in) (const uint8_t *data, int length) {
$1 = JCALL2(GetByteArrayElements, jenv, $input, NULL);
$2 = JCALL1(GetArrayLength, jenv, $input);
}
%feature("director") IsrCallback;
%include ../mraa.i

View File

@@ -68,3 +68,8 @@ class Spi;
}
%include ../mraa.i
%init %{
//Adding mraa_init() to the module initialisation process
mraa_init();
%}

View File

@@ -27,11 +27,6 @@
#include "uart.hpp"
%}
%init %{
//Adding mraa_init() to the module initialisation process
mraa_init();
%}
%exception {
try {
$action

View File

@@ -106,3 +106,7 @@ class Spi;
%include ../mraa.i
%init %{
//Adding mraa_init() to the module initialisation process
mraa_init();
%}