diff --git a/src/main/java/test/TM1638.java b/src/main/java/test/TM1638.java new file mode 100644 index 0000000..ba959f1 --- /dev/null +++ b/src/main/java/test/TM1638.java @@ -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); + } +} diff --git a/src/main/java/test/Test.java b/src/main/java/test/Test.java index e2d4569..1b2d399 100644 --- a/src/main/java/test/Test.java +++ b/src/main/java/test/Test.java @@ -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(); } diff --git a/src/main/proto/beerduino.proto b/src/main/proto/beerduino.proto deleted file mode 100644 index 98b33d8..0000000 --- a/src/main/proto/beerduino.proto +++ /dev/null @@ -1,10 +0,0 @@ -package beerduino; - -message Ping { - required int32 id = 1; -} - -message Echo { - required int32 id = 1; - optional string message = 2; -} \ No newline at end of file diff --git a/src/main/proto/tm1638.proto b/src/main/proto/tm1638.proto new file mode 100644 index 0000000..e70fbd6 --- /dev/null +++ b/src/main/proto/tm1638.proto @@ -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]; +} \ No newline at end of file