Right now I have an object:
class Dog
{
public:
Dog(uint8_t I2c, Stream& feedback);
void Speak();
private:
uint8_t _I2c;
Stream& _output;
};
Dog::Dog(uint8_t I2c, Stream& feedback):_output(feedback),_I2c(I2c){}
Dog::Speak(){
_output.println("Hi There.");
}
That is currently constructed in my sketch via:
Dog Dug([I2cPort], Serial);
How can I modify my class so that to be compatible with taking either a Serial object or an LCD display (using Adafruit's RGB LCD library )?
Eg:
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
Dog Dug([I2cPort], Serial);
Dog Sam([I2cPort], lcd);
Additional info:
The LCD in question is mostly used as a UI display in its own library this library refreshes the display every 1.5 sec. As information from speak();
and the UI have similar priority, it is acceptable if one instantly overwrites the other.
I am open to solutions that involve modifying the construction of Dog
the only requirement is that I need to be able to indicate print("[something]");
in the Dog.cpp
I'm interested in asking this question on the Arduino SE as I believe there are differences in how you would approach this if the problem was on a traditional computer.
-
it is not a buffer. only a referenceJuraj– Juraj ♦2018年09月12日 16:36:53 +00:00Commented Sep 12, 2018 at 16:36
-
Noted, edited the question for clarity.ATE-ENGE– ATE-ENGE2018年09月12日 16:39:04 +00:00Commented Sep 12, 2018 at 16:39
-
nice. now you put the answer into the questions title :-)Juraj– Juraj ♦2018年09月12日 16:40:41 +00:00Commented Sep 12, 2018 at 16:40
-
And just like that, it works. Thanks for the magic.ATE-ENGE– ATE-ENGE2018年09月12日 17:02:42 +00:00Commented Sep 12, 2018 at 17:02
1 Answer 1
If your class is only writing to the stream you probably want to have your class take a Print object instead of a Stream object.
The Adafruit library inherits from Print.
-
That can change, I only need this line for feedback to either my COM port (Serial) or to the LCD.ATE-ENGE– ATE-ENGE2018年09月12日 16:40:47 +00:00Commented Sep 12, 2018 at 16:40
-
both are derived from Print class. arduino.stackexchange.com/questions/52563/…2018年09月12日 16:41:23 +00:00Commented Sep 12, 2018 at 16:41