Last active
November 29, 2016 20:08
-
-
Save vshymanskyy/99f1ddeafb131828db251ddc40dd4c75 to your computer and use it in GitHub Desktop.
This class wraps and Arduino Stream, and dumps all data to another Stream for debug purposes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* This class wraps and Arduino Stream, | |
* and dumps all data to another Stream for debug purposes. | |
* | |
* Usage: | |
* StreamDebugger StreamDbg(Serial1, Serial); | |
*/ | |
#ifndef StreamDebugger_h | |
#define StreamDebugger_h | |
class StreamDebugger | |
: public Stream | |
{ | |
public: | |
StreamDebugger(Stream& data, Stream& dump) | |
: _data(data), _dump(dump) | |
{} | |
virtual size_t write(uint8_t ch) { | |
_dump.write(ch); | |
return _data.write(ch); | |
} | |
virtual int read() { | |
int ch = _data.read(); | |
if (ch != -1) { _dump.write(ch); } | |
return ch; | |
} | |
virtual int available() { return _data.available(); } | |
virtual int peek() { return _data.peek(); } | |
virtual void flush() { _data.flush(); } | |
void directAccess() { | |
while(true) { | |
if (_data.available()) { | |
_dump.write(_data.read()); | |
} | |
if (_dump.available()) { | |
_data.write(_dump.read()); | |
} | |
delay(0); | |
} | |
} | |
private: | |
Stream& _data; | |
Stream& _dump; | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment