From cf6a713e7a5e231a07d33d19647cbcce83f8ec05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Blanchon?= Date: Sun, 24 Jan 2016 16:02:38 +0100 Subject: [PATCH] Created Bag of Tricks (markdown) --- Bag-of-Tricks.md | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 Bag-of-Tricks.md diff --git a/Bag-of-Tricks.md b/Bag-of-Tricks.md new file mode 100644 index 0000000..82b841f --- /dev/null +++ b/Bag-of-Tricks.md @@ -0,0 +1,43 @@ +Here are helper class that can help you add missing features. + +## Buffered output: + +Here is a proxy that will put bytes in a buffer before actually writing them to the destination: + +```c++ +template +class BufferedPrint : public Print { + public: + BufferedPrint(Print& destination) : _destination(destination), _size(0) {} + + ~BufferedPrint() { flush(); } + + virtual size_t write(uint8_t c) { + _buffer[_size++] = c; + + if (_size + 1 == CAPACITY) { + flush(); + } + } + + void flush() { + buffer[_size] = '\0'; + _destination.print(_buffer); + _size = 0; + } + + private: + Print& _destination; + size_t _size; + char _buffer[CAPACITY]; +}; +``` + +To use this in your code: + +```c++ +root.printTo(BufferedPrint<256>(Serial)); +``` + +See issue [#166](https://github.com/bblanchon/ArduinoJson/issues/166). +