mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-08-14 01:54:48 +02:00
Created Bag of Tricks (markdown)
43
Bag-of-Tricks.md
Normal file
43
Bag-of-Tricks.md
Normal file
@@ -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 <size_t CAPACITY>
|
||||
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).
|
||||
|
Reference in New Issue
Block a user