Possible to override println()?

Status
Not open for further replies.

Frank B

Senior Member
For my FM Stereo / RDS transmitter I want to change the behavior of println.
It should only output \r instead of \r\n.
\n is "Clear Screen" in the RDS protocol - and this is, let's call it "suboptimal".
(I curse the descendants of the inventor to the 3rd generation)

I can override println(void) - but then i need need to change all other println variants with parameters, too.
Is there a better way?

I have:
Code:
class rds : public Print
{
    virtual size_t write(uint8_t);    
    virtual size_t write(const uint8_t *buffer, size_t size);
[COLOR=#ff0000]    size_t println(void)            { return write('\r'); }[/COLOR]
[COLOR=#00ff00]    size_t println(const String &s) { return print(s) + println(); }[/COLOR]
[COLOR=#00ff00]    size_t println(char c)          { return print(c) + println(); }[/COLOR]
[COLOR=#00ff00]    size_t println(const char s[])  { return print(s) + println(); }[/COLOR]
[COLOR=#00ff00]    size_t println(const __FlashStringHelper *f)  { return print(f) + println(); }[/COLOR]
[COLOR=#00ff00]    size_t println(uint8_t b)       { return print(b) + println(); }[/COLOR]
[COLOR=#00ff00]    size_t println(int n)           { return print(n) + println(); }[/COLOR]
[COLOR=#00ff00]    size_t println(unsigned int n)  { return print(n) + println(); }[/COLOR]
[COLOR=#00ff00]    size_t println(long n)          { return print(n) + println(); }[/COLOR]
[COLOR=#00ff00]    size_t println(unsigned long n) { return print(n) + println(); }[/COLOR]
[COLOR=#00ff00]    size_t println(int64_t n)       { return print(n) + println(); }[/COLOR]
[COLOR=#00ff00]    size_t println(uint64_t n)      { return print(n) + println(); }[/COLOR]
[COLOR=#00ff00]    size_t println(unsigned char n, int base) { return print(n, base) + println(); }[/COLOR]
[COLOR=#00ff00]    size_t println(int n, int base) { return print(n, base) + println(); }[/COLOR]
[COLOR=#00ff00]    size_t println(unsigned int n, int base)  { return print(n, base) + println(); }[/COLOR]
[COLOR=#00ff00]    size_t println(long n, int base) { return print(n, base) + println(); }[/COLOR]
[COLOR=#00ff00]    size_t println(unsigned long n, int base) { return print(n, base) + println(); }[/COLOR]
[COLOR=#00ff00]    size_t println(int64_t n, int base)  { return print(n, base) + println(); }[/COLOR]
[COLOR=#00ff00]    size_t println(uint64_t n, int base) { return print(n, base) + println(); }   [/COLOR]
[..]
}
So..are the green lines really needed? They are just copy&paste from Print.h ....
 
Last edited:
Hi Frank,

Sorry I know you know all of this, but just in case I thought I would mention a few things.

I guess the question is do you call all of these or not, also you then also run into issues
That is there is a lot of code out there that does do things like:
Code:
for (int i=0; i< 10; i++) Serial.println(i);
Or many others of the different parameter types.

Also unclear if your override here will work in some different cases. That is if you are calling from the rds call, it will use yours, if it is calling from some Print class it will probably not.

Another hack approach may be to have your: virtual size_t write(const uint8_t *buffer, size_t size);
method, check (real hack version), that if (size==2 && buffer[0] =='\r' && buffer[1] == '\n') { write('\n'); return 1;}

As this is virtual function should catch wherever it may be called...
 
Another hack approach may be to have your: virtual size_t write(const uint8_t *buffer, size_t size);
method, check (real hack version), that if (size==2 && buffer[0] =='\r' && buffer[1] == '\n') { write('\n'); return 1;}

Thank you Kurt, I had the same idea, and I used your approach.
I've asked because - someone might have the ultimative genious Idea ;)
 
Status
Not open for further replies.
Back
Top