From 6e7a2b16a18f8b7ccff5756a716e7671c6fadbab Mon Sep 17 00:00:00 2001 From: Jakob Odersky Date: Sat, 2 Mar 2013 13:10:29 +0100 Subject: add scala ACE over RxTx implementation --- scala/rxtx/.gitignore | 18 +++++++ scala/rxtx/README | 2 + scala/rxtx/build.sbt | 11 +++++ .../com/github/jodersky/ace/rxtx/Serial.scala | 56 ++++++++++++++++++++++ .../com/github/jodersky/ace/rxtx/test/Main.scala | 27 +++++++++++ 5 files changed, 114 insertions(+) create mode 100644 scala/rxtx/.gitignore create mode 100644 scala/rxtx/README create mode 100644 scala/rxtx/build.sbt create mode 100644 scala/rxtx/src/main/scala/com/github/jodersky/ace/rxtx/Serial.scala create mode 100644 scala/rxtx/src/test/scala/com/github/jodersky/ace/rxtx/test/Main.scala diff --git a/scala/rxtx/.gitignore b/scala/rxtx/.gitignore new file mode 100644 index 0000000..94730a8 --- /dev/null +++ b/scala/rxtx/.gitignore @@ -0,0 +1,18 @@ +*.class +*.log + +# sbt specific +dist/* +target/ +.target/ +lib_managed/ +src_managed/ +project/boot/ +project/plugins/project/ + +# Scala-IDE specific +.scala_dependencies +.project +.classpath +.cache +.settings/ diff --git a/scala/rxtx/README b/scala/rxtx/README new file mode 100644 index 0000000..4361c2f --- /dev/null +++ b/scala/rxtx/README @@ -0,0 +1,2 @@ +This implementation uses ACE over RxTx to provide access to a physical serial port. +See http://rxtx.qbang.org for information about RxTx. diff --git a/scala/rxtx/build.sbt b/scala/rxtx/build.sbt new file mode 100644 index 0000000..e7068da --- /dev/null +++ b/scala/rxtx/build.sbt @@ -0,0 +1,11 @@ +name := "ace-rxtx" + +organization := "com.github.jodersky" + +version := "1.0-SNAPSHOT" + +scalaVersion := "2.10.0" + +scalacOptions ++= Seq("-deprecation","-feature") + +libraryDependencies += "com.github.jodersky" %% "ace" % "1.0-SNAPSHOT" diff --git a/scala/rxtx/src/main/scala/com/github/jodersky/ace/rxtx/Serial.scala b/scala/rxtx/src/main/scala/com/github/jodersky/ace/rxtx/Serial.scala new file mode 100644 index 0000000..1af3898 --- /dev/null +++ b/scala/rxtx/src/main/scala/com/github/jodersky/ace/rxtx/Serial.scala @@ -0,0 +1,56 @@ +package com.github.jodersky.ace.rxtx + +import scala.concurrent._ +import scala.concurrent.ExecutionContext.Implicits.global +import java.io.IOException +import gnu.io.CommPort +import gnu.io.CommPortIdentifier +import gnu.io.SerialPort +import gnu.io.SerialPortEvent +import gnu.io.SerialPortEventListener +import scala.collection.mutable.ArrayBuffer +import com.github.jodersky.ace.PhysicalLayer + +class Serial( + portName: String, + baudRate: Int = 9600, + dataBits: Int = SerialPort.DATABITS_8, + stopBits: Int = SerialPort.STOPBITS_1, + parity: Int = SerialPort.PARITY_NONE) extends PhysicalLayer { + + lazy val portIdentifier = { + System.setProperty("gnu.io.rxtx.SerialPorts", portName); + CommPortIdentifier.getPortIdentifier(portName) + } + + private lazy val port: SerialPort = { + if (portIdentifier.isCurrentlyOwned()) throw new IOException("Port " + portName + " is currently in use") + val commPort = portIdentifier.open(this.getClass().getName(), 2000); + val serialPort = commPort.asInstanceOf[SerialPort] + serialPort.setSerialPortParams(baudRate, dataBits, stopBits, parity) + serialPort + } + + private lazy val listener = new SerialPortEventListener { + override def serialEvent(event: SerialPortEvent) = { + val in = port.getInputStream() + while (in.available() > 0) { + notifyHigher(Seq(in.read() & 0xff)) + } + } + } + + def send(data: Seq[Int]) = future { + port.getOutputStream().write(data.map(_.toByte).toArray) + } map { _ => data } + + def begin() = { + port.addEventListener(listener) + port.notifyOnDataAvailable(true) + } + + def close() = { + port.close() + } + +} \ No newline at end of file diff --git a/scala/rxtx/src/test/scala/com/github/jodersky/ace/rxtx/test/Main.scala b/scala/rxtx/src/test/scala/com/github/jodersky/ace/rxtx/test/Main.scala new file mode 100644 index 0000000..1b42fa3 --- /dev/null +++ b/scala/rxtx/src/test/scala/com/github/jodersky/ace/rxtx/test/Main.scala @@ -0,0 +1,27 @@ +package com.github.jodersky.ace.rxtx.test + +import com.github.jodersky.ace.Arq; +import com.github.jodersky.ace.Framer; +import com.github.jodersky.ace._ +import com.github.jodersky.ace.rxtx._ +import scala.concurrent.ExecutionContext.Implicits.global + + +object Main { + + def main(args: Array[String]): Unit = { + val serial = new Serial("/dev/ttyACM0") + val framer = new Framer + val arq = new Arq(200) + val app = new SimpleActionLayer((s: Seq[Int]) => println(s)) + + serial connect framer connect arq connect app + serial.begin() + + while (true) { + app.send(Console.readLine.getBytes().map(_.toInt)).map(sent => Console.println("> " + sent)) + } + + } + +} \ No newline at end of file -- cgit v1.2.3