Skip to content

Commit 029a6d9

Browse files
committed
IOIO: generate api - wip
1 parent 654e34f commit 029a6d9

File tree

2 files changed

+31
-216
lines changed

2 files changed

+31
-216
lines changed

ioio/main.cpp

Lines changed: 9 additions & 214 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,6 @@ int nextId = 1;
2323
#define CLASS_DIGITAL_INPUT "net/sourceforge/smallbasic/ioio/DigitalOutput"
2424
#define CLASS_ANALOG_INPUT "net/sourceforge/smallbasic/ioio/AnalogInput"
2525
#define CLASS_IOCLASS 1
26-
#define METHOD_BEGIN_BATCH "beginBatch"
27-
#define METHOD_DISCONNECT "disconnect"
28-
#define METHOD_END_BATCH "endBatch"
29-
#define METHOD_HARD_RESET "hardReset"
30-
#define METHOD_OPEN "open"
31-
#define METHOD_READY "isReady"
32-
#define METHOD_SOFT_RESET "softReset"
33-
#define METHOD_SYNC "sync"
34-
#define METHOD_WAIT_FOR_CONNECT "waitForConnect"
35-
#define METHOD_WAIT_FOR_DISCONNECT "waitForDisconnect"
36-
#define METHOD_WRITE "write"
3726

