Skip to content

Commit dc0a70f

Browse files
committed
IOIO: ioio methods now invoked direct from import
1 parent 02a8c70 commit dc0a70f

File tree

5 files changed

+68
-55
lines changed

5 files changed

+68
-55
lines changed

ioio/README.md

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,24 @@
22

33
see: https://github.com/ytai/ioio/wiki
44

5+
## IOIO
6+
7+
| Name | Description |
8+
|---------|---------------|
9+
| void ioio.beginBatch()|Start a batch of operations. This is strictly an optimization and will not change functionality|
10+
| void ioio.disconnect()|Closes the connection to the board, or aborts a connection process started with waitForConnect()|
11+
| void ioio.endBatch()|End a batch of operations.|
12+
| void ioio.hardReset()|Equivalent to disconnecting and reconnecting the board power supply.|
13+
| void ioio.softReset()|Resets the entire state (returning to initial state), without dropping the connection.|
14+
| void ioio.sync()|Sends a message to the IOIO and waits for an echo.|
15+
| void ioio.waitForConnect()|Establishes connection with the IOIO board.|
16+
| void ioio.waitForDisconnect()|Blocks until IOIO has been disconnected and all connection-related resources have been freed so that a new connection can be attempted.|
17+
518
## AnalogInput
619

720
This interface represents AnalogInput functionality, providing methods to obtain analog input readings and buffered samples.
821

9-
`io = openAnalogInput(pin [, 1])`
22+
`io = ioio.openAnalogInput(pin)`
1023

1124
| Name | Description |
1225
|---------|---------------|
@@ -25,7 +38,7 @@ This interface represents AnalogInput functionality, providing methods to obtain
2538

2639
This interface represents PulseInput functionality, providing methods for pulse and frequency measurements.
2740

28-
`io = openPulseInput(pin [, 1])`
41+
`io = ioio.openPulseInput(pin)`
2942

3043
| Name | Description |
3144
|---------|---------------|
@@ -39,7 +52,7 @@ This interface represents PulseInput functionality, providing methods for pulse
3952

4053
This interface represents DigitalInput functionality, providing methods to read digital input pin values and wait for specific logical levels.
4154

42-
`io = openDigitalInput(pin [, 1])`
55+
`io = ioio.openDigitalInput(pin)`
4356

4457
| Name | Description |
4558
|---------|---------------|
@@ -50,7 +63,7 @@ This interface represents DigitalInput functionality, providing methods to read
5063

5164
This interface represents the CapSense functionality, allowing capacitance readings and threshold-based operations.
5265

53-
`io = openCapSense(pin [, 1])`
66+
`io = ioio.openCapSense(pin)`
5467

5568
| Name | Description |
5669
|---------|---------------|
@@ -66,7 +79,7 @@ This interface represents the CapSense functionality, allowing capacitance readi
6679

6780
A pin used for digital output. A digital output pin can be used to generate logic-level signals. DigitalOutput instances are obtained by calling IOIO#openDigitalOutput. The value of the pin is set by calling write. The instance is alive since its creation. If the connection with the IOIO drops at any point, the instance transitions to a disconnected state, in which every attempt to use the pin (except close()) will throw a ConnectionLostException. Whenever close() is invoked the instance may no longer be used. Any resources associated with it are freed and can be reused. Typical usage:
6881

69-
`io = openDigitalOutput(pin [, 1])`
82+
`io = ioio.openDigitalOutput(pin)`
7083

7184
| Name | Description |
7285
|---------|---------------|
@@ -76,24 +89,10 @@ A pin used for digital output. A digital output pin can be used to generate logi
7689

7790
A pin used for PWM (Pulse-Width Modulation) output. A PWM pin produces a logic-level PWM signal. These signals are typically used for simulating analog outputs for controlling the intensity of LEDs, the rotation speed of motors, etc. They are also frequently used for controlling hobby servo motors. PwmOutput instances are obtained by calling IOIO#openPwmOutput. When used for motors and LEDs, a frequency of several KHz is typically used, where there is a trade-off between switching power-loses and smoothness of operation. The pulse width is typically set by specifying the duty cycle, with the setDutyCycle method. A duty cycle of 0 is \"off\", a duty cycle of 1 is \"on\", and every intermediate value produces an intermediate intensity. Please note that any devices consuming more than 20mA of current (e.g. motors) should not by directly connected the the IOIO pins, but rather through an amplification circuit suited for the specific load. When used for hobby servos, the PWM signal is rather used for encoding of the desired angle the motor should go to. By standard, a 100Hz signal is used and the pulse width is varied between 1ms and 2ms (corresponding to both extremes of the shaft angle), using setPulseWidth. The instance is alive since its creation. If the connection with the IOIO drops at any point, the instance transitions to a disconnected state, in which every attempt to use the pin (except close()) will throw a ConnectionLostException. Whenever close() is invoked the instance may no longer be used. Any resources associated with it are freed and can be reused. Typical usage (fading LED):
7891

