aboutsummaryrefslogtreecommitdiff
path: root/elegoo/pb/pb_stream.cpp
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2017-08-27 00:49:24 +0200
committerDimitri Sokolyuk <demon@dim13.org>2017-08-27 00:49:24 +0200
commit64a552f7a69e20c174f929c5d4e800aa24b480f3 (patch)
tree2e27e65e623021904fcce43b2382ecd7d95e6baa /elegoo/pb/pb_stream.cpp
parente4cabee7b53ba8f71826b79cc09b3ecba1a02f13 (diff)
Make go getable
Diffstat (limited to 'elegoo/pb/pb_stream.cpp')
-rw-r--r--elegoo/pb/pb_stream.cpp48
1 files changed, 48 insertions, 0 deletions
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 *>(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<Print *>(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
+}