Skip to content

Commit bdf0286

Browse files
committed
IOIO: generate api - wip
1 parent 567b801 commit bdf0286

File tree

6 files changed

+225
-159
lines changed

6 files changed

+225
-159
lines changed

ioio/Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ lib_LTLIBRARIES = libioio.la
2525
libioio_la_SOURCES = ../include/param.cpp ../include/hashmap.cpp ../include/apiexec.cpp main.cpp $(generated)
2626
libioio_la_LDFLAGS = -module -rpath '$(libdir)' @PLATFORM_LDFLAGS@ -L/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/amd64/server -ljvm
2727

28-
$(generated): api.json mkapi.bas
28+
$(generated): api.json mkapi.bas mkdoc.bas
2929
$(sbasic) mkapi.bas > $@
30+
$(sbasic) mkdoc.bas > README.md
3031
@touch main.cpp

ioio/README.md

Lines changed: 81 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,101 @@
1-
## Links
1+
# IOIO for SmallBASIC
22

3-
https://github.com/ytai/ioio/wiki
4-
https://github.com/ytai/ioio/blob/master/applications/HelloIOIOService/src/main/java/ioio/examples/hello_service/HelloIOIOService.java
5-
https://github.com/ytai/ioio/wiki/IOIOLib-Core-API
3+
see: https://github.com/ytai/ioio/wiki
64

5+
## AnalogInput
76

8-
## Setup
7+
This interface represents AnalogInput functionality, providing methods to obtain analog input readings and buffered samples.
98

10-
/etc/udev/rules.d/50-ioio.rules
9+
`io = openAnalogInput(pin [, 1])`
1110

12-
```
13-
ACTION=="add", SUBSYSTEM=="tty", SUBSYSTEMS=="usb", ATTRS{idVendor}=="1b4f", ATTRS{idProduct}=="0008", SYMLINK+="IOIO%n", MODE="666"
14-
ATTRS{idVendor}=="1b4f", ATTRS{idProduct}=="0008", ENV{ID_MM_DEVICE_IGNORE}="1"
15-
```
11+
| Name | Description |
12+
|---------|---------------|
13+
|float getVoltage(void)|Gets the analog input reading, as an absolute voltage in Volt units.|
14+
|float getVoltageSync(void)|This is very similar to getVoltage(), but will wait for a new sample to arrive before returning.|
15+
|float getReference(void)|Gets the maximum value against which read() values are scaled.|
16+
|float read(void)|Gets the analog input reading, as a scaled real value between 0 and 1.|
17+
|float readSync(void)|This is very similar to read(), but will wait for a new sample to arrive before returning.|
18+
|void setBuffer(int)|Initializes or destroys an internal buffer, used for queuing sampled data.|
19+
|int getOverflowCount(void)|Gets the number of samples that have been dropped as a result of overflow.|
20+
|int available(void)|Gets the number of samples currently in the buffer. Reading that many samples is guaranteed not to block.|
21+
|float readBuffered(void)|Read a sample from the internal buffer. This method will block until at least one sample is available.|
22+
|float getVoltageBuffered(void)|Read a sample from the internal buffer. This method will block until at least one sample is available.|
23+
|float getSampleRate(void)|Gets the sample rate used for obtaining buffered samples.|
1624

17-
## Investigating connection issues
25+
## PulseInput
1826

19-
When you plug an IOIO-OTG board into a USB port on your Linux machine, you can check which serial port has been
20-
assigned to the device by using various commands and tools. Here are a few methods:
27+
This interface represents PulseInput functionality, providing methods for pulse and frequency measurements.
2128

22-
## Check dmesg Logs:
29+
`io = openPulseInput(pin [, 1])`
2330

24-
Open a terminal and run the following command to view the kernel logs:
31+
| Name | Description |
32+
|---------|---------------|
33+
|float getDuration(void)|Gets the pulse duration in case of pulse measurement mode, or the period in case of frequency mode.|
34+
|float getDurationSync(void)|This is very similar to getDuration(), but will wait for a new sample to arrive before returning.|
35+
|float getDurationBuffered(void)|Reads a single measurement from the queue. If the queue is empty, will block until more data arrives.|
36+
|float waitPulseGetDuration(void)|@deprecated Please use getDurationBuffered() instead.|
37+
|float getFrequency(void)|Gets the momentary frequency of the measured signal. When scaling is used, this is compensated for here.|
38+
|float getFrequencySync(void)|This is very similar to getFrequency(), but will wait for a new sample to arrive before returning.|
2539

26-
```
27-
dmesg | tail
28-
```
40+
## DigitalInput
2941

30-
Look for lines related to the connected USB device. The assigned serial port might be mentioned in the logs.
42+
This interface represents DigitalInput functionality, providing methods to read digital input pin values and wait for specific logical levels.
3143

32-
## Use lsusb
44+
`io = openDigitalInput(pin [, 1])`
3345

34-
Use lsusb to list the connected USB devices and note the device ID.
46+
| Name | Description |
47+
|---------|---------------|
48+
|int read(void)|Read the value sensed on the pin. May block for a few milliseconds if called right after creation of the instance.|
49+
|void waitForValue(int)|Block until a desired logical level is sensed. The calling thread can be interrupted for aborting this operation.|
3550

36-
```
37-
lsusb
38-
```
51+
## CapSense
3952

40-
## Use udevadm to get detailed information about the device:
53+
This interface represents the CapSense functionality, allowing capacitance readings and threshold-based operations.
4154

42-
```
43-
udevadm info -a -n /dev/ttyUSB0
44-
```
45-
Replace /dev/ttyUSB0 with the appropriate device file.
55+
`io = openCapSense(pin [, 1])`
4656

47-
## Check /dev/ Directory:
57+
| Name | Description |
58+
|---------|---------------|
59+
|float read(void)|Gets the capacitance reading. It typically takes a few milliseconds.|
60+
|float readSync(void)|This is very similar to read(), but will wait for a new sample to arrive before returning.|
61+
|void setFilterCoef(float)|Sets the low-pass filter coefficient. This coefficient is the typical time constant of the system.|
62+
|void waitOver(float)|Block until sensed capacitance becomes greater than a given threshold.|
63+
|void waitOverSync(float)|This is very similar to waitOver(float), but will wait for a new sample to arrive before returning.|
64+
|void waitUnder(float)|Block until sensed capacitance becomes less than a given threshold.|
65+
|void waitUnderSync(float)|This is very similar to waitUnder(float), but will wait for a new sample to arrive before returning.|
4866

49-
After connecting the device, run the following command to list the /dev/ directory:
67+
## DigitalOutput
5068

51-
``
52-
ls /dev/ttyUSB*
53-
``
69+
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:
5470

55-
This should show you the assigned serial port(s) for connected USB devices.
71+
`io = openDigitalOutput(pin [, 1])`
72+
73+
| Name | Description |
74+
|---------|---------------|
75+
|void write(int)|The output. true is logical \"HIGH\", false is logical \"LOW\".|
76+
77+
## PwmOutput
78+
79+
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):
80+
81+
`io = openPwmOutput(pin [, 1])`
82+
83+
| Name | Description |
84+
|---------|---------------|
85+
|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.|
86+
|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.|
87+
88+
## Optional second argument
89+
90+
Option second argument `1` causes the following additional IOIO methods to be attached:
91+
92+
| Name | Description |
93+
|---------|---------------|
94+
| void beginBatch()|Start a batch of operations. This is strictly an optimization and will not change functionality|
95+
| void disconnect()|Closes the connection to the board, or aborts a connection process started with waitForConnect()|
96+
| void endBatch()|End a batch of operations.|
97+
| void hardReset()|Equivalent to disconnecting and reconnecting the board power supply.|
98+
| void softReset()|Resets the entire state (returning to initial state), without dropping the connection.|
99+
| void sync()|Sends a message to the IOIO and waits for an echo.|
100+
| void waitForConnect()|Establishes connection with the IOIO board.|
101+
| 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: 11 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ JNIEnv *env;
2020
JavaVM *jvm;
2121
int nextId = 1;
2222