79-
`io = openPwmOutput(pin [, 1])`
92+
`io = ioio.openPwmOutput(pin)`
8093

8194
| Name | Description |
8295
|---------|---------------|
8396
|void setDutyCycle(float)|Sets the duty cycle of the PWM output. The duty cycle is defined to be the pulse width divided by the total cycle period. For absolute control of the pulse with, consider using setPulseWidth.|
8497
|void setPulseWidth(float)|Sets the pulse width of the PWM output. The pulse width is duration of the high-time within a single period of the signal. For relative control of the pulse with, consider using setDutyCycle.|
8598

86-
## Optional second argument
87-
88-
Option second argument `1` causes the following additional IOIO methods to be attached:
89-
90-
| Name | Description |
91-
|---------|---------------|
92-
| void beginBatch()|Start a batch of operations. This is strictly an optimization and will not change functionality|
93-
| void disconnect()|Closes the connection to the board, or aborts a connection process started with waitForConnect()|
94-
| void endBatch()|End a batch of operations.|
95-
| void hardReset()|Equivalent to disconnecting and reconnecting the board power supply.|
96-
| void softReset()|Resets the entire state (returning to initial state), without dropping the connection.|
97-
| void sync()|Sends a message to the IOIO and waits for an echo.|
98-
| void waitForConnect()|Establishes connection with the IOIO board.|
99-
| void waitForDisconnect()|Blocks until IOIO has been disconnected and all connection-related resources have been freed so that a new connection can be attempted.|

ioio/main.cpp

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

19+
struct IOClass;
20+
1921
JNIEnv *env;
2022
JavaVM *jvm;
23+
IOClass *ioioClass;
2124
int nextId = 1;
2225

2326
#define CLASS_ANALOGINPUT "net/sourceforge/smallbasic/ioio/AnalogInputImpl"
@@ -26,7 +29,8 @@ int nextId = 1;
2629
#define CLASS_PULSEINPUT "net/sourceforge/smallbasic/ioio/PulseInputImpl"
2730
#define CLASS_PWMOUTPUT "net/sourceforge/smallbasic/ioio/PwmOutputImpl"
2831
#define CLASS_CAPSENSE "net/sourceforge/smallbasic/ioio/CapsenseImpl"
29-
#define CLASS_IOCLASS 1
32+
#define CLASS_IOIO "net/sourceforge/smallbasic/ioio/IOIOImpl"
33+
#define CLASS_IOCLASS_ID 1
3034

