diff --git a/app/build.gradle b/app/build.gradle index 9b0ca34e3..889eb0073 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -120,6 +120,9 @@ dependencies { //implementation 'com.google.protobuf:protobuf-java-util:3.11.1' implementation 'com.google.protobuf:protobuf-javalite:3.12.2' + // For UART access + implementation 'com.google.android.things:androidthings:1.0' + // mapbox implementation 'com.mapbox.mapboxsdk:mapbox-android-sdk:9.1.0' diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 42cf9f729..b63ea3fb2 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -69,6 +69,8 @@ android:hardwareAccelerated="true" android:theme="@style/AppTheme"> + + @@ -105,10 +107,10 @@ android:exported="false" /> - + android:exported="false" /> { + val manager = PeripheralManager.getInstance() + return manager.uartDeviceList + } + + private val START1 = 0x94.toByte() + private val START2 = 0xc3.toByte() + } + + private var uart: UartDevice? = null + + /** The index of the next byte we are hoping to receive */ + private var rxPtr = 0 + + private val callback = object : UartDeviceCallback { + override fun onUartDeviceDataAvailable(p0: UartDevice): Boolean { + + return uart != null // keep reading until our device goes away + } + + override fun onUartDeviceError(uart: UartDevice, error: Int) { + super.onUartDeviceError(uart, error) + } + } + + override fun handleSendToRadio(p: ByteArray) { + uart?.apply { + val header = ByteArray(4) + header[0] = START1 + header[1] = START2 + header[2] = (p.size shr 8).toByte() + header[3] = (p.size and 0xff).toByte() + write(header, header.size) + write(p, p.size) + // flush(UartDevice.FLUSH_OUT) - I don't think we need to stall for htis + } + } + + /* + private fun readerLoop() { + val scratch = ByteArray(1) + var ptr = 0 + while (true) { // FIXME wait for someone to ask us to exit, and catch continuation exception + uart?.apply { + read(scratch, 1) + when (ptr) { + 0 -> + } + } + } + } */ + + + override fun onCreate() { + super.onCreate() + + val port = findPorts()[0] + val manager = PeripheralManager.getInstance() + uart = manager.openUartDevice(port) + uart?.apply { + setBaudrate(921600) + registerUartDeviceCallback(callback) + } + } + + override fun onDestroy() { + uart?.close() + uart = null + super.onDestroy() + } +} \ No newline at end of file