Skip to content

Commit 78965df

Browse files
committed
IOIO: refactoring for SPIMaster
1 parent d57d4c5 commit 78965df

14 files changed

+106
-67
lines changed

ioio/api.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,5 +248,10 @@
248248
"name": "TwiMaster":
249249
"comment": "An interface for controlling a TWI module, in TWI bus-master mode, enabling communication with multiple TWI-enabled slave modules.",
250250
"methods": []
251+
},
252+
{
253+
"name": "SpiMaster":
254+
"comment": "An interface for controlling an SPI module, in SPI bus-master mode, enabling communication with multiple SPI-enabled slave modules",
255+
"methods": []
251256
}
252257
]

ioio/main.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ int nextId = 1;
3030
#define CLASS_PWMOUTPUT "net/sourceforge/smallbasic/ioio/PwmOutputImpl"
3131
#define CLASS_CAPSENSE "net/sourceforge/smallbasic/ioio/CapsenseImpl"
3232
#define CLASS_TWIMASTER "net/sourceforge/smallbasic/ioio/TwiMasterImpl"
33+
#define CLASS_SPIMASTER "net/sourceforge/smallbasic/ioio/SpiMasterImpl"
3334
#define CLASS_IOIO "net/sourceforge/smallbasic/ioio/IOIOImpl"
3435
#define CLASS_IOTASK_ID 1
3536

@@ -228,10 +229,25 @@ static int cmd_twimaster_writeread(var_s *self, int argc, slib_par_t *arg, var_s
228229
return result;
229230
}
230231

232+
static int cmd_spimaster_write(var_s *self, int argc, slib_par_t *arg, var_s *retval) {
233+
int result = 0;
234+
if (argc != 2) {
235+
error(retval, "writeRead", 0);
236+
} else {
237+
// TODO
238+
//result = ioioTask->invokeVoidVoid("waitForDisconnect", retval);
239+
}
240+
return result;
241+
}
242+
231243
static void create_twimaster(var_t *map) {
232244
v_create_callback(map, "writeRead", cmd_twimaster_writeread);
233245
}
234246

247+
static void create_spimaster(var_t *map) {
248+
v_create_callback(map, "write", cmd_spimaster_write);
249+
}
250+
235251
#include "api.h"
236252

