Created
September 10, 2018 15:48
-
-
Save josmithua/c3ff322d060a5cd23d1c4a9afa5b8ef5 to your computer and use it in GitHub Desktop.
InputStream for Android Things UartDevice
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mycompany.uartproj | |
import com.google.android.things.pio.UartDevice | |
import java.io.InputStream | |
class UartInputStream(private val uartDevice: UartDevice) : InputStream() { | |
override fun read(): Int { | |
val byteArray = ByteArray(1) | |
while (true) { | |
val count = uartDevice.read(byteArray, 1) | |
if (count == 0) { | |
Thread.sleep(20) | |
continue | |
} | |
return byteArray[0].toInt() | |
} | |
} | |
override fun read(b: ByteArray?, off: Int, len: Int): Int { | |
if (b == null) { | |
throw NullPointerException() | |
} else if (off < 0 || len < 0 || b.size - off < len) { | |
throw IndexOutOfBoundsException() | |
} else if (len == 0) { | |
return 0 | |
} | |
val byteArray = ByteArray(len) | |
val count = uartDevice.read(byteArray, len) | |
System.arraycopy(byteArray, 0, b, off, len) | |
return count | |
} | |
override fun available(): Int { | |
// What to do? | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment