Skip to content

Commit 70ba3b0

Browse files
committed
IOIO: partially implement some java io classes
1 parent bdf0286 commit 70ba3b0

File tree

10 files changed

+358
-92
lines changed

10 files changed

+358
-92
lines changed

ioio/main.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,10 @@ SBLIB_API void sblib_free(int cls_id, int id) {
196196
if (id != -1) {
197197
switch (cls_id) {
198198
case CLASS_IOCLASS:
199-
_classMap.erase(id);
199+
if (id != -1 && _classMap.find(id) != _classMap.end()) {
200+
_classMap.at(id).invokeVoidVoid("close", nullptr);
201+
_classMap.erase(id);
202+
}
200203
break;
201204
}
202205
}

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

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

3-
import java.util.concurrent.BlockingQueue;
4-
import java.util.concurrent.CountDownLatch;
5-
import java.util.concurrent.LinkedBlockingQueue;
6-
73
import ioio.lib.api.IOIO;
84
import ioio.lib.spi.Log;
95
import ioio.lib.util.IOIOLooperProvider;
106

7+
import java.util.concurrent.BlockingQueue;
8+
import java.util.concurrent.CountDownLatch;
9+
import java.util.concurrent.LinkedBlockingQueue;
10+
1111
public abstract class AbstractLooperProvider implements IOIOLooperProvider {
1212
static final protected String TAG = "AbstractLooperProvider";
1313
static final protected BlockingQueue<Consumer<IOIO>> QUEUE = new LinkedBlockingQueue<>();
@@ -23,6 +23,11 @@ public void beginBatch() {
2323
invoke(IOIO::beginBatch);
2424
}
2525

26+
public void close() {
27+
this.controller.stop();
28+
this.ready = false;
29+
}
30+
2631
public void disconnect() {
2732
invoke(IOIO::disconnect);
2833
}
@@ -39,11 +44,6 @@ public void softReset() {
3944
invoke(IOIO::softReset);
4045
}
4146

42-
public void stop() {
43-
this.controller.stop();
44-
this.ready = false;
45-
}
46-
4747
public void sync() {
4848
invoke(IOIO::sync);
4949
}

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
package net.sourceforge.smallbasic.ioio;
22

3+
import ioio.lib.api.IOIO;
4+
import ioio.lib.api.exception.ConnectionLostException;
35
import ioio.lib.spi.Log;
46
import ioio.lib.util.IOIOLooper;
57

8+
import java.util.concurrent.BlockingQueue;
9+
610
public class AnalogInput extends AbstractLooperProvider {
711
private static final String TAG = "AnalogInput";
812
private AnalogInputLooper looper;
913

1014
public AnalogInput() {
1115
super();
12-
Log.i(TAG, "created AnalogInput");
16+
Log.i(TAG, "created");
17+
}
18+
19+
@Override
20+
public void close() {
21+
super.close();
22+
this.looper.close();
23+
this.looper = null;
1324
}
1425

1526
@Override
@@ -22,4 +33,30 @@ public void open(int pin) {
2233
looper = new AnalogInputLooper(QUEUE, pin);
2334
start();
2435
}
36+
37+
static class AnalogInputLooper extends AbstractLooper {
38+
private ioio.lib.api.AnalogInput analogInput;
39+
40+
public AnalogInputLooper(BlockingQueue<Consumer<IOIO>> queue, int pin) {
41+
super(queue, pin);
42+
Log.i(TAG, "creating AnalogInputLooper");
43+
}
44+
45+
public void close() {
46+
this.analogInput.close();
47+
this.analogInput = null;
48+
}
49+
50+
@Override
51+
public void setup(IOIO ioio) {
52+
Log.i(TAG, "setup entered");
53+
super.setup(ioio);
54+
try {
55+
analogInput = ioio.openAnalogInput(pin);
56+
}
57+
catch (ConnectionLostException e) {
58+
throw new RuntimeException(e);
59+
}
60+
}
61+
}
2562
}

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

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package net.sourceforge.smallbasic.ioio;
2+
3+
import ioio.lib.api.IOIO;
4+
import ioio.lib.api.exception.ConnectionLostException;
5+
import ioio.lib.spi.Log;
6+
import ioio.lib.util.IOIOLooper;
7+
8+
import java.util.concurrent.BlockingQueue;
9+
10+
public class CapSense extends AbstractLooperProvider {
11+
private static final String TAG = "CapSense";
12+
private CapSenseLooper looper;
13+
14+
public CapSense() {
15+
super();
16+
Log.i(TAG, "created");
17+
}
18+
19+
public void close() {
20+
super.close();
21+
this.looper.close();
22+
looper = null;
23+
}
24+
25+
@Override
26+
public IOIOLooper createIOIOLooper(String type, Object extra) {
27+
return looper;
28+
}
29+
30+
public void open(int pin) {
31+
Log.i(TAG, "open");
32+
looper = new CapSenseLooper(QUEUE, pin);
33+
start();
34+
}
35+
36+
static class CapSenseLooper extends AbstractLooper {
37+
private ioio.lib.api.CapSense capSense;
38+
39+
public CapSenseLooper(BlockingQueue<Consumer<IOIO>> queue, int pin) {
40+
super(queue, pin);
41+
}
42+
43+
public void close() {
44+
this.capSense.close();
45+
this.capSense = null;
46+
}
47+
48+
@Override
49+
public void setup(IOIO ioio) {
50+
Log.i(TAG, "setup entered");
51+
super.setup(ioio);
52+
try {
53+
this.capSense = ioio.openCapSense(pin);
54+
}
55+
catch (ConnectionLostException e) {
56+
throw new RuntimeException(e);
57+
}
58+
}
59+
}
60+
}
61+
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package net.sourceforge.smallbasic.ioio;
2+
3+
import ioio.lib.api.IOIO;
4+
import ioio.lib.api.exception.ConnectionLostException;
5+
import ioio.lib.spi.Log;
6+
import ioio.lib.util.IOIOLooper;
7+
8+
import java.util.concurrent.BlockingQueue;
9+
10+
public class DigitalInput extends AbstractLooperProvider {
11+
private static final String TAG = "DigitalInput";
12+
private DigitalInputLooper looper;
13+
14+
public DigitalInput() {
15+
super();
16+
Log.i(TAG, "created DigitalInput");
17+
}
18+
19+
public void close() {
20+
super.close();
21+
this.looper.close();
22+
looper = null;
23+
}
24+
25+
@Override
26+
public IOIOLooper createIOIOLooper(String type, Object extra) {
27+
return looper;
28+
}
29+
30+
public void open(int pin) {
31+
Log.i(TAG, "open");
32+
looper = new DigitalInputLooper(QUEUE, pin);
33+
start();
34+
}
35+
36+
public int read() {
37+
return looper.getValue() ? 1 : 0;
38+
}
39+
40+
static class DigitalInputLooper extends AbstractLooper {
41+
private ioio.lib.api.DigitalInput input;
42+
private volatile boolean value;
43+
44+
public DigitalInputLooper(BlockingQueue<Consumer<IOIO>> queue, int pin) {
45+
super(queue, pin);
46+
value = false;
47+
Log.i(TAG, "creating DigitalInputLooper");
48+
}
49+
50+
public void close() {
51+
this.input.close();
52+
this.input = null;
53+
}
54+
55+
@Override
56+
public void loop() throws InterruptedException, ConnectionLostException {
57+
super.loop();
58+
value = input.read();
59+
Thread.sleep(10);
60+
}
61+
62+
@Override
63+
public void setup(IOIO ioio) {
64+
Log.i(TAG, "setup entered");
65+
super.setup(ioio);
66+
try {
67+
this.input = ioio.openDigitalInput(pin);
68+
}
69+
catch (ConnectionLostException e) {
70+
throw new RuntimeException(e);
71+
}
72+
}
73+
74+
boolean getValue() {
75+
return this.value;
76+
}
77+
}
78+
}

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

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package net.sourceforge.smallbasic.ioio;
22

3+
import ioio.lib.api.IOIO;
4+
import ioio.lib.api.exception.ConnectionLostException;
35
import ioio.lib.spi.Log;
46
import ioio.lib.util.IOIOLooper;
57

8+
import java.util.concurrent.BlockingQueue;
9+
610
public class DigitalOutput extends AbstractLooperProvider {
711
private static final String TAG = "DigitalOutput";
812
private DigitalOutputLooper outputLooper;
@@ -13,7 +17,8 @@ public DigitalOutput() {
1317
}
1418

1519
public void close() {
16-
stop();
20+
super.close();
21+
outputLooper.close();
1722
outputLooper = null;
1823
}
1924

@@ -22,10 +27,6 @@ public IOIOLooper createIOIOLooper(String type, Object extra) {
2227
return outputLooper;
2328
}
2429

25-
public int isReady() {
26-
return outputLooper.isReady() ? 1 : 0;
27-
}
28-
2930
public void open(int pin) {
3031
Log.i(TAG, "open");
3132
outputLooper = new DigitalOutputLooper(QUEUE, pin);
@@ -35,4 +36,43 @@ public void open(int pin) {
3536
public void write(int value) {
3637
outputLooper.setValue(value == 1);
3738
}
39+
40+
static class DigitalOutputLooper extends AbstractLooper {
41+
private ioio.lib.api.DigitalOutput output;
42+
private volatile boolean value;
43+
44+
public DigitalOutputLooper(BlockingQueue<Consumer<IOIO>> queue, int pin) {
45+
super(queue, pin);
46+
value = false;
47+
Log.i(TAG, "creating DigitalOutputLooper");
48+
}
49+
50+
public void close() {
51+
output.close();
52+
output = null;
53+
}
54+
55+
@Override
56+
public void loop() throws InterruptedException, ConnectionLostException {
57+
super.loop();
58+
output.write(value);
59+
Thread.sleep(10);
60+
}
61+
62+
@Override
63+
public void setup(IOIO ioio) {
64+
Log.i(TAG, "setup entered");
65+
super.setup(ioio);
66+
try {
67+
this.output = ioio.openDigitalOutput(pin);
68+
}
69+
catch (ConnectionLostException e) {
70+
throw new RuntimeException(e);
71+
}
72+
}
73+
74+
void setValue(boolean value) {
75+
this.value = value;
76+
}
77+
}
3878
}

0 commit comments

Comments
 (0)