commit a7863182d1caedf38a9428936e145e3cc3e7c3d3 Author: Rik Veenboer Date: Fri Mar 13 11:57:41 2015 +0000 Add protobuf (https://github.com/google/protobuf) and nanopb (https://github.com/metormote/nanopb) to build example beerduino project (https://github.com/ilyevsky/beerduino) using platformio (http://docs.platformio.org/en/latest/). diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0f84a2c --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/.pioenvs +/.sconsign.dblite diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..a188876 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,6 @@ +[submodule "protobuf"] + path = protobuf + url = https://github.com/google/protobuf.git +[submodule "nanopb"] + path = nanopb + url = https://code.google.com/p/nanopb/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9aeb4ad --- /dev/null +++ b/Makefile @@ -0,0 +1,24 @@ +PROTO_DIR = proto +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 + +nanopb: nanopb_nanopb nanopb_plugin + cp nanopb/pb.h src + cp nanopb/pb_common.c src + cp nanopb/pb_common.h src + cp nanopb/pb_decode.c src + cp nanopb/pb_decode.h src + cp nanopb/pb_encode.h src + cp nanopb/pb_encode.c src + +nanopb_%: $(NANOPB_DIR)/proto/%.proto + protoc --python_out=$(NANOPB_DIR)/proto $(PROTO_INCLUDES) $< + +%.pb: $(PROTO_DIR)/%.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 diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..123a8fc --- /dev/null +++ b/platformio.ini @@ -0,0 +1,25 @@ +# +# Project Configuration File +# +# A detailed documentation with the EXAMPLES is located here: +# http://docs.platformio.org/en/latest/projectconf.html +# + +# A sign `#` at the beginning of the line indicates a comment +# Comment lines are ignored. + +# Simple and base environment +# [env:mybaseenv] +# platform = %INSTALLED_PLATFORM_NAME_HERE% +# framework = +# board = +# +# Automatic targets - enable auto-uploading +# targets = upload + +[env:autogen_uno] +platform = atmelavr +framework = arduino +board = uno +; targets = upload +upload_port = COM3 \ No newline at end of file diff --git a/proto/beerduino.proto b/proto/beerduino.proto new file mode 100644 index 0000000..0e260e2 --- /dev/null +++ b/proto/beerduino.proto @@ -0,0 +1,14 @@ + +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/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..94e589c --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,47 @@ +#include +#include "Arduino.h" + +extern "C" { + #include "pb_decode.h" + #include "pb_encode.h" + #include "beerduino.pb.h" +} + +void setup() { + Serial.begin(9600); + Serial.println("Hello."); +} + +void loop() { +} + +void serialEvent() { + 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) { + // Well... fuck + return; + } + + 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); + } + } +} +