File tree Expand file tree Collapse file tree 3 files changed +43
-4
lines changed
net/sourceforge/smallbasic/ioio Expand file tree Collapse file tree 3 files changed +43
-4
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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 );
Original file line number Diff line number Diff 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 }
You can’t perform that action at this time.
0 commit comments