Skip to content

Commit 22e136e

Browse files
committed
IOIO: add handling for SpiMaster
1 parent 78965df commit 22e136e

File tree

4 files changed

+77
-16
lines changed

4 files changed

+77
-16
lines changed

ioio/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,12 @@ An interface for controlling a TWI module, in TWI bus-master mode, enabling comm
107107
| Name | Description |
108108
|---------|---------------|
109109

110+
## SpiMaster
111+
112+
An interface for controlling an SPI module, in SPI bus-master mode, enabling communication with multiple SPI-enabled slave modules
113+
114+
`io = ioio.openSpiMaster(pin)`
115+
116+
| Name | Description |
117+
|---------|---------------|
118+

ioio/api.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,13 @@
247247
{
248248
"name": "TwiMaster":
249249
"comment": "An interface for controlling a TWI module, in TWI bus-master mode, enabling communication with multiple TWI-enabled slave modules.",
250-
"methods": []
250+
"methods": [],
251+
"pins": 2
251252
},
252253
{
253254
"name": "SpiMaster":
254255
"comment": "An interface for controlling an SPI module, in SPI bus-master mode, enabling communication with multiple SPI-enabled slave modules",
255-
"methods": []
256+
"methods": [],
257+
"pins": 4
256258
}
257259
]

