nodejs: Add isr support to node.js
authorBrendan Le Foll <brendan.le.foll@intel.com>
Wed, 18 Feb 2015 14:27:50 +0000 (14:27 +0000)
committerBrendan Le Foll <brendan.le.foll@intel.com>
Wed, 18 Feb 2015 16:41:01 +0000 (16:41 +0000)
This commit adds isr support to node.js mraa module, it also forces
SWIGJAVASCRIPT to be set at compile time by cmake (SWIG uses SWIGJAVASCRIPT and
not SWIGNODE in it's preprocessor). This uses libuv uv_queue_work to call v8isr
and is all done at a C++ level unlike the python isr, so this reuses the
mraa_gpio_isr call. This closes #110

Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
api/mraa/gpio.hpp
src/javascript/CMakeLists.txt
src/javascript/binding.gyp.cmake
src/javascript/mraajs.i
src/mraa.i

index 13612ba..93044d3 100644 (file)
@@ -118,6 +118,31 @@ class Gpio {
         mraa_result_t isr(Edge mode, PyObject *pyfunc, PyObject* args) {
             return mraa_gpio_isr(m_gpio, (gpio_edge_t) mode, (void (*) (void *)) pyfunc, (void *) args);
         }
+#elif defined(SWIGJAVASCRIPT)
+        static void v8isr(uv_work_t* req, int status) {
+            mraa::Gpio *This = (mraa::Gpio *)req->data;
+            int argc = 1;
+            v8::Local<v8::Value> argv[] = { v8::Integer::New(-1) };
+            This->m_v8isr->Call(v8::Context::GetCurrent()->Global(), argc, argv);
+            delete req;
+        }
+
+        static void nop(uv_work_t* req)
+        {
+            // Do nothing.
+        }
+
+        static void uvwork(void *ctx) {
+            uv_work_t* req = new uv_work_t;
+            req->data = ctx;
+            uv_queue_work(uv_default_loop(), req, nop, v8isr);
+        }
+
+        mraa_result_t isr(Edge mode, v8::Handle<v8::Function> func) {
+            m_v8isr = v8::Persistent<v8::Function>::New(func);
+            mraa_gpio_isr(m_gpio, (gpio_edge_t) mode, &uvwork, this);
+            return MRAA_SUCCESS;
+        }
 #else
         /**
          * Sets a callback to be called when pin value changes
@@ -139,6 +164,9 @@ class Gpio {
          * @return Result of operation
          */
         mraa_result_t isrExit() {
+#if defined(SWIGJAVASCRIPT)
+            m_v8isr.Dispose();
+#endif
             return mraa_gpio_isr_exit(m_gpio);
         }
         /**
@@ -200,6 +228,9 @@ class Gpio {
         }
     private:
         mraa_gpio_context m_gpio;
+#if defined(SWIGJAVASCRIPT)
+        v8::Persistent<v8::Function> m_v8isr;
+#endif
 };
 
 }
index af5707b..d4d9356 100644 (file)
@@ -27,7 +27,7 @@ if (DOXYGEN_FOUND)
 endif ()
 
 set_target_properties (mraajs PROPERTIES
-  COMPILE_FLAGS "${CMAKE_CXX_FLAGS} -DBUILDING_NODE_EXTENSION"
+  COMPILE_FLAGS "${CMAKE_CXX_FLAGS} -DBUILDING_NODE_EXTENSION -DSWIGJAVASCRIPT=${SWIG_FOUND}"
   PREFIX ""
   OUTPUT_NAME mraa
   SUFFIX ".node"
index 9e470b6..5406642 100644 (file)
@@ -12,7 +12,7 @@
       'cflags_cc!': [ '-fno-rtti', '-fno-exceptions' ],
       'cflags!': [ '-fno-exceptions' ],
       'defines' : [ 'SWIG',
-                    'SWIGNODE',
+                    'SWIGJAVASCRIPT',
                    'BUILDING_NODE_EXTENSION=1' ],
       'conditions' : [
           [ 'target_arch=="x64"',
index bfc3b24..c894b38 100644 (file)
@@ -3,6 +3,7 @@
 %feature("autodoc", "3");
 
 %include carrays.i
+%include cpointer.i
 %array_class(uint8_t, uint8Array);
 
 %inline %{
   $2 = node::Buffer::Length($input);
 }
 
+%typemap(in) (v8::Handle<v8::Function> func) {
+  $1 = v8::Local<v8::Function>::Cast($input);
+}
+
 namespace mraa {
 class Spi;
 %typemap(out) uint8_t*
index 3ec6e5a..cc381bb 100644 (file)
 
 %include "types.h"
 
+%ignore Gpio::nop(uv_work_t* req);
+%ignore Gpio::v8isr(uv_work_t* req);
+%ignore Gpio::v8isr(uv_work_t* req, int status);
+%ignore Gpio::uvwork(void *ctx);
+
 %include "gpio.hpp"
 
 %include "i2c.hpp"