Hello all,
I don't claim to be proficient at C++ or classes but I want to implement something like this :-
int serial;
int initserial(int x)
{
if(!x)
{
serial=Serial;
}
else
{
serial=SerialUSB;
}
return serial;
}
void setup()
{
initserial(0);
serial.begin (115200);
while(!serial);
serial.println ();
serial.println ();
}
Obviously this gives 100's of errors but could someone point me in the right direction please?
Thankyou.
system
3
Perhaps you want this:
#ifdef __SAM3X8E__
#define SERIAL SerialUSB
#else
#define SERIAL Serial
#endif
...
setup()
SERIAL.begin();
while (!SERIAL) {}
SERIAL.println("Hello");
The above doesn't use a variable, and is faster. If you really want a variable, then
Stream *stream;
setup()
{
if (flag)
stream = &SerialUSB;
else
stream = &Serial;
stream->println("Hello");
...
Pass a class derived from Stream by reference:
class myMenu
{
private:
Stream & port_;
public:
myMenu (Stream & port) : port_ (port) { }
void begin ();
};
void myMenu::begin ()
{
port_.println ("Menu initialized.");
}
myMenu menu (Serial);
void setup ()
{
Serial.begin (115200);
menu.begin ();
} // end of setup
void loop () { }