Closed twrackers closed 3 months ago
The way Arduino intends you to implement printing for your own data structure is like this:
#include <Printable.h>
const long BAUD = 115200;
struct MyCustomDatastructure : public Printable
{
float member;
size_t printTo(Print& p) const { return p.print(member); }
};
void setup()
{
Serial.begin(BAUD);
MyCustomDatastructure structure;
Serial.print(structure);
}
void loop() {}
However, this would break some of the current library features and most of the examples, since they use the streaming printing mechanism that is implemented here currently. The solution would be to implement something like this and one of these options:
Printable
interface.Printable
interface. Use the current streaming implementation only if the ARDUINO_STREAMING
macro is not defined. This would signal that the current sketch is not using the streaming library.Personally, I would argue that the first solution is the best, since this library is inherently a Linear Algebra-library with a Matrix type, but not a library for helping with printing. However, this will break the backwards compatibility. So maybe the second option is better? In the end, the decision is up to @tomstewart89 since he maintains this project. I could submit a PR for this but will wait for a decision.
Thanks for the input @insalt-glitch ! I'm happy to drop the streaming operators here and embrace Printable
instead. The API breakage is unfortunate but those operators aren't a core part of the library. If you'd like to make a PR, I'd welcome it, otherwise I can take a look at some point when I have some time
This compiles: Serial << << "\n";
unless your sketch #include's to permit another line to:
Serial << << endl;
The BLA definition of '<<' clashes with that in Streaming. Had to replace second line with: Serial.println();
which is annoying.