From 48048379d727c054bbd60c6a4e5acd4b536d7120 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Tue, 24 Jan 2017 13:50:11 +0100 Subject: Rename common --- car/car.ino | 2 +- car/common.cpp | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 car/common.cpp diff --git a/car/car.ino b/car/car.ino index c9c6b8e..c36e0c3 100644 --- a/car/car.ino +++ b/car/car.ino @@ -3,7 +3,7 @@ #include #include -#include +//#include #include "config.h" #include "ir.h" diff --git a/car/common.cpp b/car/common.cpp new file mode 100644 index 0000000..86f195c --- /dev/null +++ b/car/common.cpp @@ -0,0 +1,33 @@ +#include +#include +#include "pb_encode.h" +#include "pb_decode.h" + +bool +write_callback(pb_ostream_t *stream, const uint8_t *buf, size_t count) +{ + return Serial.write(buf, count) == count; +} + +bool +read_callback(pb_istream_t *stream, uint8_t *buf, size_t count) +{ + int result = Serial.readBytes(buf, count); + if (result <= 0) + stream->bytes_left = 0; // EOF + return result == count; +} + +pb_ostream_t +pb_ostream_from_serial() +{ + pb_ostream_t stream = {&write_callback, NULL, SIZE_MAX, 0}; + return stream; +} + +pb_istream_t +pb_istream_from_serial() +{ + pb_istream_t stream = {&read_callback, NULL, SIZE_MAX}; + return stream; +} -- cgit v1.2.3