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 ++++++++++++++++++++++++++++++++------------ config-check.h | 19 ++++--- trygvisio_soil_moisture.ino | 16 ++++-- 3 files changed, 116 insertions(+), 44 deletions(-) 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 diff --git a/config-check.h b/config-check.h index 077d391..e33e1ad 100644 --- a/config-check.h +++ b/config-check.h @@ -18,6 +18,11 @@ #error *** Read the notes in config-check.h *** #endif +/* + * 20150725: Initial version + * 20150808: Changed values of DEBUG_SINK. + */ + /* * * Configuration Section. @@ -36,7 +41,6 @@ * 1: prototype-a, the hand assembled PCB * 2: prototype-b, the china produced PCB * 3: attiny85 - * 4: OLIMEXINO-32U4 * * No default, has to be specified in config.h */ @@ -100,15 +104,14 @@ * Selects where all debug logging will go. * * Values: - * 1: no logging, all logging is discarded - * 2: Use Serial. Uses the platforms default Serial port, usually the USB or hardware serial ports on your board. - * - * TODO: Change this to 0=no logging, 1=serial, 2=software serial + * 0: no logging, all logging is discarded + * 1: Use Serial. Uses the platforms default Serial port, usually the USB or hardware serial ports on your board. + * 2: Use Software serial */ #ifndef DEBUG_SINK // Default to Serial -#define DEBUG_SINK 2 +#define DEBUG_SINK 1 #endif /******************************************************************************* @@ -127,8 +130,8 @@ #undef USE_LOW_POWER_MODE #endif -#if DEBUG_SINK == 2 -#warning DEBUG_SINK must be 1 on ATTiny85 boards +#if DEBUG_SINK != 0 && DEBUG_SINK != 2 +#warning DEBUG_SINK must be 0 or 2 on ATTiny85 boards #undef DEBUG_SINK #define DEBUG_SINK 1 #endif diff --git a/trygvisio_soil_moisture.ino b/trygvisio_soil_moisture.ino index bb55b70..25baac0 100644 --- a/trygvisio_soil_moisture.ino +++ b/trygvisio_soil_moisture.ino @@ -7,6 +7,7 @@ #ifdef PERSISTENT_CONFIGURATION_SUPPORT #include #endif +#include #include #include #include @@ -83,6 +84,7 @@ void __ble_assert(const char *file, uint16_t line) static void go_to_sleep(); +extern SoftwareSerial ss; void setup() { #ifdef TXLED0 // An attempt to make sure that the RX and TX LEDs are turned off when running normally. @@ -100,7 +102,15 @@ void setup() { CLKPR = 0; #endif - debug.begin(115200); + if (debug.sink == DEBUG_SINK_SERIAL) { + debug.begin(115200); + } else if (debug.sink == DEBUG_SINK_SOFTWARE_SERIAL) { + debug.begin(9600); + + while(true) { + debug.println("hello debug world!"); + } + } #if WAIT_FOR_SERIAL_BEFORE_STARING // Wait until the serial port is available (useful only for the Leonardo) @@ -153,8 +163,8 @@ static void setup_rf() { aci_state.aci_pins.interface_is_interrupt = false; aci_state.aci_pins.interrupt_number = 4; -#error wat -#elif ARDUINO_AVR_LEONARDO // Assumes OLIMEXINO-32U4 and UEXT +#elif ARDUINO_AVR_LEONARDO + // Assumes OLIMEXINO-32U4 and UEXT aci_state.aci_pins.board_name = BOARD_DEFAULT; aci_state.aci_pins.reqn_pin = 2; aci_state.aci_pins.rdyn_pin = 13; -- cgit v1.2.3