Extend the Serial class at compile time?

I’d like to add a HelloWolrd() method to the Serial class. I’m not familiar with C++ and extending a class so maybe someone can help with the syntax? I could edit the HardwareSerial code directly and just add the method, but that requires anyone else using my code to also do the same thing. I’d like to be able to say:

#include <scott_serial.h>

And have it extend and add the HelloWorld() method. That way I can just ship scott_serial.h with my project and not require people to modify their arduino supplied HardwareSerial.

class MySerial: public HardwareSerial {
  public:
    void HelloWorld();
};

void MySerial::HelloWorld() {
  Serial.println("Hello world"); 
}

////////////////////////////////////////////////

void setup() { 
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly: 
  
  Serial.println("hello");
  //Serial.hw();
  delay(1000);
}

I’d like to be able to say:

#include <scott_serial.h>

And have it extend and add the HelloWorld() method.

You’re off to a good start. What is the problem?

You should have a constructor for your class.

Is it safe to assume that the derived class is going to have more than a silly HelloWorld() method added?

Ideally I want to have Serial.printf()... but for starters I just want HelloWorld() as proof of concept.

Check out this:
http://arduino.cc/forum/index.php/topic,120440.0.html

I’m pretty sure it’s impossible to make Serial.helloWorld() a meaningful statement while using the HardwareSerial class (alternative is to rewrite entire class), because Serial is defined as HardwareSerial. However, you can make mySerial.helloWorld() a meaningful statement:

#include <Arduino.h>
class ScottSerial : public HardwareSerial {
public:
  ScottSerial() : HardwareSerial(Serial){
  }
  size_t helloWorld() {
    return println(F("Hello, world!"));
  }
};

ScottSerial mySerial;

void setup() {
  mySerial.begin(115200);
  mySerial.print("Okay it's hello world time.");
  mySerial.helloWorld();
}
void loop() {}

[quote author=Scott Baker link=topic=128910.msg970006#msg970006 date=1351118108]

Serial.println("Hello world");

[/quote]

I think that's a mistake. You're printing to the globally defined instance named Serial, instead of printing to this instance of HardwareSerial.

Since the global variable Serial has already been declared and defined, you can't change its type (i.e. from HardwareSerial to ScottSerial) unless you change the library that defines it.

The closest you can sensibly get is to define a subclass of HardwareSerial that acts as a proxy for a HardwareSerial and then define your own global instance of that subclass that wraps the Serial object. In order to have it act as a proxy you would need to provide implementations of all the public methods exposed by HardwareSerial that delegate the call to the underlying HardwareSerial.

Of course you do have the option of using a #define Serial MyExtendedSerial to make user code that appears to use Serial actually use your global proxy instead, although personally I wouldn't recommend it. I think that if people are going to code to an extended API it is reasonable for their code to reflect that, rather than try to pretend that the behaviour of the 'standard' API is no longer standard.