Skip to content

Commit 16ce829

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

File tree

10 files changed

+208
-27
lines changed

10 files changed

+208
-27
lines changed

ioio/api.json

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,6 @@
3333
"arg": "void",
3434
"comment": "This is very similar to read(), but will wait for a new sample to arrive before returning."
3535
},
36-
{
37-
"name": "setBuffer",
38-
"rtn": "void",
39-
"arg": "int",
40-
"comment": "Initializes or destroys an internal buffer, used for queuing sampled data."
41-
},
4236
{
4337
"name": "getOverflowCount",
4438
"rtn": "int",
@@ -93,12 +87,6 @@
9387
"arg": "void",
9488
"comment": "Reads a single measurement from the queue. If the queue is empty, will block until more data arrives."
9589
},
96-
{
97-
"name": "waitPulseGetDuration",
98-
"rtn": "float",
99-
"arg": "void",
100-
"comment": "@deprecated Please use getDurationBuffered() instead."
101-
},
10290
{
10391
"name": "getFrequency",
10492
"rtn": "float",
@@ -119,7 +107,7 @@
119107
"methods": [
120108
{
121109
"name": "read",
122-
"rtn": "boolean",
110+
"rtn": "boolean",
123111
"arg": "void",
124112
"comment": "Read the value sensed on the pin. May block for a few milliseconds if called right after creation of the instance."
125113
},
@@ -210,5 +198,3 @@
210198
]
211199
}
212200
]
213-
214-

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

Lines changed: 2 additions & 2 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-
53
import ioio.lib.api.IOIO;
64
import ioio.lib.api.exception.ConnectionLostException;
75
import ioio.lib.api.exception.IncompatibilityException;
86
import ioio.lib.spi.Log;
97
import ioio.lib.util.IOIOLooper;
108

