I have two devices that run on separate ports and am using the SC16IS7xxRK
library to communicate with them. When I use the library directly (see below), everything works great.
void setup()
{
...
extSerial.withI2C(&Wire, 0x4D);
Wire.setSpeed(CLOCK_SPEED_400KHZ);
extSerial.softwareReset();
extSerial.powerOnCheck();
extSerial.a().begin(19200);
extSerial.b().begin(9600);
...
}
void loop()
{
if (extSerial.a().available()) {
// do stuff here
}
if (extSerial.b().available()) {
// do stuff here
}
}
However, I would ideally like to pass a reference/pointer to a separate library and tell it to use extSerial.a()
as the serial to read from, but have encountered troubles I have been unable to resolve, mostly due to intermediate (at best) C++ skills. As an example, it could look something like:
Say I have a Sensor
class like the following:
class Sensor {
public:
Sensor();
virtual ~Sensor();
Sensor &withPort(SC16IS7xxPort *port) { m_port = port; return *this; };
// These wouldn't normally be written inline like this
void setup() {
m_port->begin(19200);
};
void loop() {
if (m_port->available()) {
// do something
}
};
private:
SC16IS7xxPort *m_port;
}
And then code that looks something like this:
void setup()
{
...
extSerial.withI2C(&Wire, 0x4D);
Wire.setSpeed(CLOCK_SPEED_400KHZ);
extSerial.softwareReset();
extSerial.powerOnCheck();
// assume I want to use a sensor that allows me to set it's serial port
sensor.withPort(&extSerial.a())
.setup();
...
}
void loop()
{
sensor.loop();
}
Everything appears to initialize just fine and the call to begin
from within the Sensor
class returns true
, but m_port->available()
always returns nothing. Am I lost? Is there something I can do to use the SC16IS7xxRK
ports and pass them to another library in this way?