0

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.

asked Sep 12, 2018 at 16:16
4
  • it is not a buffer. only a reference Commented Sep 12, 2018 at 16:36
  • Noted, edited the question for clarity. Commented Sep 12, 2018 at 16:39
  • nice. now you put the answer into the questions title :-) Commented Sep 12, 2018 at 16:40
  • And just like that, it works. Thanks for the magic. Commented Sep 12, 2018 at 17:02

1 Answer 1

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.

answered Sep 12, 2018 at 16:28
2
  • That can change, I only need this line for feedback to either my COM port (Serial) or to the LCD. Commented Sep 12, 2018 at 16:40
  • both are derived from Print class. arduino.stackexchange.com/questions/52563/… Commented Sep 12, 2018 at 16:41

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.