Skip to content

Commit ee0ef1d

Browse files
committed
IOIO: refactoring so there's only one looper
1 parent 9e8da42 commit ee0ef1d

File tree

13 files changed

+81
-19
lines changed

13 files changed

+81
-19
lines changed

ioio/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ see: https://github.com/ytai/ioio/wiki
88
|---------|---------------|
99
| void ioio.beginBatch()|Start a batch of operations. This is strictly an optimization and will not change functionality|
1010
| void ioio.disconnect()|Closes the connection to the board, or aborts a connection process started with waitForConnect()|
11+
| void ioio.start()|Starts processing|
1112
| void ioio.endBatch()|End a batch of operations.|
1213
| void ioio.hardReset()|Equivalent to disconnecting and reconnecting the board power supply.|
1314
| void ioio.softReset()|Resets the entire state (returning to initial state), without dropping the connection.|

ioio/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ FUNC_SIG lib_proc[] = {
234234
{0, 0, "HARDRESET", cmd_hardreset},
235235
{0, 0, "SOFTRESET", cmd_softreset},
236236
{0, 0, "SYNC", cmd_sync},
237+
{0, 0, "START", cmd_start},
237238
{0, 0, "WAITFORCONNECT", cmd_waitforconnect},
238239
{0, 0, "WAITFORDISCONNECT", cmd_waitfordisconnect},
239240
};

ioio/mkapi.bas

Lines changed: 1 addition & 1 deletion
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-
ioioApi = ["beginBatch", "disconnect", "endBatch", "hardReset", "softReset", "sync", "waitForConnect", "waitForDisconnect"]
8+
ioioApi = ["beginBatch", "disconnect", "endBatch", "hardReset", "softReset", "start", "sync", "waitForConnect", "waitForDisconnect"]
99

1010
func get_method_name(method)
1111
local result

