PicoGpioNetClient

class PicoGpioNetClient(ip: String, port: Int, autoFlush: Boolean = false) : Closeable

Client for interacting with the PGN daemon.

This is the class responsible for translating human-readable function names and variables to raw byte data, and transmitting that data back and forth over the socket.

To use this class, you must first call connect(), and you should remember to close the client after you've finished using it.

While connected, you can issue commands to the PGN daemon by calling the functions within this class.

Example usage:

val serverName = PicoGpioNetClient(ip, port).use{ client ->
client.connect()
val apiVersion = client.getApiVersion()
if (apiVersion >= Command.CMD_GET_NAME.apiVersion)
client.getName()
else
"Unknown device name"
}

Parameters

ip

IP address of the Pico device running PGN which we want to connect to.

port

Port on which PGN is running. This is usually port 8080.

autoFlush

If true, flush all writes to the PGN daemon automatically.

Constructors

Link copied to clipboard
constructor(ip: String, port: Int, autoFlush: Boolean = false)

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
open override fun close()
Link copied to clipboard
suspend fun connect()

Attempts to establish a connection to the PGN daemon.

Link copied to clipboard
suspend fun delay(millis: Short)

Tells the Pico server to wait for a defined amount of time before moving onto the next request.

Link copied to clipboard
suspend fun flush(): List<Boolean>

Manually flushes the queue.

Link copied to clipboard
suspend fun getApiVersion(): Byte

Asks the Pico device which version of pico-gpio-net it is running.

Link copied to clipboard
suspend fun getName(): String

Asks the Pico device to identify itself.

Link copied to clipboard
suspend fun getPin(pin: Byte): Byte

Retrieves the value of a single pin.

Link copied to clipboard
suspend fun getPins(pins: ByteArray): ByteArray

Retrieves the value of multiple pins.

Link copied to clipboard
suspend fun read(length: Int): ByteArray

Reads the specified number of bytes from the socket.

suspend fun read(packet: Packet, length: Int): ByteArray

Performs a read request after flushing the queue, if needed.

Link copied to clipboard
suspend fun setPin(pin: Byte, value: Byte)

Sets the state of a single pin.

Link copied to clipboard
suspend fun setPins(pinsAndValues: List<Pair<Byte, Byte>>)

Sets the states of multiple pins.

Link copied to clipboard
suspend fun spiWrite(data: ByteArray)

Sends raw byte data to write over SPI.

Link copied to clipboard
suspend fun waitForPin(pin: Byte, value: Byte, millis: Short)

Waits for a given pin to reach a particular value before continuing execution.

Link copied to clipboard
suspend fun write(packet: Packet)

Queues up an arbitrary write request.