Using Library Constructor in Another Library

Hi Guys,

I'm not sure how to do this. But, I'm trying to use the NewSoftSerial library inside another library.
The library that I wanted to write will use the constructor of the NewSoftSerial library something like this:

In ModuleX.cpp:

#include "ModuleX.h"

ModuleX::ModuleX(uint8_t rxPin, uint8_t txPin)
{
    uart(rxPin, txPin);
} 

in ModuleX.h:

#include <NewSoftSerial.h>

class ModuleX
{
  public:
    ModuleX(uint8_t rxPin, uint8_t txPin);
  private:
    NewSoftSerial uart;
};

When I compile this, it says that "no matching function for call to NewSoftSerial::NewSoftSerial()".
Do I need the NewSoftSerial class to be protected to use within the ModuleX class?

Thanks. :slight_smile:

Because the NewSoftSerial constructor is called before the ModuleX constructor you have to put the arguments on the same line as the ModuleX constructor declaration:

ModuleX::ModuleX(uint8_t rxPin, uint8_t txPin) : uart(rxPin, txPin)
{
}

Thanks John! It works now. :slight_smile:

John,

I have a slightly different situation now. I would have 2 variations of the ModuleX constructor now. One is using the NewSoftSerial UART and the other using the hardware UART. In the hardware UART case, I would omit the software UART object construction. Like this:

// This one works for software UART.
ModuleX::ModuleX(uint8_t rxPin, uint8_t txPin):uart(rxPin, txPin)
{
}
// This one is complaining about no matching function for hardware UART case.
ModuleX::ModuleX()
{
}

Sounds like you need two different classes: one for Serial and one for NewSoftSerial. They can both inherit from a base class to do most of the work.

If you are only doing output you could use a pointer to a Print object. NewSoftSerial uses Print as a base class and Serial uses Stream as a base class which uses Print as a base class. Unfortunately that won't work for receiving because Stream and NewSoftSerial implement available() and read() separately.

Basically I'm trying to write a library to support a piece of hardware that I can select between using the hardware UART and software UART. I thought by having 1 class it would be less confusing for people using them. I guess I had to split them.

Is there a way to get the destructor of NewSoftSerial into the picture to enable the use of single class?

You can write an AnySerial class with a NewSoftSerial pointer and a HardwareSerial pointer and use that. Something like:

class AnySerial : public Print
{
private:
    bool UsingNewSoftSerial;
    NewSoftSerial *nss;  
    HardwareSerial *hs;
public:
    AnySerial(HardwareSerial *hardwareport = Serial);  // Serial, Serial1, Serial2... (default is Serial)
        {UsingNewSoftSerial = false; hs = hardwareport;}

    AnySerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic = false);  // NewSoftSerial
       {UsingNewSoftSerial = true; nss = NewSoftSerial(receivePin, transmitPin, inverse_logic);}

    virtual int available(void);
    virtual int peek(void);
    virtual int read(void);
    virtual void flush(void);
    virtual void write(uint8_t);
}

Thanks John for the suggestions. I'm going to try this out. :slight_smile: