From 77db2136559ccef7d84cf6c0fd0166a970224680 Mon Sep 17 00:00:00 2001 From: Jakob Odersky Date: Tue, 26 Feb 2013 17:14:53 +0100 Subject: restructure scala directory in view of providing seperate projects for serial implementations --- .../jodersky/ace/protocol/PhysicalLayer.scala | 37 ++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 scala/ace/src/main/scala/com/github/jodersky/ace/protocol/PhysicalLayer.scala (limited to 'scala/ace/src/main/scala/com/github/jodersky/ace/protocol/PhysicalLayer.scala') 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 -- cgit v1.2.3