From e7d02be8493a7b9fc70518b23bec53f885391e79 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Fri, 19 May 2023 21:55:21 +0300 Subject: [PATCH] Add Pi serial port --- .../kotlin/space/kscience/controls/ports/Port.kt | 2 +- .../space/kscience/controls/ports/phrases.kt | 5 +++++ .../space/kscience/controls/pi/PiSerialPort.kt | 14 ++++++++++++-- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/controls-core/src/commonMain/kotlin/space/kscience/controls/ports/Port.kt b/controls-core/src/commonMain/kotlin/space/kscience/controls/ports/Port.kt index 4fad1db..db19817 100644 --- a/controls-core/src/commonMain/kotlin/space/kscience/controls/ports/Port.kt +++ b/controls-core/src/commonMain/kotlin/space/kscience/controls/ports/Port.kt @@ -82,7 +82,7 @@ public abstract class AbstractPort( /** * 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. - * For example [delimitedIncoming] generates phrases with fixed delimiter. + * For example [stringsDelimitedIncoming] generates phrases with fixed delimiter. */ override fun receiving(): Flow = incoming.receiveAsFlow() diff --git a/controls-core/src/commonMain/kotlin/space/kscience/controls/ports/phrases.kt b/controls-core/src/commonMain/kotlin/space/kscience/controls/ports/phrases.kt index 21afa8d..1214d01 100644 --- a/controls-core/src/commonMain/kotlin/space/kscience/controls/ports/phrases.kt +++ b/controls-core/src/commonMain/kotlin/space/kscience/controls/ports/phrases.kt @@ -48,3 +48,8 @@ public fun Flow.withStringDelimiter(delimiter: String): Flow * A flow of delimited phrases */ public fun Port.delimitedIncoming(delimiter: ByteArray): Flow = receiving().withDelimiter(delimiter) + +/** + * A flow of delimited phrases with string content + */ +public fun Port.stringsDelimitedIncoming(delimiter: String): Flow = receiving().withStringDelimiter(delimiter) diff --git a/controls-pi/src/main/kotlin/space/kscience/controls/pi/PiSerialPort.kt b/controls-pi/src/main/kotlin/space/kscience/controls/pi/PiSerialPort.kt index 0437698..4924b8d 100644 --- a/controls-pi/src/main/kotlin/space/kscience/controls/pi/PiSerialPort.kt +++ b/controls-pi/src/main/kotlin/space/kscience/controls/pi/PiSerialPort.kt @@ -1,6 +1,7 @@ package space.kscience.controls.pi import com.pi4j.Pi4J +import com.pi4j.io.serial.Baud import com.pi4j.io.serial.Serial import com.pi4j.io.serial.SerialConfigBuilder 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.logger 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 kotlin.coroutines.CoroutineContext @@ -54,10 +58,16 @@ public class PiSerialPort( override val type: String get() = "pi" 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) { - Pi4J.newAutoContext().serial() + val device: String = meta["device"].string ?: error("Device name not defined") + val baudRate: Baud = meta["baudRate"].enum() ?: Baud._9600 + Pi4J.newAutoContext().serial(device) { + baud8N1(baudRate) + } } }