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 {
        try {
            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());
        }
    }
    
    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.

Reply via email to