Skip to content

Commit cfb9dee

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

File tree

13 files changed

+81
-132
lines changed

13 files changed

+81
-132
lines changed

ioio/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ 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|
1211
| void ioio.endBatch()|End a batch of operations.|
1312
| void ioio.hardReset()|Equivalent to disconnecting and reconnecting the board power supply.|
1413
| void ioio.softReset()|Resets the entire state (returning to initial state), without dropping the connection.|

ioio/main.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,6 @@ 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},
238237
{0, 0, "WAITFORCONNECT", cmd_waitforconnect},
239238
{0, 0, "WAITFORDISCONNECT", cmd_waitfordisconnect},
240239
};

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", "start", "sync", "waitForConnect", "waitForDisconnect"]
8+
ioioApi = ["beginBatch", "disconnect", "endBatch", "hardReset", "softReset", "sync", "waitForConnect", "waitForDisconnect"]
99

1010
func get_method_name(method)
1111
local result

ioio/mkdoc.bas

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ 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"
1613
},{
1714
"name": "endBatch",
1815
"comment": "End a batch of operations."

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

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

3-
import java.io.IOException;
4-
import java.util.concurrent.BlockingQueue;
5-
import java.util.concurrent.CountDownLatch;
6-
import java.util.concurrent.LinkedBlockingQueue;
7-
83
import ioio.lib.api.AnalogInput;
94
import ioio.lib.api.IOIO;
105
import ioio.lib.api.exception.ConnectionLostException;
116
import ioio.lib.api.exception.IncompatibilityException;
127
import ioio.lib.spi.Log;
138

14-
public class AnalogInputImpl implements AnalogInput, IOTask {
9+
import java.util.concurrent.BlockingQueue;
10+
import java.util.concurrent.CountDownLatch;
11+
import java.util.concurrent.LinkedBlockingQueue;
12+
13+
public class AnalogInputImpl extends IOTask implements AnalogInput {
1514
private static final String TAG = "AnalogInput";
1615
private final BlockingQueue<FloatConsumer<AnalogInput>> queue = new LinkedBlockingQueue<>();
1716
private AnalogInput input;
18-
private int pin;
1917

2018
public AnalogInputImpl() {
2119
super();
@@ -29,7 +27,7 @@ public int available() throws ConnectionLostException {
2927

3028
@Override
3129
public void close() {
32-
IOService.getInstance().removeTask(this);
30+
super.close();
3331
input.close();
3432
input = null;
3533
}
@@ -64,11 +62,6 @@ public float getVoltageSync() {
6462
return invoke(AnalogInput::getVoltageSync);
6563
}
6664

67-
@Override
68-
public int getPin() {
69-
return pin;
70-
}
71-
7265
@Override
7366
public void loop() throws InterruptedException, ConnectionLostException {
7467
if (!queue.isEmpty()) {
@@ -81,12 +74,6 @@ public void loop() throws InterruptedException, ConnectionLostException {
8174
}
8275
}
8376

84-
public void open(int pin) throws IOException {
85-
Log.i(TAG, "openInput");
86-
this.pin = pin;
87-
IOService.getInstance().addTask(this);
88-
}
89-
9077
@Override
9178
public float read() throws InterruptedException, ConnectionLostException {
9279
return invoke(AnalogInput::read);

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

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

3-
import java.io.IOException;
4-
53
import ioio.lib.api.CapSense;
64
import ioio.lib.api.IOIO;
75
import ioio.lib.api.exception.ConnectionLostException;
86
import ioio.lib.spi.Log;
97

10-
public class CapSenseImpl implements CapSense, IOTask {
8+
public class CapSenseImpl extends IOTask implements CapSense {
119
private static final String TAG = "CapSense";
1210
private CapSense capSense;
13-
private int pin;
1411

1512
public CapSenseImpl() {
1613
super();
@@ -19,26 +16,15 @@ public CapSenseImpl() {
1916

2017
@Override
2118
public void close() {
22-
IOService.getInstance().removeTask(this);
19+
super.close();
2320
capSense.close();
2421
capSense = null;
2522
}
2623

27-
@Override
28-
public int getPin() {
29-
return pin;
30-
}
31-
3224
@Override
3325
public void loop() throws ConnectionLostException, InterruptedException {
3426
}
3527

36-
public void open(int pin) throws IOException {
37-
Log.i(TAG, "open");
38-
this.pin = pin;
39-
IOService.getInstance().addTask(this);
40-
}
41-
4228
@Override
4329
public float read() throws InterruptedException, ConnectionLostException {
4430
return 0;

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

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

3-
import java.io.IOException;
4-
import java.util.concurrent.CountDownLatch;
5-
63
import ioio.lib.api.DigitalInput;
74
import ioio.lib.api.IOIO;
85
import ioio.lib.api.exception.ConnectionLostException;
96
import ioio.lib.spi.Log;
107

11-
public class DigitalInputImpl implements DigitalInput, IOTask {
8+
import java.util.concurrent.CountDownLatch;
9+
10+
public class DigitalInputImpl extends IOTask implements DigitalInput {
1211
private static final String TAG = "DigitalInput";
1312
private CountDownLatch latch;
1413
private DigitalInput input;
15-
private int pin;
1614
private volatile boolean value;
1715

1816
public DigitalInputImpl() {
@@ -23,27 +21,16 @@ public DigitalInputImpl() {
2321

2422
@Override
2523
public void close() {
26-
IOService.getInstance().removeTask(this);
24+
super.close();
2725
input.close();
2826
input = null;
2927
}
3028

31-
@Override
32-
public int getPin() {
33-
return pin;
34-
}
35-
3629
@Override
3730
public void loop() throws InterruptedException, ConnectionLostException {
3831
value = input.read();
3932
}
4033

41-
public void open(int pin) throws IOException {
42-
Log.i(TAG, "open");
43-
this.pin = pin;
44-
IOService.getInstance().addTask(this);
45-
}
46-
4734
@Override
4835
public boolean read() {
4936
return value;

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

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

3-
import java.io.IOException;
4-
53
import ioio.lib.api.DigitalOutput;
64
import ioio.lib.api.IOIO;
75
import ioio.lib.api.exception.ConnectionLostException;
86
import ioio.lib.spi.Log;
97

10-
public class DigitalOutputImpl implements DigitalOutput, IOTask {
8+
public class DigitalOutputImpl extends IOTask implements DigitalOutput {
119
private static final String TAG = "DigitalOutput";
1210
private DigitalOutput output;
1311
private volatile boolean value;
14-
private int pin;
1512

1613
public DigitalOutputImpl() {
1714
super();
@@ -20,27 +17,16 @@ public DigitalOutputImpl() {
2017

2118
@Override
2219
public void close() {
23-
IOService.getInstance().removeTask(this);
20+
super.close();
2421
output.close();
2522
output = null;
2623
}
2724

28-
@Override
29-
public int getPin() {
30-
return pin;
31-
}
32-
3325
@Override
3426
public void loop() throws InterruptedException, ConnectionLostException {
3527
output.write(value);
3628
}
3729

38-
public void open(int pin) throws IOException {
39-
Log.i(TAG, "open");
40-
this.pin = pin;
41-
IOService.getInstance().addTask(this);
42-
}
43-
4430
@Override
4531
public void setup(IOIO ioio) {
4632
Log.i(TAG, "setup entered");

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

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

3-
import java.io.IOException;
4-
import java.util.concurrent.BlockingQueue;
5-
import java.util.concurrent.CountDownLatch;
6-
import java.util.concurrent.LinkedBlockingQueue;
7-
83
import ioio.lib.api.IOIO;
94
import ioio.lib.api.exception.ConnectionLostException;
105
import ioio.lib.api.exception.IncompatibilityException;
116
import ioio.lib.spi.Log;
127

13-
public class IOIOImpl implements IOTask {
8+
import java.io.IOException;
9+
import java.util.concurrent.BlockingQueue;
10+
import java.util.concurrent.CountDownLatch;
11+
import java.util.concurrent.LinkedBlockingQueue;
12+
13+
public class IOIOImpl extends IOTask {
1414
private static final String TAG = "IOIOImpl";
1515
private final BlockingQueue<Consumer<IOIO>> queue = new LinkedBlockingQueue<>();
1616
private IOIO ioio;
@@ -64,15 +64,12 @@ public void softReset() {
6464
invoke(IOIO::softReset);
6565
}
6666

67-
public void start() {
68-
IOService.getInstance().start();
69-
}
70-
7167
public void sync() {
7268
invoke(IOIO::sync);
7369
}
7470

7571
public void waitForConnect() {
72+
IOService.getInstance().start();
7673
invoke(IOIO::waitForConnect);
7774
}
7875

@@ -84,9 +81,9 @@ protected void invoke(Consumer<IOIO> consumer) {
8481
CountDownLatch latch = new CountDownLatch(1);
8582
try {
8683
queue.put(ioio -> {
87-
consumer.invoke(ioio);
88-
latch.countDown();
89-
});
84+
consumer.invoke(ioio);
85+
latch.countDown();
86+
});
9087
latch.await();
9188
} catch (InterruptedException e) {
9289
Log.e(TAG, "Error putting message handler to the queue: ", e);

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

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

3-
import java.io.IOException;
4-
import java.util.ArrayList;
5-
import java.util.List;
6-
73
import ioio.lib.api.IOIO;
84
import ioio.lib.api.exception.ConnectionLostException;
95
import ioio.lib.spi.Log;
106
import ioio.lib.util.IOIOLooper;
117
import ioio.lib.util.IOIOLooperProvider;
128

9+
import java.io.IOException;
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
1313
public class IOService implements IOIOLooperProvider {
1414
private static final String TAG = "IOService";
1515
private static final int MAX_PINS = 43;
@@ -45,6 +45,9 @@ public IOIOLooper createIOIOLooper(String connectionType, Object extra) {
4545
}
4646

4747
public void removeTask(IOTask task) {
48+
if (task.getPin() != -1) {
49+
usedPins[task.getPin()] = false;
50+
}
4851
ioTasks.remove(task);
4952
}
5053

@@ -57,7 +60,7 @@ private void registerPin(int pin) throws IOException {
5760
if (pin < 0 || pin >= MAX_PINS) {
5861
throw new IOException("invalid pin: " + pin);
5962
}
60-
if (usedPins[pin] != null) {
63+
if (usedPins[pin] != null && usedPins[pin]) {
6164
throw new IOException("pin already used: " + pin);
6265
}
6366
usedPins[pin] = true;

0 commit comments

Comments
 (0)