diff options
Diffstat (limited to 'Debug.h')
-rw-r--r-- | Debug.h | 125 |
1 files changed, 92 insertions, 33 deletions
@@ -3,6 +3,14 @@ #include "config-check.h" +template<bool B, class T, class F> +struct conditional { typedef T type; }; + +template<class T, class F> +struct conditional<false, T, F> { typedef F type; }; + +#include <SoftwareSerial.h> + #ifdef __AVR_ATtiny85__ class Serial { public: @@ -20,43 +28,77 @@ extern Serial Serial; #endif enum DebugSink { + DEBUG_SINK_VOID, DEBUG_SINK_SERIAL, DEBUG_SINK_SOFTWARE_SERIAL, - DEBUG_SINK_VOID, }; -template <enum DebugSink sink> -class Debug { +struct DebugEmptyBase {}; +struct DebugSoftwareSerialBase { + // TODO: these should be customizable + static const int rx_pin = 1; + static const int tx_pin = 0; + +public: + SoftwareSerial ss; + + DebugSoftwareSerialBase() : ss(rx_pin, tx_pin) { + } +}; + +template <enum DebugSink sink_> +class Debug : public conditional<sink_ == DEBUG_SINK_SOFTWARE_SERIAL, DebugSoftwareSerialBase, DebugEmptyBase>::type +{ + public: - inline void begin(const long baud_rate) const { - if (sink == DEBUG_SINK_SERIAL) { + const enum DebugSink sink; + + Debug() : sink(sink_) { + } + + inline void begin(const long baud_rate) { + if (sink_ == DEBUG_SINK_SERIAL) { Serial.begin(baud_rate); + } else if (sink_ == DEBUG_SINK_SOFTWARE_SERIAL) { + this->ss.begin(baud_rate); } } - inline operator bool() const { - return Serial.operator bool(); + inline operator bool() { + if (sink_ == DEBUG_SINK_SERIAL) { + Serial.operator bool(); + } else if (sink_ == DEBUG_SINK_SOFTWARE_SERIAL) { + this->ss.operator bool(); + } else { + return false; + } }; - inline bool available() const { - if (sink == DEBUG_SINK_SERIAL) { + inline bool available() { + if (sink_ == DEBUG_SINK_SERIAL) { return Serial.available(); + } if (sink_ == DEBUG_SINK_SOFTWARE_SERIAL) { + return this->ss.available(); } return false; }; - inline int read() const { - if (sink == DEBUG_SINK_SERIAL) { + inline int read() { + if (sink_ == DEBUG_SINK_SERIAL) { return Serial.read(); + } else if (sink_ == DEBUG_SINK_SOFTWARE_SERIAL) { + return this->ss.read(); } return '\0'; }; - size_t write(uint8_t value) const { - if (sink == DEBUG_SINK_SERIAL) { + size_t write(uint8_t value) { + if (sink_ == DEBUG_SINK_SERIAL) { Serial.write(value); + } else if (sink_ == DEBUG_SINK_SOFTWARE_SERIAL) { + this->ss.write(value); } return value; @@ -64,74 +106,91 @@ public: template<typename T> inline - void print(T t, int format = DEC) const { - if (sink == DEBUG_SINK_SERIAL) { + void print(T t, int format = DEC) { + if (sink_ == DEBUG_SINK_SERIAL) { Serial.print(t, format); + } else if (sink_ == DEBUG_SINK_SOFTWARE_SERIAL) { + this->ss.print(t, format); } }; template<typename T> inline - void print(const T *t) const { - if (sink == DEBUG_SINK_SERIAL) { + void print(const T *t) { + if (sink_ == DEBUG_SINK_SERIAL) { Serial.print(t); + } else if (sink_ == DEBUG_SINK_SOFTWARE_SERIAL) { + this->ss.print(t); } }; template<typename T> inline - void print(T *t) const { - if (sink == DEBUG_SINK_SERIAL) { + void print(T *t) { + if (sink_ == DEBUG_SINK_SERIAL) { Serial.print(t); + } else if (sink_ == DEBUG_SINK_SOFTWARE_SERIAL) { + this->ss.print(t); } }; template<typename T> inline - void println(T t, int format = DEC) const { - if (sink == DEBUG_SINK_SERIAL) { + void println(T t, int format = DEC) { + if (sink_ == DEBUG_SINK_SERIAL) { Serial.println(t, format); + } else if (sink_ == DEBUG_SINK_SOFTWARE_SERIAL) { + this->ss.println(t, format); } }; template<typename T> inline - void println(const T *t) const { - if (sink == DEBUG_SINK_SERIAL) { + void println(const T *t) { + if (sink_ == DEBUG_SINK_SERIAL) { Serial.println(t); + } else if (sink_ == DEBUG_SINK_SOFTWARE_SERIAL) { + this->ss.println(t); } }; template<typename T> inline - void println(T *t) const { - if (sink == DEBUG_SINK_SERIAL) { + void println(T *t) { + if (sink_ == DEBUG_SINK_SERIAL) { Serial.println(t); + } else if (sink_ == DEBUG_SINK_SOFTWARE_SERIAL) { + this->ss.println(t); } }; inline - void println() const { - if (sink == DEBUG_SINK_SERIAL) { + void println() { + if (sink_ == DEBUG_SINK_SERIAL) { Serial.println(); + } else if (sink_ == DEBUG_SINK_SOFTWARE_SERIAL) { + this->ss.println(); } }; inline void flush() { - if (sink == DEBUG_SINK_SERIAL) { + if (sink_ == DEBUG_SINK_SERIAL) { Serial.flush(); + } else if (sink_ == DEBUG_SINK_SOFTWARE_SERIAL) { + this->ss.flush(); } }; }; -#if DEBUG_SINK == 1 -static const Debug<DEBUG_SINK_VOID> debug; +#if DEBUG_SINK == 0 +static Debug<DEBUG_SINK_VOID> debug; +#elif DEBUG_SINK == 1 +static Debug<DEBUG_SINK_SERIAL> debug; #elif DEBUG_SINK == 2 -static const Debug<DEBUG_SINK_SERIAL> debug; +static Debug<DEBUG_SINK_SOFTWARE_SERIAL> debug; #else -#error Invalid value for DEBUG_SINK, must be one of 1 (for no output), 2 for serial port -// , or 3 for software serial. +#error Invalid value for DEBUG_SINK, must be one of 1 (for no output), 2 for serial port, or 3 for software serial. #endif #endif |