• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 char* data, int length) {
14   if (!node::Buffer::HasInstance($input)) {
15       SWIG_exception_fail(SWIG_ERROR, "Expected a node Buffer");
16   }
17   $1 = (char*) node::Buffer::Data($input);
18   $2 = node::Buffer::Length($input);
19 }
20 
21 %typemap(in) (const uint8_t *data, int length) {
22   if (!node::Buffer::HasInstance($input)) {
23       SWIG_exception_fail(SWIG_ERROR, "Expected a node Buffer");
24   }
25   $1 = (uint8_t*) node::Buffer::Data($input);
26   $2 = node::Buffer::Length($input);
27 }
28 
29 %typemap(in) (uint8_t *txBuf, int length) {
30   if (!node::Buffer::HasInstance($input)) {
31       SWIG_exception_fail(SWIG_ERROR, "Expected a node Buffer");
32   }
33   $1 = (uint8_t*) node::Buffer::Data($input);
34   $2 = node::Buffer::Length($input);
35 }
36 
37 %typemap(in) (v8::Handle<v8::Function> func) {
38   $1 = v8::Local<v8::Function>::Cast($input);
39 }
40 
41 namespace mraa {
42 class Spi;
43 %typemap(out) uint8_t*
44 {
45 %#if SWIG_V8_VERSION > 0x040000
46    v8::MaybeLocal<v8::Object> objret = node::Buffer::Copy(v8::Isolate::GetCurrent(), (char*) result, arg3);
47    free(result);
48    if(!objret.ToLocal(&$result)){
49 	SWIG_exception_fail(SWIG_ERROR, "Spi buffer failed");
50    }
51 %#elif SWIG_V8_VERSION > 0x032870
52   $result = node::Buffer::New((char*) result, arg3);
53   free(result);
54 %#else
55   $result = node::Buffer::New((char*) result, arg3)->handle_;
56   free(result);
57 %#endif
58 }
59 }
60 
61 %newobject Uart::read(char* data, int length);
62 %newobject I2c::read(uint8_t *data, int length);
63 %newobject Spi::write(uint8_t *data, int length);
64 
65 //Uart::read()
66 %typemap(in) (char* data, int length) {
67    int x;
68    int ecode = SWIG_AsVal_int($input, &x);
69    if (!SWIG_IsOK(ecode)) {
70      SWIG_exception_fail(SWIG_ArgError(ecode), "Expected an int");
71    }
72    $2 = x;
73    if ($2 < 0) {
74        SWIG_exception_fail(SWIG_ERROR, "Positive integer expected");
75    }
76    $1 = (char*) malloc($2 * sizeof(uint8_t));
77 }
78 
79 %typemap(argout) (char* data, int length) {
80    if (result < 0) {      /* Check for I/O error */
81        free($1);
82        SWIG_exception_fail(SWIG_ERROR, "Uart write failed");
83    }
84 %#if SWIG_V8_VERSION > 0x040000
85    v8::MaybeLocal<v8::Object> objret = node::Buffer::Copy(v8::Isolate::GetCurrent(), (char*) $1, result);
86    free($1);
87    if(!objret.ToLocal(&$result)) {
88       SWIG_exception_fail(SWIG_ERROR, "Uart buffer failed");
89    }
90 %#elif SWIG_V8_VERSION > 0x032870
91    $result = node::Buffer::New((char*) $1, result);
92    free($1);
93 %#else
94    $result = node::Buffer::New((char*) $1, result)->handle_;
95    free($1);
96 %#endif
97 
98 }
99 
100 //I2c::read()
101 %typemap(in) (uint8_t *data, int length) {
102    int x;
103    int ecode = SWIG_AsVal_int($input, &x);
104    if (!SWIG_IsOK(ecode)) {
105      SWIG_exception_fail(SWIG_ArgError(ecode), "Expected an int");
106    }
107    $2 = x;
108    if ($2 < 0) {
109        SWIG_exception_fail(SWIG_ERROR, "Positive integer expected");
110    }
111    $1 = (uint8_t*) malloc($2 * sizeof(uint8_t));
112 }
113 
114 %typemap(argout) (uint8_t *data, int length) {
115    if (result < 0) {      /* Check for I/O error */
116        free($1);
117        SWIG_exception_fail(SWIG_ERROR, "I2c write failed");
118    }
119 %#if SWIG_V8_VERSION > 0x040000
120    v8::MaybeLocal<v8::Object> objret = node::Buffer::Copy(v8::Isolate::GetCurrent(), (char*) $1, result);
121    free($1);
122    if(!objret.ToLocal(&$result)) {
123       SWIG_exception_fail(SWIG_ERROR, "I2c buffer failed");
124    }
125 %#elif SWIG_V8_VERSION > 0x032870
126    $result = node::Buffer::New((char*) $1, result);
127    free($1);
128 %#else
129    $result = node::Buffer::New((char*) $1, result)->handle_;
130    free($1);
131 %#endif
132 }
133 
134 %include ../mraa.i
135 
136 %init %{
137     //Adding mraa_init() to the module initialisation process
138     mraa_init();
139 %}
140