237253
FUNC_SIG lib_func[] = {

ioio/src/main/java/ioio/lib/pc/SerialPortIOIOConnection.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@
2828
*/
2929
package ioio.lib.pc;
3030

31-
import java.io.BufferedOutputStream;
32-
import java.io.IOException;
33-
import java.io.InputStream;
34-
import java.io.OutputStream;
35-
3631
import ioio.PausedInputStream;
3732
import ioio.lib.api.IOIOConnection;
3833
import ioio.lib.api.exception.ConnectionLostException;
@@ -43,6 +38,11 @@
4338
import purejavacomm.NoSuchPortException;
4439
import purejavacomm.SerialPort;
4540

41+
import java.io.BufferedOutputStream;
42+
import java.io.IOException;
43+
import java.io.InputStream;
44+
import java.io.OutputStream;
45+
4646
class SerialPortIOIOConnection implements IOIOConnection {
4747
private final String name_;
4848
// private static final String TAG = "SerialPortIOIOConnection";

ioio/src/main/java/ioio/lib/pc/SerialPortIOIOConnectionBootstrap.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@
2929

3030
package ioio.lib.pc;
3131

32-
import java.util.Collection;
33-
import java.util.Enumeration;
34-
import java.util.LinkedList;
35-
import java.util.List;
36-
3732
import ioio.lib.api.IOIOConnection;
3833
import ioio.lib.spi.IOIOConnectionBootstrap;
3934
import ioio.lib.spi.IOIOConnectionFactory;
@@ -42,6 +37,11 @@
4237
import purejavacomm.CommPortIdentifier;
4338
import purejavacomm.PortInUseException;
4439

40+
import java.util.Collection;
41+
import java.util.Enumeration;
42+
import java.util.LinkedList;
43+
import java.util.List;
44+
4545
public class SerialPortIOIOConnectionBootstrap implements IOIOConnectionBootstrap {
4646
private static final String TAG = "SerialPortIOIOConnectionBootstrap";
4747

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,8 @@ public void setBuffer(int capacity) {
8383
}
8484

8585
@Override
86-
public void setup(IOIO ioio) {
86+
public void setup(IOIO ioio) throws ConnectionLostException {
8787
Log.i(TAG, "setup entered");
88-
try {
89-
input = ioio.openAnalogInput(pin);
90-
}
91-
catch (ConnectionLostException e) {
92-
throw new RuntimeException(e);
93-
}
88+
input = ioio.openAnalogInput(pin);
9489
}
9590
}

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,9 @@ public void setFilterCoef(float t) {
4343
}
4444

4545
@Override
46-
public void setup(IOIO ioio) {
46+
public void setup(IOIO ioio) throws ConnectionLostException {
4747
Log.i(TAG, "setup entered");
48-
try {
49-
capSense = ioio.openCapSense(pin);
50-
}
51-
catch (ConnectionLostException e) {
52-
throw new RuntimeException(e);
53-
}
48+
capSense = ioio.openCapSense(pin);
5449
}
5550

5651
@Override

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
import ioio.lib.api.exception.ConnectionLostException;
66
import ioio.lib.spi.Log;
77

8+
import java.util.concurrent.atomic.AtomicBoolean;
9+
810
public class DigitalInputImpl extends IOTask implements DigitalInput {
911
private static final String TAG = "DigitalInput";
12+
private final AtomicBoolean value = new AtomicBoolean();
1013
private DigitalInput input;
11-
private volatile boolean value;
1214

1315
public DigitalInputImpl() {
1416
super();
@@ -24,23 +26,18 @@ public void close() {
2426

2527
@Override
2628
public void loop() throws InterruptedException, ConnectionLostException {
27-
value = input.read();
29+
value.set(input.read());
2830
}
2931

3032
@Override
3133
public boolean read() {
32-
return value;
34+
return value.get();
3335
}
3436

3537
@Override
36-
public void setup(IOIO ioio) {
38+
public void setup(IOIO ioio) throws ConnectionLostException {
3739
Log.i(TAG, "setup entered");
38-
try {
39-
input = ioio.openDigitalInput(pin);
40-
}
41-
catch (ConnectionLostException e) {
42-
throw new RuntimeException(e);
43-
}
40+
input = ioio.openDigitalInput(pin);
4441
}
4542

4643
@Override

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,13 @@ public void loop() throws ConnectionLostException {
2828
}
2929

3030
@Override
31-
public void setup(IOIO ioio) {
31+
public void setup(IOIO ioio) throws ConnectionLostException {
3232
Log.i(TAG, "setup entered");
33-
try {
34-
output = ioio.openDigitalOutput(pin);
35-
}
36-
catch (ConnectionLostException e) {
37-
throw new RuntimeException(e);
38-
}
33+
output = ioio.openDigitalOutput(pin);
3934
}
4035

4136
@Override
42-
public void write(final boolean value) {
37+
public void write(boolean value) {
4338
handleError();
4439
lock.invoke((i) -> {
4540
i.write(value);

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public void loop() throws ConnectionLostException, InterruptedException {
9696
next.loop();
9797
} catch (Throwable e) {
9898
next.setError(e.getLocalizedMessage());
99+
break;
99100
}
100101
}
101102
}
@@ -105,7 +106,12 @@ public void setup(IOIO ioio) {
105106
this.ioio = ioio;
106107
this.lastAccessMillis = System.currentTimeMillis();
107108
for (IOTask next: ioTasks) {
108-
next.setup(ioio);
109+
try {
110+
next.setup(ioio);
111+
} catch (Throwable e) {
112+
next.setError(e.getLocalizedMessage());
113+
break;
114+
}
109115
}
110116
}
111117
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,5 @@ int getPin() {
5353
* Subclasses should override this method for performing operations to be
5454
* done once as soon as IOIO communication is established.
5555
*/
56-
abstract void setup(IOIO ioio);
56+
abstract void setup(IOIO ioio) throws ConnectionLostException;
5757
}

0 commit comments

Comments
 (0)