3135
struct IOClass {
3236
IOClass(): _clazz(nullptr), _instance(nullptr) {}
@@ -221,6 +225,14 @@ FUNC_SIG lib_func[] = {
221225
{1, 2, "OPENDIGITALOUTPUT", cmd_opendigitaloutput},
222226
{1, 2, "OPENPULSEINPUT", cmd_openpulseinput},
223227
{1, 2, "OPENPWMOUTPUT", cmd_openpwmoutput},
228+
{0, 0, "BEGINBATCH", cmd_beginbatch},
229+
{0, 0, "DISCONNECT", cmd_disconnect},
230+
{0, 0, "ENDBATCH", cmd_endbatch},
231+
{0, 0, "HARDRESET", cmd_hardreset},
232+
{0, 0, "SOFTRESET", cmd_softreset},
233+
{0, 0, "SYNC", cmd_sync},
234+
{0, 0, "WAITFORCONNECT", cmd_waitforconnect},
235+
{0, 0, "WAITFORDISCONNECT", cmd_waitfordisconnect},
224236
};
225237

226238
FUNC_SIG lib_proc[] = {};
@@ -250,13 +262,19 @@ int sblib_init(const char *sourceFile) {
250262
if (!result) {
251263
fprintf(stderr, "Failed to create JVM\n");
252264
}
265+
266+
ioioClass = new IOClass();
267+
if (!ioioClass || !ioioClass->create(CLASS_IOIO)) {
268+
fprintf(stderr, "Failed to IOIOClass\n");
269+
result = 0;
270+
}
253271
return result;
254272
}
255273

256274
SBLIB_API void sblib_free(int cls_id, int id) {
257275
if (id != -1) {
258276
switch (cls_id) {
259-
case CLASS_IOCLASS:
277+
case CLASS_IOCLASS_ID:
260278
if (id != -1 && _classMap.find(id) != _classMap.end()) {
261279
_classMap.at(id).invokeVoidVoid("close", nullptr);
262280
_classMap.erase(id);
@@ -267,6 +285,9 @@ SBLIB_API void sblib_free(int cls_id, int id) {
267285
}
268286

269287
void sblib_close(void) {
288+
if (ioioClass) {
289+
delete ioioClass;
290+
}
270291
if (!_classMap.empty()) {
271292
fprintf(stderr, "IOClass leak detected\n");
272293
_classMap.clear();

ioio/mkapi.bas

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,19 @@ sub generate_command(objName, method)
7878
print
7979
end
8080

81+
sub generate_ioio_command(name)
82+
print "static int cmd_" + lower(name) + "(int argc, slib_par_t *arg, var_s *retval) {"
83+
print " int result = 0;"
84+
print " if (argc != 0) {"
85+
print " error(retval, \"" + name + "\", 0);"
86+
print " } else {"
87+
print " result = ioioClass->invokeVoidVoid(\"" + name + "\", retval);"
88+
print " }"
89+
print " return result;"
90+
print "}"
91+
print
92+
end
93+
8194
sub generate_constructor(byref obj)
8295
print "static void create_" + lower(obj.name) + "(var_t *map) {"
8396
local method
@@ -88,16 +101,6 @@ sub generate_constructor(byref obj)
88101
print
89102
end
90103

91-
sub generate_ioio_constructor()
92-
print "static void create_ioio(var_t *map) {"
93-
local s
94-
for s in ioioApi
95-
print " v_create_callback(map, \"" + s + "\", cmd_" + lower(s) + ");"
96-
next
97-
print "}"
98-
print
99-
end
100-
101104
sub generate_open_function(byref obj)
102105
print "static int cmd_open" + lower(obj.name) + "(int argc, slib_par_t *params, var_t *retval) {"
103106
print " int result;"
@@ -106,11 +109,8 @@ sub generate_open_function(byref obj)
106109
print " IOClass &instance = _classMap[id];"
107110
print " if (instance.create(CLASS_" + upper(obj.name) + ") &&"
108111
print " instance.open(pin, retval)) {"
109-
print " map_init_id(retval, id, CLASS_IOCLASS);"
112+
print " map_init_id(retval, id, CLASS_IOCLASS_ID);"
110113
print " create_" + lower(obj.name) + "(retval);"
111-
print " if (get_param_int(argc, params, 1, 0)) {"
112-
print " create_ioio(retval);"
113-
print " }"
114114
print " result = 1;"
115115
print " } else {"
116116
print " _classMap.erase(id);"
@@ -129,18 +129,13 @@ for obj in api
129129
next
130130

131131
for s in ioioApi
132-
method.name = s
133-
method.rtn = "void"
134-
method.arg = "void"
135-
generate_command(0, method)
132+
generate_ioio_command(s)
136133
next
137134

138135
for obj in api
139136
generate_constructor(obj)
140137
next
141138

142-
generate_ioio_constructor()
143-
144139
for obj in api
145140
generate_open_function(obj)
146141
next

ioio/mkdoc.bas

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,21 @@ print
6161
print "see: https://github.com/ytai/ioio/wiki"
6262
print
6363

64+
print "## IOIO"
65+
print
66+
print "| Name | Description |"
67+
print "|---------|---------------|"
68+
for obj in ioioApi
69+
print "| void ioio." + obj.name + "()|" + obj.comment + "|"
70+
next s
71+
print
72+
6473
for obj in api
6574
print "## " + obj.name
6675
print
6776
print obj.comment
6877
print
69-
print "`io = open" + obj.name + "(pin [, 1])`"
78+
print "`io = ioio.open" + obj.name + "(pin)`"
7079
print ""
7180
print "| Name | Description |"
7281
print "|---------|---------------|"
@@ -76,14 +85,3 @@ for obj in api
7685
print
7786
next
7887

79-
print "## Optional second argument"
80-
print
81-
print "Option second argument `1` causes the following additional IOIO methods to be attached:"
82-
print
83-
print "| Name | Description |"
84-
print "|---------|---------------|"
85-
for obj in ioioApi
86-
print "| void " + obj.name + "()|" + obj.comment + "|"
87-
next s
88-
89-

ioio/samples/led.bas

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

55
print "wait for connect"
6-
out.waitForConnect()
6+
ioio.waitForConnect()
77
print "ready"
88

99
value = false

0 commit comments

Comments
 (0)