diff options
Diffstat (limited to 'flow-samples/terminal/src/main')
2 files changed, 100 insertions, 0 deletions
diff --git a/flow-samples/terminal/src/main/scala/com/github/jodersky/flow/samples/terminal/Main.scala b/flow-samples/terminal/src/main/scala/com/github/jodersky/flow/samples/terminal/Main.scala new file mode 100644 index 0000000..b8d332c --- /dev/null +++ b/flow-samples/terminal/src/main/scala/com/github/jodersky/flow/samples/terminal/Main.scala @@ -0,0 +1,46 @@ +package com.github.jodersky.flow +package samples.terminal + +import com.github.jodersky.flow._ +import akka.actor.ActorSystem +import akka.actor.Props +import akka.actor.actorRef2Scala +import akka.util.ByteString + +object Main { + + def defaultIfEmpty(in: String, default: String): String = if (in.isEmpty) default else in + + def ask(label: String, default: String) = { + print(s"${label} [${default}]: ") + val in = Console.readLine() + println("") + defaultIfEmpty(in, default) + } + + def main(args: Array[String]): Unit = { + val port = ask("Device", "/dev/ttyACM0") + val baud = ask("Baud rate", "115200").toInt + val cs = ask("Char size", "8").toInt + val tsb = ask("Use two stop bits", "false").toBoolean + val parity = Parity(ask("Parity [0=None, 1=Odd, 2=Even]", "0").toInt) + println("Starting terminal, enter :q to exit.") + + internal.InternalSerial.debug(true) + val system = ActorSystem("flow") + val serial = system.actorOf(Props(classOf[SerialHandler], port, baud, cs, tsb, parity), name = "serial-handler") + + var continue = true + while (continue) { + val in = Console.readLine() + if (in == ":q") { + continue = false + serial ! "close" + } else { + serial ! ByteString(in.getBytes()) + } + } + system.shutdown() + println("Stopped terminal.") + } +}
\ No newline at end of file diff --git a/flow-samples/terminal/src/main/scala/com/github/jodersky/flow/samples/terminal/SerialHandler.scala b/flow-samples/terminal/src/main/scala/com/github/jodersky/flow/samples/terminal/SerialHandler.scala new file mode 100644 index 0000000..33f276a --- /dev/null +++ b/flow-samples/terminal/src/main/scala/com/github/jodersky/flow/samples/terminal/SerialHandler.scala @@ -0,0 +1,54 @@ +package com.github.jodersky.flow.samples.terminal + +import com.github.jodersky.flow.Serial._ +import akka.actor.Actor +import akka.actor.ActorLogging +import akka.actor.ActorRef +import akka.util.ByteString +import akka.io.IO +import com.github.jodersky.flow.Serial +import akka.actor.Terminated +import com.github.jodersky.flow.Parity + + +class SerialHandler(port: String, baud: Int, cs: Int, tsb: Boolean, parity: Parity.Parity) extends Actor with ActorLogging { + import context._ + + log.info(s"Requesting manager to open port: ${port}, baud: ${baud}") + IO(Serial) ! Serial.Open(self, port, baud) + + def receive = { + case OpenFailed(reason, _, _, _, _, _) => { + log.error(s"Connection failed, stopping handler. Reason: ${reason}") + context stop self + } + case Opened(port, _, _, _, _) => { + log.info(s"Port ${port} is now open.") + context become opened(sender) + } + } + + def opened(operator: ActorRef): Receive = { + + case Received(data) => { + log.info(s"Received data: ${formatData(data)} (${new String(data.toArray, "UTF-8")})") + } + case Wrote(data) => log.info(s"Wrote data: ${formatData(data)} (${new String(data.toArray, "UTF-8")})") + case Closed(None) => { + log.info("Operator closed normally, exiting handler.") + context stop self + } + case Closed(Some(ex)) => { + log.error("Operator crashed, exiting handler.") + context stop self + } + case "close" => { + log.info("Initiating close.") + operator ! Close + } + case data: ByteString => operator ! Write(data, true) + } + + private def formatData(data: ByteString) = data.mkString("[", ",", "]") + +}
\ No newline at end of file |