aboutsummaryrefslogtreecommitdiff
path: root/scala/ace/src/main/scala/com/github/jodersky/ace/protocol/PhysicalLayer.scala
diff options
context:
space:
mode:
Diffstat (limited to 'scala/ace/src/main/scala/com/github/jodersky/ace/protocol/PhysicalLayer.scala')
-rw-r--r--scala/ace/src/main/scala/com/github/jodersky/ace/protocol/PhysicalLayer.scala37
1 files changed, 37 insertions, 0 deletions
diff --git a/scala/ace/src/main/scala/com/github/jodersky/ace/protocol/PhysicalLayer.scala b/scala/ace/src/main/scala/com/github/jodersky/ace/protocol/PhysicalLayer.scala
new file mode 100644
index 0000000..39c2d25
--- /dev/null
+++ b/scala/ace/src/main/scala/com/github/jodersky/ace/protocol/PhysicalLayer.scala
@@ -0,0 +1,37 @@
+package com.github.jodersky.ace.protocol
+
+import scala.concurrent._
+import scala.concurrent.ExecutionContext.Implicits.global
+import jssc.SerialPort
+import java.io.IOException
+import jssc.SerialPortEvent
+import jssc.SerialPortEventListener
+
+class PhysicalLayer(serial: SerialPort) extends ReactiveLayer[Nothing, Array[Byte]] {
+
+ def receive(nothing: Nothing) = throw new UnsupportedOperationException("A receive function cannot be called on the lowest layer.")
+
+ private val listener = new SerialPortEventListener {
+ override def serialEvent(event: SerialPortEvent) = {
+ if (event.isRXCHAR()) {
+ val bytes = serial.readBytes
+ if (bytes != null) notifyHigher(bytes)
+ }
+ }
+ }
+
+
+ def write(data: Array[Byte]) = future {
+ serial.writeBytes(data)
+ } map { success =>
+ if (success) data
+ else throw new IOException("Could not write to serial port.")
+ }
+
+ def begin() = {
+ val mask = SerialPort.MASK_RXCHAR + SerialPort.MASK_CTS + SerialPort.MASK_DSR
+ serial.setEventsMask(mask)
+ serial.addEventListener(listener)
+ }
+
+} \ No newline at end of file