Skip to content

Commit 44973de

Browse files
committed
Create Digital output handling
1 parent 562effe commit 44973de

13 files changed

Lines changed: 174 additions & 79 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.AnalogInput'
8+
# javap -s -p -cp target/ioio-1.0.jar 'net.sourceforge.smallbasic.ioio.input.AnalogInput'
99
#
1010

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

ioio/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,26 @@ ACTION=="add", SUBSYSTEM=="tty", SUBSYSTEMS=="usb", ATTRS{idVendor}=="1b4f", ATT
1212
ATTRS{idVendor}=="1b4f", ATTRS{idProduct}=="0008", ENV{ID_MM_DEVICE_IGNORE}="1"
1313
```
1414

15-
looper:
16-
15+
looper:
16+
1717
init
1818
AnalogInput in = ioio.openAnalogInput(pinNum);
1919

2020
loop:
21-
22-
21+
22+
2323
```
2424
SB-> openAnalogInput(pin) -> JNI -> JAVA -> (pin)
2525
service.methods.push(function (ioio) { ioio.openAnalogInput(pin) });
2626
service.initMethods.push(function (ioio) { ioio.openAnalogInput(pin) });
2727
returns handle
28-
28+
2929
init:
3030
initmethods.pop().invoke();
31-
31+
3232
loop
3333
methods.pop().invoke()
34-
34+
3535
```
3636

3737

ioio/main.cpp

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,8 @@ JavaVM *jvm;
2020
jclass analogInputClass = nullptr;
2121
jobject analogInput = nullptr;
2222

23-
// void callVoidMethod(jmethodID method, int n) {
24-
// env->CallVoidMethod(instance, method, n);
25-
// }
26-
27-
// jmethodID getMethodID(const char *name, const char *signature) {
28-
// return env->GetMethodID(clazz, name, signature);
29-
// }
30-
31-
// jmethodID getStaticMethodId(const char *name, const char *signature) {
32-
// return env->GetStaticMethodID(clazz, name, signature);
33-
// }
23+
jclass digitalOutputClass = nullptr;
24+
jobject digitalOutput = nullptr;
3425

3526
jobject createInstance(jclass clazz) {
3627
jobject result = nullptr;
@@ -94,8 +85,37 @@ static int cmd_openanaloginput(int argc, slib_par_t *params, var_t *retval) {
9485
return result;
9586
}
9687

88+
static int cmd_opendigitaloutput(int argc, slib_par_t *params, var_t *retval) {
89+
int pin = get_param_int(argc, params, 0, 0);
90+
int result = 0;
91+
if (digitalOutput == nullptr && jvm->AttachCurrentThread((void**)&env, nullptr) == JNI_OK) {
92+
digitalOutputClass = env->FindClass("net/sourceforge/smallbasic/ioio/DigitalOutput");
93+
digitalOutput = createInstance(digitalOutputClass);
94+
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+
}
107+
}
108+
jvm->DetachCurrentThread();
109+
}
110+
if (!result) {
111+
error(retval, "openDigitalOutput() failed");
112+
}
113+
return result;
114+
}
115+
97116
FUNC_SIG lib_func[] = {
98117
{1, 1, "OPENANALOGINPUT", cmd_openanaloginput},
118+
{1, 1, "OPENDIGITALOUTPUT", cmd_opendigitaloutput},
99119
};
100120

101121
FUNC_SIG lib_proc[] = {

ioio/pom.xml

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,43 +10,17 @@
1010
<maven.compiler.source>1.8</maven.compiler.source>
1111
<maven.compiler.target>1.8</maven.compiler.target>
1212
</properties>
13-
14-
<!--
15-
<repositories>
16-
<repository>
17-
<id>sparetimelabs</id>
18-
<url>https://www.sparetimelabs.com/maven2</url>
19-
</repository>
20-
</repositories>
21-
-->
2213
<dependencies>
23-
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
24-
<dependency>
25-
<groupId>com.google.guava</groupId>
26-
<artifactId>guava</artifactId>
27-
<version>33.0.0-jre</version>
28-
</dependency>
29-
<!-- https://mvnrepository.com/artifact/com.github.ytai.ioio/IOIOLibPC -->
30-
3114
<dependency>
3215
<groupId>com.github.ytai.ioio</groupId>
3316
<artifactId>IOIOLibCore</artifactId>
3417
<version>5.07</version>
3518
</dependency>
36-
<!-- https://mvnrepository.com/artifact/com.github.purejavacomm/purejavacomm -->
3719
<dependency>
3820
<groupId>com.github.purejavacomm</groupId>
3921
<artifactId>purejavacomm</artifactId>
4022
<version>1.0.2.RELEASE</version>
4123
</dependency>
42-
43-
<!--
44-
<dependency>
45-
<groupId>com.sparetimelabs</groupId>
46-
<artifactId>purejavacomm</artifactId>
47-
<version>1.0.1</version>
48-
</dependency>
49-
-->
5024
</dependencies>
5125
<build>
5226
<plugins>

ioio/samples/led.bas

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import ioio
22

3-
n = ioio.openAnalogInput(1)
4-
delay 5000
3+
out = ioio.openDigitalOutput(0)
4+
value = false
5+
while (true)
6+
print "setting LED "+ value
7+
out.write(value)
8+
value = !value
9+
delay 1000
10+
wend
11+
12+
513

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

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,13 @@
1010

1111
public abstract class AbstractLooper implements IOIOLooper {
1212
static final String TAG = "AbstractLooper";
13-
final BlockingQueue<Consumer<IOIO>> messageQueue;
14-
final String connectionType;
15-
final Object extra;
16-
IOIO ioio;
17-
boolean isIncompatible;
18-
int pin;
13+
protected IOIO ioio;
14+
protected boolean isIncompatible;
15+
protected int pin;
16+
private final BlockingQueue<Consumer<IOIO>> queue;
1917

20-
public AbstractLooper(BlockingQueue<Consumer<IOIO>> messageQueue, String connectionType, Object extra, int pin) {
21-
this.messageQueue = messageQueue;
22-
this.connectionType = connectionType;
23-
this.extra = extra;
18+
public AbstractLooper(BlockingQueue<Consumer<IOIO>> queue, int pin) {
19+
this.queue = queue;
2420
this.pin = pin;
2521
this.ioio = null;
2622
this.isIncompatible = false;
@@ -45,11 +41,10 @@ public void incompatible(IOIO ioio) {
4541
}
4642

4743
@Override
48-
public void loop() throws InterruptedException {
49-
Log.i(TAG, "loop entered");
50-
if (!messageQueue.isEmpty()) {
44+
public void loop() throws InterruptedException, ConnectionLostException {
45+
if (!queue.isEmpty()) {
5146
try {
52-
messageQueue.take().invoke(ioio);
47+
queue.take().invoke(ioio);
5348
}
5449
catch (ConnectionLostException | IncompatibilityException e) {
5550
throw new RuntimeException(e);

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
import java.util.concurrent.LinkedBlockingQueue;
99

1010
public abstract class AbstractLooperProvider implements IOIOLooperProvider {
11-
static final String TAG = "AbstractLooperProvider";
12-
static final BlockingQueue<Consumer<IOIO>> QUEUE = new LinkedBlockingQueue<>();
13-
final ConnectionController controller;
14-
boolean ready;
11+
static final protected String TAG = "AbstractLooperProvider";
12+
static final protected BlockingQueue<Consumer<IOIO>> QUEUE = new LinkedBlockingQueue<>();
13+
final protected ConnectionController controller;
14+
protected boolean ready;
1515

1616
protected AbstractLooperProvider() {
1717
this.controller = new ConnectionController(this);

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

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

35
import ioio.lib.spi.Log;
46
import ioio.lib.util.IOIOLooper;
57

68
public class AnalogInput extends AbstractLooperProvider {
7-
private static final String TAG = "AnalogController";
8-
private int pin;
9+
private static final String TAG = "AnalogInput";
10+
private AnalogInputLooper looper;
911

1012
public AnalogInput() {
1113
super();
12-
Log.i(TAG, "created AnalogController");
14+
Log.i(TAG, "created AnalogInput");
1315
}
1416

1517
@Override
1618
public IOIOLooper createIOIOLooper(String connectionType, Object extra) {
17-
return new AnalogInputLooper(QUEUE, connectionType, extra, pin);
19+
return looper;
1820
}
1921

2022
public void openInput(int pin) {
2123
Log.i(TAG, "openInput");
22-
this.pin = pin;
24+
looper = new AnalogInputLooper(QUEUE, pin);
2325
start();
2426
}
2527
}

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
package net.sourceforge.smallbasic.ioio;
1+
package net.sourceforge.smallbasic.ioio.input;
2+
3+
import net.sourceforge.smallbasic.ioio.AbstractLooper;
4+
import net.sourceforge.smallbasic.ioio.Consumer;
25

36
import ioio.lib.api.AnalogInput;
47
import ioio.lib.api.IOIO;
@@ -8,13 +11,11 @@
811
import java.util.concurrent.BlockingQueue;
912

1013
public class AnalogInputLooper extends AbstractLooper {
14+
private static final String TAG = "AnalogInput";
1115
private AnalogInput analogInput;
1216

13-
public AnalogInputLooper(BlockingQueue<Consumer<IOIO>> messageQueue,
14-
String connectionType,
15-
Object extra,
16-
int pin) {
17-
super(messageQueue, connectionType, extra, pin);
17+
public AnalogInputLooper(BlockingQueue<Consumer<IOIO>> queue, int pin) {
18+
super(queue, pin);
1819
Log.i(TAG, "creating AnalogInputLooper");
1920
}
2021

@@ -23,7 +24,7 @@ public void setup(IOIO ioio) {
2324
Log.i(TAG, "setup entered");
2425
super.setup(ioio);
2526
try {
26-
this.analogInput = ioio.openAnalogInput(pin);
27+
analogInput = ioio.openAnalogInput(pin);
2728
}
2829
catch (ConnectionLostException e) {
2930
throw new RuntimeException(e);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package net.sourceforge.smallbasic.ioio.output;
2+
3+
import net.sourceforge.smallbasic.ioio.AbstractLooperProvider;
4+
5+
import ioio.lib.spi.Log;
6+
import ioio.lib.util.IOIOLooper;
7+
8+
public class DigitalOutput extends AbstractLooperProvider {
9+
private static final String TAG = "DigitalOutput";
10+
private DigitalOutputLooper outputLooper;
11+
12+
public DigitalOutput() {
13+
super();
14+
Log.i(TAG, "created DigitalOutput");
15+
}
16+
17+
public void setValue(boolean value) {
18+
outputLooper.setValue(value);
19+
}
20+
21+
@Override
22+
public IOIOLooper createIOIOLooper(String type, Object extra) {
23+
return outputLooper;
24+
}
25+
26+
public void openOutput(int pin) {
27+
Log.i(TAG, "openOutput");
28+
outputLooper = new DigitalOutputLooper(QUEUE, pin);
29+
start();
30+
}
31+
}

0 commit comments

Comments
 (0)