From 008184c305cb58f88b2a5fac53ef83eb09e75f5a Mon Sep 17 00:00:00 2001 From: Brendan Le Foll Date: Wed, 18 Feb 2015 14:27:50 +0000 Subject: [PATCH] nodejs: Add isr support to node.js 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 --- api/mraa/gpio.hpp | 31 +++++++++++++++++++++++++++++++ src/javascript/CMakeLists.txt | 2 +- src/javascript/binding.gyp.cmake | 2 +- src/javascript/mraajs.i | 5 +++++ src/mraa.i | 5 +++++ 5 files changed, 43 insertions(+), 2 deletions(-) diff --git a/api/mraa/gpio.hpp b/api/mraa/gpio.hpp index 13612ba..93044d3 100644 --- a/api/mraa/gpio.hpp +++ b/api/mraa/gpio.hpp @@ -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 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 func) { + m_v8isr = v8::Persistent::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 m_v8isr; +#endif }; } diff --git a/src/javascript/CMakeLists.txt b/src/javascript/CMakeLists.txt index af5707b..d4d9356 100644 --- a/src/javascript/CMakeLists.txt +++ b/src/javascript/CMakeLists.txt @@ -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" diff --git a/src/javascript/binding.gyp.cmake b/src/javascript/binding.gyp.cmake index 9e470b6..5406642 100644 --- a/src/javascript/binding.gyp.cmake +++ b/src/javascript/binding.gyp.cmake @@ -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"', diff --git a/src/javascript/mraajs.i b/src/javascript/mraajs.i index bfc3b24..c894b38 100644 --- a/src/javascript/mraajs.i +++ b/src/javascript/mraajs.i @@ -3,6 +3,7 @@ %feature("autodoc", "3"); %include carrays.i +%include cpointer.i %array_class(uint8_t, uint8Array); %inline %{ @@ -19,6 +20,10 @@ $2 = node::Buffer::Length($input); } +%typemap(in) (v8::Handle func) { + $1 = v8::Local::Cast($input); +} + namespace mraa { class Spi; %typemap(out) uint8_t* diff --git a/src/mraa.i b/src/mraa.i index 3ec6e5a..cc381bb 100644 --- a/src/mraa.i +++ b/src/mraa.i @@ -43,6 +43,11 @@ %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" -- 2.7.4