From 64a552f7a69e20c174f929c5d4e800aa24b480f3 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sun, 27 Aug 2017 00:49:24 +0200 Subject: Make go getable --- elegoo/pb/pb_stream.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 elegoo/pb/pb_stream.cpp (limited to 'elegoo/pb/pb_stream.cpp') diff --git a/elegoo/pb/pb_stream.cpp b/elegoo/pb/pb_stream.cpp new file mode 100644 index 0000000..41100ed --- /dev/null +++ b/elegoo/pb/pb_stream.cpp @@ -0,0 +1,48 @@ +// From https://github.com/amorellgarcia/arduino-nanopb + +#include "pb_stream.h" + +#define MAXSZ (size_t)-1 + +bool +os_read(pb_istream_t *stream, uint8_t *buf, size_t count) +{ + Stream *s = static_cast(stream->state); + while (s->available() > 0 && count > 0) { + count -= s->readBytes((char *)buf, count); + } + return count == 0; +} + + +void +pb_istream_from_stream(Stream &stream, pb_istream_t &istream) +{ + istream.callback = &os_read; + istream.state = &stream; + istream.bytes_left = MAXSZ; +#ifndef PB_NO_ERRMSG + istream.errmsg = NULL; +#endif +} + +bool +os_write(pb_ostream_t *stream, const uint8_t *buf, size_t count) +{ + if (stream == NULL || buf == NULL) { + return false; + } + Print *s = static_cast(stream->state); + return (s->write(buf, count) == count); +} + +void +pb_ostream_from_stream(Print &stream, pb_ostream_t &ostream) { + ostream.callback = &os_write; + ostream.state = &stream; + ostream.max_size = MAXSZ; + ostream.bytes_written = 0; +#ifndef PB_NO_ERRMSG + ostream.errmsg = NULL; +#endif +} -- cgit v1.2.3