23-
#define CLASS_ANALOG_INPUT "net/sourceforge/smallbasic/ioio/AnalogInput"
24-
#define CLASS_DIGITAL_INPUT "net/sourceforge/smallbasic/ioio/DigitalInput"
25-
#define CLASS_DIGITAL_OUTPUT "net/sourceforge/smallbasic/ioio/DigitalOutput"
26-
#define CLASS_PULSE_INPUT "net/sourceforge/smallbasic/ioio/PulseInput"
27-
#define CLASS_PWM_OUTPUT "net/sourceforge/smallbasic/ioio/PwmOutput"
23+
#define CLASS_ANALOGINPUT "net/sourceforge/smallbasic/ioio/AnalogInput"
24+
#define CLASS_DIGITALINPUT "net/sourceforge/smallbasic/ioio/DigitalInput"
25+
#define CLASS_DIGITALOUTPUT "net/sourceforge/smallbasic/ioio/DigitalOutput"
26+
#define CLASS_PULSEINPUT "net/sourceforge/smallbasic/ioio/PulseInput"
27+
#define CLASS_PWMOUTPUT "net/sourceforge/smallbasic/ioio/PwmOutput"
2828
#define CLASS_CAPSENSE "net/sourceforge/smallbasic/ioio/Capsense"
2929
#define CLASS_IOCLASS 1
3030

@@ -153,121 +153,13 @@ static int get_io_class_id(var_s *map, var_s *retval) {
153153

154154
#include "api.h"
155155

156-
static int cmd_openanaloginput(int argc, slib_par_t *params, var_t *retval) {
157-
int result;
158-
int pin = get_param_int(argc, params, 0, 0);
159-
int id = ++nextId;
160-
IOClass &input = _classMap[id];
161-
if (input.create(CLASS_ANALOG_INPUT) &&
162-
input.open(pin, retval)) {
163-
map_init_id(retval, id, CLASS_IOCLASS);
164-
create_analoginput(retval);
165-
result = 1;
166-
} else {
167-
_classMap.erase(id);
168-
error(retval, "openAnalogInput() failed");
169-
result = 0;
170-
}
171-
return result;
172-
}
173-
174-
static int cmd_opencapsense(int argc, slib_par_t *params, var_t *retval) {
175-
int result;
176-
int pin = get_param_int(argc, params, 0, 0);
177-
int id = ++nextId;
178-
IOClass &output = _classMap[id];
179-
if (output.create(CLASS_CAPSENSE) &&
180-
output.open(pin, retval)) {
181-
map_init_id(retval, id, CLASS_IOCLASS);
182-
create_capsense(retval);
183-
result = 1;
184-
} else {
185-
_classMap.erase(id);
186-
error(retval, "openCapsense() failed");
187-
result = 0;
188-
}
189-
return result;
190-
}
191-
192-
static int cmd_opendigitalinput(int argc, slib_par_t *params, var_t *retval) {
193-
int result;
194-
int pin = get_param_int(argc, params, 0, 0);
195-
int id = ++nextId;
196-
IOClass &output = _classMap[id];
197-
if (output.create(CLASS_DIGITAL_INPUT) &&
198-
output.open(pin, retval)) {
199-
map_init_id(retval, id, CLASS_IOCLASS);
200-
create_digitalinput(retval);
201-
result = 1;
202-
} else {
203-
_classMap.erase(id);
204-
error(retval, "openDigitalInput() failed");
205-
result = 0;
206-
}
207-
return result;
208-
}
209-
210-
static int cmd_opendigitaloutput(int argc, slib_par_t *params, var_t *retval) {
211-
int result;
212-
int pin = get_param_int(argc, params, 0, 0);
213-
int id = ++nextId;
214-
IOClass &output = _classMap[id];
215-
if (output.create(CLASS_DIGITAL_OUTPUT) &&
216-
output.open(pin, retval)) {
217-
map_init_id(retval, id, CLASS_IOCLASS);
218-
create_digitaloutput(retval);
219-
result = 1;
220-
} else {
221-
_classMap.erase(id);
222-
error(retval, "openDigitalOutput() failed");
223-
result = 0;
224-
}
225-
return result;
226-
}
227-
228-
static int cmd_openpulseinput(int argc, slib_par_t *params, var_t *retval) {
229-
int result;
230-
int pin = get_param_int(argc, params, 0, 0);
231-
int id = ++nextId;
232-
IOClass &output = _classMap[id];
233-
if (output.create(CLASS_PULSE_INPUT) &&
234-
output.open(pin, retval)) {
235-
map_init_id(retval, id, CLASS_IOCLASS);
236-
create_pulseinput(retval);
237-
result = 1;
238-
} else {
239-
_classMap.erase(id);
240-
error(retval, "openPulseInput() failed");
241-
result = 0;
242-
}
243-
return result;
244-
}
245-
246-
static int cmd_openpwmoutput(int argc, slib_par_t *params, var_t *retval) {
247-
int result;
248-
int pin = get_param_int(argc, params, 0, 0);
249-
int id = ++nextId;
250-
IOClass &output = _classMap[id];
251-
if (output.create(CLASS_PWM_OUTPUT) &&
252-
output.open(pin, retval)) {
253-
map_init_id(retval, id, CLASS_IOCLASS);
254-
create_pwmoutput(retval);
255-
result = 1;
256-
} else {
257-
_classMap.erase(id);
258-
error(retval, "openPwmOutput() failed");
259-
result = 0;
260-
}
261-
return result;
262-
}
263-
264156
FUNC_SIG lib_func[] = {
265-
{1, 1, "OPENANALOGINPUT", cmd_openanaloginput},
266-
{1, 1, "OPENCAPSENSE", cmd_opencapsense},
267-
{1, 1, "OPENDIGITALINPUT", cmd_opendigitalinput},
268-
{1, 1, "OPENDIGITALOUTPUT", cmd_opendigitaloutput},
269-
{1, 1, "OPENPULSEINPUT", cmd_openpulseinput},
270-
{1, 1, "OPENPWMOUTPUT", cmd_openpwmoutput},
157+
{1, 2, "OPENANALOGINPUT", cmd_openanaloginput},
158+
{1, 2, "OPENCAPSENSE", cmd_opencapsense},
159+
{1, 2, "OPENDIGITALINPUT", cmd_opendigitalinput},
160+
{1, 2, "OPENDIGITALOUTPUT", cmd_opendigitaloutput},
161+
{1, 2, "OPENPULSEINPUT", cmd_openpulseinput},
162+
{1, 2, "OPENPWMOUTPUT", cmd_openpwmoutput},
271163
};
272164

273165
FUNC_SIG lib_proc[] = {};

ioio/mkapi.bas

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

8-
baseApi = ["beginBatch", "disconnect", "endBatch", "hardReset", "softReset", "sync", "waitForConnect", "waitForDisconnect"]
8+
ioioApi = ["beginBatch", "disconnect", "endBatch", "hardReset", "softReset", "sync", "waitForConnect", "waitForDisconnect"]
99

1010
func get_method_name(method)
1111
local result
@@ -80,20 +80,51 @@ sub generate_constructor(byref obj)
8080
for method in obj.methods
8181
print " v_create_callback(map, \"" + method.name + "\", cmd_" + lower(obj.name) + "_" + lower(method.name) + ");"
8282
next
83-
for s in baseApi
83+
print "}"
84+
print
85+
end
86+
87+
sub generate_ioio_constructor()
88+
print "static void create_ioio(var_t *map) {"
89+
local s
90+
for s in ioioApi
8491
print " v_create_callback(map, \"" + s + "\", cmd_" + lower(s) + ");"
8592
next
8693
print "}"
8794
print
8895
end
8996

97+
sub generate_open_function(byref obj)
98+
print "static int cmd_open" + lower(obj.name) + "(int argc, slib_par_t *params, var_t *retval) {"
99+
print " int result;"
100+
print " int pin = get_param_int(argc, params, 0, 0);"
101+
print " int id = ++nextId;"
102+
print " IOClass &instance = _classMap[id];"
103+
print " if (instance.create(CLASS_" + upper(obj.name) + ") &&"
104+
print " instance.open(pin, retval)) {"
105+
print " map_init_id(retval, id, CLASS_IOCLASS);"
106+
print " create_" + lower(obj.name) + "(retval);"
107+
print " if (get_param_int(argc, params, 1, 0)) {"
108+
print " create_ioio(retval);"
109+
print " }"
110+
print " result = 1;"
111+
print " } else {"
112+
print " _classMap.erase(id);"
113+
print " error(retval, \"open" + obj.name + "() failed\");"
114+
print " result = 0;"
115+
print " }"
116+
print " return result;"
117+
print "}"
118+
print
119+
end
120+
90121
for obj in api
91122
for method in obj.methods
92123
generate_command(obj.name, method)
93124
next
94125
next
95126

96-
for s in baseApi
127+
for s in ioioApi
97128
method.name = s
98129
method.rtn = "void"
99130
method.arg = "void"
@@ -103,3 +134,10 @@ next
103134
for obj in api
104135
generate_constructor(obj)
105136
next
137+
138+
generate_ioio_constructor()
139+
140+
for obj in api
141+
generate_open_function(obj)
142+
next
143+

0 commit comments

Comments
 (0)