diff --git a/.gitignore b/.gitignore index 34f14b9..531d4cf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,8 @@ /.pioenvs -/.sconsign.dblite \ No newline at end of file +/.sconsign.dblite +/src/*.pb.h +/src/*.pb.c +/src/pb_*.h +/src/pb_*.c +/src/pb.h +/*.pb diff --git a/Makefile b/Makefile index 9aeb4ad..d1295fe 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ GOOGLE_PROTOBUF_INCLUDES = protobuf/src NANOPB_DIR = nanopb/generator PROTO_INCLUDES = -I$(PROTO_DIR) -I$(GOOGLE_PROTOBUF_INCLUDES) -I$(NANOPB_DIR)/proto -all: nanopb beerduino.pb +all: nanopb tm1638.pb nanopb: nanopb_nanopb nanopb_plugin cp nanopb/pb.h src @@ -21,4 +21,7 @@ nanopb_%: $(NANOPB_DIR)/proto/%.proto protoc -o$@ $< $(PROTO_INCLUDES) python $(NANOPB_DIR)/nanopb_generator.py $@ -L '#include "%s"' mv -f $@.h src - mv -f $@.c src \ No newline at end of file + mv -f $@.c src + +run: + platformio run \ No newline at end of file diff --git a/proto/beerduino.proto b/proto/beerduino.proto deleted file mode 100644 index 0e260e2..0000000 --- a/proto/beerduino.proto +++ /dev/null @@ -1,14 +0,0 @@ - -import "nanopb.proto"; - -package beerduino; - -message Ping { - required int32 id = 1; -} - -message Echo { - required int32 id = 1; - optional string message = 2 [(nanopb).max_size = 40]; -} - diff --git a/proto/tm1638.proto b/proto/tm1638.proto new file mode 100644 index 0000000..dbced6a --- /dev/null +++ b/proto/tm1638.proto @@ -0,0 +1,54 @@ +import "nanopb.proto"; + +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;// [(nanopb).max_size = 40]; +} + +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 diff --git a/src/main.cpp b/src/main.cpp index 94e589c..4c3510e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,47 +1,104 @@ #include #include "Arduino.h" +#include +#include +#include + extern "C" { #include "pb_decode.h" #include "pb_encode.h" #include "beerduino.pb.h" + #include "tm1638.pb.h" } +TM1640 *tm1640; +TM1638 *tm1638[6]; +TM16XX *module; + +int t = 123; +int id; +_tm1638_Construct construct; + void setup() { - Serial.begin(9600); - Serial.println("Hello."); + Serial.begin(9600); } void loop() { + +} + +void sendEcho(int id, char message[]) { + _tm1638_Echo echo = {id, true}; + strncpy(echo.message, message, 40); + uint8_t out_buffer[256]; + pb_ostream_t ostream = pb_ostream_from_buffer(out_buffer, sizeof(out_buffer)); + if (pb_encode_delimited(&ostream, tm1638_Echo_fields, &echo)) { + Serial.write(out_buffer, ostream.bytes_written); + } } void serialEvent() { - if(Serial.available()) { - int packet_size = Serial.read(); + if(Serial.available()) { + int packet_size = Serial.read(); + uint8_t in_buffer[256]; + int bytes_read = Serial.readBytes((char*) in_buffer, packet_size); + if (bytes_read != packet_size) { + return; + } + pb_istream_t istream = pb_istream_from_buffer(in_buffer, bytes_read); + _tm1638_Command command; + if (!pb_decode(&istream, tm1638_Command_fields, &command)) { + return; + } - uint8_t in_buffer[256]; - int bytes_read = Serial.readBytes((char*)in_buffer, packet_size); - if (bytes_read != packet_size) { - // Well... fuck - return; + switch (command.type) { + case tm1638_Command_Type_PING: + if (command.has_ping) { + _tm1638_Ping ping = command.ping; + sendEcho(ping.id, "Pong"); + tm1638[0]->setDisplayToDecNumber(ping.id, 0); + } + break; + case tm1638_Command_Type_CONSTRUCT: + construct = command.construct; + id = construct.id; + sendEcho(0, "ID:"); + switch (construct.module) { + case tm1638_Module_TM1638: + tm1638[0] = new TM1638((byte) construct.dataPin, (byte) construct.clockPin, (byte) construct.strobePin, construct.activateDisplay, (byte) construct.intensity); + // tm1638[0] = new TM1638(8, 9, 7); + tm1638[0]->setupDisplay(true, 1); + break; + case tm1638_Module_InvertedTM1638: + tm1638[0] = new InvertedTM1638((byte) construct.dataPin, (byte) construct.clockPin, (byte) construct.strobePin, construct.activateDisplay, (byte) construct.intensity); + break; + case tm1638_Module_TM1640: + tm1640 = new TM1640((byte) construct.dataPin, (byte) construct.clockPin, construct.activateDisplay, (byte) construct.intensity); + break; + } + break; + case tm1638_Command_Type_SET_LED: + if (command.has_setLed) { + _tm1638_SetLed setLed = command.setLed; + byte color; + switch (setLed.color) { + case tm1638_Color_GREEN: + color = TM1638_COLOR_GREEN; + break; + case tm1638_Color_RED: + color = TM1638_COLOR_RED; + break; + case tm1638_Color_BOTH: + color = TM1638_COLOR_GREEN + TM1638_COLOR_RED; + break; + case tm1638_Color_NONE: + color = TM1638_COLOR_NONE; + break; + } + tm1638[0]->setLED(color, setLed.pos); + } + break; + } } - - pb_istream_t istream = pb_istream_from_buffer(in_buffer, bytes_read); - beerduino_Ping ping_msg; - if(!pb_decode(&istream, beerduino_Ping_fields, &ping_msg)) { - // Well... fuck - return; - } - - beerduino_Echo echo_msg = {ping_msg.id, true, "Hello"}; - - uint8_t out_buffer[256]; - pb_ostream_t ostream = pb_ostream_from_buffer(out_buffer, - sizeof(out_buffer)); - if (pb_encode(&ostream, beerduino_Echo_fields, &echo_msg)) { - Serial.write(ostream.bytes_written); - Serial.write(out_buffer, ostream.bytes_written); - } - } -} - +} \ No newline at end of file