Skip to content

Commit 5670e1c

Browse files
committed
IOIO: added working analogInput example
1 parent c89ac1e commit 5670e1c

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package ioio.lib.pc;
2+
3+
import java.io.BufferedInputStream;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
7+
/**
8+
* Pause between read() to avoid excessive CPU usage
9+
*/
10+
class PausedInputStream extends BufferedInputStream {
11+
public PausedInputStream(InputStream inputStream) {
12+
super(inputStream);
13+
}
14+
15+
@Override
16+
public synchronized int read(byte[] bytes, int i, int i1) throws IOException {
17+
pause();
18+
return super.read(bytes, i, i1);
19+
}
20+
21+
@Override
22+
public int read(byte[] bytes) throws IOException {
23+
pause();
24+
return super.read(bytes);
25+
}
26+
27+
@Override
28+
public synchronized int read() throws IOException {
29+
pause();
30+
return super.read();
31+
}
32+
33+
private void pause() throws IOException {
34+
try {
35+
Thread.sleep(10);
36+
} catch (InterruptedException e) {
37+
throw new IOException(e);
38+
}
39+
}
40+
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,10 @@ public void waitForConnect() throws ConnectionLostException {
6868
serialPort_.enableReceiveThreshold(1);
6969
serialPort_.enableReceiveTimeout(500);
7070

71-
inputStream_ = new FixedReadBufferedInputStream(new GracefullyClosingInputStream(serialPort_.getInputStream()), 1024);
71+
inputStream_ = new FixedReadBufferedInputStream(new PausedInputStream(serialPort_.getInputStream()), 1024);
7272
outputStream_ = new BufferedOutputStream(serialPort_.getOutputStream(), 256);
7373

74-
// This is only required on Windows and OSX El Capitan, but otherwise
75-
// harmless.
74+
// This is only required on Windows and OSX El Capitan, but otherwise harmless.
7675
//serialPort_.setDTR(false);
7776
serialPort_.setDTR(true);
7877
Thread.sleep(100);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public void incompatible(IOIO ioio) {
8888

8989
@Override
9090
public void loop() throws ConnectionLostException, InterruptedException {
91-
Thread.sleep(100);
91+
Thread.sleep(10);
9292
for (IOTask next: ioTasks) {
9393
next.loop();
9494
}

0 commit comments

Comments
 (0)