From 2d7b6ae598ec414c8080a66bdfe6dfa80c78304e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Blanchon?= Date: Sun, 24 Jan 2016 16:10:31 +0100 Subject: [PATCH] Updated Bag of Tricks (markdown) --- Bag-of-Tricks.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/Bag-of-Tricks.md b/Bag-of-Tricks.md index 82b841f..57aa4df 100644 --- a/Bag-of-Tricks.md +++ b/Bag-of-Tricks.md @@ -41,3 +41,37 @@ root.printTo(BufferedPrint<256>(Serial)); See issue [#166](https://github.com/bblanchon/ArduinoJson/issues/166). +## Chunked output + +Here is a proxy that allow to get only part of the output: + +```c++ +class ChunkPrint : public Print { + public: + ChunkPrint(Print& destination, size_t from, size_t to) + : _destination(destination), _to_skip(from), _to_write(to - from) {} + + virtual size_t write(uint8_t c) { + if (_to_skip > 0) { + _to_skip--; + } else if (_to_write > 0) { + _to_write--; + return _destination.write(c); + } + return 0; + } + + private: + Print& _destination; + size_t _to_skip; + size_t _to_write; +}; +``` + +To use this in your code: + +```c++ +root.printTo(ChunkPrint(Serial,10,20)); // print only range [10,20[ +``` + +See issue [#206](https://github.com/bblanchon/ArduinoJson/issues/206). \ No newline at end of file