ioio/mkdoc.bas

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ ioioApi = [{
1010
},{
1111
"name": "disconnect",
1212
"comment": "Closes the connection to the board, or aborts a connection process started with waitForConnect()"
13+
},{
14+
"name": "start",
15+
"comment": "Starts processing"
1316
},{
1417
"name": "endBatch",
1518
"comment": "End a batch of operations."

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public int available() throws ConnectionLostException {
2929

3030
@Override
3131
public void close() {
32+
IOService.getInstance().removeTask(this);
3233
input.close();
3334
input = null;
3435
}
@@ -63,6 +64,11 @@ public float getVoltageSync() {
6364
return invoke(AnalogInput::getVoltageSync);
6465
}
6566

67+
@Override
68+
public int getPin() {
69+
return pin;
70+
}
71+
6672
@Override
6773
public void loop() throws InterruptedException, ConnectionLostException {
6874
if (!queue.isEmpty()) {
@@ -78,7 +84,7 @@ public void loop() throws InterruptedException, ConnectionLostException {
7884
public void open(int pin) throws IOException {
7985
Log.i(TAG, "openInput");
8086
this.pin = pin;
81-
IOService.getInstance().addTask(this, pin);
87+
IOService.getInstance().addTask(this);
8288
}
8389

8490
@Override

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,24 @@ public CapSenseImpl() {
1919

2020
@Override
2121
public void close() {
22+
IOService.getInstance().removeTask(this);
2223
capSense.close();
2324
capSense = null;
2425
}
2526

27+
@Override
28+
public int getPin() {
29+
return pin;
30+
}
31+
2632
@Override
2733
public void loop() throws ConnectionLostException, InterruptedException {
2834
}
2935

3036
public void open(int pin) throws IOException {
3137
Log.i(TAG, "open");
3238
this.pin = pin;
33-
IOService.getInstance().addTask(this, pin);
39+
IOService.getInstance().addTask(this);
3440
}
3541

3642
@Override

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,16 @@ public DigitalInputImpl() {
2323

2424
@Override
2525
public void close() {
26+
IOService.getInstance().removeTask(this);
2627
input.close();
2728
input = null;
2829
}
2930

31+
@Override
32+
public int getPin() {
33+
return pin;
34+
}
35+
3036
@Override
3137
public void loop() throws InterruptedException, ConnectionLostException {
3238
value = input.read();
@@ -35,7 +41,7 @@ public void loop() throws InterruptedException, ConnectionLostException {
3541
public void open(int pin) throws IOException {
3642
Log.i(TAG, "open");
3743
this.pin = pin;
38-
IOService.getInstance().addTask(this, pin);
44+
IOService.getInstance().addTask(this);
3945
}
4046

4147
@Override

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,16 @@ public DigitalOutputImpl() {
2020

2121
@Override
2222
public void close() {
23+
IOService.getInstance().removeTask(this);
2324
output.close();
2425
output = null;
2526
}
2627

28+
@Override
29+
public int getPin() {
30+
return pin;
31+
}
32+
2733
@Override
2834
public void loop() throws InterruptedException, ConnectionLostException {
2935
output.write(value);
@@ -32,7 +38,7 @@ public void loop() throws InterruptedException, ConnectionLostException {
3238
public void open(int pin) throws IOException {
3339
Log.i(TAG, "open");
3440
this.pin = pin;
35-
IOService.getInstance().addTask(this, pin);
41+
IOService.getInstance().addTask(this);
3642
}
3743

3844
@Override

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

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

3+
import java.io.IOException;
34
import java.util.concurrent.BlockingQueue;
45
import java.util.concurrent.CountDownLatch;
56
import java.util.concurrent.LinkedBlockingQueue;
@@ -14,8 +15,9 @@ public class IOIOImpl implements IOTask {
1415
private final BlockingQueue<Consumer<IOIO>> queue = new LinkedBlockingQueue<>();
1516
private IOIO ioio;
1617

17-
public IOIOImpl() {
18+
public IOIOImpl() throws IOException {
1819
Log.i(TAG, "created");
20+
IOService.getInstance().addTask(this);
1921
}
2022

2123
public void beginBatch() {
@@ -30,6 +32,11 @@ public void endBatch() {
3032
invoke(IOIO::endBatch);
3133
}
3234

35+
@Override
36+
public int getPin() {
37+
return -1;
38+
}
39+
3340
public void hardReset() {
3441
invoke(IOIO::hardReset);
3542
}
@@ -57,6 +64,10 @@ public void softReset() {
5764
invoke(IOIO::softReset);
5865
}
5966

67+
public void start() {
68+
IOService.getInstance().start();
69+
}
70+
6071
public void sync() {
6172
invoke(IOIO::sync);
6273
}
@@ -70,7 +81,7 @@ public void waitForDisconnect() {
7081
}
7182

7283
protected void invoke(Consumer<IOIO> consumer) {
73-
final CountDownLatch latch = new CountDownLatch(1);
84+
CountDownLatch latch = new CountDownLatch(1);
7485
try {
7586
queue.put(ioio -> {
7687
consumer.invoke(ioio);

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public static IOService getInstance() {
3434
return instance;
3535
}
3636

37-
public void addTask(IOTask ioTask, int pin) throws IOException {
38-
registerPin(pin);
37+
public void addTask(IOTask ioTask) throws IOException {
38+
registerPin(ioTask.getPin());
3939
ioTasks.add(ioTask);
4040
}
4141

@@ -44,18 +44,24 @@ public IOIOLooper createIOIOLooper(String connectionType, Object extra) {
4444
return looper;
4545
}
4646

47+
public void removeTask(IOTask task) {
48+
ioTasks.remove(task);
49+
}
50+
4751
public void start() {
4852
connectionController.start();
4953
}
5054

5155
private void registerPin(int pin) throws IOException {
52-
if (pin < 0 || pin >= MAX_PINS) {
53-
throw new IOException("invalid pin: " + pin);
54-
}
55-
if (usedPins[pin] != null) {
56-
throw new IOException("pin already used: " + pin);
56+
if (pin != -1) {
57+
if (pin < 0 || pin >= MAX_PINS) {
58+
throw new IOException("invalid pin: " + pin);
59+
}
60+
if (usedPins[pin] != null) {
61+
throw new IOException("pin already used: " + pin);
62+
}
63+
usedPins[pin] = true;
5764
}
58-
usedPins[pin] = true;
5965
}
6066

6167
public class IOServiceLooper implements IOIOLooper {

0 commit comments

Comments
 (0)