Question: How to Handle Derived Objects?

I need some help to improve my 4D Systems ?LCD-32PT(SGC) 3.2” Serial LCD Library for Arduino & chipKIT.

Presently, I use a proxy serial port which accept three classes, all derived from the Stream class: HardwareSerial, NewSoftSerial and i2cSerial.

NewSoftSerial is not compatible with chipKIT. i2cSerial needs the exclusion of both HardwareSerial and NewSoftSerial.

Problem is, I need to configure the port twice:

  • in the main program and
  • in the proxy serial library.

I'd like to

  • declare the serial port on the main program only,

  • send the reference of the serial port to the proxy serial library, but without declaring the class on the proxy library,

  • ask the proxy serial library to call-back the reference of the serial port for sending and receiving data to and from the screen.

I hope you get the idea, even if I don't use the right C++ words!

The repository is hosted on GitHub and more information is on my website.

Thank you very much for your help.

Without dedicating it too much thought, you could implement an abstract class that implements the methods that your proxi should use and, specific derived classes to manage each individual serial device.
Your proxi should just use an abstract class and the particular object is determined during construction.
Just a though that may not be what you are looking for though.

Thank you for the hint :slight_smile:

I've found Stream.h is an abstract class and all derived serial ports (hardware, software and I2C) could thus be addressed the same way through the Stream object.

So I've updated my Serial_LCD library accordingly!

How would it look if you change the serialproxy in the constructor of your Serial_LCD by a reference to the base class that they all inheret from, ie. Stream?

Yes, this is what I did.

ProxySerial::ProxySerial(Stream * port0) {
  _proxyPort = port0; 

No, I was asking. How would it look like if you removed the SerialProxy from the Serial_LCD and used a Stream in the Serial_LCD class constructor instead.

Sorry for the misunderstanding. I'll give a try.

I gave a try.

Result is, I should re-write the LCD_Serial entirely to deal with the Stream and Print classes.

The Stream and Print classes management is done by the proxySerial library. Everything works fine so I stay unchanged.

But feel free to participate and improve that library! You're welcome :slight_smile: