Skip to content

Commit 77c9179

Browse files
committed
refactoring
1 parent 44973de commit 77c9179

9 files changed

Lines changed: 87 additions & 117 deletions

File tree

ioio/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# Download the GNU Public License (GPL) from www.gnu.org
66
#
77
# export LD_LIBRARY_PATH=/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/amd64/server:$LD_LIBRARY_PATH
8-
# javap -s -p -cp target/ioio-1.0.jar 'net.sourceforge.smallbasic.ioio.input.AnalogInput'
8+
# javap -s -p -cp target/ioio-1.0.jar 'net.sourceforge.smallbasic.ioio.AnalogInput'
99
#
1010

1111
AM_CXXFLAGS=-fno-rtti -std=c++14

ioio/README.md

Lines changed: 21 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,55 @@
1+
## Links
12

23
https://github.com/ytai/ioio/wiki
3-
4-
54
https://github.com/ytai/ioio/blob/master/applications/HelloIOIOService/src/main/java/ioio/examples/hello_service/HelloIOIOService.java
65
https://github.com/ytai/ioio/wiki/IOIOLib-Core-API
76

7+
8+
## Setup
9+
810
/etc/udev/rules.d/50-ioio.rules
911

1012
```
1113
ACTION=="add", SUBSYSTEM=="tty", SUBSYSTEMS=="usb", ATTRS{idVendor}=="1b4f", ATTRS{idProduct}=="0008", SYMLINK+="IOIO%n", MODE="666"
1214
ATTRS{idVendor}=="1b4f", ATTRS{idProduct}=="0008", ENV{ID_MM_DEVICE_IGNORE}="1"
1315
```
1416

15-
looper:
17+
## Investigating connection issues
1618

17-
init
18-
AnalogInput in = ioio.openAnalogInput(pinNum);
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:
1921

20-
loop:
22+
## Check dmesg Logs:
2123

24+
Open a terminal and run the following command to view the kernel logs:
2225

2326
```
24-
SB-> openAnalogInput(pin) -> JNI -> JAVA -> (pin)
25-
service.methods.push(function (ioio) { ioio.openAnalogInput(pin) });
26-
service.initMethods.push(function (ioio) { ioio.openAnalogInput(pin) });
27-
returns handle
28-
29-
init:
30-
initmethods.pop().invoke();
31-
32-
loop
33-
methods.pop().invoke()
34-
27+
dmesg | tail
3528
```
3629

37-
38-
When you plug an IOIO-OTG board into a USB port on your Linux machine, you can check which serial port has been assigned to the device by using various commands and tools. Here are a few methods:
39-
40-
Check dmesg Logs:
41-
42-
Open a terminal and run the following command to view the kernel logs:
43-
44-
bash
45-
Copy code
46-
dmesg | tail
4730
Look for lines related to the connected USB device. The assigned serial port might be mentioned in the logs.
4831

49-
Use lsusb and udevadm:
32+
## Use lsusb
5033

5134
Use lsusb to list the connected USB devices and note the device ID.
5235

53-
bash
54-
Copy code
36+
```
5537
lsusb
56-
Use udevadm to get detailed information about the device:
38+
```
39+
40+
## Use udevadm to get detailed information about the device:
5741

58-
bash
59-
Copy code
42+
```
6043
udevadm info -a -n /dev/ttyUSB0
44+
```
6145
Replace /dev/ttyUSB0 with the appropriate device file.
6246

63-
Check /dev/ Directory:
47+
## Check /dev/ Directory:
6448

6549
After connecting the device, run the following command to list the /dev/ directory:
6650

67-
bash
68-
Copy code
51+
``
6952
ls /dev/ttyUSB*
70-
This should show you the assigned serial port(s) for connected USB devices.
53+
``
7154

72-
Use minicom or screen:
73-
74-
Install minicom if it's not already installed:
75-
76-
bash
77-
Copy code
78-
sudo apt-get install minicom
79-
Run minicom:
80-
81-
bash
82-
Copy code
83-
minicom -D /dev/ttyUSB0
84-
Replace /dev/ttyUSB0 with the actual serial port.
85-
86-
Alternatively, use screen:
87-
88-
bash
89-
Copy code
90-
screen /dev/ttyUSB0
91-
Replace /dev/ttyUSB0 with the actual serial port.
55+
This should show you the assigned serial port(s) for connected USB devices.

ioio/main.cpp

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// This program is distributed under the terms of the GPL v2.0 or later
44
// Download the GNU Public License (GPL) from www.gnu.org
55
//
6-
// Copyright(C) 2020 Chris Warren-Smith
6+
// Copyright(C) 2024 Chris Warren-Smith
77

88
#include "config.h"
99
#include "include/var.h"
@@ -38,28 +38,30 @@ jobject createInstance(jclass clazz) {
3838
return result;
3939
}
4040

41-
int sblib_init(const char *sourceFile) {
42-
JavaVMInitArgs vm_args;
43-
JavaVMOption options[2];
44-
options[0].optionString = (char *)"-Djava.class.path=./target/ioio-1.0-jar-with-dependencies.jar";
45-
options[1].optionString = (char *)"-Dioio.SerialPorts=ACM0";
46-
vm_args.version = JNI_VERSION_1_8;
47-
vm_args.nOptions = 2;
48-
vm_args.options = options;
49-
vm_args.ignoreUnrecognized = 0;
50-
int result = (JNI_CreateJavaVM(&jvm, (void **)&env, &vm_args) == JNI_OK);
51-
if (!result) {
52-
fprintf(stderr, "Unable to create JVM\n");
41+
int invokeIV(jclass clazz, jobject instance, const char *name, int value) {
42+
int result = 0;
43+
if (instance != nullptr) {
44+
jmethodID method = env->GetMethodID(clazz, name, "(I)V");
45+
if (method != nullptr) {
46+
env->CallVoidMethod(instance, method, value);
47+
result = 1;
48+
} else {
49+
env->ExceptionDescribe();
50+
}
5351
}
5452
return result;
5553
}
5654

57-
void sblib_close(void) {
58-
jvm->DestroyJavaVM();
59-
analogInputClass = nullptr;
60-
analogInput = nullptr;
61-
env = nullptr;
62-
jvm = nullptr;
55+
int invokeOpen(jclass clazz, jobject instance, int pin) {
56+
return invokeIV(clazz, instance, "open", pin);
57+
}
58+
59+
static void cmd_digital_output_write(var_s *self, var_s *retval) {
60+
int value = 0;
61+
if (digitalOutput != nullptr && jvm->AttachCurrentThread((void**)&env, nullptr) == JNI_OK) {
62+
invokeIV(digitalOutputClass, digitalOutput, "write", value);
63+
jvm->DetachCurrentThread();
64+
}
6365
}
6466

6567
static int cmd_openanaloginput(int argc, slib_par_t *params, var_t *retval) {
@@ -68,15 +70,7 @@ static int cmd_openanaloginput(int argc, slib_par_t *params, var_t *retval) {
6870
if (analogInput == nullptr && jvm->AttachCurrentThread((void**)&env, nullptr) == JNI_OK) {
6971
analogInputClass = env->FindClass("net/sourceforge/smallbasic/ioio/AnalogInput");
7072
analogInput = createInstance(analogInputClass);
71-
if (analogInput != nullptr) {
72-
jmethodID method = env->GetMethodID(analogInputClass, "openInput", "(I)V");
73-
if (method != nullptr) {
74-
env->CallVoidMethod(analogInput, method, pin);
75-
result = 1;
76-
} else {
77-
env->ExceptionDescribe();
78-
}
79-
}
73+
result = invokeOpen(analogInputClass, analogInput, pin);
8074
jvm->DetachCurrentThread();
8175
}
8276
if (!result) {
@@ -92,23 +86,15 @@ static int cmd_opendigitaloutput(int argc, slib_par_t *params, var_t *retval) {
9286
digitalOutputClass = env->FindClass("net/sourceforge/smallbasic/ioio/DigitalOutput");
9387
digitalOutput = createInstance(digitalOutputClass);
9488
if (digitalOutput != nullptr) {
95-
jmethodID method = env->GetMethodID(digitalOutputClass, "openOutput", "(I)V");
96-
if (method != nullptr) {
97-
env->CallVoidMethod(digitalOutput, method, pin);
98-
map_init(retval);
99-
100-
//v_setint(map_add_var(var, "width", 0), width);
101-
//v_setint(map_add_var(var, "height", 0), height);
102-
103-
result = 1;
104-
} else {
105-
env->ExceptionDescribe();
106-
}
89+
result = invokeOpen(digitalOutputClass, digitalOutput, pin);
10790
}
10891
jvm->DetachCurrentThread();
10992
}
11093
if (!result) {
11194
error(retval, "openDigitalOutput() failed");
95+
} else {
96+
map_init(retval);
97+
v_create_func(retval, "write", cmd_digital_output_write);
11298
}
11399
return result;
114100
}
@@ -129,4 +115,27 @@ SBLIB_API int sblib_func_count() {
129115
return (sizeof(lib_func) / sizeof(lib_func[0]));
130116
}
131117

118+
int sblib_init(const char *sourceFile) {
119+
JavaVMInitArgs vm_args;
120+
JavaVMOption options[2];
121+
options[0].optionString = (char *)"-Djava.class.path=./target/ioio-1.0-jar-with-dependencies.jar";
122+
options[1].optionString = (char *)"-Dioio.SerialPorts=ACM0";
123+
vm_args.version = JNI_VERSION_1_8;
124+
vm_args.nOptions = 2;
125+
vm_args.options = options;
126+
vm_args.ignoreUnrecognized = 0;
127+
int result = (JNI_CreateJavaVM(&jvm, (void **)&env, &vm_args) == JNI_OK);
128+
if (!result) {
129+
fprintf(stderr, "Unable to create JVM\n");
130+
}
131+
return result;
132+
}
133+
134+
void sblib_close(void) {
135+
jvm->DestroyJavaVM();
136+
analogInputClass = nullptr;
137+
analogInput = nullptr;
138+
env = nullptr;
139+
jvm = nullptr;
140+
}
132141

ioio/src/main/java/net/sourceforge/smallbasic/ioio/input/AnalogInput.java renamed to ioio/src/main/java/net/sourceforge/smallbasic/ioio/AnalogInput.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
package net.sourceforge.smallbasic.ioio.input;
2-
3-
import net.sourceforge.smallbasic.ioio.AbstractLooperProvider;
1+
package net.sourceforge.smallbasic.ioio;
42

53
import ioio.lib.spi.Log;
64
import ioio.lib.util.IOIOLooper;
@@ -19,7 +17,7 @@ public IOIOLooper createIOIOLooper(String connectionType, Object extra) {
1917
return looper;
2018
}
2119

22-
public void openInput(int pin) {
20+
public void open(int pin) {
2321
Log.i(TAG, "openInput");
2422
looper = new AnalogInputLooper(QUEUE, pin);
2523
start();

ioio/src/main/java/net/sourceforge/smallbasic/ioio/input/AnalogInputLooper.java renamed to ioio/src/main/java/net/sourceforge/smallbasic/ioio/AnalogInputLooper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package net.sourceforge.smallbasic.ioio.input;
1+
package net.sourceforge.smallbasic.ioio;
22

33
import net.sourceforge.smallbasic.ioio.AbstractLooper;
44
import net.sourceforge.smallbasic.ioio.Consumer;

ioio/src/main/java/net/sourceforge/smallbasic/ioio/output/DigitalOutput.java renamed to ioio/src/main/java/net/sourceforge/smallbasic/ioio/DigitalOutput.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
package net.sourceforge.smallbasic.ioio.output;
2-
3-
import net.sourceforge.smallbasic.ioio.AbstractLooperProvider;
1+
package net.sourceforge.smallbasic.ioio;
42

53
import ioio.lib.spi.Log;
64
import ioio.lib.util.IOIOLooper;
@@ -14,18 +12,23 @@ public DigitalOutput() {
1412
Log.i(TAG, "created DigitalOutput");
1513
}
1614

17-
public void setValue(boolean value) {
18-
outputLooper.setValue(value);
15+
public void close() {
16+
stop();
17+
outputLooper = null;
1918
}
2019

2120
@Override
2221
public IOIOLooper createIOIOLooper(String type, Object extra) {
2322
return outputLooper;
2423
}
2524

26-
public void openOutput(int pin) {
25+
public void open(int pin) {
2726
Log.i(TAG, "openOutput");
2827
outputLooper = new DigitalOutputLooper(QUEUE, pin);
2928
start();
3029
}
30+
31+
public void setValue(boolean value) {
32+
outputLooper.setValue(value);
33+
}
3134
}

ioio/src/main/java/net/sourceforge/smallbasic/ioio/output/DigitalOutputLooper.java renamed to ioio/src/main/java/net/sourceforge/smallbasic/ioio/DigitalOutputLooper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package net.sourceforge.smallbasic.ioio.output;
1+
package net.sourceforge.smallbasic.ioio;
22

33
import net.sourceforge.smallbasic.ioio.AbstractLooper;
44
import net.sourceforge.smallbasic.ioio.Consumer;
@@ -12,7 +12,7 @@
1212
class DigitalOutputLooper extends AbstractLooper {
1313
private static final String TAG = "DigitalOutput";
1414
private ioio.lib.api.DigitalOutput output;
15-
private boolean value;
15+
private volatile boolean value;
1616

1717
public DigitalOutputLooper(BlockingQueue<Consumer<IOIO>> queue, int pin) {
1818
super(queue, pin);
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package net.sourceforge.smallbasic.ioio;
22

3-
import net.sourceforge.smallbasic.ioio.input.AnalogInput;
4-
53
class AnalogInputTest {
64
public static void main(String[] args) throws InterruptedException {
75
//jtermios.JTermios.JTermiosLogging.setLogLevel(4);
86
AnalogInput input = new AnalogInput();
9-
input.openInput(1);
7+
input.open(1);
108
Thread.sleep(30000);
119
}
1210
}

ioio/src/test/java/net/sourceforge/smallbasic/ioio/DigitalOutputTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package net.sourceforge.smallbasic.ioio;
22

3-
import net.sourceforge.smallbasic.ioio.output.DigitalOutput;
4-
53
import ioio.lib.api.IOIO;
64

75
class DigitalOutputTest {
86
public static void main(String[] args) throws InterruptedException {
97
DigitalOutput output = new DigitalOutput();
10-
output.openOutput(IOIO.LED_PIN);
8+
output.open(IOIO.LED_PIN);
119
boolean value = true;
1210
while (true) {
1311
output.setValue(value);

0 commit comments

Comments
 (0)