Private
Public Access
2
0

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 <brendan.le.foll@intel.com>
This commit is contained in:
Brendan Le Foll
2015-02-18 14:27:50 +00:00
parent ffe40033d4
commit 008184c305
5 changed files with 43 additions and 2 deletions

View 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
};
}

View 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"

View 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"',

View File

@@ -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<v8::Function> func) {
$1 = v8::Local<v8::Function>::Cast($input);
}
namespace mraa {
class Spi;
%typemap(out) uint8_t*

View File

@@ -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"