├── .gitignore ├── README.md ├── library.json └── src ├── pb_arduino.cpp └── pb_arduino.h /.gitignore: -------------------------------------------------------------------------------- 1 | .pioenvs 2 | .piolibdeps 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | nanopb-arduino 2 | ============== 3 | 4 | A simple [PlatformIO](https://platformio.org/) library for wrapping Arduino's [`Stream`](https://www.arduino.cc/en/Reference/Stream) and `Print` objects into [Nanopb](https://koti.kapsi.fi/jpa/nanopb/)'s `pb_istream_s` and `pb_ostream_s`. 5 | 6 | Exposes two functions that do exactly what you'd expect: 7 | 8 | * `pb_istream_s as_pb_istream(Stream& p)` 9 | * `pb_ostream_s as_pb_ostream(Print& p)` 10 | 11 | In most cases, all you'll need is: 12 | 13 | ```c++ 14 | #include 15 | 16 | pb_istream_s pb_in = as_pb_istream(Serial); 17 | pb_ostream_s pb_out = as_pb_ostream(Serial); 18 | ``` 19 | 20 | and then use `pb_encode(&pb_out, ...)` and `pb_decode(&pb_in)`. 21 | 22 | This isn't restricted to [`Serial`](https://www.arduino.cc/en/reference/serial), and will work for any object that implements 23 | `Stream`, such as `File` (returned from [`SD.open`](https://www.arduino.cc/en/Reference/SDopen) and the ethernet [`Client`](https://www.arduino.cc/en/Reference/ClientConstructor). 24 | 25 | --- 26 | 27 | This can be used on top of [PacketIO](https://github.com/eric-wieser/packet-io), with 28 | 29 | ```c++ 30 | #include 31 | #include 32 | #include 33 | 34 | COBSStream cobs_in(Serial); 35 | pb_istream_s pb_in = as_pb_istream(cobs_in); 36 | 37 | COBSPrint cobs_out(Serial); 38 | pb_ostream_s pb_out = as_pb_ostream(cobs_out); 39 | ``` 40 | 41 | and then calling `cobs_out.end()` after each `pb_encode`, and `cobs_in.next()` after each `pb_decode`. 42 | -------------------------------------------------------------------------------- /library.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nanopb-arduino", 3 | "description": "Simple layer to convert arduino Streams into nanopb's pb_streams", 4 | "keywords": "Print, Stream, nanopb, protobuf", 5 | "authors": 6 | { 7 | "name": "Eric Wieser" 8 | }, 9 | "repository": 10 | { 11 | "type": "git", 12 | "url": "https://github.com/eric-wieser/nanopb-arduino.git" 13 | }, 14 | "version": "1.1.0", 15 | "license": "MIT", 16 | "homepage": "https://github.com/eric-wieser/nanopb-arduino", 17 | "frameworks": "arduino", 18 | "platforms": "*", 19 | "dependencies": [ 20 | {"name": "Nanopb"} 21 | ] 22 | } 23 | -------------------------------------------------------------------------------- /src/pb_arduino.cpp: -------------------------------------------------------------------------------- 1 | #include "pb_arduino.h" 2 | #include 3 | #include 4 | 5 | static bool pb_print_write(pb_ostream_t *stream, const pb_byte_t *buf, size_t count) { 6 | Print* p = reinterpret_cast(stream->state); 7 | size_t written = p->write(buf, count); 8 | return written == count; 9 | }; 10 | 11 | pb_ostream_s as_pb_ostream(Print& p) { 12 | return {pb_print_write, &p, SIZE_MAX, 0}; 13 | }; 14 | 15 | static bool pb_stream_read(pb_istream_t *stream, pb_byte_t *buf, size_t count) { 16 | Stream* s = reinterpret_cast(stream->state); 17 | size_t written = s->readBytes(buf, count); 18 | return written == count; 19 | }; 20 | 21 | pb_istream_s as_pb_istream(Stream& s) { 22 | #ifndef PB_NO_ERRMSG 23 | return {pb_stream_read, &s, SIZE_MAX, 0}; 24 | #else 25 | return {pb_stream_read, &s, SIZE_MAX}; 26 | #endif 27 | }; 28 | -------------------------------------------------------------------------------- /src/pb_arduino.h: -------------------------------------------------------------------------------- 1 | #include "pb_encode.h" 2 | #include "pb_decode.h" 3 | 4 | class Print; 5 | class Stream; 6 | 7 | //! Convert an object implementing Print into a nanopb ostream 8 | pb_ostream_s as_pb_ostream(Print& p); 9 | 10 | //! Convert an object implementing Print into a nanopb istream 11 | pb_istream_s as_pb_istream(Stream& p); 12 | --------------------------------------------------------------------------------