Migrate to initial protocol for communication that allows setup of display and control of LEDs
This commit is contained in:
6
.gitignore
vendored
6
.gitignore
vendored
@@ -1,2 +1,8 @@
|
|||||||
/.pioenvs
|
/.pioenvs
|
||||||
/.sconsign.dblite
|
/.sconsign.dblite
|
||||||
|
/src/*.pb.h
|
||||||
|
/src/*.pb.c
|
||||||
|
/src/pb_*.h
|
||||||
|
/src/pb_*.c
|
||||||
|
/src/pb.h
|
||||||
|
/*.pb
|
||||||
|
|||||||
5
Makefile
5
Makefile
@@ -3,7 +3,7 @@ GOOGLE_PROTOBUF_INCLUDES = protobuf/src
|
|||||||
NANOPB_DIR = nanopb/generator
|
NANOPB_DIR = nanopb/generator
|
||||||
PROTO_INCLUDES = -I$(PROTO_DIR) -I$(GOOGLE_PROTOBUF_INCLUDES) -I$(NANOPB_DIR)/proto
|
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
|
nanopb: nanopb_nanopb nanopb_plugin
|
||||||
cp nanopb/pb.h src
|
cp nanopb/pb.h src
|
||||||
@@ -22,3 +22,6 @@ nanopb_%: $(NANOPB_DIR)/proto/%.proto
|
|||||||
python $(NANOPB_DIR)/nanopb_generator.py $@ -L '#include "%s"'
|
python $(NANOPB_DIR)/nanopb_generator.py $@ -L '#include "%s"'
|
||||||
mv -f $@.h src
|
mv -f $@.h src
|
||||||
mv -f $@.c src
|
mv -f $@.c src
|
||||||
|
|
||||||
|
run:
|
||||||
|
platformio run
|
||||||
@@ -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];
|
|
||||||
}
|
|
||||||
|
|
||||||
54
proto/tm1638.proto
Normal file
54
proto/tm1638.proto
Normal file
@@ -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];
|
||||||
|
}
|
||||||
111
src/main.cpp
111
src/main.cpp
@@ -1,47 +1,104 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include "Arduino.h"
|
#include "Arduino.h"
|
||||||
|
|
||||||
|
#include <TM1638.h>
|
||||||
|
#include <InvertedTM1638.h>
|
||||||
|
#include <TM1640.h>
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#include "pb_decode.h"
|
#include "pb_decode.h"
|
||||||
#include "pb_encode.h"
|
#include "pb_encode.h"
|
||||||
#include "beerduino.pb.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() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
Serial.println("Hello.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
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() {
|
void serialEvent() {
|
||||||
if(Serial.available()) {
|
if(Serial.available()) {
|
||||||
int packet_size = Serial.read();
|
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];
|
switch (command.type) {
|
||||||
int bytes_read = Serial.readBytes((char*)in_buffer, packet_size);
|
case tm1638_Command_Type_PING:
|
||||||
if (bytes_read != packet_size) {
|
if (command.has_ping) {
|
||||||
// Well... fuck
|
_tm1638_Ping ping = command.ping;
|
||||||
return;
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user