com.google.android.things.pio.UartDevice Java Examples

The following examples show how to use com.google.android.things.pio.UartDevice. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UartLowpanModule.java    From contrib-drivers with Apache License 2.0 6 votes vote down vote up
/**
 * Invoked by mUartDeviceCallback when new data arrives in the UART buffer.
 */
private synchronized boolean onUartDeviceDataAvailable(UartDevice uart) {
    try {
        final int count = uart.read(mInboundRawBuffer, mInboundRawBuffer.length);

        if (count > 0) {
            processBuffer(mInboundRawBuffer, count);
        }

    } catch (IOException e) {
        Log.w(TAG, "Unable to read UART data", e);
        onUnexpectedClose();
    }

    return true;
}
 
Example #2
Source File: MainActivity.java    From androidthings-cameraCar with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onUartDeviceDataAvailable(UartDevice uart) {
    // Queue up a data transfer
    transferUartData();
    //Continue listening for more interrupts
    return true;
}
 
Example #3
Source File: NmeaGpsModule.java    From contrib-drivers with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onUartDeviceDataAvailable(UartDevice uart) {
    try {
        readUartBuffer();
    } catch (IOException e) {
        Log.w(TAG, "Unable to read UART data", e);
    }

    return true;
}
 
Example #4
Source File: NmeaGpsModule.java    From contrib-drivers with Apache License 2.0 5 votes vote down vote up
/**
 * Initialize peripheral defaults from the constructor.
 */
private void init(UartDevice device, int baudRate, float accuracy, Handler handler) throws IOException {
    mDevice = device;
    mDevice.setBaudrate(baudRate);
    mDevice.registerUartDeviceCallback(handler, mCallback);
    mGpsAccuracy = accuracy;
    mParser = new NmeaParser(accuracy);
}
 
Example #5
Source File: NmeaGpsModule.java    From contrib-drivers with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new NmeaGpsModule.
 *
 * @param uartName UART port name where the module is attached. Cannot be null.
 * @param baudRate Baud rate used for the module UART.
 * @param accuracy specified accuracy, in meters CEP.
 * @param handler optional {@link Handler} for software polling and callback events.
 */
public NmeaGpsModule(String uartName, int baudRate, float accuracy, Handler handler) throws IOException {
    try {
        PeripheralManager manager = PeripheralManager.getInstance();
        UartDevice device = manager.openUartDevice(uartName);
        init(device, baudRate, accuracy, handler);
    } catch (IOException | RuntimeException e) {
        close();
        throw e;
    }
}
 
Example #6
Source File: ZXGestureSensorUart.java    From contrib-drivers with Apache License 2.0 5 votes vote down vote up
/**
 * Sets up UART port for sensor communication
 * @param port UART port on which the sensor is connected
 * @throws IOException
 */
private void connectUart(String port) throws IOException {
    mDevice = pioService.openUartDevice(port);
    mDevice.setBaudrate(115200);
    mDevice.setDataSize(8);
    mDevice.setParity(UartDevice.PARITY_NONE);
    mDevice.setStopBits(1);

    mDevice.registerUartDeviceCallback(onUart);
}
 
Example #7
Source File: ThermalPrinter.java    From contrib-drivers with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
/* package */ ThermalPrinter(UartDevice uartDevice, Handler handler) throws IOException {
    mCsnA2 = new CsnA2(uartDevice);
    mHandler = handler;
    // The first 500ms are meant for initialization. Delay all prints until after.
    mResumeTime = System.currentTimeMillis() + DELAY_INIT;
}
 
Example #8
Source File: CsnA2.java    From contrib-drivers with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes a new CsnA2 with a given baud rate and specific heating configurations.
 *
 * @param uartBus The UART bus to send data through
 * @param baudRate The symbol rate to send data
 * @param configuration A configuration object specifying heating parameters for all print tasks
 */
/* package */ CsnA2(String uartBus, int baudRate, Configuration configuration) throws IOException {
    UartDevice serial = PeripheralManager.getInstance().openUartDevice(uartBus);
    serial.setBaudrate(baudRate);
    serial.setDataSize(8);
    serial.setParity(UartDevice.PARITY_NONE);
    serial.setStopBits(1);
    mUartDevice = serial;

    begin(configuration);
}
 
Example #9
Source File: LoopbackActivity.java    From sample-uartloopback with Apache License 2.0 5 votes vote down vote up
/**
 * Access and configure the requested UART device for 8N1.
 *
 * @param name Name of the UART peripheral device to open.
 * @param baudRate Data transfer rate. Should be a standard UART baud,
 *                 such as 9600, 19200, 38400, 57600, 115200, etc.
 *
 * @throws IOException if an error occurs opening the UART port.
 */
private void openUart(String name, int baudRate) throws IOException {
    mLoopbackDevice = PeripheralManager.getInstance().openUartDevice(name);
    // Configure the UART
    mLoopbackDevice.setBaudrate(baudRate);
    mLoopbackDevice.setDataSize(DATA_BITS);
    mLoopbackDevice.setParity(UartDevice.PARITY_NONE);
    mLoopbackDevice.setStopBits(STOP_BITS);

    mLoopbackDevice.registerUartDeviceCallback(mInputHandler, mCallback);
}
 
Example #10
Source File: LoopbackActivity.java    From sample-uartloopback with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onUartDeviceDataAvailable(UartDevice uart) {
    // Queue up a data transfer
    transferUartData();
    //Continue listening for more interrupts
    return true;
}
 
Example #11
Source File: SerialPortAT.java    From jlibmodbus with Apache License 2.0 5 votes vote down vote up
@Override
public void open() throws SerialPortException {
    try {
        SerialParameters sp = getSerialParameters();
        port = PeripheralManager.getInstance().openUartDevice(sp.getDevice());
        port.setBaudrate(sp.getBaudRate());
        port.setDataSize(sp.getDataBits());
        port.setParity(sp.getParity().getValue());
        port.setStopBits(sp.getStopBits());
        port.setHardwareFlowControl(UartDevice.HW_FLOW_CONTROL_NONE);
    } catch (Exception ex) {
        throw new SerialPortException(ex);
    }
}
 
Example #12
Source File: MainActivity.java    From androidthings-cameraCar with Apache License 2.0 5 votes vote down vote up
private void openUart(PeripheralManager service, String name, int baudRate) throws IOException {
    echoDevice = service.openUartDevice(name);
    // Configure the UART
    echoDevice.setBaudrate(baudRate);
    echoDevice.setDataSize(8);
    echoDevice.setParity(UartDevice.PARITY_NONE);
    echoDevice.setStopBits(1);

    Log.i(TAG, "Open UART device");

    echoDevice.registerUartDeviceCallback(mInputHandler, mCallback);
}
 
Example #13
Source File: CsnA2.java    From contrib-drivers with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
/* package */ CsnA2(UartDevice serial) throws IOException {
    mUartDevice = serial;
    begin(null);
}
 
Example #14
Source File: CsnA2.java    From contrib-drivers with Apache License 2.0 4 votes vote down vote up
/**
 * Closes the printer and disables printing.
 */
/* package */ void close() throws IOException {
    mUartDevice.flush(UartDevice.FLUSH_OUT);
    // setPrinting(false);
    mUartDevice.close();
}
 
Example #15
Source File: LoopbackActivity.java    From sample-uartloopback with Apache License 2.0 4 votes vote down vote up
@Override
public void onUartDeviceError(UartDevice uart, int error) {
    Log.w(TAG, uart + ": Error event " + error);
}
 
Example #16
Source File: ZXGestureSensorUart.java    From contrib-drivers with Apache License 2.0 4 votes vote down vote up
/**
 * parse data from UART connection
 *
 * @param uart UART device object to be read
 * @throws IOException
 */
private void readUartBuffer(UartDevice uart) throws IOException {
    final int maxCount = 256;
    byte[] buffer = new byte[maxCount];

    int count;
    SensorResult result = null;
    while ((count = uart.read(buffer, buffer.length)) > 0) {
        for (int i = 0; i < count; i++) {
            try {
                result = mParser.parse(buffer[i]);
            } catch (InvalidByteException e) {
                Log.e(TAG, "Invalid byte sequence encountered " +
                        "while reading from sensor connected to UART", e);
            }
            if (result != null) {
                switch (result.resultType) {
                    case PEN_UP:
                        mGestureDetector.penUp();
                        break;
                    case X_POS:
                        mGestureDetector.setXpos(result.xPosition);
                        break;
                    case Z_POS:
                        mGestureDetector.setZpos(result.zPosition);
                        break;
                    case GESTURE:
                        mGestureDetector.setGesture(result.gesture,
                                result.gestureParams);
                        break;
                    case RANGE:
                        mGestureDetector.setRanges(result.rangeL, result.rangeR);
                        break;
                    case ID:
                        break;
                }
                result = null;
            }
        }
    }
}
 
Example #17
Source File: NmeaGpsModule.java    From contrib-drivers with Apache License 2.0 4 votes vote down vote up
/**
 * Constructor invoked from unit tests.
 */
@VisibleForTesting
/*package*/ NmeaGpsModule(UartDevice device, int baudRate, float accuracy,
                          Handler handler) throws IOException {
    init(device, baudRate, accuracy, handler);
}
 
Example #18
Source File: MainActivity.java    From androidthings-cameraCar with Apache License 2.0 4 votes vote down vote up
@Override
public void onUartDeviceError(UartDevice uart, int error) {
    Log.w(TAG, uart + ": Error event " + error);
}
 
Example #19
Source File: NmeaGpsModule.java    From contrib-drivers with Apache License 2.0 4 votes vote down vote up
@Override
public void onUartDeviceError(UartDevice uart, int error) {
    Log.w(TAG, "Error receiving incoming data: " + error);
}
 
Example #20
Source File: UartLowpanModule.java    From contrib-drivers with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onUartDeviceDataAvailable(UartDevice uart) {
    return UartLowpanModule.this.onUartDeviceDataAvailable(uart);
}
 
Example #21
Source File: UartLowpanModule.java    From contrib-drivers with Apache License 2.0 4 votes vote down vote up
@Override
public void onUartDeviceError(UartDevice uart, int error) {
    UartLowpanModule.this.onUartDeviceError(uart, error);
}
 
Example #22
Source File: UartLowpanModule.java    From contrib-drivers with Apache License 2.0 4 votes vote down vote up
/**
 * Invoked by mUartDeviceCallback when the UART buffer experiences an error while reading.
 */
private synchronized void onUartDeviceError(UartDevice uart, int error) {
    Log.w(TAG, "Error receiving incoming data: " + error);
    onUnexpectedClose();
}
 
Example #23
Source File: UartLowpanDriver.java    From contrib-drivers with Apache License 2.0 2 votes vote down vote up
/**
 * Create a new UartLowpanDriver to send/receive commands and events to the
 * Android lowpan framework.
 *
 * @param uartName UART port name where the module is attached. Cannot be null.
 * @param baudRate Baud rate used for the module UART.
 */
public UartLowpanDriver(String uartName, int baudRate)
        throws IOException {
    this(uartName, baudRate, UartDevice.HW_FLOW_CONTROL_NONE, null);
}