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). +