I’m looking to create a new Stream object Debug that acts as a proxy for two other streams: Serial and DebugSerial. The Debug object should:
Redirect everything written to it to both Serial and DebugSerial.
Read operations on Debug should automatically read from both Serial and DebugSerial.
Example:
#include <RemoteDebug.h>
RemoteDebug DebugSerial;
// Magic code that creates the `Debug` Serial proxy
Debug.println("Hello, World!"); // Sends "Hello, World!" to both Serial and DebugSerial.
if (Debug.available()) {
char c = Debug.read(); // Reads from either Serial or DebugSerial.
Debug.print(c); // Echoes `c` to both streams.
}
Does a library or existing solution exist for this? I would like to manually override every single Stream method in order to achieve this behavior.
You only need to implement these methods from Stream:
public:
virtual int available() = 0;
virtual int read() = 0;
virtual int peek() = 0;
And this one from Print (from which Stream inherits):
virtual size_t write(uint8_t) = 0;
Outputting from your new class will to two different Streams(s) will be trivial. You may have to think about how you would interleave data coming from two different Stream(s) into a single (composite) one.
What is the actual Print-based class whose write() function returned 0? Look at the source code for that class. Under what circumstances does it return 0?
It's this RemoteDebug library. I think it's supposed to return the amount of bytes actually written.
Edit: Nevermind, I think it was just mistake on my side that has nothing to do with the code I shared. After fixing that, it prints correctly to both Print objects. Although I suppose there still is a small chance of misbehavior if that case of different return values for those write methods would actually happen.