3827
struct IOClass {
3928
IOClass(): _clazz(nullptr), _instance(nullptr) {}
@@ -104,7 +93,7 @@ struct IOClass {
10493
}
10594

10695
// void foo(int)
107-
int invokeVoidInt(const char *name, int value, var_s *) {
96+
int invokeVoidInt(const char *name, int value, var_s *retval) {
10897
int result = 0;
10998
if (_instance != nullptr) {
11099
jmethodID method = env->GetMethodID(_clazz, name, "(I)V");
@@ -119,7 +108,7 @@ struct IOClass {
119108
}
120109

121110
// void foo(void)
122-
int invokeVoidVoid(const char *name, var_s *) {
111+
int invokeVoidVoid(const char *name, var_s *retval) {
123112
int result = 0;
124113
if (_instance != nullptr) {
125114
jmethodID method = env->GetMethodID(_clazz, name, "()V");
@@ -133,50 +122,10 @@ struct IOClass {
133122
return result;
134123
}
135124

136-
// int isReady() {
137-
// return invokeI(METHOD_READY);
138-
// }
139-
140-
bool open(int pin) {
141-
return 1;//invokeIV(METHOD_OPEN, pin);
125+
int open(int pin, var_s *retval) {
126+
return invokeVoidInt("open", pin, retval);
142127
}
143128

144-
// void beginBatch() {
145-
// invokeV(METHOD_BEGIN_BATCH);
146-
// }
147-
148-
// void endBatch() {
149-
// invokeV(METHOD_END_BATCH);
150-
// }
151-
152-
// void disconnect() {
153-
// invokeV(METHOD_DISCONNECT);
154-
// }
155-
156-
// void hardReset() {
157-
// invokeV(METHOD_HARD_RESET);
158-
// }
159-
160-
// void softReset() {
161-
// invokeV(METHOD_SOFT_RESET);
162-
// }
163-
164-
// void sync() {
165-
// invokeV(METHOD_SYNC);
166-
// }
167-
168-
// void waitForConnect() {
169-
// invokeV(METHOD_WAIT_FOR_CONNECT);
170-
// }
171-
172-
// void waitForDisconnect() {
173-
// invokeV(METHOD_WAIT_FOR_DISCONNECT);
174-
// }
175-
176-
// bool write(int value) {
177-
// return invokeIV(METHOD_WRITE, value);
178-
// }
179-
180129
private:
181130
jclass _clazz;
182131
jobject _instance;
@@ -200,169 +149,15 @@ static int get_io_class_id(var_s *map, var_s *retval) {
200149

201150
#include "api.h"
202151

203-
/*
204-
static int cmd_begin_batch(var_s *self, int param_count, slib_par_t *params, var_s *retval) {
205-
int result = 0;
206-
if (param_count != 0) {
207-
error(retval, METHOD_BEGIN_BATCH, 0);
208-
} else {
209-
int id = get_io_class_id(self, retval);
210-
if (id != -1) {
211-
_classMap.at(id).beginBatch();
212-
result = 1;
213-
}
214-
}
215-
return result;
216-
}
217-
218-
static int cmd_disconnect(var_s *self, int param_count, slib_par_t *params, var_s *retval) {
219-
int result = 0;
220-
if (param_count != 0) {
221-
error(retval, METHOD_DISCONNECT, 0);
222-
} else {
223-
int id = get_io_class_id(self, retval);
224-
if (id != -1) {
225-
_classMap.at(id).disconnect();
226-
result = 1;
227-
}
228-
}
229-
return result;
230-
}
231-
232-
static int cmd_end_batch(var_s *self, int param_count, slib_par_t *params, var_s *retval) {
233-
int result = 0;
234-
if (param_count != 0) {
235-
error(retval, METHOD_END_BATCH, 0);
236-
} else {
237-
int id = get_io_class_id(self, retval);
238-
if (id != -1) {
239-
_classMap.at(id).endBatch();
240-
result = 1;
241-
}
242-
}
243-
return result;
244-
}
245-
246-
static int cmd_hard_reset(var_s *self, int param_count, slib_par_t *params, var_s *retval) {
247-
int result = 0;
248-
if (param_count != 0) {
249-
error(retval, METHOD_HARD_RESET, 0);
250-
} else {
251-
int id = get_io_class_id(self, retval);
252-
if (id != -1) {
253-
_classMap.at(id).hardReset();
254-
result = 1;
255-
}
256-
}
257-
return result;
258-
}
259-
260-
static int cmd_is_ready(var_s *self, int param_count, slib_par_t *params, var_s *retval) {
261-
int result = 0;
262-
if (param_count != 0) {
263-
error(retval, METHOD_READY, 0);
264-
} else {
265-
int id = get_io_class_id(self, retval);
266-
if (id != -1) {
267-
v_setint(retval, _classMap.at(id).isReady());
268-
result = 1;
269-
}
270-
}
271-
return result;
272-
}
273-
274-
static int cmd_soft_reset(var_s *self, int param_count, slib_par_t *params, var_s *retval) {
275-
int result = 0;
276-
if (param_count != 0) {
277-
error(retval, METHOD_SOFT_RESET, 0);
278-
} else {
279-
int id = get_io_class_id(self, retval);
280-
if (id != -1) {
281-
_classMap.at(id).softReset();
282-
result = 1;
283-
}
284-
}
285-
return result;
286-
}
287-
288-
static int cmd_sync(var_s *self, int param_count, slib_par_t *params, var_s *retval) {
289-
int result = 0;
290-
if (param_count != 0) {
291-
error(retval, METHOD_SYNC, 0);
292-
} else {
293-
int id = get_io_class_id(self, retval);
294-
if (id != -1) {
295-
_classMap.at(id).sync();
296-
result = 1;
297-
}
298-
}
299-
return result;
300-
}
301-
302-
static int cmd_wait_for_connect(var_s *self, int param_count, slib_par_t *params, var_s *retval) {
303-
int result = 0;
304-
if (param_count != 0) {
305-
error(retval, METHOD_WAIT_FOR_CONNECT, 0);
306-
} else {
307-
int id = get_io_class_id(self, retval);
308-
if (id != -1) {
309-
_classMap.at(id).waitForConnect();
310-
result = 1;
311-
}
312-
}
313-
return result;
314-
}
315-
316-
static int cmd_wait_for_disconnect(var_s *self, int param_count, slib_par_t *params, var_s *retval) {
317-
int result = 0;
318-
if (param_count != 0) {
319-
error(retval, METHOD_WAIT_FOR_DISCONNECT, 0);
320-
} else {
321-
int id = get_io_class_id(self, retval);
322-
if (id != -1) {
323-
_classMap.at(id).waitForDisconnect();
324-
result = 1;
325-
}
326-
}
327-
return result;
328-
}
329-
330-
static int cmd_digital_output_write(var_s *self, int param_count, slib_par_t *params, var_s *retval) {
331-
int result = 0;
332-
if (param_count != 1) {
333-
error(retval, METHOD_WRITE, 1);
334-
} else {
335-
int id = get_io_class_id(self, retval);
336-
if (id != -1) {
337-
int value = get_param_int(param_count, params, 0, 0);
338-
_classMap.at(id).write(value);
339-
result = 1;
340-
}
341-
}
342-
return result;
343-
}
344-
*/
345-
static void create_io_class(var_t *map, int id) {
346-
map_init_id(map, id, CLASS_IOCLASS);
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);
356-
}
357-
358152
static int cmd_openanaloginput(int argc, slib_par_t *params, var_t *retval) {
359153
int result;
360154
int pin = get_param_int(argc, params, 0, 0);
361155
int id = ++nextId;
362156
IOClass &input = _classMap[id];
363157
if (input.create(CLASS_ANALOG_INPUT) &&
364-
input.open(pin)) {
365-
create_io_class(retval, id);
158+
input.open(pin, retval)) {
159+
map_init_id(retval, id, CLASS_IOCLASS);
160+
//create_io_class(retval, id);
366161
//v_create_func(retval, "write", cmd_digital_output_write);
367162
result = 1;
368163
} else {
@@ -379,8 +174,8 @@ static int cmd_opendigitaloutput(int argc, slib_par_t *params, var_t *retval) {
379174
int id = ++nextId;
380175
IOClass &output = _classMap[id];
381176
if (output.create(CLASS_DIGITAL_INPUT) &&
382-
output.open(pin)) {
383-
create_io_class(retval, id);
177+
output.open(pin, retval)) {
178+
map_init_id(retval, id, CLASS_IOCLASS);
384179
//v_create_callback(retval, METHOD_WRITE, cmd_digital_output_write);
385180
result = 1;
386181
} else {

ioio/mkapi.bas

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ rem
55
tload "api.json", s, 1
66
api = array(s)
77

8+
baseApi = ["beginBatch", "disconnect", "endBatch", "hardReset", "softReset", "sync", "waitForConnect", "waitForDisconnect"]
9+
810
func get_method_name(method)
911
local result
1012

@@ -32,11 +34,19 @@ sub generate_command(objName, method)
3234
local uname = upper(method.name)
3335
local param_count = iff(method.arg == "void", 0, 1)
3436
local invoke = get_method_name(method)
37+
local cmd_name, err_name
38+
if (isstring(objName)) then
39+
cmd_name = lower(objName) + "_" + lname
40+
err_name = objName + "." + method.name
41+
else
42+
cmd_name = lname
43+
err_name = method.name
44+
endif
3545

36-
print "static int cmd_" + lower(objName) + "_" + lname + "(var_s *self, int argc, slib_par_t *arg, var_s *retval) {"
46+
print "static int cmd_" + cmd_name + "(var_s *self, int argc, slib_par_t *arg, var_s *retval) {"
3747
print " int result = 0;"
3848
print " if (argc != " + param_count + ") {"
39-
print " error(retval, \"" + objName + "." + method.name + "\", " + param_count + ");"
49+
print " error(retval, \"" + err_name + "\", " + param_count + ");"
4050
print " } else {"
4151
print " int id = get_io_class_id(self, retval);"
4252
print " if (id != -1) {"
@@ -70,6 +80,9 @@ sub generate_constructor(byref obj)
7080
for method in obj.methods
7181
print " v_create_callback(map, \"" + method.name + "\", cmd_" + lower(obj.name) + "_" + lower(method.name) + ");"
7282
next
83+
for s in baseApi
84+
print " v_create_callback(map, \"" + s + "\", cmd_" + lower(s) + ");"
85+
next
7386
print "}"
7487
print
7588
end
@@ -80,6 +93,13 @@ for obj in api
8093
next
8194
next
8295

96+
for s in baseApi
97+
method.name = s
98+
method.rtn = "void"
99+
method.arg = "void"
100+
generate_command(0, method)
101+
next
102+
83103
for obj in api
84104
generate_constructor(obj)
85105
next

0 commit comments

Comments
 (0)