Start implementing complete protocol buffer protocol

This commit is contained in:
2015-07-16 15:57:08 +01:00
parent 0e8f953f0f
commit 676ac1748f
9 changed files with 242 additions and 68 deletions

View File

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

View File

@@ -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 {

View File

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

View File

@@ -5,7 +5,18 @@ import tm1638.Tm1638.Color;
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);
public void construct(int dataPin, int clockPin, int strobePin);
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);
}

View File

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

View File

@@ -15,45 +15,114 @@ enum Module {
message Command {
enum Type {
SERVER = 1;
PING = 2;
CONSTRUCT = 3;
SET_LED = 4;
PING = 1;
CONSTRUCT = 2;
CLEAR_DISPLAY = 3;
CLEAR_DISPLAY_DIGIT = 4;
SET_DISPLAY = 5;
SET_DISPLAY_DIGIT = 6;
SET_DISPLAY_TO_BIN_NUMBER = 7;
SET_DISPLAY_TO_DEC_NUMBER = 8;
SET_DISPLAY_TO_HEX_NUMBER = 9;
SET_DISPLAY_TO_ERROR = 10;
SET_DISPLAY_TO_STRING = 11;
SET_LED = 12;
SET_LEDS = 13;
SETUP_DISPLAY = 14;
}
required Type type = 1;
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;
optional Ping ping = 2;
optional Construct construct = 3;
optional ClearDisplayDigit clearDisplayDigit = 4;
optional SetDisplay setDisplay = 5;
optional SetDisplayDigit setDisplayDigit = 6;
optional SetDisplayToNumber setDisplayToNumber = 7;
optional SetDisplayToString setDisplayToString = 8;
optional SetLED setLED = 9;
optional SetLEDs setLEDs = 10;
optional SetupDisplay setupDisplay = 11;
}
message Ping {
required int32 id = 1;
}
message Echo {
required int32 id = 1;
optional string message = 2;
}
message Construct {
required int32 dataPin = 1;
required int32 clockPin = 2;
optional int32 strobePin = 3;
optional bool activateDisplay = 4 [default = true];
optional int32 intensity = 5 [default = 7];
optional Module module = 6 [default = TM1638];
optional int32 id = 7 [default = 0];
}
message SetLed {
message ClearDisplayDigit {
required int32 pos = 1;
required bool dot = 2;
}
message SetDisplay {
required bytes values = 1;
}
message SetDisplayDigit {
required int32 digit = 1;
required int32 pos = 2;
required bool dot = 3;
optional bytes font = 4;
}
message SetDisplayToNumber {
required int32 number = 1;
required bool dots = 2;
optional bytes font = 3;
optional bool leadingZeros = 4 [default = false];
optional bool sign = 5 [default = true];
}
message SetDisplayToString {
required string string = 1;
required int32 dots = 2;
required int32 pos = 3;
optional bytes font = 4;
}
message SetLED {
required Color color = 1;
required int32 pos = 2;
optional int32 id = 3 [default = 1];
}
message SetLEDs {
required int32 led = 1;
}
message SetupDisplay {
optional bool active = 1 [default = true];
optional int32 intensity = 2 [default = 7];
}
message Message {
enum Type {
PONG = 1;
BUTTONS = 2;
TEXT = 3;
}
required Type type = 1;
optional Pong pong = 2;
optional Buttons buttons = 3;
optional Text text = 4;
}
message Pong {
required int32 id = 1;
}
message Buttons {
required int32 buttons = 1;
}
message Text {
required string text = 1;
}

View File

@@ -0,0 +1,40 @@
package test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.lang.reflect.Method;
import tm1638.Tm1638.Buttons;
import tm1638.Tm1638.Message;
import tm1638.Tm1638.Ping;
import tm1638.Tm1638.Pong;
import tm1638.Tm1638.Text;
public class Test {
public static void main(String[] args) throws Exception {
Ping ping = Ping.newBuilder().setId(123).build();
ByteArrayOutputStream output = new ByteArrayOutputStream();
ping.writeDelimitedTo(output);
byte[] buffer = output.toByteArray();
ByteArrayInputStream input = new ByteArrayInputStream(buffer);
Class<?> messageClass = Message.class;
Method m = messageClass.getMethod("parseDelimitedFrom", InputStream.class);
Object object = m.invoke(null, input);
Message message = (Message) object;
switch (message.getType()) {
case PONG:
Pong pong = message.getPong();
System.out.println(pong.getId());
break;
case TEXT:
Text text = message.getText();
System.out.println(text.getText());
break;
case BUTTONS:
Buttons buttons = message.getButtons();
System.out.println(buttons.getButtons());
break;
}
}
}

View File

@@ -1,7 +1,6 @@
package test;
import tm1638.Tm1638.Color;
import tm1638.Tm1638.Echo;
import base.work.Listen;
import com.github.boukefalos.tm1638.TM1638;
@@ -17,11 +16,12 @@ public class TestLocal extends Listen<Object> {
TM1638.register(new TestLocal());
TM1638.start();
TM1638.construct(8, 9, 7);
int i = 123;
TM1638.setupDisplay(true, 1);
int i = 0;
while (i < 10000) {
//TM1638.setLED(i % 2 == 0 ? Color.GREEN : Color.RED, i % 8);
//Thread.sleep(500);
TM1638.ping(i++);
TM1638.setLed(i % 3 == 0 ? Color.GREEN : Color.RED, i % 7);
Thread.sleep(1000);
}
}
@@ -30,8 +30,4 @@ public class TestLocal extends Listen<Object> {
super();
start();
}
public void input(Echo echo) {
System.out.println("> " + echo.getMessage() + " " + echo.getId());
}
}

View File

@@ -2,7 +2,6 @@ package test;
import java.util.Properties;
import base.exception.LoaderException;
import base.exception.worker.ActivateException;
import com.github.boukefalos.arduino.exception.ArduinoException;