java: Added Java SWIG binding creation
authorAlexander Komarov <alexander.komarov@intel.com>
Mon, 13 Apr 2015 13:23:13 +0000 (13:23 +0000)
committerBrendan Le Foll <brendan.le.foll@intel.com>
Fri, 24 Apr 2015 11:12:11 +0000 (12:12 +0100)
%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>
12 files changed:
CMakeLists.txt
api/mraa/gpio.hpp
docs/building.md
examples/java/bmp85.java [new file with mode: 0644]
examples/java/example.java [new file with mode: 0644]
examples/java/isr.java [new file with mode: 0644]
src/CMakeLists.txt
src/java/CMakeLists.txt [new file with mode: 0644]
src/java/mraajava.i [new file with mode: 0644]
src/javascript/mraajs.i
src/mraa.i
src/python/mraa.i

index 96c8eda..8906d9a 100644 (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 (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)
 option (IPK "Generate IPK using CPack" OFF)
 option (BUILDPYTHON3 "Use python3 for building/installing" OFF)
 option (INSTALLGPIOTOOL "Install gpio tool" OFF)
index a5dc24b..a0c2f0d 100644 (file)
@@ -68,6 +68,30 @@ typedef enum {
     EDGE_FALLING = 3 /**< Interupt on falling only */
 } Edge;
 
     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
  *
 /**
  * @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);
     }
 #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
 #else
     /**
      * Sets a callback to be called when pin value changes
index 8240600..2fc1163 100644 (file)
@@ -115,3 +115,15 @@ cmake -DBUILDDOC=OFF -DBUILDSWIG=OFF ..
 cov-build --dir cov-int make
 tar caf mraa.tar.bz2 cov-int
 ~~~~~~~~~~~~~
 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
+~~~~~~~~~~~~~
diff --git a/examples/java/bmp85.java b/examples/java/bmp85.java
new file mode 100644 (file)
index 0000000..e71544e
--- /dev/null
@@ -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());
+  };
+}
+;
diff --git a/examples/java/example.java b/examples/java/example.java
new file mode 100644 (file)
index 0000000..7d25da3
--- /dev/null
@@ -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());
+  };
+}
+;
diff --git a/examples/java/isr.java b/examples/java/isr.java
new file mode 100644 (file)
index 0000000..f50be90
--- /dev/null
@@ -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()"); }
+}
index b9f986a..588f5bf 100644 (file)
@@ -105,6 +105,9 @@ if (BUILDSWIG)
     if (BUILDSWIGPYTHON)
       add_subdirectory (python)
     endif ()
     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)
     if (BUILDSWIGNODE)
       if (SWIG_VERSION VERSION_GREATER 3.0.4)
         add_subdirectory (javascript)
diff --git a/src/java/CMakeLists.txt b/src/java/CMakeLists.txt
new file mode 100644 (file)
index 0000000..1ce63a6
--- /dev/null
@@ -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
+)
diff --git a/src/java/mraajava.i b/src/java/mraajava.i
new file mode 100644 (file)
index 0000000..4d4bc75
--- /dev/null
@@ -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
index d6ac158..b872f0c 100644 (file)
@@ -68,3 +68,8 @@ class Spi;
 }
 
 %include ../mraa.i
 }
 
 %include ../mraa.i
+
+%init %{
+    //Adding mraa_init() to the module initialisation process
+    mraa_init();
+%}
index 19fadfd..7f18133 100644 (file)
     #include "uart.hpp"
 %}
 
     #include "uart.hpp"
 %}
 
-%init %{
-    //Adding mraa_init() to the module initialisation process
-    mraa_init();
-%}
-
 %exception {
     try {
         $action
 %exception {
     try {
         $action
index 8d68020..af85029 100644 (file)
@@ -106,3 +106,7 @@ class Spi;
 
 %include ../mraa.i
 
 
 %include ../mraa.i
 
+%init %{
+    //Adding mraa_init() to the module initialisation process
+    mraa_init();
+%}