Adapting iPod Control Libary to use NewSoftSerial

Hi,

I'm looking to adapt Recotana's iPod Control library to use NewSoftSerial so I can use the HW UART for other things.

I'd welcome any advice on creating a library which depends on another library.

I would imagine that it should be something like this:

MyCode -> includes iPodControlNSS -> includes NewSoftSerial.

I'd like to be able to pass the RX and TX pins which the iPodControl library should use from MyCode, but the NewSoftSerial constructor requires the RX and TX pins on instantiation.

At the moment I've got it working as follows:

MyCode -> includes NewSoftSerial
-> includes iPodControlNSS (which creates an instance of NSS on some hard coded pins).

Fine for my purposes right now, but not how it should be.

If anyone with a better idea of how to structure things correctly could suggest a route that would be great. You can imagine how recursive/generic searches on forums for the terms "creating libraries which reference libraries" are!

Thanks,

Andy

Your sketch needs to include both the NewSoftSerial library and the iPodControlNSS library. But, your sketch does not need to create an instance of NewSoftSerial. It can simply pass the pins to use to the iPodControlNSS constructor. The iPodControlNSS library should have a begin() method in which the NewSoftSerial instance is actually created.

You will most likely need to refer to the NewSoftSerial instance using a pointer, since the pointer can be declared without actually pointing to anything. This might force you to actually implement the new and delete operators, but that isn't that difficult.

Alternatively, your sketch could create the NewSoftSerial instance, and pass that instance, by reference, to the iPodControlNSS instance.

It would be nice if the NewSoftSerial class had a no argument constructor, in addition to the default constructor, and a setPins() method. That would make what you are trying to do much easier.

Perhaps you could define and implement those methods, and see if Mikal will incorporate them into NewSoftSerial.

Thanks for the response. I'll probably go down the main sketch passing a reference route for the meantime.

It would be nice if the NewSoftSerial class had a no argument constructor, in addition to the default constructor, and a setPins() method. That would make what you are trying to do much easier.

Perhaps you could define and implement those methods, and see if Mikal will incorporate them into NewSoftSerial.

Agreed. I hope I'll get a chance to look at that.

Thanks,

Andy

It would be nice if the NewSoftSerial class had a no argument constructor, in addition to the default constructor, and a setPins() method. That would make what you are trying to do much easier.

In another context I initialized the class with pinnr -1 for TX as I only needed the receive line. Maybe initializing with (-1,-1) works.

just a thought,

I don't think there's any need to make a no-argument constructor to do what you want. In C++, if you want to build a class that contains another class like NewSoftSerial, you do it this way:

class iPodControlNSS
{
  NewSoftSerial nss; // declare the member

public:
  // Here's a constructor that passes its parameter onto nss
  IPodControlNSS(int rx, int tx) : nss(rx, tx)
  {
    ...
  }
  ...

  void begin() { nss.begin(57600); }
};

I don't think there's any need to make a no-argument constructor to do what you want. In C++, if you want to build a class that contains another class like NewSoftSerial, you do it this way:

  NewSoftSerial nss; // declare the member

Which invokes the constructor...

NewSoftSerial *nss would declare a pointer to an instance, without creating an instance. But, then there is a need to actuall point nss at an existing instance.