java: Added Java SWIG binding creation
[contrib/mraa.git] / src / javascript / mraajs.i
1 %module (docstring="Javascript interface to libmraa") mraa
2
3 %feature("autodoc", "3");
4
5 %include carrays.i
6 %include cpointer.i
7 %array_class(uint8_t, uint8Array);
8
9 %inline %{
10   #include <node_buffer.h>
11 %}
12
13 %typemap(in) (const uint8_t *data, int length) {
14   $1 = (uint8_t*) node::Buffer::Data($input);
15   $2 = node::Buffer::Length($input);
16 }
17
18 %typemap(in) (uint8_t *txBuf, int length) {
19   $1 = (uint8_t*) node::Buffer::Data($input);
20   $2 = node::Buffer::Length($input);
21 }
22
23 %typemap(in) (v8::Handle<v8::Function> func) {
24   $1 = v8::Local<v8::Function>::Cast($input);
25 }
26
27 namespace mraa {
28 class Spi;
29 %typemap(out) uint8_t*
30 {
31 %#if SWIG_V8_VERSION > 0x032872
32   $result = node::Buffer::New((char*) $1, arg3);
33 %#else
34   $result = node::Buffer::New((char*) $1, arg3)->handle_;
35 %#endif
36 }
37 }
38
39 %newobject I2c::read(uint8_t *data, int length);
40 %newobject Spi::write(uint8_t *data, int length);
41
42 %typemap(in) (uint8_t *data, int length) {
43    int x;
44    int ecode = SWIG_AsVal_int($input, &x);
45    if (!SWIG_IsOK(ecode)) {
46      SWIG_exception_fail(SWIG_ArgError(ecode), "Expected an int");
47    }
48    $2 = x;
49    if ($2 < 0) {
50        SWIG_exception_fail(SWIG_ERROR, "Positive integer expected");
51        SWIGV8_RETURN(SWIGV8_UNDEFINED());
52    }
53    $1 = (uint8_t*) malloc($2 * sizeof(uint8_t));
54 }
55
56 %typemap(argout) (uint8_t *data, int length) {
57    if (result < 0) {      /* Check for I/O error */
58        free($1);
59        SWIG_exception_fail(SWIG_ERROR, "I2c write failed");
60        SWIGV8_RETURN(SWIGV8_UNDEFINED());
61    }
62 %#if SWIG_V8_VERSION > 0x032872
63    $result = node::Buffer::New((char*) $1, result);
64 %#else
65    $result = node::Buffer::New((char*) $1, result)->handle_;
66 %#endif
67    free($1);
68 }
69
70 %include ../mraa.i
71
72 %init %{
73     //Adding mraa_init() to the module initialisation process
74     mraa_init();
75 %}