itsanjan / arduino

Automatically exported from code.google.com/p/arduino
Other
0 stars 0 forks source link

Abstract Serial base class for HardwareSerial and SoftwareSerial #570

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I would like to see the method begin() added to the Stream class.

I'm currently writing a hardware abstraction class that works with both 
HardwareSerial and NewSoftSerial (because they inherit from Stream) but i can't 
see how i can call the begin() method of either HardwareSerial or NewSoftSerial 
from within my class, leaving that task to the user and not being as abstract 
as it could be.

the MIDI class could benefit from this, as it could work with both 
HardwareSerial and NewSoftSerial, thus allowing more than one MIDI object.

I don't know if this enhancement would cause conflict with anything else, but i 
don't see it could cause any harm.

Original issue reported on code.google.com by tiagocus...@gmail.com on 18 Jul 2011 at 2:13

GoogleCodeExporter commented 9 years ago
This should probably be a new abstract base class (between Stream and 
HardwareSerial), since the begin() method doesn't make sense for every Stream - 
or, at least, wouldn't mean the same thing for all of them.  

Original comment by dmel...@gmail.com on 1 Aug 2011 at 1:20

GoogleCodeExporter commented 9 years ago
my suggestion on how to implement this feature, for starters would be something 
like a serial class extending Stream:

class AbstractSerial : public Stream
{
  public:
    virtual void begin(unsigned long);
    virtual void begin(unsigned long, uint8_t);
    virtual void end();
};

then, on HardwareSerial.h:

class HardwareSerial : public AbstractSerial
{
  ...
}

on SoftwareSerial.h:

class SoftwareSerial : public AbstractSerial
{
  ...
}

on USBAPI.h:

class Serial_ : public AbstractSerial
{
 ...
}

After this we could happily do things like:

#include <SoftwareSerial.h>

SoftwareSerial mySerial1(2, 3);
SoftwareSerial mySerial2(4, 5);

MIDI midiShield(mySerial1);
MIDI midiSynth(mySerial2);

void setup(){
...

...because now we could call the begin method regardless of what kind of Serial 
port we're giving to the object using it.

I hope you look into this.

cheers.

Original comment by tiagocus...@gmail.com on 13 Mar 2013 at 4:55