From 87fdaaabd62268936615321bc14679b3e59e286e Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Mon, 10 Aug 2015 19:30:14 +0200 Subject: o Implementing the SoftwareSerial version of debugging. --- Debug.h | 125 +++++++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 92 insertions(+), 33 deletions(-) (limited to 'Debug.h') diff --git a/Debug.h b/Debug.h index 83eb43f..4883083 100644 --- a/Debug.h +++ b/Debug.h @@ -3,6 +3,14 @@ #include "config-check.h" +template +struct conditional { typedef T type; }; + +template +struct conditional { typedef F type; }; + +#include + #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 -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 +class Debug : public conditional::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 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 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 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 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 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 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; +#if DEBUG_SINK == 0 +static Debug debug; +#elif DEBUG_SINK == 1 +static Debug debug; #elif DEBUG_SINK == 2 -static const Debug debug; +static Debug 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 -- cgit v1.2.3