This commit is contained in:
2015-03-13 11:57:41 +00:00
commit a7863182d1
6 changed files with 118 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
/.pioenvs
/.sconsign.dblite

6
.gitmodules vendored Normal file
View File

@@ -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/

24
Makefile Normal file
View File

@@ -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

25
platformio.ini Normal file
View File

@@ -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

14
proto/beerduino.proto Normal file
View File

@@ -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];
}

47
src/main.cpp Normal file
View File

@@ -0,0 +1,47 @@
#include <stdint.h>
#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);
}
}
}