diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2015-07-25 17:35:57 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2015-07-25 18:00:42 +0200 |
commit | 77e261f12089bd7db854d21fc81a42eaa60e0b67 (patch) | |
tree | b925284f465b97625f35f1d3a7a1dbeaf78f05e8 /Debug.h | |
parent | 29e624aaa14b98a45f0408e0b1925b60136871d3 (diff) | |
download | trygvisio_soil_moisture-77e261f12089bd7db854d21fc81a42eaa60e0b67.tar.gz trygvisio_soil_moisture-77e261f12089bd7db854d21fc81a42eaa60e0b67.tar.bz2 trygvisio_soil_moisture-77e261f12089bd7db854d21fc81a42eaa60e0b67.tar.xz trygvisio_soil_moisture-77e261f12089bd7db854d21fc81a42eaa60e0b67.zip |
o Even better debug code.
Diffstat (limited to 'Debug.h')
-rw-r--r-- | Debug.h | 129 |
1 files changed, 101 insertions, 28 deletions
@@ -3,77 +3,150 @@ #include "config.h" -template <bool enable> -class DebugImpl { +enum DebugSink { + DEBUG_SINK_SERIAL, + DEBUG_SINK_SOFTWARE_SERIAL, + DEBUG_SINK_VOID, +}; + +template <enum DebugSink sink> +class Debug { public: + inline void begin(unsigned int baud_rate); + + inline operator bool(); + + inline bool available(); + + size_t write(uint8_t); + template<typename T> - static inline + inline void print(T t, int format = DEC); - template<typename T> - static inline - void print(const T *t); + inline + void print(const char *t); + + inline + void print(const __FlashStringHelper *t); template<typename T> - static inline + inline void println(T t, int format = DEC); template<typename T> - static inline + inline void println(const T *t); - static inline + inline + void println(const __FlashStringHelper *t); + + inline void println(); + + inline + void flush(); }; -template<bool enable> +template<enum DebugSink sink> +inline void Debug<sink>::begin(unsigned int baud_rate) { + if (sink == DEBUG_SINK_SERIAL) { + Serial.begin(baud_rate); + } +} + +template<enum DebugSink sink> +inline Debug<sink>::operator bool() { + if (sink == DEBUG_SINK_SERIAL) { + return Serial; + } + + return false; +} + +template<enum DebugSink sink> +inline bool Debug<sink>::available() { + if (sink == DEBUG_SINK_SERIAL) { + return Serial.available(); + } + + return false; +} + +template<enum DebugSink sink> +inline size_t Debug<sink>::write(uint8_t value) { + if (sink == DEBUG_SINK_SERIAL) { + return Serial.write(value); + } + + return 0; +} + +template<enum DebugSink sink> template<typename T> inline -void DebugImpl<enable>::print(T t, int format) { - if (enable) { +void Debug<sink>::print(T t, int format) { + if (sink == DEBUG_SINK_SERIAL) { Serial.print(t, format); } } -template<bool enable> -template<typename T> +template<enum DebugSink sink> inline -void DebugImpl<enable>::print(const T *t) { - if (enable) { +void Debug<sink>::print(const char *t) { + if (sink == DEBUG_SINK_SERIAL) { + Serial.print(t); + } +} +template<enum DebugSink sink> +inline +void Debug<sink>::print(const __FlashStringHelper *t) { + if (sink == DEBUG_SINK_SERIAL) { Serial.print(t); } } -template<bool enable> +template<enum DebugSink sink> template<typename T> inline -void DebugImpl<enable>::println(T t, int format) { - if (enable) { +void Debug<sink>::println(T t, int format) { + if (sink == DEBUG_SINK_SERIAL) { Serial.println(t, format); } } -template<bool enable> +template<enum DebugSink sink> template<typename T> inline -void DebugImpl<enable>::println(const T *t) { - if (enable) { +void Debug<sink>::println(const T *t) { + if (sink == DEBUG_SINK_SERIAL) { + Serial.println(t); + } +} + +template<enum DebugSink sink> +inline +void Debug<sink>::println(const __FlashStringHelper *t) { + if (sink == DEBUG_SINK_SERIAL) { Serial.println(t); } } -template<bool enable> +template<enum DebugSink sink> inline -void DebugImpl<enable>::println() { - if (enable) { +void Debug<sink>::println() { + if (sink == DEBUG_SINK_SERIAL) { Serial.println(); } } -#if ENABLE_DEBUG_LOG == 1 -typedef DebugImpl<false> Debug; +#if DEBUG_SINK == 1 +static Debug<DEBUG_SINK_VOID> debug; +#elif DEBUG_SINK == 2 +static Debug<DEBUG_SINK_SERIAL> debug; #else -typedef DebugImpl<true> 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 |