Skip to content

Commit 654e34f

Browse files
committed
generate api - wip
1 parent b24b3a8 commit 654e34f

File tree

4 files changed

+202
-55
lines changed

4 files changed

+202
-55
lines changed

ioio/Makefile.am

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,23 @@
88
# javap -s -p -cp target/ioio-1.0.jar 'net.sourceforge.smallbasic.ioio.AnalogInput'
99
#
1010

11+
sbasic=sbasic
12+
13+
generated = api.h
14+
15+
main.lo: $(generated)
16+
17+
all-am: $(generated)
18+
19+
CLEANFILES = $(generated)
20+
1121
AM_CXXFLAGS=-fno-rtti -std=c++14
1222
AM_CPPFLAGS = -I../include -I/usr/lib/jvm/java-1.8.0-openjdk-amd64/include -I/usr/lib/jvm/java-1.8.0-openjdk-amd64/include/linux -Wall
1323
lib_LTLIBRARIES = libioio.la
14-
libioio_la_SOURCES = ../include/param.cpp ../include/hashmap.cpp ../include/apiexec.cpp main.cpp
15-
libioio_la_LDFLAGS = -module -rpath '$(libdir)' @PLATFORM_LDFLAGS@ -L/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/amd64/server -ljvm
1624

25+
libioio_la_SOURCES = ../include/param.cpp ../include/hashmap.cpp ../include/apiexec.cpp main.cpp $(generated)
26+
libioio_la_LDFLAGS = -module -rpath '$(libdir)' @PLATFORM_LDFLAGS@ -L/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/amd64/server -ljvm
1727

28+
$(generated): api.json mkapi.bas
29+
$(sbasic) mkapi.bas > $@
30+
@touch main.cpp

ioio/api.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[{
2+
"name": "CapSense",
3+
"methods" : [{
4+
"name": "read",
5+
"rtn": "float",
6+
"arg": "void"
7+
}, {
8+
"name": "waitOver",
9+
"rtn": "void",
10+
"arg": "float"
11+
}]
12+
},{
13+
"name": "DigitalInput",
14+
"methods": [{
15+
"name": "read",
16+
"rtn": "boolean",
17+
"arg": "void"
18+
},{
19+
"name": "waitForValue",
20+
"rtn": "void",
21+
"arg": "boolean"
22+
}]
23+
}]

ioio/main.cpp

Lines changed: 79 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -74,85 +74,108 @@ struct IOClass {
7474
return exc;
7575
}
7676

