From 0178f0ec8da470143d92145f341e70aab73995ab Mon Sep 17 00:00:00 2001 From: Rik Veenboer Date: Thu, 11 Jun 2015 22:44:09 +0100 Subject: [PATCH] Snapshot of implementation of limited functionality --- .../com/github/boukefalos/tm1638/Arduino.java | 124 ++++++++++++++++++ .../boukefalos/tm1638/EchoReceiver.java} | 4 +- .../com/github/boukefalos/tm1638/Loader.java | 73 +++++++++++ .../com/github/boukefalos/tm1638/TM1638.java | 25 ++++ .../tm1638/exception/ArduinoException.java | 10 ++ .../tm1638/helper/ReceiverHelper.java | 20 +++ .../tm1638/helper/SenderHelper.java | 60 +++++++++ .../tm1638/helper/ServerHelper.java | 56 ++++++++ .../implementation/Localmplementation.java | 34 +++++ .../implementation/TcpImplementation.java | 37 ++++++ .../implementation/UdpImplementation.java | 34 +++++ .../tm1638/server/TM1638Server.java | 5 + .../tm1638/server/TM1638TcpClient.java | 19 +++ .../tm1638/server/TM1638TcpServer.java | 30 +++++ .../tm1638/server/TM1638UdpServer.java | 26 ++++ src/main/java/test/Arduino.java | 92 ------------- src/main/java/test/TM1638.java | 52 -------- src/main/java/test/Test.java | 33 ----- src/main/proto/tm1638.proto | 19 ++- src/test/java/test/TestLocal.java | 26 ++++ src/test/java/test/TestTcpCommunication.java | 49 +++++++ src/test/java/test/TestUdpCommunication.java | 49 +++++++ .../java/test/dummy/DummyEchoReceiver.java | 11 ++ 23 files changed, 703 insertions(+), 185 deletions(-) create mode 100644 src/main/java/com/github/boukefalos/tm1638/Arduino.java rename src/main/java/{test/EchoListener.java => com/github/boukefalos/tm1638/EchoReceiver.java} (50%) create mode 100644 src/main/java/com/github/boukefalos/tm1638/Loader.java create mode 100644 src/main/java/com/github/boukefalos/tm1638/TM1638.java create mode 100644 src/main/java/com/github/boukefalos/tm1638/exception/ArduinoException.java create mode 100644 src/main/java/com/github/boukefalos/tm1638/helper/ReceiverHelper.java create mode 100644 src/main/java/com/github/boukefalos/tm1638/helper/SenderHelper.java create mode 100644 src/main/java/com/github/boukefalos/tm1638/helper/ServerHelper.java create mode 100644 src/main/java/com/github/boukefalos/tm1638/implementation/Localmplementation.java create mode 100644 src/main/java/com/github/boukefalos/tm1638/implementation/TcpImplementation.java create mode 100644 src/main/java/com/github/boukefalos/tm1638/implementation/UdpImplementation.java create mode 100644 src/main/java/com/github/boukefalos/tm1638/server/TM1638Server.java create mode 100644 src/main/java/com/github/boukefalos/tm1638/server/TM1638TcpClient.java create mode 100644 src/main/java/com/github/boukefalos/tm1638/server/TM1638TcpServer.java create mode 100644 src/main/java/com/github/boukefalos/tm1638/server/TM1638UdpServer.java delete mode 100644 src/main/java/test/Arduino.java delete mode 100644 src/main/java/test/TM1638.java delete mode 100644 src/main/java/test/Test.java create mode 100644 src/test/java/test/TestLocal.java create mode 100644 src/test/java/test/TestTcpCommunication.java create mode 100644 src/test/java/test/TestUdpCommunication.java create mode 100644 src/test/java/test/dummy/DummyEchoReceiver.java diff --git a/src/main/java/com/github/boukefalos/tm1638/Arduino.java b/src/main/java/com/github/boukefalos/tm1638/Arduino.java new file mode 100644 index 0000000..c504021 --- /dev/null +++ b/src/main/java/com/github/boukefalos/tm1638/Arduino.java @@ -0,0 +1,124 @@ +package com.github.boukefalos.tm1638; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.TooManyListenersException; + +import com.github.boukefalos.tm1638.exception.ArduinoException; + +import purejavacomm.CommPortIdentifier; +import purejavacomm.PortInUseException; +import purejavacomm.SerialPort; +import purejavacomm.SerialPortEvent; +import purejavacomm.SerialPortEventListener; +import purejavacomm.UnsupportedCommOperationException; +import beerduino.Beerduino.Echo; + +public class Arduino implements SerialPortEventListener { + public static final int TIME_OUT = 1000; + public static final String PORT_NAMES[] = { + "tty.usbmodem", // Mac OS X + "usbdev", // Linux + "tty", // Linux + "serial", // Linux + "COM3", // Windows + }; + + protected static Arduino arduino; + + protected SerialPort port = null; + protected InputStream inputStream = null; + protected ArrayList echoReceiverList = new ArrayList(); + + private Arduino() {} + + public static Arduino getInstance() { + if (arduino == null) { + arduino = new Arduino(); + } + return arduino; + } + + protected void connect() throws ArduinoException { + CommPortIdentifier portid = null; + Enumeration portEnum = CommPortIdentifier.getPortIdentifiers(); + while (portEnum.hasMoreElements()) { + portid = (CommPortIdentifier)portEnum.nextElement(); + if (portid != null) { + System.out.println("Trying: " + portid.getName()); + for ( String portName: PORT_NAMES) { + if (portid.getName().equals(portName) || portid.getName().contains(portName)) { + try { + port = (SerialPort) portid.open("", TIME_OUT); + port.setFlowControlMode( + SerialPort.FLOWCONTROL_XONXOFF_IN + + SerialPort.FLOWCONTROL_XONXOFF_OUT); + inputStream = port.getInputStream(); + System.out.println( "Connected on port: " + portid.getName()); + port.addEventListener(this); + } catch (UnsupportedCommOperationException | PortInUseException | IOException | TooManyListenersException e) { + throw new ArduinoException("Failed to connect"); + } + port.notifyOnDataAvailable(true); + return; + } + } + } + } + throw new ArduinoException("No Arduino available"); + } + + public void serialEvent(SerialPortEvent event) { + try { + switch (event.getEventType()) { + case SerialPortEvent.DATA_AVAILABLE: + Echo echo = Echo.parseDelimitedFrom(inputStream); + for (EchoReceiver echoReceiver : echoReceiverList) { + echoReceiver.receive(echo); + } + break; + default: + break; + } + } catch (IOException e) { + e.printStackTrace(); + } + + } + + public InputStream getInputStream() throws ArduinoException { + if (port == null) { + connect(); + } + try { + return port.getInputStream(); + } catch (IOException e) { + throw new ArduinoException("Failed to get inputstream"); + } + } + + public OutputStream getOutputStream() throws ArduinoException { + if (port == null) { + connect(); + } + try { + return port.getOutputStream(); + } catch (IOException e) { + throw new ArduinoException("Failed to get inputstream"); + } + } + + public void addReceiver(EchoReceiver receiver) { + echoReceiverList.add(receiver); + } + + public void removeReceiver(EchoReceiver receiver) { + echoReceiverList.remove(receiver); + } + + public void close() { + port.close(); + } +} \ No newline at end of file diff --git a/src/main/java/test/EchoListener.java b/src/main/java/com/github/boukefalos/tm1638/EchoReceiver.java similarity index 50% rename from src/main/java/test/EchoListener.java rename to src/main/java/com/github/boukefalos/tm1638/EchoReceiver.java index 749ac42..91b33de 100644 --- a/src/main/java/test/EchoListener.java +++ b/src/main/java/com/github/boukefalos/tm1638/EchoReceiver.java @@ -1,7 +1,7 @@ -package test; +package com.github.boukefalos.tm1638; import beerduino.Beerduino.Echo; -public interface EchoListener { +public interface EchoReceiver { public void receive(Echo echo); } diff --git a/src/main/java/com/github/boukefalos/tm1638/Loader.java b/src/main/java/com/github/boukefalos/tm1638/Loader.java new file mode 100644 index 0000000..2b74585 --- /dev/null +++ b/src/main/java/com/github/boukefalos/tm1638/Loader.java @@ -0,0 +1,73 @@ +package com.github.boukefalos.tm1638; + +import java.util.Properties; + +import org.picocontainer.Parameter; +import org.picocontainer.parameters.ConstantParameter; + +import base.loader.AbstractLoader; +import base.work.Work; + +import com.github.boukefalos.tm1638.implementation.Localmplementation; +import com.github.boukefalos.tm1638.implementation.TcpImplementation; +import com.github.boukefalos.tm1638.implementation.UdpImplementation; +import com.github.boukefalos.tm1638.server.TM1638Server; +import com.github.boukefalos.tm1638.server.TM1638TcpServer; +import com.github.boukefalos.tm1638.server.TM1638UdpServer; + +public class Loader extends AbstractLoader { + protected static final String PROPERTIES_FILE = "TM1638.properties"; + + public Loader(Properties properties) { + super(); + + /* Add implementation */ + switch (properties.getProperty("implementation")) { + case "local": + pico.addComponent(Localmplementation.class); + break; + case "remote": + //pico.addComponent(Remote.class); + break; + } + + /* Add protocol */ + if (properties.getProperty("protocol") != null) { + switch (properties.getProperty("protocol")) { + case "tcp": + pico.addComponent(TcpImplementation.class, TcpImplementation.class, new Parameter[]{ + new ConstantParameter(properties.getProperty("remote.host")), + new ConstantParameter(Integer.valueOf(properties.getProperty("remote.port")))}); + break; + case "udp": + pico.addComponent(UdpImplementation.class, UdpImplementation.class, new Parameter[] { + new ConstantParameter(properties.getProperty("remote.host")), + new ConstantParameter(Integer.valueOf(properties.getProperty("remote.port")))}); + break; + } + } + + /* Add server */ + if (properties.getProperty("server") != null) { + switch (properties.getProperty("server.protocol")) { + case "tcp": + pico.addComponent(TM1638TcpServer.class, TM1638TcpServer.class, new Parameter[]{ + new ConstantParameter(getTM1638()), + new ConstantParameter(Integer.valueOf(properties.getProperty("server.port")))}); + break; + case "udp": + pico.addComponent(TM1638UdpServer.class, TM1638UdpServer.class, new Parameter[]{ + new ConstantParameter(getTM1638()), + new ConstantParameter(Integer.valueOf(properties.getProperty("server.port")))}); + } + } + } + + public TM1638 getTM1638() { + return pico.getComponent(TM1638.class); + } + + public Work getServer() { + return (Work) pico.getComponent(TM1638Server.class); + } +} diff --git a/src/main/java/com/github/boukefalos/tm1638/TM1638.java b/src/main/java/com/github/boukefalos/tm1638/TM1638.java new file mode 100644 index 0000000..504c1bf --- /dev/null +++ b/src/main/java/com/github/boukefalos/tm1638/TM1638.java @@ -0,0 +1,25 @@ +package com.github.boukefalos.tm1638; + +import com.github.boukefalos.tm1638.helper.SenderHelper; + +import tm1638.Tm1638.Color; +import base.sender.Sender; + +public abstract class TM1638 implements Sender { + public void ping(int id) { + SenderHelper.ping(this, id); + } + + public void setLed(Color color, int pos) { + SenderHelper.setLed(this, color, pos); + } + + public void construct(int dataPin, int clockPin, int strobePin) { + SenderHelper.construct(this, dataPin, clockPin, strobePin); + } + + public void start() {} + public void stop() {} + + public abstract void addReceiver(EchoReceiver receiver); +} \ No newline at end of file diff --git a/src/main/java/com/github/boukefalos/tm1638/exception/ArduinoException.java b/src/main/java/com/github/boukefalos/tm1638/exception/ArduinoException.java new file mode 100644 index 0000000..398e1ef --- /dev/null +++ b/src/main/java/com/github/boukefalos/tm1638/exception/ArduinoException.java @@ -0,0 +1,10 @@ +package com.github.boukefalos.tm1638.exception; + +public class ArduinoException extends Exception { + + protected static final long serialVersionUID = 1L; + + public ArduinoException(String message) { + super(message); + } +} diff --git a/src/main/java/com/github/boukefalos/tm1638/helper/ReceiverHelper.java b/src/main/java/com/github/boukefalos/tm1638/helper/ReceiverHelper.java new file mode 100644 index 0000000..e7fd5f8 --- /dev/null +++ b/src/main/java/com/github/boukefalos/tm1638/helper/ReceiverHelper.java @@ -0,0 +1,20 @@ +package com.github.boukefalos.tm1638.helper; + +import java.util.ArrayList; + +import beerduino.Beerduino.Echo; + +import com.github.boukefalos.tm1638.EchoReceiver; +import com.google.protobuf.InvalidProtocolBufferException; + +public class ReceiverHelper { + + public static void receive(ArrayList echoReceiverList, byte[] buffer) { + try { + Echo echo = Echo.parseFrom(buffer); + for (EchoReceiver echoReceiver : echoReceiverList) { + echoReceiver.receive(echo); + } + } catch (InvalidProtocolBufferException e) {} + } +} diff --git a/src/main/java/com/github/boukefalos/tm1638/helper/SenderHelper.java b/src/main/java/com/github/boukefalos/tm1638/helper/SenderHelper.java new file mode 100644 index 0000000..040b9f8 --- /dev/null +++ b/src/main/java/com/github/boukefalos/tm1638/helper/SenderHelper.java @@ -0,0 +1,60 @@ +package com.github.boukefalos.tm1638.helper; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import tm1638.Tm1638.Color; +import tm1638.Tm1638.Command; +import tm1638.Tm1638.Construct; +import tm1638.Tm1638.Ping; +import tm1638.Tm1638.Command.Type; +import tm1638.Tm1638.SetLed; +import base.sender.Sender; + + +public class SenderHelper { + public static final int BUFFER_SIZE = 1024; + protected static Logger logger = LoggerFactory.getLogger(SenderHelper.class); + + public static void command(Sender sender, Command command) { + ByteArrayOutputStream output = new ByteArrayOutputStream(BUFFER_SIZE); + try { + command.writeDelimitedTo(output); + byte[] buffer = output.toByteArray(); + System.out.println("command() " + new String(buffer).trim()); + sender.send(buffer); + } catch (IOException e) { + logger.error("Failed to send command"); + } + } + + public static void construct(Sender sender, int dataPin, int clockPin, int strobePin) { + command(sender, Command.newBuilder() + .setType(Type.CONSTRUCT) + .setConstruct( + Construct.newBuilder() + .setDataPin(dataPin) + .setClockPin(clockPin) + .setStrobePin(strobePin).build()).build()); + } + + public static void ping(Sender sender, int id) { + command(sender, Command.newBuilder() + .setType(Type.PING) + .setPing(Ping.newBuilder() + .setId(id)).build()); + } + + public static void setLed(Sender sender, Color color, int pos) { + command(sender, Command.newBuilder() + .setType(Type.SET_LED) + .setSetLed( + SetLed.newBuilder() + .setColor(color) + .setPos(pos).build()).build()); + + } +} diff --git a/src/main/java/com/github/boukefalos/tm1638/helper/ServerHelper.java b/src/main/java/com/github/boukefalos/tm1638/helper/ServerHelper.java new file mode 100644 index 0000000..0c9cbb5 --- /dev/null +++ b/src/main/java/com/github/boukefalos/tm1638/helper/ServerHelper.java @@ -0,0 +1,56 @@ +package com.github.boukefalos.tm1638.helper; + +import java.io.ByteArrayInputStream; +import java.io.IOException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import tm1638.Tm1638.Color; +import tm1638.Tm1638.Command; +import tm1638.Tm1638.Ping; +import tm1638.Tm1638.Server; +import tm1638.Tm1638.SetLed; + +import com.github.boukefalos.tm1638.TM1638; + +public class ServerHelper { + protected static Logger logger = LoggerFactory.getLogger(ServerHelper.class); + + public static void receive(TM1638 tm1638, byte[] buffer) { + ByteArrayInputStream input = new ByteArrayInputStream(buffer); + System.out.println("receive() " + new String(buffer).trim()); + try { + //Command command = Command.parseFrom(buffer); + Command command = Command.parseDelimitedFrom(input); + logger.debug("Command type = " + command.getType().name()); + switch (command.getType()) { + case SERVER: + Server server = command.getServer(); + // setup new udp server connection, multicast? + break; + case PING: + Ping ping = command.getPing(); + tm1638.ping(ping.getId()); + break; + case SET_LED: + SetLed setLed = command.getSetLed(); + logger.debug("Color = " + setLed.getColor().name()); + switch (setLed.getColor()) { + case RED: + tm1638.setLed(Color.RED, 1); + case GREEN: + tm1638.setLed(Color.GREEN, 1); + default: + break; + } + break; + default: + break; + } + } catch (IOException e) { + logger.error("Failed to parse input"); + return; + } + } +} diff --git a/src/main/java/com/github/boukefalos/tm1638/implementation/Localmplementation.java b/src/main/java/com/github/boukefalos/tm1638/implementation/Localmplementation.java new file mode 100644 index 0000000..39ecdb1 --- /dev/null +++ b/src/main/java/com/github/boukefalos/tm1638/implementation/Localmplementation.java @@ -0,0 +1,34 @@ +package com.github.boukefalos.tm1638.implementation; + +import java.io.IOException; +import java.io.OutputStream; + +import com.github.boukefalos.tm1638.Arduino; +import com.github.boukefalos.tm1638.EchoReceiver; +import com.github.boukefalos.tm1638.TM1638; + +public class Localmplementation extends TM1638 { + protected Arduino arduino; + protected OutputStream outputStream; + + public Localmplementation() throws Exception { + this(Arduino.getInstance()); + } + + public Localmplementation(Arduino arduino) throws Exception { + this.arduino = arduino; + outputStream = arduino.getOutputStream(); + } + + public void send(byte[] buffer) throws IOException { + outputStream.write(buffer); + } + + public void stop() { + arduino.close(); + } + + public void addReceiver(EchoReceiver receiver) { + arduino.addReceiver(receiver); + } +} diff --git a/src/main/java/com/github/boukefalos/tm1638/implementation/TcpImplementation.java b/src/main/java/com/github/boukefalos/tm1638/implementation/TcpImplementation.java new file mode 100644 index 0000000..86601f7 --- /dev/null +++ b/src/main/java/com/github/boukefalos/tm1638/implementation/TcpImplementation.java @@ -0,0 +1,37 @@ +package com.github.boukefalos.tm1638.implementation; + +import java.io.IOException; +import java.util.ArrayList; + +import base.receiver.Receiver; +import base.server.channel.TcpClient; + +import com.github.boukefalos.tm1638.EchoReceiver; +import com.github.boukefalos.tm1638.TM1638; +import com.github.boukefalos.tm1638.helper.ReceiverHelper; + +public class TcpImplementation extends TM1638 implements Receiver { + protected TcpClient client; + protected ArrayList echoReceiverList = new ArrayList(); + + public TcpImplementation(String host, int port) { + client = new TcpClient(host, port); + } + + public void send(byte[] buffer) throws IOException { + if (!client.active()) { + client.start(); + } + client.send(buffer); + } + + public void addReceiver(EchoReceiver echoReceiver) { + client.register(this); + echoReceiverList.add(echoReceiver); + } + + public void receive(byte[] buffer) { + System.out.println(new String(buffer).trim()); + ReceiverHelper.receive(echoReceiverList, buffer); + } +} diff --git a/src/main/java/com/github/boukefalos/tm1638/implementation/UdpImplementation.java b/src/main/java/com/github/boukefalos/tm1638/implementation/UdpImplementation.java new file mode 100644 index 0000000..a971d32 --- /dev/null +++ b/src/main/java/com/github/boukefalos/tm1638/implementation/UdpImplementation.java @@ -0,0 +1,34 @@ +package com.github.boukefalos.tm1638.implementation; + +import java.io.IOException; +import java.net.UnknownHostException; +import java.util.ArrayList; + +import base.receiver.Receiver; +import base.sender.UdpSender; + +import com.github.boukefalos.tm1638.EchoReceiver; +import com.github.boukefalos.tm1638.TM1638; +import com.github.boukefalos.tm1638.helper.ReceiverHelper; + +public class UdpImplementation extends TM1638 implements Receiver { + protected UdpSender sender; + protected ArrayList echoReceiverList = new ArrayList(); + + public UdpImplementation(String host, int port) throws UnknownHostException { + sender = new UdpSender(host, port); + } + + public void send(byte[] buffer) throws IOException { + sender.send(buffer); + } + + public void addReceiver(EchoReceiver receiver) { + // Start 2-way communication + + } + + public void receive(byte[] buffer) { + ReceiverHelper.receive(echoReceiverList, buffer); + } +} diff --git a/src/main/java/com/github/boukefalos/tm1638/server/TM1638Server.java b/src/main/java/com/github/boukefalos/tm1638/server/TM1638Server.java new file mode 100644 index 0000000..cf3ce31 --- /dev/null +++ b/src/main/java/com/github/boukefalos/tm1638/server/TM1638Server.java @@ -0,0 +1,5 @@ +package com.github.boukefalos.tm1638.server; + +public interface TM1638Server { + +} \ No newline at end of file diff --git a/src/main/java/com/github/boukefalos/tm1638/server/TM1638TcpClient.java b/src/main/java/com/github/boukefalos/tm1638/server/TM1638TcpClient.java new file mode 100644 index 0000000..c4fad16 --- /dev/null +++ b/src/main/java/com/github/boukefalos/tm1638/server/TM1638TcpClient.java @@ -0,0 +1,19 @@ +package com.github.boukefalos.tm1638.server; + +import java.nio.channels.SocketChannel; + +import base.server.channel.TcpServerClient; + +public class TM1638TcpClient extends TcpServerClient { + protected TM1638TcpServer server; + + public TM1638TcpClient(TM1638TcpServer server, SocketChannel socketChannel, Integer bufferSize) { + super(server, socketChannel, bufferSize); + this.server = server; + } + + public void receive(byte[] buffer) { + System.out.println("serverClient.receive() " + new String(buffer).trim()); + server.receive(buffer); + } +} diff --git a/src/main/java/com/github/boukefalos/tm1638/server/TM1638TcpServer.java b/src/main/java/com/github/boukefalos/tm1638/server/TM1638TcpServer.java new file mode 100644 index 0000000..a453a53 --- /dev/null +++ b/src/main/java/com/github/boukefalos/tm1638/server/TM1638TcpServer.java @@ -0,0 +1,30 @@ +package com.github.boukefalos.tm1638.server; + +import base.server.channel.TcpServerClient; +import base.server.channel.TcpServer; + +import com.github.boukefalos.tm1638.TM1638; +import com.github.boukefalos.tm1638.helper.ServerHelper; + +public class TM1638TcpServer extends TcpServer implements TM1638Server { + protected TM1638 tm1638; + + public TM1638TcpServer(TM1638 tm1638, int port) { + this(tm1638, port, BUFFER_SIZE); + } + + public TM1638TcpServer(TM1638 tm1638, int port, int bufferSize) { + super(port, TM1638TcpClient.class, bufferSize); + this.tm1638 = tm1638; + //addReceiver(this); make sure client messages get passed on to arduino + } + + protected void initClient(TcpServerClient serverClient) { + + } + + protected void receive(byte[] buffer) { + // write directly to arduino instead of decoding? + ServerHelper.receive(tm1638, buffer); + } +} \ No newline at end of file diff --git a/src/main/java/com/github/boukefalos/tm1638/server/TM1638UdpServer.java b/src/main/java/com/github/boukefalos/tm1638/server/TM1638UdpServer.java new file mode 100644 index 0000000..2c55a2e --- /dev/null +++ b/src/main/java/com/github/boukefalos/tm1638/server/TM1638UdpServer.java @@ -0,0 +1,26 @@ +package com.github.boukefalos.tm1638.server; + +import base.receiver.Receiver; +import base.server.datagram.UdpServer; + +import com.github.boukefalos.tm1638.TM1638; +import com.github.boukefalos.tm1638.helper.ServerHelper; + +public class TM1638UdpServer extends UdpServer implements TM1638Server, Receiver { + protected TM1638 tm1638; + + public TM1638UdpServer(TM1638 tm1638, int port) { + this(tm1638, port, BUFFER_SIZE); + } + + public TM1638UdpServer(TM1638 tm1638, int port, int bufferSize) { + super(port, bufferSize); + this.tm1638 = tm1638; + this.bufferSize = bufferSize; + addReceiver(this); + } + + public void receive(byte[] buffer) { + ServerHelper.receive(tm1638, buffer); + } +} \ No newline at end of file diff --git a/src/main/java/test/Arduino.java b/src/main/java/test/Arduino.java deleted file mode 100644 index 78755f4..0000000 --- a/src/main/java/test/Arduino.java +++ /dev/null @@ -1,92 +0,0 @@ -package test; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.util.ArrayList; -import java.util.Enumeration; -import java.util.TooManyListenersException; - -import purejavacomm.CommPortIdentifier; -import purejavacomm.PortInUseException; -import purejavacomm.SerialPort; -import purejavacomm.SerialPortEvent; -import purejavacomm.SerialPortEventListener; -import purejavacomm.UnsupportedCommOperationException; - -import beerduino.Beerduino.Echo; - -public class Arduino implements SerialPortEventListener { - public static final int TIME_OUT = 1000; - public static final String PORT_NAMES[] = { - "tty.usbmodem", // Mac OS X - "usbdev", // Linux - "tty", // Linux - "serial", // Linux - "COM3", // Windows - }; - - protected SerialPort port = null; - protected InputStream inputStream = null; - protected ArrayList listenerList = new ArrayList(); - - public void connect() throws UnsupportedCommOperationException, PortInUseException, IOException, TooManyListenersException { - CommPortIdentifier portid = null; - Enumeration portEnum = CommPortIdentifier.getPortIdentifiers(); - while (portEnum.hasMoreElements()) { - portid = (CommPortIdentifier)portEnum.nextElement(); - if (portid != null) { - System.out.println("Trying: " + portid.getName()); - for ( String portName: PORT_NAMES) { - if ( portid.getName().equals(portName) || portid.getName().contains(portName)) { - port = (SerialPort) portid.open("", TIME_OUT); - port.setFlowControlMode( - SerialPort.FLOWCONTROL_XONXOFF_IN + - SerialPort.FLOWCONTROL_XONXOFF_OUT); - inputStream = port.getInputStream(); - System.out.println( "Connected on port: " + portid.getName()); - port.addEventListener(this); - port.notifyOnDataAvailable(true); - } - } - } - } - } - - public void serialEvent(SerialPortEvent event) { - try { - switch (event.getEventType()) { - case SerialPortEvent.DATA_AVAILABLE: - Echo echo = Echo.parseDelimitedFrom(inputStream); - for (EchoListener listener : listenerList) { - listener.receive(echo); - } - break; - default: - break; - } - } catch (IOException e) { - e.printStackTrace(); - } - - } - - public InputStream getInputStream() throws IOException { - return port.getInputStream(); - } - - public OutputStream getOutputStream() throws IOException { - return port.getOutputStream(); - } - - public void addListener(EchoListener listener) { - listenerList.add(listener); - } - - public void removeListener(EchoListener listener) { - listenerList.remove(listener); - } - - public void close() { - port.close(); - } -} \ No newline at end of file diff --git a/src/main/java/test/TM1638.java b/src/main/java/test/TM1638.java deleted file mode 100644 index ba959f1..0000000 --- a/src/main/java/test/TM1638.java +++ /dev/null @@ -1,52 +0,0 @@ -package test; - -import java.io.IOException; -import java.io.OutputStream; - -import tm1638.Tm1638.Color; -import tm1638.Tm1638.Command; -import tm1638.Tm1638.Construct; -import tm1638.Tm1638.Command.Type; -import tm1638.Tm1638.Ping; -import tm1638.Tm1638.SetLed; - -public class TM1638 { - protected Arduino arduino; - protected Command command; - protected OutputStream outputStream; - - public TM1638(Arduino arduino) throws IOException { - this.arduino = arduino; - outputStream = arduino.getOutputStream(); - } - - public void ping(int id) throws IOException { - command = Command.newBuilder() - .setType(Type.PING) - .setPing(Ping.newBuilder() - .setId(id)).build(); - command.writeDelimitedTo(outputStream); - - } - - public void setLed(Color color, int pos) throws IOException { - command = Command.newBuilder() - .setType(Type.SET_LED) - .setSetLed( - SetLed.newBuilder() - .setColor(color) - .setPos(pos).build()).build(); - command.writeDelimitedTo(outputStream); - } - - public void construct(int dataPin, int clockPin, int strobePin) throws IOException { - command = Command.newBuilder() - .setType(Type.CONSTRUCT) - .setConstruct( - Construct.newBuilder() - .setDataPin(dataPin) - .setClockPin(clockPin) - .setStrobePin(strobePin).build()).build(); - command.writeDelimitedTo(outputStream); - } -} diff --git a/src/main/java/test/Test.java b/src/main/java/test/Test.java deleted file mode 100644 index 1b2d399..0000000 --- a/src/main/java/test/Test.java +++ /dev/null @@ -1,33 +0,0 @@ -package test; - -import tm1638.Tm1638.Color; -import beerduino.Beerduino.Echo; - -public class Test implements EchoListener { - public static void main(String[] argv) { - try { - new Test().start(); - } catch (Exception e) { - e.printStackTrace(); - } - } - - private void start() throws Exception { - Arduino arduino = new Arduino(); - arduino.connect(); - arduino.addListener(this); - int i = 123; - TM1638 TM1638 = new TM1638(arduino); - TM1638.construct(8, 9, 7); - while (i < 10000) { - TM1638.ping(i++); - TM1638.setLed(i % 3 == 0 ? Color.GREEN : Color.RED, i % 7); - Thread.sleep(1000); - } - arduino.close(); - } - - public void receive(Echo echo) { - System.out.println("> " + echo.getMessage() + " " + echo.getId()); - } -} diff --git a/src/main/proto/tm1638.proto b/src/main/proto/tm1638.proto index e70fbd6..8aae2f4 100644 --- a/src/main/proto/tm1638.proto +++ b/src/main/proto/tm1638.proto @@ -15,15 +15,22 @@ enum Module { message Command { enum Type { - PING = 1; - CONSTRUCT = 2; - SET_LED = 10; + SERVER = 1; + PING = 2; + CONSTRUCT = 3; + SET_LED = 4; } required Type type = 1; - optional Ping ping = 2; - optional Construct construct = 3; - optional SetLed setLed = 10; + optional Server server = 2; + optional Ping ping = 3; + optional Construct construct = 4; + optional SetLed setLed = 5; +} + +message Server { + optional string host = 1; + required int32 port = 2; } message Ping { diff --git a/src/test/java/test/TestLocal.java b/src/test/java/test/TestLocal.java new file mode 100644 index 0000000..4cc3ace --- /dev/null +++ b/src/test/java/test/TestLocal.java @@ -0,0 +1,26 @@ +package test; + +import test.dummy.DummyEchoReceiver; +import tm1638.Tm1638.Color; + +import com.github.boukefalos.tm1638.TM1638; +import com.github.boukefalos.tm1638.implementation.Localmplementation; + +public class TestLocal { + public void start() throws Exception { + int i = 123; + TM1638 TM1638 = new Localmplementation(); + TM1638.addReceiver(new DummyEchoReceiver()); + TM1638.construct(8, 9, 7); + while (i < 10000) { + TM1638.ping(i++); + TM1638.setLed(i % 3 == 0 ? Color.GREEN : Color.RED, i % 7); + Thread.sleep(1000); + } + TM1638.stop(); + } + + public static void main(String[] args) throws Exception { + new TestLocal().start(); + } +} diff --git a/src/test/java/test/TestTcpCommunication.java b/src/test/java/test/TestTcpCommunication.java new file mode 100644 index 0000000..b2dc084 --- /dev/null +++ b/src/test/java/test/TestTcpCommunication.java @@ -0,0 +1,49 @@ +package test; + +import java.util.Properties; + +import test.dummy.DummyEchoReceiver; +import tm1638.Tm1638.Color; +import base.work.Work; + +import com.github.boukefalos.tm1638.Loader; +import com.github.boukefalos.tm1638.TM1638; + +public class TestTcpCommunication { + public static void main(String[] args) { + try { + Properties localProperties = new Properties(); + localProperties.setProperty("implementation", "local"); + localProperties.setProperty("server", "true"); + localProperties.setProperty("server.port", "8883"); + localProperties.setProperty("server.protocol", "tcp"); + + Properties remoteProperties = new Properties(); + remoteProperties.setProperty("implementation", "remote"); + remoteProperties.setProperty("protocol", "tcp"); + remoteProperties.setProperty("remote.host", "localhost"); + remoteProperties.setProperty("remote.port", "8883"); + + Loader localLoader = new Loader(localProperties); + Loader remoteLoader = new Loader(remoteProperties); + + TM1638 localTM1638 = localLoader.getTM1638(); + TM1638 remoteTM1638 = remoteLoader.getTM1638(); + + Work server = localLoader.getServer(); + server.start(); + + remoteTM1638.addReceiver(new DummyEchoReceiver()); + remoteTM1638.start(); + remoteTM1638.construct(8, 9, 7); + int i = 123; + while (i < 10000) { + remoteTM1638.ping(i++); + remoteTM1638.setLed(i % 3 == 0 ? Color.GREEN : Color.RED, i % 7); + Thread.sleep(1000); + } + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/src/test/java/test/TestUdpCommunication.java b/src/test/java/test/TestUdpCommunication.java new file mode 100644 index 0000000..75e54df --- /dev/null +++ b/src/test/java/test/TestUdpCommunication.java @@ -0,0 +1,49 @@ +package test; + +import java.util.Properties; + +import test.dummy.DummyEchoReceiver; +import tm1638.Tm1638.Color; +import base.work.Work; + +import com.github.boukefalos.tm1638.Loader; +import com.github.boukefalos.tm1638.TM1638; + +public class TestUdpCommunication { + public static void main(String[] args) { + try { + Properties localProperties = new Properties(); + localProperties.setProperty("implementation", "local"); + localProperties.setProperty("server", "true"); + localProperties.setProperty("server.port", "8883"); + localProperties.setProperty("server.protocol", "udp"); + + Properties remoteProperties = new Properties(); + remoteProperties.setProperty("implementation", "remote"); + remoteProperties.setProperty("protocol", "udp"); + remoteProperties.setProperty("remote.host", "255.255.255.255"); + remoteProperties.setProperty("remote.port", "8883"); + + Loader localLoader = new Loader(localProperties); + Loader remoteLoader = new Loader(remoteProperties); + + TM1638 localTM1638 = localLoader.getTM1638(); + TM1638 remoteTM1638 = remoteLoader.getTM1638(); + + Work server = localLoader.getServer(); + server.start(); + + remoteTM1638.addReceiver(new DummyEchoReceiver()); + remoteTM1638.start(); + remoteTM1638.construct(8, 9, 7); + int i = 123; + while (i < 10000) { + remoteTM1638.ping(i++); + remoteTM1638.setLed(i % 3 == 0 ? Color.GREEN : Color.RED, i % 7); + Thread.sleep(1000); + } + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/src/test/java/test/dummy/DummyEchoReceiver.java b/src/test/java/test/dummy/DummyEchoReceiver.java new file mode 100644 index 0000000..e884de6 --- /dev/null +++ b/src/test/java/test/dummy/DummyEchoReceiver.java @@ -0,0 +1,11 @@ +package test.dummy; + +import beerduino.Beerduino.Echo; + +import com.github.boukefalos.tm1638.EchoReceiver; + +public class DummyEchoReceiver implements EchoReceiver { + public void receive(Echo echo) { + System.out.println("> " + echo.getMessage() + " " + echo.getId()); + } +}