Initial design of protocol to allow setup of display and control of LEDs

This commit is contained in:
2015-03-18 20:36:10 +00:00
parent 6cb5db6c10
commit ecc4ea198f
4 changed files with 111 additions and 19 deletions

View File

@@ -0,0 +1,52 @@
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);
}
}

View File

@@ -1,9 +1,7 @@
package test;
import java.io.OutputStream;
import tm1638.Tm1638.Color;
import beerduino.Beerduino.Echo;
import beerduino.Beerduino.Ping;
public class Test implements EchoListener {
public static void main(String[] argv) {
@@ -18,13 +16,13 @@ public class Test implements EchoListener {
Arduino arduino = new Arduino();
arduino.connect();
arduino.addListener(this);
OutputStream outputStream = arduino.getOutputStream();
int i = 123;
while ( i < 10000) {
Ping ping = Ping.newBuilder().setId(i++).build();
System.out.println("writing!");
ping.writeDelimitedTo(outputStream);
Thread.sleep(1000);
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();
}

View File

@@ -1,10 +0,0 @@
package beerduino;
message Ping {
required int32 id = 1;
}
message Echo {
required int32 id = 1;
optional string message = 2;
}

View File

@@ -0,0 +1,52 @@
package tm1638;
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];
}