9+
import java.util.concurrent.BlockingQueue;
10+
1111
public abstract class AbstractLooper implements IOIOLooper {
1212
static final String TAG = "AbstractLooper";
1313
protected IOIO ioio;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ protected void start() {
6161
this.ready = true;
6262
}
6363

64-
private void invoke(Consumer<IOIO> consumer) {
64+
protected void invoke(Consumer<IOIO> consumer) {
6565
final CountDownLatch latch = new CountDownLatch(1);
6666
try {
6767
if (this.ready) {

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

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,29 @@
22

33
import ioio.lib.api.IOIO;
44
import ioio.lib.api.exception.ConnectionLostException;
5+
import ioio.lib.api.exception.IncompatibilityException;
56
import ioio.lib.spi.Log;
67
import ioio.lib.util.IOIOLooper;
78

89
import java.util.concurrent.BlockingQueue;
10+
import java.util.concurrent.CountDownLatch;
11+
import java.util.concurrent.LinkedBlockingQueue;
912

10-
public class AnalogInput extends AbstractLooperProvider {
13+
public class AnalogInput extends AbstractLooperProvider implements ioio.lib.api.AnalogInput {
1114
private static final String TAG = "AnalogInput";
1215
private AnalogInputLooper looper;
16+
static final protected BlockingQueue<FloatConsumer<ioio.lib.api.AnalogInput>> INPUT_QUEUE = new LinkedBlockingQueue<>();
1317

1418
public AnalogInput() {
1519
super();
1620
Log.i(TAG, "created");
1721
}
1822

23+
@Override
24+
public int available() throws ConnectionLostException {
25+
return 0;
26+
}
27+
1928
@Override
2029
public void close() {
2130
super.close();
@@ -28,12 +37,79 @@ public IOIOLooper createIOIOLooper(String connectionType, Object extra) {
2837
return looper;
2938
}
3039

40+
@Override
41+
public int getOverflowCount() {
42+
return (int) invokeFloat(ioio.lib.api.AnalogInput::getOverflowCount);
43+
}
44+
45+
@Override
46+
public float getReference() {
47+
return invokeFloat(ioio.lib.api.AnalogInput::getReference);
48+
}
49+
50+
@Override
51+
public float getSampleRate() {
52+
return invokeFloat(ioio.lib.api.AnalogInput::getSampleRate);
53+
}
54+
55+
@Override
56+
public float getVoltage() {
57+
return invokeFloat(ioio.lib.api.AnalogInput::getVoltage);
58+
}
59+
60+
@Override
61+
public float getVoltageBuffered() {
62+
return invokeFloat(ioio.lib.api.AnalogInput::getVoltageBuffered);
63+
}
64+
65+
@Override
66+
public float getVoltageSync() {
67+
return invokeFloat(ioio.lib.api.AnalogInput::getVoltageSync);
68+
}
69+
3170
public void open(int pin) {
3271
Log.i(TAG, "openInput");
3372
looper = new AnalogInputLooper(QUEUE, pin);
3473
start();
3574
}
3675

76+
@Override
77+
public float read() throws InterruptedException, ConnectionLostException {
78+
return invokeFloat(ioio.lib.api.AnalogInput::read);
79+
}
80+
81+
@Override
82+
public float readBuffered() {
83+
return invokeFloat(ioio.lib.api.AnalogInput::readBuffered);
84+
}
85+
86+
@Override
87+
public float readSync() {
88+
return invokeFloat(ioio.lib.api.AnalogInput::readSync);
89+
}
90+
91+
@Override
92+
public void setBuffer(int capacity) {
93+
94+
}
95+
96+
protected float invokeFloat(FloatConsumer<ioio.lib.api.AnalogInput> consumer) {
97+
final CountDownLatch latch = new CountDownLatch(1);
98+
final float[] result = new float[1];
99+
try {
100+
INPUT_QUEUE.put(e -> {
101+
result[0] = consumer.invoke(e);
102+
latch.countDown();
103+
return result[0];
104+
});
105+
latch.await();
106+
}
107+
catch (InterruptedException e) {
108+
throw new RuntimeException(e);
109+
}
110+
return result[0];
111+
}
112+
37113
static class AnalogInputLooper extends AbstractLooper {
38114
private ioio.lib.api.AnalogInput analogInput;
39115

@@ -47,6 +123,19 @@ public void close() {
47123
this.analogInput = null;
48124
}
49125

126+
@Override
127+
public void loop() throws InterruptedException, ConnectionLostException {
128+
super.loop();
129+
if (!INPUT_QUEUE.isEmpty()) {
130+
try {
131+
INPUT_QUEUE.take().invoke(analogInput);
132+
}
133+
catch (ConnectionLostException | IncompatibilityException e) {
134+
throw new RuntimeException(e);
135+
}
136+
}
137+
}
138+
50139
@Override
51140
public void setup(IOIO ioio) {
52141
Log.i(TAG, "setup entered");

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import java.util.concurrent.BlockingQueue;
99

10-
public class CapSense extends AbstractLooperProvider {
10+
public class CapSense extends AbstractLooperProvider implements ioio.lib.api.CapSense {
1111
private static final String TAG = "CapSense";
1212
private CapSenseLooper looper;
1313

@@ -33,6 +33,41 @@ public void open(int pin) {
3333
start();
3434
}
3535

36+
@Override
37+
public float read() throws InterruptedException, ConnectionLostException {
38+
return 0;
39+
}
40+
41+
@Override
42+
public float readSync() throws InterruptedException, ConnectionLostException {
43+
return 0;
44+
}
45+
46+
@Override
47+
public void setFilterCoef(float t) throws ConnectionLostException {
48+
49+
}
50+
51+
@Override
52+
public void waitOver(float threshold) throws ConnectionLostException, InterruptedException {
53+
54+
}
55+
56+
@Override
57+
public void waitOverSync(float threshold) throws ConnectionLostException, InterruptedException {
58+
59+
}
60+
61+
@Override
62+
public void waitUnder(float threshold) throws ConnectionLostException, InterruptedException {
63+
64+
}
65+
66+
@Override
67+
public void waitUnderSync(float threshold) throws ConnectionLostException, InterruptedException {
68+
69+
}
70+
3671
static class CapSenseLooper extends AbstractLooper {
3772
private ioio.lib.api.CapSense capSense;
3873

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import java.util.concurrent.BlockingQueue;
99

10-
public class DigitalInput extends AbstractLooperProvider {
10+
public class DigitalInput extends AbstractLooperProvider implements ioio.lib.api.DigitalInput {
1111
private static final String TAG = "DigitalInput";
1212
private DigitalInputLooper looper;
1313

@@ -16,6 +16,7 @@ public DigitalInput() {
1616
Log.i(TAG, "created DigitalInput");
1717
}
1818

19+
@Override
1920
public void close() {
2021
super.close();
2122
this.looper.close();
@@ -33,8 +34,14 @@ public void open(int pin) {
3334
start();
3435
}
3536

36-
public int read() {
37-
return looper.getValue() ? 1 : 0;
37+
@Override
38+
public boolean read() {
39+
return looper.getValue();
40+
}
41+
42+
@Override
43+
public void waitForValue(boolean value) throws InterruptedException, ConnectionLostException {
44+
3845
}
3946

4047
static class DigitalInputLooper extends AbstractLooper {

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import java.util.concurrent.BlockingQueue;
99

10-
public class DigitalOutput extends AbstractLooperProvider {
10+
public class DigitalOutput extends AbstractLooperProvider implements ioio.lib.api.DigitalInput {
1111
private static final String TAG = "DigitalOutput";
1212
private DigitalOutputLooper outputLooper;
1313

@@ -33,6 +33,16 @@ public void open(int pin) {
3333
start();
3434
}
3535

36+
@Override
37+
public boolean read() throws InterruptedException, ConnectionLostException {
38+
return false;
39+
}
40+
41+
@Override
42+
public void waitForValue(boolean value) throws InterruptedException, ConnectionLostException {
43+
44+
}
45+
3646
public void write(int value) {
3747
outputLooper.setValue(value == 1);
3848
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package net.sourceforge.smallbasic.ioio;
2+
3+
import ioio.lib.api.exception.ConnectionLostException;
4+
import ioio.lib.api.exception.IncompatibilityException;
5+
6+
@FunctionalInterface
7+
public interface FloatConsumer<T> {
8+
float invoke(T t) throws ConnectionLostException, InterruptedException, IncompatibilityException;
9+
}

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
import java.util.concurrent.BlockingQueue;
99

10-
public class PulseInput extends AbstractLooperProvider {
10+
public class PulseInput extends AbstractLooperProvider implements ioio.lib.api.PulseInput {
1111
private static final String TAG = "PulseInput";
12-
private static ioio.lib.api.PulseInput.PulseMode pulseMode = ioio.lib.api.PulseInput.PulseMode.NEGATIVE;
12+
private static final ioio.lib.api.PulseInput.PulseMode pulseMode = ioio.lib.api.PulseInput.PulseMode.NEGATIVE;
1313
private PulseInputLooper looper;
1414

1515
public PulseInput() {
@@ -28,6 +28,36 @@ public IOIOLooper createIOIOLooper(String type, Object extra) {
2828
return looper;
2929
}
3030

31+
@Override
32+
public float getDuration() throws InterruptedException, ConnectionLostException {
33+
return 0;
34+
}
35+
36+
@Override
37+
public float getDurationSync() throws InterruptedException, ConnectionLostException {
38+
return 0;
39+
}
40+
41+
@Override
42+
public float getDurationBuffered() throws InterruptedException, ConnectionLostException {
43+
return 0;
44+
}
45+
46+
@Override
47+
public float waitPulseGetDuration() throws InterruptedException, ConnectionLostException {
48+
return 0;
49+
}
50+
51+
@Override
52+
public float getFrequency() throws InterruptedException, ConnectionLostException {
53+
return 0;
54+
}
55+
56+
@Override
57+
public float getFrequencySync() throws InterruptedException, ConnectionLostException {
58+
return 0;
59+
}
60+
3161
public void open(int pin) {
3262
Log.i(TAG, "open");
3363
looper = new PulseInputLooper(QUEUE, pin);

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import java.util.concurrent.BlockingQueue;
99

10-
public class PwmOutput extends AbstractLooperProvider {
10+
public class PwmOutput extends AbstractLooperProvider implements ioio.lib.api.PwmOutput {
1111
private static final String TAG = "PulseInput";
1212
private PwmOutputLooper looper;
1313

@@ -33,6 +33,21 @@ public void open(int pin, int freqHz) {
3333
start();
3434
}
3535

36+
@Override
37+
public void setDutyCycle(float dutyCycle) throws ConnectionLostException {
38+
39+
}
40+
41+
@Override
42+
public void setPulseWidth(int pulseWidthUs) throws ConnectionLostException {
43+
44+
}
45+
46+
@Override
47+
public void setPulseWidth(float pulseWidthUs) throws ConnectionLostException {
48+
49+
}
50+
3651
static class PwmOutputLooper extends AbstractLooper {
3752
private ioio.lib.api.PwmOutput output;
3853
private final int freqHz;

0 commit comments

Comments
 (0)