Inline

On Tue Oct 28 2014 at 10:37:54 PM Al B <[email protected]> wrote:

> I created a class similar to the TcpServer class so the loop() method
> isn't blocked until bytes have been read.  It seems to work fine, but I
> want to check with this group in case someone notices any potential issue.
> Note that I start the thread in the loop() method instead of in the
> UARTServer constructor.
>
>
> class BalancerLooper extends BaseIOIOLooper implements LineListener {
>
>     UARTServer _uart;
>     boolean oneTime = true;
>
>     @Override
>     public void setup() throws ConnectionLostException {
>         _uart = new UARTServer(ioio_, this);
>     }
>
>     @Override
>     public void loop() throws ConnectionLostException {
>

No need for the try-catch. loop() is allowed to throw an
InterruptedException.


>
>         try {
>

This is super-weird. That's what setup() is for.


>
>             if (oneTime) {
>                 new Thread(_uart).start();
>                 oneTime = false;
>             }
>
>
>         } catch (InterruptedException e) {
>                 ioio_.disconnect();
>         }
>     }
>
>     @Override
>     public void disconnected() {
>         _sequencer.close();
>         _uart.abort();
>     }
>
>     @Override
>     public void onLine(float throttle) {
>         Log.e("onLine", "MORE TODO HERE");
>     }
> }
>
>
> public class UARTServer implements Runnable {
>     interface LineListener {
>         public void onLine(float throttle);
>     }
>
>     private LineListener _listener;
>
>     private Uart _uart;
>     private IOIO _ioio;
>     private InputStream _uartInput;
>     int _inByte = 0; // in-coming serial byte
>
>
>     public UARTServer(IOIO ioio, LineListener listener) {
>         _listener = listener;
>         _ioio = ioio;
> //        new Thread(this).start();
>     }
>
>     @Override
>     public void run() {
>         try {
>             Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
>             _uart = _ioio.openUart(5, 4, 9600, Uart.Parity.NONE, Uart.
> StopBits.ONE);
>             _uartInput = _uart.getInputStream();
>             while (true) {
>
>                 _inByte = _uartInput.read();
>
>                 // decode message here…
>
>             }
>         } catch (IOException e) {
>             Log.e("IOException", e.getMessage());
>         } catch (ConnectionLostException e) {
>             Log.e("ConnectionLostException", e.getMessage());
>         }
>     }
>
>

You probably don't actually need abort(). When the IOIO disconnects for any
reason that read() method above will throw and you will exit the loop and
the thread.


>     void abort() {
>         try {
>             Log.e("NEW", "HERE");
>             if (_uartInput != null)
>                 _uartInput.close();
>             if (_uart != null)
>                 _uart.close();
>         } catch (IOException e) {
>             // Nothing to do at this point!
>         } finally {
>             _uartInput = null;
>             _uart = null;
>         }
>     }
> }
>
>
>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "ioio-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/ioio-users.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"ioio-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/ioio-users.
For more options, visit https://groups.google.com/d/optout.

Reply via email to