Created Bag of Tricks (markdown)

Benoît Blanchon
2016-01-24 16:02:38 +01:00
parent e32c1cb2d0
commit cf6a713e7a

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