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();
}