Start implementing complete protocol buffer protocol
This commit is contained in:
@@ -1,34 +1,44 @@
|
||||
package com.github.boukefalos.tm1638;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import tm1638.Tm1638.Buttons;
|
||||
import tm1638.Tm1638.Color;
|
||||
import tm1638.Tm1638.Command;
|
||||
import tm1638.Tm1638.Command.Type;
|
||||
import tm1638.Tm1638.Construct;
|
||||
import tm1638.Tm1638.Echo;
|
||||
import tm1638.Tm1638.Message;
|
||||
import tm1638.Tm1638.Ping;
|
||||
import tm1638.Tm1638.SetLed;
|
||||
import tm1638.Tm1638.Pong;
|
||||
import tm1638.Tm1638.SetLED;
|
||||
import tm1638.Tm1638.SetupDisplay;
|
||||
import tm1638.Tm1638.Text;
|
||||
import base.work.Listen;
|
||||
|
||||
import com.github.boukefalos.arduino.AbstractArduino;
|
||||
import com.google.protobuf.InvalidProtocolBufferException;
|
||||
import com.github.boukefalos.arduino.exception.ArduinoException;
|
||||
|
||||
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 class AbstractTM1638 extends AbstractArduino implements TM1638 {
|
||||
public void input(Message message) {
|
||||
System.out.println(message);
|
||||
switch (message.getType()) {
|
||||
case PONG:
|
||||
input(message.getPong());
|
||||
break;
|
||||
case TEXT:
|
||||
input(message.getText());
|
||||
break;
|
||||
case BUTTONS:
|
||||
input(message.getButtons());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void input(Pong pong) {}
|
||||
public void input(Text text) {}
|
||||
public void input(Buttons buttons) {}
|
||||
|
||||
public void command(Command command) {
|
||||
ByteArrayOutputStream output = new ByteArrayOutputStream(BUFFER_SIZE);
|
||||
try {
|
||||
@@ -40,6 +50,13 @@ public abstract class AbstractTM1638 extends AbstractArduino implements TM1638 {
|
||||
}
|
||||
}
|
||||
|
||||
public void ping(int id) {
|
||||
command(Command.newBuilder()
|
||||
.setType(Type.PING)
|
||||
.setPing(Ping.newBuilder()
|
||||
.setId(id)).build());
|
||||
}
|
||||
|
||||
public void construct(int dataPin, int clockPin, int strobePin) {
|
||||
command(Command.newBuilder()
|
||||
.setType(Type.CONSTRUCT)
|
||||
@@ -50,19 +67,60 @@ public abstract class AbstractTM1638 extends AbstractArduino implements TM1638 {
|
||||
.setStrobePin(strobePin).build()).build());
|
||||
}
|
||||
|
||||
public void ping(int id) {
|
||||
command(Command.newBuilder()
|
||||
.setType(Type.PING)
|
||||
.setPing(Ping.newBuilder()
|
||||
.setId(id)).build());
|
||||
public void clearDisplay() {
|
||||
}
|
||||
|
||||
public void setLed(Color color, int pos) {
|
||||
public void clearDisplayDigit(int pos, boolean dot) {
|
||||
}
|
||||
|
||||
public void setDisplay(byte[] values) {
|
||||
}
|
||||
|
||||
public void setDisplayDigit(int digit, int pos, boolean dot, byte[] font) {
|
||||
}
|
||||
|
||||
public void setDisplayToBinNumber(int number, int dots, byte[] font) {
|
||||
}
|
||||
|
||||
public void setDisplayToDecNumber(int number, int dots, boolean leadingZeros, byte[] font) {
|
||||
}
|
||||
|
||||
public void setDisplayToHexNumber(int number, int dots, boolean leadingZeros, byte[] font) {
|
||||
}
|
||||
|
||||
public void setDisplayToError() {
|
||||
}
|
||||
|
||||
public void setDisplayToString(String string, int dots, int pos, byte[] font) {
|
||||
}
|
||||
|
||||
public void setLED(Color color, int pos) {
|
||||
command(Command.newBuilder()
|
||||
.setType(Type.SET_LED)
|
||||
.setSetLed(
|
||||
SetLed.newBuilder()
|
||||
.setSetLED(
|
||||
SetLED.newBuilder()
|
||||
.setColor(color)
|
||||
.setPos(pos).build()).build());
|
||||
}
|
||||
|
||||
public void setLEDs(int led) {
|
||||
}
|
||||
|
||||
public void setupDisplay(boolean active, int intensity) {
|
||||
command(Command.newBuilder()
|
||||
.setType(Type.SETUP_DISPLAY)
|
||||
.setSetupDisplay(
|
||||
SetupDisplay.newBuilder()
|
||||
.setActive(active)
|
||||
.setIntensity(intensity).build()).build());
|
||||
}
|
||||
|
||||
public void register(Listen<Object> listen) {
|
||||
}
|
||||
|
||||
public void remove(Listen<Object> listen) {
|
||||
}
|
||||
|
||||
public void send(byte[] buffer) throws ArduinoException {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import java.util.Properties;
|
||||
|
||||
import org.picocontainer.PicoCompositionException;
|
||||
|
||||
import tm1638.Tm1638.Echo;
|
||||
import tm1638.Tm1638.Message;
|
||||
import base.exception.LoaderException;
|
||||
|
||||
import com.github.boukefalos.arduino.exception.ArduinoException;
|
||||
@@ -17,7 +17,7 @@ public class Loader extends com.github.boukefalos.arduino.Loader {
|
||||
|
||||
public Loader(Properties properties) throws LoaderException {
|
||||
super(Local.class, Remote.class, Server.class, properties);
|
||||
pico.addComponent(ParsingPort.getInstance(Echo.class));
|
||||
pico.addComponent(ParsingPort.getInstance(Message.class));
|
||||
}
|
||||
|
||||
public TM1638 getTM1638() throws ArduinoException {
|
||||
|
||||
@@ -6,7 +6,7 @@ import java.io.IOException;
|
||||
import tm1638.Tm1638.Color;
|
||||
import tm1638.Tm1638.Command;
|
||||
import tm1638.Tm1638.Ping;
|
||||
import tm1638.Tm1638.SetLed;
|
||||
import tm1638.Tm1638.SetLED;
|
||||
import base.Duplex;
|
||||
|
||||
import com.github.boukefalos.arduino.exception.ArduinoException;
|
||||
@@ -38,13 +38,13 @@ public class Server extends com.github.boukefalos.arduino.Server {
|
||||
tm1638.ping(ping.getId());
|
||||
break;
|
||||
case SET_LED:
|
||||
SetLed setLed = command.getSetLed();
|
||||
logger.debug("Color = " + setLed.getColor().name());
|
||||
switch (setLed.getColor()) {
|
||||
SetLED setLED = command.getSetLED();
|
||||
logger.debug("Color = " + setLED.getColor().name());
|
||||
switch (setLED.getColor()) {
|
||||
case RED:
|
||||
tm1638.setLed(Color.RED, 1);
|
||||
tm1638.setLED(Color.RED, 1);
|
||||
case GREEN:
|
||||
tm1638.setLed(Color.GREEN, 1);
|
||||
tm1638.setLED(Color.GREEN, 1);
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -4,8 +4,19 @@ import tm1638.Tm1638.Color;
|
||||
|
||||
import com.github.boukefalos.arduino.Arduino;
|
||||
|
||||
public interface TM1638 extends Arduino {
|
||||
public interface TM1638 extends Arduino {
|
||||
public void ping(int i);
|
||||
public void construct(int dataPin, int clockPin, int strobePin);
|
||||
public void ping(int i);
|
||||
public void setLed(Color color, int pos);
|
||||
public void clearDisplay();
|
||||
public void clearDisplayDigit(int pos, boolean dot);
|
||||
public void setDisplay(byte[] values);
|
||||
public void setDisplayDigit(int digit, int pos, boolean dot, byte[] font);
|
||||
public void setDisplayToBinNumber(int number, int dots, byte[] font);
|
||||
public void setDisplayToDecNumber(int number, int dots, boolean leadingZeros, byte[] font);
|
||||
public void setDisplayToHexNumber(int number, int dots, boolean leadingZeros, byte[] font);
|
||||
public void setDisplayToError();
|
||||
public void setDisplayToString(String string, int dots, int pos, byte[] font);
|
||||
public void setLED(Color color, int pos);
|
||||
public void setLEDs(int led);
|
||||
public void setupDisplay(boolean active, int intensity);
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import tm1638.Tm1638.Echo;
|
||||
import tm1638.Tm1638.Message;
|
||||
import base.work.Listen;
|
||||
|
||||
import com.github.boukefalos.arduino.exception.ArduinoException;
|
||||
@@ -18,7 +18,7 @@ public class Local extends AbstractTM1638 {
|
||||
protected ArrayList<Listen<Object>> listenList;
|
||||
|
||||
public Local() throws ArduinoException {
|
||||
this(ParsingPort.getInstance(Echo.class));
|
||||
this(ParsingPort.getInstance(Message.class));
|
||||
}
|
||||
|
||||
public Local(Port arduino) throws ArduinoException {
|
||||
@@ -43,6 +43,7 @@ public class Local extends AbstractTM1638 {
|
||||
public void send(byte[] buffer) throws ArduinoException {
|
||||
try {
|
||||
outputStream.write(buffer);
|
||||
outputStream.flush();
|
||||
} catch (IOException e) {
|
||||
throw new ArduinoException("Failed to write to arduino");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user