Pages: [1]   Go Down
Author Topic: Adapting iPod Control Libary to use NewSoftSerial  (Read 2510 times)
0 Members and 1 Guest are viewing this topic.
London
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

London
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Quote
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
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Quote
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,
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Austin, TX USA
Offline Offline
God Member
*****
Karma: 4
Posts: 997
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
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); }
};

« Last Edit: March 31, 2011, 10:12:00 pm by mikalhart » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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:

Code:
  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.
Logged

Pages: [1]   Go Up
Jump to: