Skip to content

Commit d1dcbcf

Browse files
committed
Initial version
1 parent b5ad77d commit d1dcbcf

File tree

7 files changed

+275
-1
lines changed

7 files changed

+275
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.o
2+
*~

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
all:
2+
node-gyp configure build
3+
ut:
4+
node unit_test.js
5+

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
11
Arduinojs
22
=========
33

4-
javascript for openFPGAdunino
4+
javascript wrapper for openFPGAdunino library
5+
6+
Only support basic number argument for api. There is no string or structure support.
7+
8+
Install the nodejs and nodejs-gyp:
9+
10+
apt-get install nodejs
11+
12+
npm install -g node-gyp
13+
14+
Build the project:
15+
16+
node-gyp configure build
17+
18+
Test the project:
19+
20+
node unit_test.js

binding.gyp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"targets": [
3+
{
4+
"target_name": "openfpgadunino",
5+
"sources": [ "openfpgadunino.cc" ]
6+
}
7+
]
8+
}

openfgpadunino.cc

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
#define BUILDING_NODE_EXTENSION
2+
#include <string>
3+
#include <sstream>
4+
#include <map>
5+
#include <node.h>
6+
#include "simulation.h"
7+
8+
using namespace v8;
9+
using namespace std;
10+
11+
#define DEFINE_FUNCTION_HANDLE(fun_name) \
12+
Handle<Value> fun_name##_agent(const Arguments& args) {return agent(#fun_name, args);}
13+
#define GET_FUNCTION_HANDLE(fun_name) \
14+
fun_name##_agent
15+
#define DEFINE_FUNCTION(ret,function,arg) \
16+
{#ret,#function,#arg, (void*)function, GET_FUNCTION_HANDLE(function)}
17+
18+
typedef struct table_t {
19+
string return_type;
20+
string name;
21+
string arg_list;
22+
void* addr;
23+
Handle<Value> (*call)(const Arguments&);
24+
} fun_table;
25+
26+
fun_table TABLE_NULL = { "", "", "", NULL, NULL };
27+
28+
extern fun_table table[];
29+
30+
int parser_func_arg_count(string arg_list) {
31+
int i = 1;
32+
const char* arg = arg_list.c_str();
33+
if (arg_list.length() == 0)
34+
return 0;
35+
while (*arg != '\0') {
36+
if (*arg == ',')
37+
i++;
38+
arg++;
39+
}
40+
return i;
41+
}
42+
43+
#define REGISTER int
44+
#define MAX_ARGC 8
45+
#define ARG(x) (arg[x])
46+
#define ARG0
47+
#define ARG1 ARG(0)
48+
#define ARG2 ARG1, ARG(1)
49+
#define ARG3 ARG2, ARG(2)
50+
#define ARG4 ARG3, ARG(3)
51+
#define ARG5 ARG4, ARG(4)
52+
#define ARG6 ARG5, ARG(5)
53+
#define ARG7 ARG6, ARG(6)
54+
#define ARG8 ARG7, ARG(7)
55+
typedef int (*FUNC0)(void);
56+
typedef int (*FUNC1)(REGISTER);
57+
typedef int (*FUNC2)(REGISTER, REGISTER);
58+
typedef int (*FUNC3)(REGISTER, REGISTER, REGISTER);
59+
typedef int (*FUNC4)(REGISTER, REGISTER, REGISTER, REGISTER);
60+
typedef int (*FUNC5)(REGISTER, REGISTER, REGISTER, REGISTER, REGISTER);
61+
typedef int (*FUNC6)(REGISTER, REGISTER, REGISTER, REGISTER, REGISTER,
62+
REGISTER);
63+
typedef int (*FUNC7)(REGISTER, REGISTER, REGISTER, REGISTER, REGISTER, REGISTER,
64+
REGISTER);
65+
typedef int (*FUNC8)(REGISTER, REGISTER, REGISTER, REGISTER, REGISTER, REGISTER,
66+
REGISTER, REGISTER);
67+
68+
Handle<Value> agent(string name, const Arguments& args) {
69+
HandleScope scope;
70+
fun_table* table_p = table;
71+
void* func_address = NULL;
72+
int argc;
73+
REGISTER ret = 0;
74+
REGISTER arg[MAX_ARGC];
75+
76+
while (table_p != &TABLE_NULL) {
77+
if (table_p->name == name) {
78+
func_address = table_p->addr;
79+
argc = parser_func_arg_count(table_p->arg_list);
80+
}
81+
table_p++;
82+
}
83+
84+
if (func_address == NULL) {
85+
string message = "Wrong method name ";
86+
message+= name;
87+
ThrowException(Exception::TypeError(String::New(message.c_str())));
88+
return scope.Close(Undefined());
89+
}
90+
91+
if (args.Length() != argc) {
92+
stringstream s;
93+
string message ="Wrong number of arguments need ";
94+
s << argc;
95+
message+= s.str();
96+
ThrowException(
97+
Exception::TypeError(String::New(message.c_str())));
98+
return scope.Close(Undefined());
99+
}
100+
101+
for(int i=0; i< argc;i++) {
102+
if(!args[i]->IsNumber()) {
103+
stringstream s;
104+
string message ="Wrong arguments ";
105+
s << i;
106+
message+= s.str();
107+
ThrowException(Exception::TypeError(String::New(message.c_str())));
108+
return scope.Close(Undefined());
109+
}
110+
}
111+
112+
for(int i=0; i< argc;i++)
113+
arg[i] = args[i]->NumberValue();
114+
115+
switch (argc) {
116+
case 0:
117+
ret = ((FUNC0) func_address)(ARG0);
118+
break;
119+
case 1:
120+
ret = ((FUNC1) func_address)(ARG1);
121+
break;
122+
case 2:
123+
ret = ((FUNC2) func_address)(ARG2);
124+
break;
125+
case 3:
126+
ret = ((FUNC3) func_address)(ARG3);
127+
break;
128+
case 4:
129+
ret = ((FUNC4) func_address)(ARG4);
130+
break;
131+
case 5:
132+
ret = ((FUNC5) func_address)(ARG5);
133+
break;
134+
case 6:
135+
ret = ((FUNC6) func_address)(ARG6);
136+
break;
137+
case 7:
138+
ret = ((FUNC7) func_address)(ARG7);
139+
break;
140+
case 8:
141+
ret = ((FUNC8) func_address)(ARG8);
142+
break;
143+
default:
144+
break;
145+
}
146+
147+
Local < Number > num = Number::New(ret);
148+
return scope.Close(num);
149+
}
150+
151+
int add(int a, int b) {
152+
return a + b;
153+
}
154+
155+
//Add handle
156+
DEFINE_FUNCTION_HANDLE(add);
157+
DEFINE_FUNCTION_HANDLE(led);
158+
DEFINE_FUNCTION_HANDLE(ioa);
159+
DEFINE_FUNCTION_HANDLE(iob);
160+
DEFINE_FUNCTION_HANDLE(sleep);
161+
162+
//Add c function
163+
fun_table table[] = {
164+
DEFINE_FUNCTION(char,add,(char a, char b)),
165+
DEFINE_FUNCTION(void,led,(int id, char r, char g, char b)),
166+
DEFINE_FUNCTION(void,ioa,(int id, int value)),
167+
DEFINE_FUNCTION(void,iob,(int id, int value)),
168+
DEFINE_FUNCTION(unsigned int,sleep,(unsigned int seconds)),
169+
TABLE_NULL
170+
};
171+
//todo map<string, void*> fun_table;
172+
173+
void Init(Handle<Object> exports) {
174+
fun_table* table_p = table;
175+
while (table_p != &TABLE_NULL) {
176+
exports->Set(String::NewSymbol(table_p->name.c_str()),
177+
FunctionTemplate::New(table_p->call)->GetFunction());
178+
table_p++;
179+
}
180+
//Add spetial case function whose parameter is not number
181+
}
182+
183+
NODE_MODULE(openfpgadunino, Init)

simulation.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//Functionality: Initial the broad
2+
int init(void)
3+
{
4+
return 1;
5+
}
6+
//Functionality: Control the led
7+
//Argument:
8+
//id: The id of led can be 0-3
9+
//r: The red color of led
10+
//g: The greem color of led
11+
//b: The blue color of led
12+
void led(int id, char r, char g, char b)
13+
{
14+
15+
}
16+
17+
//Functionality: Control the IO port a
18+
//Argument:
19+
//id: use 0-26
20+
//value: Can be 1 or 0
21+
void ioa(int id, int value)
22+
{
23+
24+
}
25+
26+
//Functionality: Control the IO port b
27+
//Argument:
28+
//id: use 0-26
29+
//value: Can be 1 or 0
30+
void iob(int id, int value)
31+
{
32+
33+
}
34+
35+
//Functionality: print the string to the console
36+
//Argument:
37+
//format:output format string
38+
int print(const char* format, ...)
39+
{
40+
return 1;
41+
}
42+
43+
//Functionality: Sleep for seconds
44+
//Argument:
45+
//seconds The seconds for sleeping
46+
47+
//Functionality: Sleep for micro seconds
48+
//Argument:
49+
//usecods: The micro seconds for sleeping
50+
51+
52+
#endif /* LOPHILO_H_ */

unit_test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var meteroi = require('./build/Release/meteroi');
2+
console.log( 'This should be eight:', meteroi.add(3,5));
3+
console.log( 'Sleep for 2 second');
4+
meteroi.sleep(2);
5+
console.log( 'Set ioa');
6+
meteroi.ioa(1,1);
7+
console.log( 'Set led');
8+
meteroi.led(1,255,255,255);

0 commit comments

Comments
 (0)