ioio/main.cpp

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ struct IOTask {
7373
jstring jstr = (jstring) env->CallObjectMethod(exc, methodId);
7474
const char *message = env->GetStringUTFChars(jstr, JNI_FALSE);
7575
error(retval, message);
76-
env->ReleaseStringUTFChars(jstr, message);
76+
env->ReleaseStringUTFChars(jstr, message);
7777
} else {
7878
env->ExceptionDescribe();
7979
env->ExceptionClear();
@@ -178,6 +178,34 @@ struct IOTask {
178178
return result;
179179
}
180180

181+
int invokeVoidInt2(const char *name, int value1, int value2, var_s *retval) {
182+
int result = 0;
183+
if (_instance != nullptr) {
184+
jmethodID method = env->GetMethodID(_clazz, name, "(II)V");
185+
if (method != nullptr) {
186+
env->CallVoidMethod(_instance, method, value1, value2);
187+
}
188+
if (!checkException(retval)) {
189+
result = 1;
190+
}
191+
}
192+
return result;
193+
}
194+
195+
int invokeVoidInt4(const char *name, int value1, int value2, int value3, int value4, var_s *retval) {
196+
int result = 0;
197+
if (_instance != nullptr) {
198+
jmethodID method = env->GetMethodID(_clazz, name, "(IIII)V");
199+
if (method != nullptr) {
200+
env->CallVoidMethod(_instance, method, value1, value2, value3, value4);
201+
}
202+
if (!checkException(retval)) {
203+
result = 1;
204+
}
205+
}
206+
return result;
207+
}
208+
181209
// void foo(void)
182210
int invokeVoidVoid(const char *name, var_s *retval) {
183211
int result = 0;
@@ -197,6 +225,14 @@ struct IOTask {
197225
return invokeVoidInt("open", pin, retval);
198226
}
199227

228+
int open2(int pin1, int pin2, var_s *retval) {
229+
return invokeVoidInt2("open", pin1, pin2, retval);
230+
}
231+
232+
int open4(int pin1, int pin2, int pin3, int pin4, var_s *retval) {
233+
return invokeVoidInt4("open", pin1, pin2, pin3, pin4, retval);
234+
}
235+
200236
private:
201237
jclass _clazz;
202238
jobject _instance;
@@ -221,7 +257,7 @@ static int get_io_class_id(var_s *map, var_s *retval) {
221257
static int cmd_twimaster_writeread(var_s *self, int argc, slib_par_t *arg, var_s *retval) {
222258
int result = 0;
223259
if (argc != 0) {
224-
error(retval, "writeRead", 0);
260+
error(retval, "TwiMaster.writeRead", 0);
225261
} else {
226262
// TODO
227263
//result = ioioTask->invokeVoidVoid("waitForDisconnect", retval);
@@ -232,10 +268,14 @@ static int cmd_twimaster_writeread(var_s *self, int argc, slib_par_t *arg, var_s
232268
static int cmd_spimaster_write(var_s *self, int argc, slib_par_t *arg, var_s *retval) {
233269
int result = 0;
234270
if (argc != 2) {
235-
error(retval, "writeRead", 0);
271+
error(retval, "SpiMaster.write", 0);
236272
} else {
237-
// TODO
238-
//result = ioioTask->invokeVoidVoid("waitForDisconnect", retval);
273+
int id = get_io_class_id(self, retval);
274+
if (id != -1) {
275+
auto address = get_param_int(argc, arg, 0, 0);
276+
auto data = get_param_int(argc, arg, 1, 0);
277+
result = _ioTaskMap.at(id).invokeVoidInt2("write", address, data, retval);
278+
}
239279
}
240280
return result;
241281
}
@@ -251,13 +291,14 @@ static void create_spimaster(var_t *map) {
251291
#include "api.h"
252292

253293
FUNC_SIG lib_func[] = {
254-
{1, 2, "OPENANALOGINPUT", cmd_openanaloginput},
255-
{1, 2, "OPENCAPSENSE", cmd_opencapsense},
256-
{1, 2, "OPENDIGITALINPUT", cmd_opendigitalinput},
257-
{1, 2, "OPENDIGITALOUTPUT", cmd_opendigitaloutput},
258-
{1, 2, "OPENPULSEINPUT", cmd_openpulseinput},
259-
{1, 2, "OPENPWMOUTPUT", cmd_openpwmoutput},
260-
{1, 2, "OPENTWIMASTER", cmd_opentwimaster},
294+
{1, 1, "OPENANALOGINPUT", cmd_openanaloginput},
295+
{1, 1, "OPENCAPSENSE", cmd_opencapsense},
296+
{1, 1, "OPENDIGITALINPUT", cmd_opendigitalinput},
297+
{1, 1, "OPENDIGITALOUTPUT", cmd_opendigitaloutput},
298+
{1, 1, "OPENPULSEINPUT", cmd_openpulseinput},
299+
{1, 1, "OPENPWMOUTPUT", cmd_openpwmoutput},
300+
{2, 2, "OPENTWIMASTER", cmd_opentwimaster},
301+
{4, 4, "OPENSPIMASTER", cmd_openspimaster},
261302
};
262303

263304
FUNC_SIG lib_proc[] = {

ioio/mkapi.bas

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,22 @@ sub generate_constructor(byref obj)
102102
end
103103

104104
sub generate_open_function(byref obj)
105+
local pin
106+
local openFunc = "open(pin1, "
105107
print "static int cmd_open" + lower(obj.name) + "(int argc, slib_par_t *params, var_t *retval) {"
106108
print " int result;"
107-
print " int pin = get_param_int(argc, params, 0, 0);"
109+
print " int pin1 = get_param_int(argc, params, 0, -1);"
110+
if (isnumber(obj.pins) && obj.pins > 0) then
111+
openFunc = "open" + obj.pins + "(pin1, "
112+
for pin = 2 to obj.pins
113+
openFunc += "pin" + pin + ", "
114+
print " int pin" + pin + " = get_param_int(argc, params, " + (pin - 1) + ", -1);"
115+
next
116+
endif
108117
print " int id = ++nextId;"
109118
print " IOTask &instance = _ioTaskMap[id];"
110119
print " if (instance.create(CLASS_" + upper(obj.name) + ") &&"
111-
print " instance.open(pin, retval)) {"
120+
print " instance." + openFunc + "retval)) {"
112121
print " map_init_id(retval, id, CLASS_IOTASK_ID);"
113122
print " create_" + lower(obj.name) + "(retval);"
114123
print " result = 1;"

0 commit comments

Comments
 (0)