77-
bool invokeIV(const char *name, int value) {
78-
bool result = false;
77+
// float foo(void)
78+
int invokeFloatVoid(const char *name, var_s *retval) {
79+
int result = 0;
80+
return result;
81+
}
82+
83+
// int foo(void)
84+
int invokeIntVoid(const char *name, var_s *retval) {
85+
int result = 0;
7986
if (_instance != nullptr) {
80-
jmethodID method = env->GetMethodID(_clazz, name, "(I)V");
87+
jmethodID method = env->GetMethodID(_clazz, name, "()I");
88+
int value = 0;
8189
if (method != nullptr) {
82-
env->CallVoidMethod(_instance, method, value);
90+
value = env->CallIntMethod(_instance, method);
8391
}
8492
if (!checkException()) {
85-
result = (method != nullptr);
93+
v_setint(retval, value);
94+
result = 1;
8695
}
8796
}
8897
return result;
8998
}
9099

91-
int invokeI(const char *name) {
100+
// void foo(float)
101+
int invokeVoidFloat(const char *name, var_num_t value, var_s *retval) {
102+
int result = 0;
103+
return result;
104+
}
105+
106+
// void foo(int)
107+
int invokeVoidInt(const char *name, int value, var_s *) {
92108
int result = 0;
93109
if (_instance != nullptr) {
94-
jmethodID method = env->GetMethodID(_clazz, name, "()I");
110+
jmethodID method = env->GetMethodID(_clazz, name, "(I)V");
95111
if (method != nullptr) {
96-
result = env->CallIntMethod(_instance, method);
112+
env->CallVoidMethod(_instance, method, value);
113+
}
114+
if (!checkException()) {
115+
result = 1;
97116
}
98-
checkException();
99117
}
100118
return result;
101119
}
102120

103-
void invokeV(const char *name) {
121+
// void foo(void)
122+
int invokeVoidVoid(const char *name, var_s *) {
123+
int result = 0;
104124
if (_instance != nullptr) {
105125
jmethodID method = env->GetMethodID(_clazz, name, "()V");
106126
if (method != nullptr) {
107127
env->CallVoidMethod(_instance, method);
108128
}
109-
checkException();
129+
if (!checkException()) {
130+
result = 1;
131+
}
110132
}
133+
return result;
111134
}
112135

113-
int isReady() {
114-
return invokeI(METHOD_READY);
115-
}
136+
// int isReady() {
137+
// return invokeI(METHOD_READY);
138+
// }
116139

117140
bool open(int pin) {
118-
return invokeIV(METHOD_OPEN, pin);
141+
return 1;//invokeIV(METHOD_OPEN, pin);
119142
}
120143

121-
void beginBatch() {
122-
invokeV(METHOD_BEGIN_BATCH);
123-
}
144+
// void beginBatch() {
145+
// invokeV(METHOD_BEGIN_BATCH);
146+
// }
124147

125-
void endBatch() {
126-
invokeV(METHOD_END_BATCH);
127-
}
148+
// void endBatch() {
149+
// invokeV(METHOD_END_BATCH);
150+
// }
128151

129-
void disconnect() {
130-
invokeV(METHOD_DISCONNECT);
131-
}
152+
// void disconnect() {
153+
// invokeV(METHOD_DISCONNECT);
154+
// }
132155

133-
void hardReset() {
134-
invokeV(METHOD_HARD_RESET);
135-
}
156+
// void hardReset() {
157+
// invokeV(METHOD_HARD_RESET);
158+
// }
136159

137-
void softReset() {
138-
invokeV(METHOD_SOFT_RESET);
139-
}
160+
// void softReset() {
161+
// invokeV(METHOD_SOFT_RESET);
162+
// }
140163

141-
void sync() {
142-
invokeV(METHOD_SYNC);
143-
}
164+
// void sync() {
165+
// invokeV(METHOD_SYNC);
166+
// }
144167

145-
void waitForConnect() {
146-
invokeV(METHOD_WAIT_FOR_CONNECT);
147-
}
168+
// void waitForConnect() {
169+
// invokeV(METHOD_WAIT_FOR_CONNECT);
170+
// }
148171

149-
void waitForDisconnect() {
150-
invokeV(METHOD_WAIT_FOR_DISCONNECT);
151-
}
172+
// void waitForDisconnect() {
173+
// invokeV(METHOD_WAIT_FOR_DISCONNECT);
174+
// }
152175

153-
bool write(int value) {
154-
return invokeIV(METHOD_WRITE, value);
155-
}
176+
// bool write(int value) {
177+
// return invokeIV(METHOD_WRITE, value);
178+
// }
156179

157180
private:
158181
jclass _clazz;
@@ -175,6 +198,9 @@ static int get_io_class_id(var_s *map, var_s *retval) {
175198
return result;
176199
}
177200

201+
#include "api.h"
202+
203+
/*
178204
static int cmd_begin_batch(var_s *self, int param_count, slib_par_t *params, var_s *retval) {
179205
int result = 0;
180206
if (param_count != 0) {
@@ -315,18 +341,18 @@ static int cmd_digital_output_write(var_s *self, int param_count, slib_par_t *pa
315341
}
316342
return result;
317343
}
318-
344+
*/
319345
static void create_io_class(var_t *map, int id) {
320346
map_init_id(map, id, CLASS_IOCLASS);
321-
v_create_callback(map, METHOD_BEGIN_BATCH, cmd_begin_batch);
322-
v_create_callback(map, METHOD_DISCONNECT, cmd_disconnect);
323-
v_create_callback(map, METHOD_END_BATCH, cmd_end_batch);
324-
v_create_callback(map, METHOD_HARD_RESET, cmd_hard_reset);
325-
v_create_callback(map, METHOD_READY, cmd_is_ready);
326-
v_create_callback(map, METHOD_SOFT_RESET, cmd_soft_reset);
327-
v_create_callback(map, METHOD_SYNC, cmd_sync);
328-
v_create_callback(map, METHOD_WAIT_FOR_CONNECT, cmd_wait_for_connect);
329-
v_create_callback(map, METHOD_WAIT_FOR_DISCONNECT, cmd_wait_for_disconnect);
347+
// v_create_callback(map, METHOD_BEGIN_BATCH, cmd_begin_batch);
348+
// v_create_callback(map, METHOD_DISCONNECT, cmd_disconnect);
349+
// v_create_callback(map, METHOD_END_BATCH, cmd_end_batch);
350+
// v_create_callback(map, METHOD_HARD_RESET, cmd_hard_reset);
351+
// v_create_callback(map, METHOD_READY, cmd_is_ready);
352+
// v_create_callback(map, METHOD_SOFT_RESET, cmd_soft_reset);
353+
// v_create_callback(map, METHOD_SYNC, cmd_sync);
354+
// v_create_callback(map, METHOD_WAIT_FOR_CONNECT, cmd_wait_for_connect);
355+
// v_create_callback(map, METHOD_WAIT_FOR_DISCONNECT, cmd_wait_for_disconnect);
330356
}
331357

332358
static int cmd_openanaloginput(int argc, slib_par_t *params, var_t *retval) {
@@ -355,7 +381,7 @@ static int cmd_opendigitaloutput(int argc, slib_par_t *params, var_t *retval) {
355381
if (output.create(CLASS_DIGITAL_INPUT) &&
356382
output.open(pin)) {
357383
create_io_class(retval, id);
358-
v_create_callback(retval, METHOD_WRITE, cmd_digital_output_write);
384+
//v_create_callback(retval, METHOD_WRITE, cmd_digital_output_write);
359385
result = 1;
360386
} else {
361387
_classMap.erase(id);

ioio/mkapi.bas

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
rem
2+
rem generate api methods from api.json
3+
rem
4+
5+
tload "api.json", s, 1
6+
api = array(s)
7+
8+
func get_method_name(method)
9+
local result
10+
11+
if (method.rtn == "void") then
12+
result = "invokeVoid"
13+
else if (method.rtn == "boolean" || method.rtn == "int") then
14+
result = "invokeInt"
15+
else if (method.rtn == "float") then
16+
result = "invokeFloat"
17+
endif
18+
19+
if (method.arg == "void") then
20+
result += "Void"
21+
else if (method.arg == "boolean" || method.arg == "int") then
22+
result += "Int"
23+
else if (method.arg == "float") then
24+
result += "Float"
25+
endif
26+
27+
return result
28+
end
29+
30+
sub generate_command(objName, method)
31+
local lname = lower(method.name)
32+
local uname = upper(method.name)
33+
local param_count = iff(method.arg == "void", 0, 1)
34+
local invoke = get_method_name(method)
35+
36+
print "static int cmd_" + lower(objName) + "_" + lname + "(var_s *self, int argc, slib_par_t *arg, var_s *retval) {"
37+
print " int result = 0;"
38+
print " if (argc != " + param_count + ") {"
39+
print " error(retval, \"" + objName + "." + method.name + "\", " + param_count + ");"
40+
print " } else {"
41+
print " int id = get_io_class_id(self, retval);"
42+
print " if (id != -1) {"
43+
44+
local argument = ""
45+
if (method.arg == "boolean" || method.arg == "int") then
46+
print " auto value = get_param_int(argc, arg, 0, 0);"
47+
argument = ", value"
48+
else if (method.arg == "float") then
49+
print " auto value = get_param_num(argc, arg, 0, 0);"
50+
argument = ", value"
51+
endif
52+
53+
if (method.rtn == "void") then
54+
print " result = _classMap.at(id)." + invoke + "(\"" + method.name + "\"" + argument + ", retval);"
55+
else if (method.rtn == "boolean" || method.rtn == "int") then
56+
print " result = _classMap.at(id)." + invoke + "(\"" + method.name + "\"" + argument + ", retval);"
57+
else if (method.rtn == "float") then
58+
print " result = _classMap.at(id)." + invoke + "(\"" + method.name + "\"" + argument + ", retval);"
59+
endif
60+
print " }"
61+
print " }"
62+
print " return result;"
63+
print "}"
64+
print
65+
end
66+
67+
sub generate_constructor(byref obj)
68+
print "static void create_" + lower(obj.name) + "(var_t *map) {"
69+
local method
70+
for method in obj.methods
71+
print " v_create_callback(map, \"" + method.name + "\", cmd_" + lower(obj.name) + "_" + lower(method.name) + ");"
72+
next
73+
print "}"
74+
print
75+
end
76+
77+
for obj in api
78+
for method in obj.methods
79+
generate_command(obj.name, method)
80+
next
81+
next
82+
83+
for obj in api
84+
generate_constructor(obj)
85+
next

0 commit comments

Comments
 (0)