Migrate to protcol buffer communication (https://github.com/google/protobuf), use preliminary buffer format from https://github.com/Boukefalos/arduino-tm1638.

This commit is contained in:
2015-03-21 21:37:32 +00:00
parent 5dc25b8858
commit f45c7b0519
6 changed files with 117 additions and 29 deletions

View File

@@ -1,5 +1,6 @@
apply plugin: 'java' apply plugin: 'java'
apply plugin: 'eclipse' apply plugin: 'eclipse'
apply plugin: 'protobuf'
apply plugin: 'maven' apply plugin: 'maven'
group = 'com.github.boukefalos' group = 'com.github.boukefalos'
@@ -10,6 +11,16 @@ task wrapper(type: Wrapper) {
gradleVersion = '2.2' gradleVersion = '2.2'
} }
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.andrewkroh.gradle:gradle-protobuf-plugin:0.4.0'
}
}
repositories { repositories {
maven { maven {
url 'https://github.com/Boukefalos/jlibloader/raw/mvn-repo/' url 'https://github.com/Boukefalos/jlibloader/raw/mvn-repo/'

View File

@@ -40,15 +40,16 @@ public class Local implements iBuddy {
} }
@Override
public void setHeadRed(boolean headRed) throws iBuddyException { public void setHeadRed(boolean headRed) throws iBuddyException {
// TODO Auto-generated method stub try {
IBuddy.sendHeadRed(headRed);
} catch (IBuddyException e) {
throw new iBuddyException();
}
} }
@Override
public void test() throws IBuddyException { public void test() throws IBuddyException {
IBuddy.sendAllOff(); IBuddy.sendAllOff();
} }
} }

View File

@@ -1,5 +1,6 @@
package com.github.boukefalos.ibuddy.implementation; package com.github.boukefalos.ibuddy.implementation;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.net.DatagramPacket; import java.net.DatagramPacket;
import java.net.DatagramSocket; import java.net.DatagramSocket;
@@ -11,9 +12,19 @@ import org.jraf.jlibibuddy.IBuddyException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.github.boukefalos.ibuddy.iBuddy; import com.github.boukefalos.ibuddy.iBuddy;
import com.github.boukefalos.ibuddy.exception.iBuddyException; import com.github.boukefalos.ibuddy.exception.iBuddyException;
import ibuddy.Ibuddy.Color;
import ibuddy.Ibuddy.Command;
import ibuddy.Ibuddy.Command.Type;
import ibuddy.Ibuddy.SetLed;
public class Remote implements iBuddy { public class Remote implements iBuddy {
protected Logger logger = LoggerFactory.getLogger(getClass()); protected Logger logger = LoggerFactory.getLogger(getClass());
@@ -28,8 +39,21 @@ public class Remote implements iBuddy {
this.port = port; this.port = port;
} }
public void sendHeadRed(boolean headRed) { public void setHeadRed(boolean headRed) {
send("RED"); Command command = Command.newBuilder()
.setType(Type.SET_LED)
.setSetLed(
SetLed.newBuilder()
.setColor(Color.RED)
.setPos(0).build()).build();
ByteArrayOutputStream output = new ByteArrayOutputStream(1024);
try {
command.writeDelimitedTo(output);
send(output.toByteArray());
} catch (IOException e) {
logger.error("Failed to send command");
}
} }
public void setHeadGreen(boolean headGreen) { public void setHeadGreen(boolean headGreen) {
@@ -72,11 +96,6 @@ public class Remote implements iBuddy {
return true; return true;
} }
@Override
public void setHeadRed(boolean headRed) throws iBuddyException {
// TODO Auto-generated method stub
}
@Override @Override
public void test() throws IBuddyException { public void test() throws IBuddyException {

View File

@@ -1,5 +1,10 @@
package com.github.boukefalos.ibuddy.server; package com.github.boukefalos.ibuddy.server;
import ibuddy.Ibuddy.Command;
import ibuddy.Ibuddy.Command.Type;
import ibuddy.Ibuddy.SetLed;
import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.net.DatagramPacket; import java.net.DatagramPacket;
import java.net.DatagramSocket; import java.net.DatagramSocket;
@@ -42,24 +47,24 @@ public class Server extends Thread {
} catch (IOException e) { } catch (IOException e) {
logger.error("Failed to receive packet", e); logger.error("Failed to receive packet", e);
} }
// Rewrite with protocol buffers! ByteArrayInputStream input = new ByteArrayInputStream(buffer);
String received = new String(datagramPacket.getData(), datagramPacket.getOffset(), datagramPacket.getLength()); logger.debug("Received input");
try { try {
Color color = IBuddy.Color.valueOf(received); Command command = Command.parseDelimitedFrom(input);
switch (color) { logger.debug("Command type = " + command.getType().name());
switch (command.getType()) {
case SET_LED:
SetLed setLed = command.getSetLed();
logger.debug("Color = " + setLed.getColor().name());
switch (setLed.getColor()) {
case RED: case RED:
iBuddy.setHeadRed(true); iBuddy.setHeadRed(true);
break; }
case GREEN:
iBuddy.setHeadGreen(true);
break;
case BLUE:
iBuddy.setHeadBlue(true);
break; break;
} }
} catch (IllegalArgumentException e) { } catch (IOException e) {
logger.error("No such command", e); logger.error("Failed to parse input");
return;
} catch (iBuddyException e) { } catch (iBuddyException e) {
logger.error("Failed to send command to iBuddy", e); logger.error("Failed to send command to iBuddy", e);
} }

View File

@@ -30,7 +30,7 @@ public class TestCommunication {
Server server = localLoader.getServer(); Server server = localLoader.getServer();
server.start(); server.start();
remoteiBuddy.setHeadGreen(true); remoteiBuddy.setHeadRed(true);
Thread.sleep(10000); Thread.sleep(10000);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();

View File

@@ -0,0 +1,52 @@
package ibuddy;
enum Color {
GREEN = 1;
RED = 2;
BOTH = 3;
NONE = 4;
}
enum Module {
TM1638 = 1;
InvertedTM1638 = 2;
TM1640 = 3;
}
message Command {
enum Type {
PING = 1;
CONSTRUCT = 2;
SET_LED = 10;
}
required Type type = 1;
optional Ping ping = 2;
optional Construct construct = 3;
optional SetLed setLed = 10;
}
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 {
required Color color = 1;
required int32 pos = 2;
optional int32 id = 3 [default = 1];
}