From 77e261f12089bd7db854d21fc81a42eaa60e0b67 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 25 Jul 2015 17:35:57 +0200 Subject: o Even better debug code. --- Debug.h | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 101 insertions(+), 28 deletions(-) (limited to 'Debug.h') diff --git a/Debug.h b/Debug.h index 455b6db..a5b3134 100644 --- a/Debug.h +++ b/Debug.h @@ -3,77 +3,150 @@ #include "config.h" -template -class DebugImpl { +enum DebugSink { + DEBUG_SINK_SERIAL, + DEBUG_SINK_SOFTWARE_SERIAL, + DEBUG_SINK_VOID, +}; + +template +class Debug { public: + inline void begin(unsigned int baud_rate); + + inline operator bool(); + + inline bool available(); + + size_t write(uint8_t); + template - static inline + inline void print(T t, int format = DEC); - template - static inline - void print(const T *t); + inline + void print(const char *t); + + inline + void print(const __FlashStringHelper *t); template - static inline + inline void println(T t, int format = DEC); template - static inline + inline void println(const T *t); - static inline + inline + void println(const __FlashStringHelper *t); + + inline void println(); + + inline + void flush(); }; -template +template +inline void Debug::begin(unsigned int baud_rate) { + if (sink == DEBUG_SINK_SERIAL) { + Serial.begin(baud_rate); + } +} + +template +inline Debug::operator bool() { + if (sink == DEBUG_SINK_SERIAL) { + return Serial; + } + + return false; +} + +template +inline bool Debug::available() { + if (sink == DEBUG_SINK_SERIAL) { + return Serial.available(); + } + + return false; +} + +template +inline size_t Debug::write(uint8_t value) { + if (sink == DEBUG_SINK_SERIAL) { + return Serial.write(value); + } + + return 0; +} + +template template inline -void DebugImpl::print(T t, int format) { - if (enable) { +void Debug::print(T t, int format) { + if (sink == DEBUG_SINK_SERIAL) { Serial.print(t, format); } } -template -template +template inline -void DebugImpl::print(const T *t) { - if (enable) { +void Debug::print(const char *t) { + if (sink == DEBUG_SINK_SERIAL) { + Serial.print(t); + } +} +template +inline +void Debug::print(const __FlashStringHelper *t) { + if (sink == DEBUG_SINK_SERIAL) { Serial.print(t); } } -template +template template inline -void DebugImpl::println(T t, int format) { - if (enable) { +void Debug::println(T t, int format) { + if (sink == DEBUG_SINK_SERIAL) { Serial.println(t, format); } } -template +template template inline -void DebugImpl::println(const T *t) { - if (enable) { +void Debug::println(const T *t) { + if (sink == DEBUG_SINK_SERIAL) { + Serial.println(t); + } +} + +template +inline +void Debug::println(const __FlashStringHelper *t) { + if (sink == DEBUG_SINK_SERIAL) { Serial.println(t); } } -template +template inline -void DebugImpl::println() { - if (enable) { +void Debug::println() { + if (sink == DEBUG_SINK_SERIAL) { Serial.println(); } } -#if ENABLE_DEBUG_LOG == 1 -typedef DebugImpl Debug; +#if DEBUG_SINK == 1 +static Debug debug; +#elif DEBUG_SINK == 2 +static Debug debug; #else -typedef DebugImpl Debug; +#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