Skip to content

Commit 9e8da42

Browse files
committed
IOIO: refactoring so there's only one looper
1 parent dc0a70f commit 9e8da42

File tree

16 files changed

+392
-428
lines changed

16 files changed

+392
-428
lines changed

ioio/main.cpp

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
#include <pthread.h>
1717
#include "robin-hood-hashing/src/include/robin_hood.h"
1818

19-
struct IOClass;
19+
struct IOTask;
2020

2121
JNIEnv *env;
2222
JavaVM *jvm;
23-
IOClass *ioioClass;
23+
IOTask *ioioTask;
2424
int nextId = 1;
2525

2626
#define CLASS_ANALOGINPUT "net/sourceforge/smallbasic/ioio/AnalogInputImpl"
@@ -30,12 +30,12 @@ int nextId = 1;
3030
#define CLASS_PWMOUTPUT "net/sourceforge/smallbasic/ioio/PwmOutputImpl"
3131
#define CLASS_CAPSENSE "net/sourceforge/smallbasic/ioio/CapsenseImpl"
3232
#define CLASS_IOIO "net/sourceforge/smallbasic/ioio/IOIOImpl"
33-
#define CLASS_IOCLASS_ID 1
33+
#define CLASS_IOTASK_ID 1
3434

35-
struct IOClass {
36-
IOClass(): _clazz(nullptr), _instance(nullptr) {}
35+
struct IOTask {
36+
IOTask(): _clazz(nullptr), _instance(nullptr) {}
3737

38-
virtual ~IOClass() {
38+
virtual ~IOTask() {
3939
_clazz = nullptr;
4040
_instance = nullptr;
4141
}
@@ -200,18 +200,18 @@ struct IOClass {
200200
jobject _instance;
201201
};
202202

203-
robin_hood::unordered_map<int, IOClass> _classMap;
203+
robin_hood::unordered_map<int, IOTask> _ioTaskMap;
204204

205205
static int get_io_class_id(var_s *map, var_s *retval) {
206206
int result = -1;
207207
if (is_map(map)) {
208208
int id = map->v.m.id;
209-
if (id != -1 && _classMap.find(id) != _classMap.end()) {
209+
if (id != -1 && _ioTaskMap.find(id) != _ioTaskMap.end()) {
210210
result = id;
211211
}
212212
}
213213
if (result == -1) {
214-
error(retval, "IOClass not found");
214+
error(retval, "IOTask not found");
215215
}
216216
return result;
217217
}
@@ -225,6 +225,9 @@ FUNC_SIG lib_func[] = {
225225
{1, 2, "OPENDIGITALOUTPUT", cmd_opendigitaloutput},
226226
{1, 2, "OPENPULSEINPUT", cmd_openpulseinput},
227227
{1, 2, "OPENPWMOUTPUT", cmd_openpwmoutput},
228+
};
229+
230+
FUNC_SIG lib_proc[] = {
228231
{0, 0, "BEGINBATCH", cmd_beginbatch},
229232
{0, 0, "DISCONNECT", cmd_disconnect},
230233
{0, 0, "ENDBATCH", cmd_endbatch},
@@ -235,8 +238,6 @@ FUNC_SIG lib_func[] = {
235238
{0, 0, "WAITFORDISCONNECT", cmd_waitfordisconnect},
236239
};
237240

238-
FUNC_SIG lib_proc[] = {};
239-
240241
SBLIB_API int sblib_proc_count() {
241242
return (sizeof(lib_proc) / sizeof(lib_proc[0]));
242243
}
@@ -263,9 +264,9 @@ int sblib_init(const char *sourceFile) {
263264
fprintf(stderr, "Failed to create JVM\n");
264265
}
265266

266-
ioioClass = new IOClass();
267-
if (!ioioClass || !ioioClass->create(CLASS_IOIO)) {
268-
fprintf(stderr, "Failed to IOIOClass\n");
267+
ioioTask = new IOTask();
268+
if (!ioioTask || !ioioTask->create(CLASS_IOIO)) {
269+
fprintf(stderr, "Failed to IOIOTask\n");
269270
result = 0;
270271
}
271272
return result;
@@ -274,23 +275,23 @@ int sblib_init(const char *sourceFile) {
274275
SBLIB_API void sblib_free(int cls_id, int id) {
275276
if (id != -1) {
276277
switch (cls_id) {
277-
case CLASS_IOCLASS_ID:
278-
if (id != -1 && _classMap.find(id) != _classMap.end()) {
279-
_classMap.at(id).invokeVoidVoid("close", nullptr);
280-
_classMap.erase(id);
278+
case CLASS_IOTASK_ID:
279+
if (id != -1 && _ioTaskMap.find(id) != _ioTaskMap.end()) {
280+
_ioTaskMap.at(id).invokeVoidVoid("close", nullptr);
281+
_ioTaskMap.erase(id);
281282
}
282283
break;
283284
}
284285
}
285286
}
286287

287288
void sblib_close(void) {
288-
if (ioioClass) {
289-
delete ioioClass;
289+
if (ioioTask) {
290+
delete ioioTask;
290291
}
291-
if (!_classMap.empty()) {
292-
fprintf(stderr, "IOClass leak detected\n");
293-
_classMap.clear();
292+
if (!_ioTaskMap.empty()) {
293+
fprintf(stderr, "IOTask leak detected\n");
294+
_ioTaskMap.clear();
294295
}
295296
jvm->DetachCurrentThread();
296297
// calling this hangs

ioio/mkapi.bas

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ sub generate_command(objName, method)
6565
endif
6666

6767
if (method.rtn == "void") then
68-
print " result = _classMap.at(id)." + invoke + "(\"" + method.name + "\"" + argument + ", retval);"
68+
print " result = _ioTaskMap.at(id)." + invoke + "(\"" + method.name + "\"" + argument + ", retval);"
6969
else if (method.rtn == "boolean" || method.rtn == "int") then
70-
print " result = _classMap.at(id)." + invoke + "(\"" + method.name + "\"" + argument + ", retval);"
70+
print " result = _ioTaskMap.at(id)." + invoke + "(\"" + method.name + "\"" + argument + ", retval);"
7171
else if (method.rtn == "float") then
72-
print " result = _classMap.at(id)." + invoke + "(\"" + method.name + "\"" + argument + ", retval);"
72+
print " result = _ioTaskMap.at(id)." + invoke + "(\"" + method.name + "\"" + argument + ", retval);"
7373
endif
7474
print " }"
7575
print " }"
@@ -84,7 +84,7 @@ sub generate_ioio_command(name)
8484
print " if (argc != 0) {"
8585
print " error(retval, \"" + name + "\", 0);"
8686
print " } else {"
87-
print " result = ioioClass->invokeVoidVoid(\"" + name + "\", retval);"
87+
print " result = ioioTask->invokeVoidVoid(\"" + name + "\", retval);"
8888
print " }"
8989
print " return result;"
9090
print "}"
@@ -106,14 +106,14 @@ sub generate_open_function(byref obj)
106106
print " int result;"
107107
print " int pin = get_param_int(argc, params, 0, 0);"
108108
print " int id = ++nextId;"
109-
print " IOClass &instance = _classMap[id];"
109+
print " IOTask &instance = _ioTaskMap[id];"
110110
print " if (instance.create(CLASS_" + upper(obj.name) + ") &&"
111111
print " instance.open(pin, retval)) {"
112-
print " map_init_id(retval, id, CLASS_IOCLASS_ID);"
112+
print " map_init_id(retval, id, CLASS_IOTASK_ID);"
113113
print " create_" + lower(obj.name) + "(retval);"
114114
print " result = 1;"
115115
print " } else {"
116-
print " _classMap.erase(id);"
116+
print " _ioTaskMap.erase(id);"
117117
print " error(retval, \"open" + obj.name + "() failed\");"
118118
print " result = 0;"
119119
print " }"

ioio/samples/led.bas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ out = ioio.openDigitalOutput(0, 1)
44

55
print "wait for connect"
66
ioio.waitForConnect()
7-
print "ready"
7+
print "ready!!!"
88

99
value = false
1010
for i = 0 to 5

ioio/src/main/java/net/sourceforge/smallbasic/ioio/AbstractLooper.java

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)