Snapshot of implementation of limited functionality

This commit is contained in:
2015-06-11 22:44:09 +01:00
parent ecc4ea198f
commit 0178f0ec8d
23 changed files with 703 additions and 185 deletions

View File

@@ -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<EchoReceiver> echoReceiverList = new ArrayList<EchoReceiver>();
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();
}
}

View File

@@ -0,0 +1,7 @@
package com.github.boukefalos.tm1638;
import beerduino.Beerduino.Echo;
public interface EchoReceiver {
public void receive(Echo echo);
}

View File

@@ -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);
}
}

View File

@@ -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);
}

View File

@@ -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);
}
}

View File

@@ -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<EchoReceiver> echoReceiverList, byte[] buffer) {
try {
Echo echo = Echo.parseFrom(buffer);
for (EchoReceiver echoReceiver : echoReceiverList) {
echoReceiver.receive(echo);
}
} catch (InvalidProtocolBufferException e) {}
}
}

View File

@@ -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());
}
}

View File

@@ -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;
}
}
}

View File

@@ -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);
}
}

View File

@@ -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<EchoReceiver> echoReceiverList = new ArrayList<EchoReceiver>();
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);
}
}

View File

@@ -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<EchoReceiver> echoReceiverList = new ArrayList<EchoReceiver>();
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);
}
}

View File

@@ -0,0 +1,5 @@
package com.github.boukefalos.tm1638.server;
public interface TM1638Server {
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}