Add Pi serial port
This commit is contained in:
parent
a5a2edc81a
commit
e7d02be849
@ -82,7 +82,7 @@ public abstract class AbstractPort(
|
|||||||
/**
|
/**
|
||||||
* Raw flow of incoming data chunks. The chunks are not guaranteed to be complete phrases.
|
* Raw flow of incoming data chunks. The chunks are not guaranteed to be complete phrases.
|
||||||
* In order to form phrases, some condition should be used on top of it.
|
* In order to form phrases, some condition should be used on top of it.
|
||||||
* For example [delimitedIncoming] generates phrases with fixed delimiter.
|
* For example [stringsDelimitedIncoming] generates phrases with fixed delimiter.
|
||||||
*/
|
*/
|
||||||
override fun receiving(): Flow<ByteArray> = incoming.receiveAsFlow()
|
override fun receiving(): Flow<ByteArray> = incoming.receiveAsFlow()
|
||||||
|
|
||||||
|
@ -48,3 +48,8 @@ public fun Flow<ByteArray>.withStringDelimiter(delimiter: String): Flow<String>
|
|||||||
* A flow of delimited phrases
|
* A flow of delimited phrases
|
||||||
*/
|
*/
|
||||||
public fun Port.delimitedIncoming(delimiter: ByteArray): Flow<ByteArray> = receiving().withDelimiter(delimiter)
|
public fun Port.delimitedIncoming(delimiter: ByteArray): Flow<ByteArray> = receiving().withDelimiter(delimiter)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A flow of delimited phrases with string content
|
||||||
|
*/
|
||||||
|
public fun Port.stringsDelimitedIncoming(delimiter: String): Flow<String> = receiving().withStringDelimiter(delimiter)
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package space.kscience.controls.pi
|
package space.kscience.controls.pi
|
||||||
|
|
||||||
import com.pi4j.Pi4J
|
import com.pi4j.Pi4J
|
||||||
|
import com.pi4j.io.serial.Baud
|
||||||
import com.pi4j.io.serial.Serial
|
import com.pi4j.io.serial.Serial
|
||||||
import com.pi4j.io.serial.SerialConfigBuilder
|
import com.pi4j.io.serial.SerialConfigBuilder
|
||||||
import com.pi4j.ktx.io.serial
|
import com.pi4j.ktx.io.serial
|
||||||
@ -13,6 +14,9 @@ import space.kscience.dataforge.context.Context
|
|||||||
import space.kscience.dataforge.context.error
|
import space.kscience.dataforge.context.error
|
||||||
import space.kscience.dataforge.context.logger
|
import space.kscience.dataforge.context.logger
|
||||||
import space.kscience.dataforge.meta.Meta
|
import space.kscience.dataforge.meta.Meta
|
||||||
|
import space.kscience.dataforge.meta.enum
|
||||||
|
import space.kscience.dataforge.meta.get
|
||||||
|
import space.kscience.dataforge.meta.string
|
||||||
import java.nio.ByteBuffer
|
import java.nio.ByteBuffer
|
||||||
import kotlin.coroutines.CoroutineContext
|
import kotlin.coroutines.CoroutineContext
|
||||||
|
|
||||||
@ -54,10 +58,16 @@ public class PiSerialPort(
|
|||||||
override val type: String get() = "pi"
|
override val type: String get() = "pi"
|
||||||
|
|
||||||
public fun open(context: Context, device: String, block: SerialConfigBuilder.() -> Unit): PiSerialPort =
|
public fun open(context: Context, device: String, block: SerialConfigBuilder.() -> Unit): PiSerialPort =
|
||||||
PiSerialPort(context) { Pi4J.newAutoContext().serial(device, block) }
|
PiSerialPort(context) {
|
||||||
|
Pi4J.newAutoContext().serial(device, block)
|
||||||
|
}
|
||||||
|
|
||||||
override fun build(context: Context, meta: Meta): Port = PiSerialPort(context) {
|
override fun build(context: Context, meta: Meta): Port = PiSerialPort(context) {
|
||||||
Pi4J.newAutoContext().serial()
|
val device: String = meta["device"].string ?: error("Device name not defined")
|
||||||
|
val baudRate: Baud = meta["baudRate"].enum<Baud>() ?: Baud._9600
|
||||||
|
Pi4J.newAutoContext().serial(device) {
|
||||||
|
baud8N1(baudRate)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user