Currently, I have hardware serial through usb for programming and monitoring, and AltSoftSerial to speak to my bluetooth.
I've used some defines and #ifdef to handle the different possible bluetooth scenarios, so that btSerial might be of type AltSoftSerial, of type Serial, or an alias to Serial itself. (but I don't seem to be doing it correctly; the error from later using "btSerial.begin(9600)" is included in the code below
//if we are going to program by usb, we need a pin-based serial for the AT-09
//#define useSoftwareSerial
#define useAltSoftSerial
//if using hardwar serial and we want to talk back to the serial monitor for debugging
//#define
//types we'll need
enum srlTyps {
NONE, HARDWARE, SOFTSERIAL, ALTSOFTSERIAL
};
//sort out the consequences ofthat choice
#ifdef useSoftwareSerial
#include <SoftwareSerial.h>
//we want an softserial. Use the rx=D8 and TX=D9 for hardware ease
#define usingSoftSer
const srlTyps btTyp=SOFTSERIAL;
SoftwareSerial btSerial (8,9);
#elif useAltSoftSerial
//we want an altsoftserial. On nano, rx=D8 and TX=D9
#include <AltSoftSerial.h>
AltSoftSerial btSerial;
#define usingSoftSer
constant srlTyps btTyp=ALTSOFTSERIAL;
#else
//we wll use the hardware serial for AT-09
#define useHardSer
//make btSerial an alias for Serial, so that we don't have t fll the code with ifDefs
const HardwareSerial * btSerial = &Serial;//but this is wrong: "cerror: request for member 'begin' in 'btSerial', which is of pointer type 'HardwareSerial*' (maybe you meant to use '->' ?)
btSerial.begin(9600);"
constant srlTyps btTyp=HARDWARE;
#endif
So far, so good, and I can simply have
btSerial.println("some message or another")
without switches or #ifdef
I'd like to be able to do the same for a monSerial for the monitor so that I could use, for example,
monSerial.println("the status message")
.
That's easy enough if monasterial might be any of the three serial types. But if I don't want the monitor (once deployed on battery power instead of debugging my bench), and I define a class with empty functions for println(), write(), etc., would the compiler optimize the monserial.println() above clean out of the results?
For my particular application, speed is a non-issue (other than the energy use for additional time that the chip stays powered before sleep, but that should be negligible). It's more a matter of saving bytes of memory.