Add rudimentary support for buttons states

This commit is contained in:
2015-06-11 22:51:52 +01:00
parent 579e07adc6
commit 3ba8b769a7
2 changed files with 43 additions and 16 deletions

View File

@@ -17,15 +17,22 @@ enum Module {
message Command {
enum Type {
PING = 1;
CONSTRUCT = 2;
SET_LED = 10;
SERVER = 1;
PING = 2;
CONSTRUCT = 3;
SET_LED = 4;
}
required Type type = 1;
optional Ping ping = 2;
optional Construct construct = 3;
optional SetLed setLed = 10;
optional Server server = 2;
optional Ping ping = 3;
optional Construct construct = 4;
optional SetLed setLed = 5;
}
message Server {
optional string host = 1;
required int32 port = 2;
}
message Ping {
@@ -34,7 +41,7 @@ message Ping {
message Echo {
required int32 id = 1;
optional string message = 2;// [(nanopb).max_size = 40];
optional string message = 2 [(nanopb).max_size = 40];
}
message Construct {
@@ -51,4 +58,8 @@ message SetLed {
required Color color = 1;
required int32 pos = 2;
optional int32 id = 3 [default = 1];
}
message Buttons {
required byte buttons;
}

View File

@@ -8,7 +8,6 @@
extern "C" {
#include "pb_decode.h"
#include "pb_encode.h"
#include "beerduino.pb.h"
#include "tm1638.pb.h"
}
@@ -17,15 +16,25 @@ TM1638 *tm1638[6];
TM16XX *module;
int t = 123;
int id;
int id = 0;
byte pressed = 0;
_tm1638_Construct construct;
void setup() {
Serial.begin(9600);
Serial.begin(9600);
tm1638[0] = new TM1638(8, 9, 7);
tm1638[0]->setupDisplay(true, 1);
tm1638[0]->setLED(TM1638_COLOR_RED, 2);
tm1638[0]->setDisplayToDecNumber(111, 0);
}
void loop() {
void sendButtons(int id, byte buttons) {
/*_tm1638_Buttons x = {id, buttons};
uint8_t out_buffer[256];
pb_ostream_t ostream = pb_ostream_from_buffer(out_buffer, sizeof(out_buffer));
if (pb_encode_delimited(&ostream, tm1638_Buttons_fields, &x)) {
Serial.write(out_buffer, ostream.bytes_written);
}*/
}
void sendEcho(int id, char message[]) {
@@ -38,8 +47,17 @@ void sendEcho(int id, char message[]) {
}
}
void loop() {
byte buttons = tm1638[0]->getButtons();
if (buttons != pressed) {
//sendButtons(0, buttons);
sendEcho(buttons, "Buttons!");
pressed = buttons;
}
}
void serialEvent() {
if(Serial.available()) {
if (Serial.available()) {
int packet_size = Serial.read();
uint8_t in_buffer[256];
int bytes_read = Serial.readBytes((char*) in_buffer, packet_size);
@@ -56,18 +74,16 @@ void serialEvent() {
case tm1638_Command_Type_PING:
if (command.has_ping) {
_tm1638_Ping ping = command.ping;
sendEcho(ping.id, "Pong");
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: