Snapshot of implementation of limited functionality
This commit is contained in:
124
src/main/java/com/github/boukefalos/tm1638/Arduino.java
Normal file
124
src/main/java/com/github/boukefalos/tm1638/Arduino.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
package test;
|
package com.github.boukefalos.tm1638;
|
||||||
|
|
||||||
import beerduino.Beerduino.Echo;
|
import beerduino.Beerduino.Echo;
|
||||||
|
|
||||||
public interface EchoListener {
|
public interface EchoReceiver {
|
||||||
public void receive(Echo echo);
|
public void receive(Echo echo);
|
||||||
}
|
}
|
||||||
73
src/main/java/com/github/boukefalos/tm1638/Loader.java
Normal file
73
src/main/java/com/github/boukefalos/tm1638/Loader.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
25
src/main/java/com/github/boukefalos/tm1638/TM1638.java
Normal file
25
src/main/java/com/github/boukefalos/tm1638/TM1638.java
Normal 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);
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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());
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package com.github.boukefalos.tm1638.server;
|
||||||
|
|
||||||
|
public interface TM1638Server {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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<EchoListener> listenerList = new ArrayList<EchoListener>();
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -15,15 +15,22 @@ enum Module {
|
|||||||
|
|
||||||
message Command {
|
message Command {
|
||||||
enum Type {
|
enum Type {
|
||||||
PING = 1;
|
SERVER = 1;
|
||||||
CONSTRUCT = 2;
|
PING = 2;
|
||||||
SET_LED = 10;
|
CONSTRUCT = 3;
|
||||||
|
SET_LED = 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
required Type type = 1;
|
required Type type = 1;
|
||||||
optional Ping ping = 2;
|
optional Server server = 2;
|
||||||
optional Construct construct = 3;
|
optional Ping ping = 3;
|
||||||
optional SetLed setLed = 10;
|
optional Construct construct = 4;
|
||||||
|
optional SetLed setLed = 5;
|
||||||
|
}
|
||||||
|
|
||||||
|
message Server {
|
||||||
|
optional string host = 1;
|
||||||
|
required int32 port = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
message Ping {
|
message Ping {
|
||||||
|
|||||||
26
src/test/java/test/TestLocal.java
Normal file
26
src/test/java/test/TestLocal.java
Normal file
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
49
src/test/java/test/TestTcpCommunication.java
Normal file
49
src/test/java/test/TestTcpCommunication.java
Normal file
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
49
src/test/java/test/TestUdpCommunication.java
Normal file
49
src/test/java/test/TestUdpCommunication.java
Normal file
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
11
src/test/java/test/dummy/DummyEchoReceiver.java
Normal file
11
src/test/java/test/dummy/DummyEchoReceiver.java
Normal file
@@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user