Factor out all general jlibarduino code
This commit is contained in:
@@ -1,29 +1,32 @@
|
||||
package com.github.boukefalos.tm1638;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
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.Command.Type;
|
||||
import tm1638.Tm1638.Construct;
|
||||
import tm1638.Tm1638.Echo;
|
||||
import tm1638.Tm1638.Ping;
|
||||
import tm1638.Tm1638.SetLed;
|
||||
import base.Sender;
|
||||
|
||||
public abstract class AbstractTM1638 implements TM1638, Sender {
|
||||
public static final int BUFFER_SIZE = 1024;
|
||||
protected Logger logger = LoggerFactory.getLogger(getClass());
|
||||
import com.github.boukefalos.arduino.AbstractArduino;
|
||||
import com.google.protobuf.InvalidProtocolBufferException;
|
||||
|
||||
public void start() {}
|
||||
|
||||
public void stop() {}
|
||||
|
||||
public void exit() {
|
||||
stop();
|
||||
public abstract class AbstractTM1638 extends AbstractArduino implements TM1638 {
|
||||
public void input(byte[] buffer) {
|
||||
System.out.println(new String(buffer));
|
||||
try {
|
||||
ByteArrayInputStream input = new ByteArrayInputStream(buffer);
|
||||
Echo echo = Echo.parseDelimitedFrom(input);
|
||||
System.out.println(echo.getMessage());
|
||||
} catch (InvalidProtocolBufferException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void command(Command command) {
|
||||
|
||||
@@ -1,138 +0,0 @@
|
||||
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 org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import purejavacomm.CommPortIdentifier;
|
||||
import purejavacomm.PortInUseException;
|
||||
import purejavacomm.SerialPort;
|
||||
import purejavacomm.SerialPortEvent;
|
||||
import purejavacomm.SerialPortEventListener;
|
||||
import purejavacomm.UnsupportedCommOperationException;
|
||||
import tm1638.Tm1638.Echo;
|
||||
import base.work.Listen;
|
||||
|
||||
import com.github.boukefalos.tm1638.exception.ArduinoException;
|
||||
|
||||
public class Arduino implements SerialPortEventListener {
|
||||
public static final int BUFFER_SIZE = 1024;
|
||||
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 Logger logger = LoggerFactory.getLogger(Arduino.class);
|
||||
protected static Arduino arduino;
|
||||
|
||||
protected int bufferSize;
|
||||
protected SerialPort port = null;
|
||||
protected InputStream inputStream = null;
|
||||
protected ArrayList<Listen<Object>> listenList = new ArrayList<Listen<Object>>();
|
||||
|
||||
protected Arduino(int bufferSize) {
|
||||
this.bufferSize = bufferSize;
|
||||
}
|
||||
|
||||
public void register(Listen<Object> listen) {
|
||||
listenList.add(listen);
|
||||
}
|
||||
|
||||
public void remove(Listen<Object> listen) {
|
||||
listenList.remove(listen);
|
||||
}
|
||||
|
||||
public static Arduino getInstance() {
|
||||
return getInstance(BUFFER_SIZE);
|
||||
}
|
||||
|
||||
public static Arduino getInstance(int bufferSize) {
|
||||
if (arduino == null) {
|
||||
arduino = new Arduino(bufferSize);
|
||||
}
|
||||
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:
|
||||
// Where should this be parsed, or should byte[] be passed directly?
|
||||
Echo echo = Echo.parseDelimitedFrom(inputStream);
|
||||
System.err.println(echo.getMessage());
|
||||
for (Listen<Object> listen : listenList) {
|
||||
listen.add(echo);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
logger.error("", e);
|
||||
}
|
||||
}
|
||||
|
||||
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 close() {
|
||||
port.close();
|
||||
}
|
||||
}
|
||||
@@ -2,69 +2,29 @@ package com.github.boukefalos.tm1638;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.picocontainer.Parameter;
|
||||
import org.picocontainer.PicoCompositionException;
|
||||
import org.picocontainer.parameters.ComponentParameter;
|
||||
import org.picocontainer.parameters.ConstantParameter;
|
||||
|
||||
import tm1638.Tm1638.Echo;
|
||||
import base.exception.LoaderException;
|
||||
import base.loader.AbstractLoader;
|
||||
|
||||
import com.github.boukefalos.tm1638.exception.ArduinoException;
|
||||
import com.github.boukefalos.arduino.exception.ArduinoException;
|
||||
import com.github.boukefalos.arduino.port.ParsingPort;
|
||||
import com.github.boukefalos.tm1638.implementation.Local;
|
||||
import com.github.boukefalos.tm1638.implementation.Remote;
|
||||
|
||||
public class Loader extends AbstractLoader<Loader> {
|
||||
protected static final String PROPERTIES_FILE = "TM1638.properties";
|
||||
public class Loader extends com.github.boukefalos.arduino.Loader {
|
||||
protected static final String PROPERTIES_FILE = "TM1638.properties";
|
||||
|
||||
public Loader(Properties properties) throws LoaderException {
|
||||
super();
|
||||
|
||||
/* Add implementation */
|
||||
switch (properties.getProperty("implementation")) {
|
||||
case "local":
|
||||
pico.addComponent(TM1638.class, Local.class);
|
||||
break;
|
||||
case "remote":
|
||||
pico.addComponent(TM1638.class, Remote.class);
|
||||
|
||||
/* Add remote duplex implementation */
|
||||
try {
|
||||
String protocol = properties.getOrDefault("server.protocol", "tcp").toString();
|
||||
String implementation = properties.getOrDefault("tcp.implementation", "socket").toString();
|
||||
String host = properties.getProperty("remote.host");
|
||||
int port = Integer.valueOf(properties.getProperty("remote.port"));
|
||||
addClientDuplex(protocol, implementation, host, port);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new LoaderException("Failed to parse remote.port");
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
/* Add server */
|
||||
if (properties.getProperty("server") != null) {
|
||||
boolean direct = Boolean.parseBoolean(properties.getOrDefault("server.direct", Server.DIRECT).toString());
|
||||
pico.addComponent(Server.class, Server.class, new Parameter[] {
|
||||
new ComponentParameter(),
|
||||
new ComponentParameter(),
|
||||
new ConstantParameter(direct)});
|
||||
|
||||
/* Add server forwarder implementation */
|
||||
try {
|
||||
String protocol = properties.getOrDefault("server.protocol", "tcp").toString();
|
||||
String implementation = properties.getOrDefault("tcp.implementation", "socket").toString();
|
||||
int port = Integer.valueOf(properties.getProperty("server.port"));
|
||||
addServerDuplex(protocol, implementation, port);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new LoaderException("Failed to parse server.port");
|
||||
}
|
||||
}
|
||||
public Loader(Properties properties) throws LoaderException {
|
||||
super(Local.class, Remote.class, Server.class, properties);
|
||||
pico.addComponent(ParsingPort.getInstance(Echo.class));
|
||||
}
|
||||
|
||||
public TM1638 getTM1638() throws ArduinoException {
|
||||
try {
|
||||
return pico.getComponent(TM1638.class);
|
||||
} catch (PicoCompositionException e) {
|
||||
logger.error("", e);
|
||||
throw new ArduinoException("Failed to load");
|
||||
}
|
||||
}
|
||||
@@ -73,6 +33,7 @@ public class Loader extends AbstractLoader<Loader> {
|
||||
try {
|
||||
return pico.getComponent(Server.class);
|
||||
} catch (PicoCompositionException e) {
|
||||
logger.error("", e);
|
||||
throw new ArduinoException("Failed to load");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,42 +7,16 @@ import tm1638.Tm1638.Color;
|
||||
import tm1638.Tm1638.Command;
|
||||
import tm1638.Tm1638.Ping;
|
||||
import tm1638.Tm1638.SetLed;
|
||||
import base.Control;
|
||||
import base.Duplex;
|
||||
import base.Receiver;
|
||||
import base.exception.worker.ActivateException;
|
||||
import base.exception.worker.DeactivateException;
|
||||
import base.work.Listen;
|
||||
|
||||
import com.github.boukefalos.tm1638.exception.ArduinoException;
|
||||
|
||||
public class Server extends Listen<Object> implements Control, Receiver {
|
||||
protected static final boolean DIRECT = false;
|
||||
import com.github.boukefalos.arduino.exception.ArduinoException;
|
||||
|
||||
public class Server extends com.github.boukefalos.arduino.Server {
|
||||
protected TM1638 tm1638;
|
||||
protected Duplex duplex;
|
||||
protected boolean direct;
|
||||
|
||||
public Server(TM1638 tm1638, Duplex duplex) {
|
||||
this(tm1638, duplex, DIRECT);
|
||||
}
|
||||
|
||||
public Server(TM1638 tm1638, Duplex duplex, boolean direct) {
|
||||
super(tm1638, duplex, direct);
|
||||
this.tm1638 = tm1638;
|
||||
this.duplex = duplex;
|
||||
this.direct = direct;
|
||||
tm1638.register(this); // Arduino > [input()]
|
||||
duplex.register(this); // Client > [receive()]
|
||||
}
|
||||
|
||||
public void activate() throws ActivateException {
|
||||
duplex.start();
|
||||
super.activate();
|
||||
}
|
||||
|
||||
public void deactivate() throws DeactivateException {
|
||||
duplex.stop();
|
||||
super.deactivate();
|
||||
}
|
||||
|
||||
public void receive(byte[] buffer) {
|
||||
@@ -84,13 +58,4 @@ public class Server extends Listen<Object> implements Control, Receiver {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void input(byte[] buffer) {
|
||||
// Arduino > [Server] > Client
|
||||
try {
|
||||
duplex.send(buffer);
|
||||
} catch (IOException e) {
|
||||
logger.error("", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,10 @@
|
||||
package com.github.boukefalos.tm1638;
|
||||
|
||||
import com.github.boukefalos.tm1638.exception.ArduinoException;
|
||||
|
||||
import tm1638.Tm1638.Color;
|
||||
import base.Control;
|
||||
import base.work.Listen;
|
||||
|
||||
public interface TM1638 extends Control {
|
||||
public void register(Listen<Object> listen);
|
||||
public void remove(Listen<Object> listen);
|
||||
|
||||
public void send(byte[] buffer) throws ArduinoException;
|
||||
import com.github.boukefalos.arduino.Arduino;
|
||||
|
||||
public interface TM1638 extends Arduino {
|
||||
public void construct(int dataPin, int clockPin, int strobePin);
|
||||
public void ping(int i);
|
||||
public void setLed(Color color, int pos);
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
package com.github.boukefalos.tm1638.exception;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class ArduinoException extends IOException {
|
||||
protected static final long serialVersionUID = 1L;
|
||||
|
||||
public ArduinoException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -2,32 +2,38 @@ package com.github.boukefalos.tm1638.implementation;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import tm1638.Tm1638.Echo;
|
||||
import base.work.Listen;
|
||||
|
||||
import com.github.boukefalos.arduino.exception.ArduinoException;
|
||||
import com.github.boukefalos.arduino.port.ParsingPort;
|
||||
import com.github.boukefalos.arduino.port.Port;
|
||||
import com.github.boukefalos.tm1638.AbstractTM1638;
|
||||
import com.github.boukefalos.tm1638.Arduino;
|
||||
import com.github.boukefalos.tm1638.exception.ArduinoException;
|
||||
|
||||
public class Local extends AbstractTM1638 {
|
||||
protected Arduino arduino;
|
||||
protected Port arduino;
|
||||
protected OutputStream outputStream;
|
||||
protected ArrayList<Listen<Object>> listenList;
|
||||
|
||||
public Local() throws Exception {
|
||||
this(Arduino.getInstance());
|
||||
public Local() throws ArduinoException {
|
||||
this(ParsingPort.getInstance(Echo.class));
|
||||
}
|
||||
|
||||
public Local(Arduino arduino) throws ArduinoException {
|
||||
public Local(Port arduino) throws ArduinoException {
|
||||
this.arduino = arduino;
|
||||
outputStream = arduino.getOutputStream();
|
||||
listenList = new ArrayList<Listen<Object>>();
|
||||
arduino.register(this);
|
||||
}
|
||||
|
||||
public void register(Listen<Object> listen) {
|
||||
arduino.register(listen);
|
||||
listenList.add(listen);
|
||||
}
|
||||
|
||||
public void remove(Listen<Object> listen) {
|
||||
arduino.remove(listen);
|
||||
listenList.remove(listen);
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
|
||||
@@ -7,8 +7,8 @@ import base.Duplex;
|
||||
import base.Receiver;
|
||||
import base.work.Listen;
|
||||
|
||||
import com.github.boukefalos.arduino.exception.ArduinoException;
|
||||
import com.github.boukefalos.tm1638.AbstractTM1638;
|
||||
import com.github.boukefalos.tm1638.exception.ArduinoException;
|
||||
|
||||
public class Remote extends AbstractTM1638 implements Receiver {
|
||||
protected Duplex duplex;
|
||||
@@ -43,7 +43,7 @@ public class Remote extends AbstractTM1638 implements Receiver {
|
||||
|
||||
public void receive(byte[] buffer) {
|
||||
// Arduino > Server > [Client]
|
||||
// Should decode here?
|
||||
// Should give option to decode here?
|
||||
for (Listen<Object> listen : listenList) {
|
||||
listen.add(buffer);
|
||||
}
|
||||
|
||||
@@ -14,8 +14,9 @@ public class TestLocal extends Listen<Object> {
|
||||
}
|
||||
|
||||
public static void main(TM1638 TM1638) throws InterruptedException {
|
||||
TM1638.register(new TestLocal());
|
||||
TM1638.construct(8, 9, 7);
|
||||
TM1638.register(new TestLocal());
|
||||
TM1638.start();
|
||||
TM1638.construct(8, 9, 7);
|
||||
|
||||
int i = 123;
|
||||
while (i < 10000) {
|
||||
|
||||
@@ -2,34 +2,39 @@ package test;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import base.exception.LoaderException;
|
||||
import base.exception.worker.ActivateException;
|
||||
|
||||
import com.github.boukefalos.arduino.exception.ArduinoException;
|
||||
import com.github.boukefalos.tm1638.Loader;
|
||||
import com.github.boukefalos.tm1638.Server;
|
||||
import com.github.boukefalos.tm1638.TM1638;
|
||||
import com.github.boukefalos.tm1638.exception.ArduinoException;
|
||||
|
||||
public class TestRemoteImplementation extends TestLocal {
|
||||
protected TM1638 tm1638;
|
||||
|
||||
public TestRemoteImplementation(Loader loader) throws ArduinoException {
|
||||
tm1638 = loader.getTM1638();
|
||||
tm1638.register(this);
|
||||
//tm1638.register(this);
|
||||
}
|
||||
|
||||
public void activate() throws ActivateException {
|
||||
tm1638.start();
|
||||
super.activate();
|
||||
}
|
||||
|
||||
|
||||
public static void main(Properties localProperties, Properties remoteProperties) throws Exception {
|
||||
Loader localLoader = new Loader(localProperties);
|
||||
Loader remoteLoader = new Loader(remoteProperties);
|
||||
|
||||
Server server = localLoader.getServer();
|
||||
server.start();
|
||||
|
||||
TM1638 TM1638 = remoteLoader.getTM1638();
|
||||
main(TM1638);
|
||||
try {
|
||||
Server server = localLoader.getServer();
|
||||
TM1638 TM1638 = remoteLoader.getTM1638();
|
||||
server.start();
|
||||
main(TM1638);
|
||||
} catch (ArduinoException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package test;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import com.github.boukefalos.tm1638.exception.ArduinoException;
|
||||
import com.github.boukefalos.arduino.exception.ArduinoException;
|
||||
|
||||
public class TestTcpImplementation {
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
@@ -6,6 +6,7 @@ public class TestUdpImplementation {
|
||||
public static void main(String[] args) throws Exception {
|
||||
Properties localProperties = new Properties();
|
||||
localProperties.setProperty("implementation", "local");
|
||||
localProperties.setProperty("protocol", "udp");
|
||||
localProperties.setProperty("server", "true");
|
||||
localProperties.setProperty("server.direct", "false");
|
||||
localProperties.setProperty("server.port", "8883");
|
||||
|
||||
Reference in New Issue
Block a user