arduino / ArduinoCore-avr

The Official Arduino AVR core

Home Page:https://www.arduino.cc

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add printing to NULL in the Print class?

m-elias opened this issue · comments

I'm working on a project where I tried Stream* stream = NULL when I didn't want output messages. While this compiled, my Teensy went into a boot loop. I've solved my issue by using overloaded functions with an if (stream != NULL) check but I wondered if adding NULL handling to the core Print class would be a interesting idea.

Hi @m-elias ,
the thing you are proposing is not very feasible at a platform level; however, adding a default NullStream object (that does nothing) is definitely a good idea.
The code would then need to be modified as
Stream* stream = &NullStream
and that instance would simply "do nothing"

Is that something that needs to be added to the core or just to my individual sketch?

I think it is too specific for the core, better for a library.

In fact, it seems this library implements exactly that: https://github.com/RobTillaart/DEVNULL
Also, see https://github.com/bblanchon/ArduinoStreamUtils which has a bunch of stream utilities (but seems to lack a null stream, even though it would fit in nicely I think).