From f2162af9c69b50d779533f0855d5fb4bbaaeb385 Mon Sep 17 00:00:00 2001 From: hg Date: Tue, 4 Mar 2014 19:08:19 +0100 Subject: [PATCH] store debug and info in eeprom too --- Config.cpp | 24 +++++++++++++++ Config.h | 19 ++++++++++++ ThermometerPro.cpp | 3 ++ thermometer.cpp | 77 ++++++++++++++++++++++++++++++---------------- thermometer.h | 4 +++ 5 files changed, 100 insertions(+), 27 deletions(-) diff --git a/Config.cpp b/Config.cpp index 509d55f..6567e17 100644 --- a/Config.cpp +++ b/Config.cpp @@ -10,6 +10,30 @@ #include "Config.h" +String ConfigInvalidateCmd::exec(String params) { + Config::setUInt(Config::MAGIC, 0); + return "done"; +} + + + + +bool Config::getBool(int pos) { + u_bool u; + for (unsigned int i = 0; i < sizeof(bool); i++) { + u.e[i] = EEPROM.read(pos + i); + } + return u.b; +} + +void Config::setBool(int pos, bool value) { + u_bool u; + u.b = value; + for (unsigned int i = 0; i < sizeof(bool); i++) { + EEPROM.write(pos + i, u.e[i]); + } +} + float Config::getFloat(int pos) { u_float u; for (unsigned int i = 0; i < sizeof(float); i++) { diff --git a/Config.h b/Config.h index b9b6d19..ad6c486 100644 --- a/Config.h +++ b/Config.h @@ -8,6 +8,16 @@ #ifndef CONFIG_H_ #define CONFIG_H_ +#include "cmd.h" + + +class ConfigInvalidateCmd : public Cmd { +public: + virtual String getCmdName() { return "CFGINV"; } + virtual String getHelp() { return "Invalidate the stored configuration"; } + virtual String exec(String params); +}; + typedef union { @@ -25,6 +35,11 @@ typedef union { uint8_t e[sizeof(unsigned long)]; } u_ulong; +typedef union { + bool b; + uint8_t e[sizeof(bool)]; +} u_bool; + namespace Config { const unsigned int MAGIC_TOKEN = 0xDEADBEEF; @@ -33,7 +48,11 @@ namespace Config { const int THERMOMETER_ALPHA = 4; // 4 const int THERMOMETER_PERIOD = 8; // 8 const int THERMOMETER_CAL[4] = {12, 16, 20, 24}; // 4 x 4 + const int THERMOMETER_DEBUG = 28; // 1 + const int THERMOMETER_INFO = 29; // 1 + bool getBool(int pos); + void setBool(int pos, bool value); float getFloat(int pos); void setFloat(int pos, float value); unsigned int getUInt(int pos); diff --git a/ThermometerPro.cpp b/ThermometerPro.cpp index d3d7f5a..ae85b6a 100644 --- a/ThermometerPro.cpp +++ b/ThermometerPro.cpp @@ -4,10 +4,12 @@ #include "test.h" #include "uptime.h" #include "thermometer.h" +#include "Config.h" static CmdServer cmdServer(&Serial); static TestCmd testCmd; +static ConfigInvalidateCmd configInvalidateCmd; static Uptime uptime; static Thermometer thermometer; @@ -15,6 +17,7 @@ static Thermometer thermometer; void setup() { cmdServer.begin(); testCmd.registerYourself(&cmdServer); + configInvalidateCmd.registerYourself(&cmdServer); uptime.begin(&cmdServer); thermometer.begin(&cmdServer); diff --git a/thermometer.cpp b/thermometer.cpp index fa84c78..d452078 100644 --- a/thermometer.cpp +++ b/thermometer.cpp @@ -31,9 +31,11 @@ String ThermConfig::exec(String params) { float v = atof(pb1); m_thermometer->setAlpha(v); } else if (params.startsWith("debug ") && (space != -1)) { - m_thermometer->m_debug = (strcmp(pb1, "on") == 0); + bool b = (strcmp(pb1, "on") == 0); + m_thermometer->setDebug(b); } else if (params.startsWith("info ") && (space != -1)) { - m_thermometer->m_info = (strcmp(pb1, "on") == 0); + bool b = (strcmp(pb1, "on") == 0); + m_thermometer->setInfo(b); } else if (params.equalsIgnoreCase("show")) { m_stream->print("PeriodMeasure (ms): "); m_stream->print(m_thermometer->getPeriodMeasure()); m_stream->println(); m_stream->print("Alpha: "); m_stream->print(m_thermometer->getAlpha()); m_stream->println(); @@ -41,8 +43,8 @@ String ThermConfig::exec(String params) { m_stream->print("Calibration: "); m_stream->print(i); m_stream->print(", "); m_stream->print(m_thermometer->getCalibrateFactor(i), 6); m_stream->println(); } - m_stream->print("Info: "); m_stream->print(m_thermometer->m_info); m_stream->println(); - m_stream->print("Debug: "); m_stream->print(m_thermometer->m_debug); m_stream->println(); + m_stream->print("Info: "); m_stream->print(m_thermometer->getInfo()); m_stream->println(); + m_stream->print("Debug: "); m_stream->print(m_thermometer->getDebug()); m_stream->println(); m_stream->print("COMPILE_TIME_DEBUG: "); m_stream->print(COMPILE_TIME_DEBUG); m_stream->println(); } else { @@ -137,6 +139,24 @@ float Thermometer::getAlpha() { return m_alpha; } +void Thermometer::setDebug(bool b) { + Config::setBool(Config::THERMOMETER_DEBUG, b); + m_debug = b; +} + +bool Thermometer::getDebug() { + return m_debug; +} + +void Thermometer::setInfo(bool b) { + Config::setBool(Config::THERMOMETER_INFO, b); + m_info = b; +} + +bool Thermometer::getInfo() { + return m_info; +} + void Thermometer::setCalibrateFactor(unsigned int i, float c) { Config::setFloat(Config::THERMOMETER_CAL[i], c); @@ -160,7 +180,7 @@ void Thermometer::setTemperature(unsigned int index, float t) { } else { m_smoothedTemperature[index] = getAlpha() * t + (1 - m_alpha) * m_lastSmoothedTemperature[index]; } - if (m_debug || m_info) { + if (getDebug() || getInfo()) { Serial.print("setTemperature: i="); Serial.print(index); Serial.print(", t="); Serial.print(t); Serial.print(", t_smoothed="); Serial.print(m_smoothedTemperature[index]); Serial.println(); @@ -176,7 +196,7 @@ void Thermometer::prepareAdc() { AD7190_CONF_CHAN(1 << AD7190_CH_AIN2P_AINCOM) | AD7190_CONF_CHAN(1 << AD7190_CH_AIN3P_AINCOM) | AD7190_CONF_CHAN(1 << AD7190_CH_AIN4P_AINCOM); - if (COMPILE_TIME_DEBUG && m_debug) { Serial.print("CONF: "); Serial.print(newRegValue, 16); Serial.println(); } + if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print("CONF: "); Serial.print(newRegValue, 16); Serial.println(); } AD7190_SetRegisterValue(AD7190_REG_CONF, newRegValue, 3, 1); newRegValue = AD7190_MODE_SEL(AD7190_MODE_IDLE) | @@ -184,7 +204,7 @@ void Thermometer::prepareAdc() { AD7190_MODE_REJ60 | AD7190_MODE_DAT_STA | AD7190_MODE_RATE(0x060); - if (COMPILE_TIME_DEBUG && m_debug) { Serial.print("MODE: "); Serial.print(newRegValue, 16); Serial.println(); } + if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print("MODE: "); Serial.print(newRegValue, 16); Serial.println(); } AD7190_SetRegisterValue(AD7190_REG_MODE, newRegValue, 3, 1); } @@ -193,7 +213,7 @@ void Thermometer::startSingleConv() { unsigned long oldRegValue = AD7190_GetRegisterValue(AD7190_REG_MODE, 3, 0); oldRegValue &= ~(AD7190_MODE_SEL(0x07)); unsigned long newRegValue = oldRegValue | AD7190_MODE_SEL(AD7190_MODE_SINGLE); - if (COMPILE_TIME_DEBUG && m_debug) { Serial.print("MODE: "); Serial.print(newRegValue, 16); Serial.println(); } + if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print("MODE: "); Serial.print(newRegValue, 16); Serial.println(); } AD7190_SetRegisterValue(AD7190_REG_MODE, newRegValue, 3, 0); } @@ -229,6 +249,8 @@ void Thermometer::begin(CmdServer *cmdServer) { for (int i = 0; i < 4; i++) { Config::setFloat(Config::THERMOMETER_CAL[i], 1.0); } + Config::setBool(Config::THERMOMETER_DEBUG, true); + Config::setBool(Config::THERMOMETER_INFO, true); Config::setMagic(); } @@ -237,7 +259,8 @@ void Thermometer::begin(CmdServer *cmdServer) { for (int i = 0; i < 4; i++) { setCalibrateFactor(i, Config::getFloat(Config::THERMOMETER_CAL[i])); } - + setDebug(Config::getBool(Config::THERMOMETER_DEBUG)); + setInfo(Config::getBool(Config::THERMOMETER_INFO)); //setCalibrateFactor(0, 1.002999); //setCalibrateFactor(1, 1.001804); @@ -260,14 +283,14 @@ void Thermometer::exec() { switch (state) { case 0: - if (COMPILE_TIME_DEBUG && m_debug) { Serial.println(); } - if (COMPILE_TIME_DEBUG && m_debug) { Serial.println("State 0"); } + if (COMPILE_TIME_DEBUG && getDebug()) { Serial.println(); } + if (COMPILE_TIME_DEBUG && getDebug()) { Serial.println("State 0"); } // start conversion SPI_Enable(AD7190_SLAVE_ID); startSingleConv(); state = 1; - if (COMPILE_TIME_DEBUG && m_debug) { Serial.println("Switching to State 1"); } + if (COMPILE_TIME_DEBUG && getDebug()) { Serial.println("Switching to State 1"); } timeOutCnt = CONV_TIMEOUT; m_cylceCnt++; @@ -284,12 +307,12 @@ void Thermometer::exec() { m_n[channelIndex] = adcValue; - if (COMPILE_TIME_DEBUG && m_debug) { Serial.print("m_n["); Serial.print(channelIndex); Serial.print("]="); Serial.print(m_n[channelIndex], 16); Serial.println(); } + if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print("m_n["); Serial.print(channelIndex); Serial.print("]="); Serial.print(m_n[channelIndex], 16); Serial.println(); } channelCnt++; if (channelCnt >= 4) { state = 10; - if (COMPILE_TIME_DEBUG && m_debug) { Serial.println("Switching to State 10"); } + if (COMPILE_TIME_DEBUG && getDebug()) { Serial.println("Switching to State 10"); } } timeOutCnt = CONV_TIMEOUT; @@ -298,7 +321,7 @@ void Thermometer::exec() { break; case 9: - if (COMPILE_TIME_DEBUG && m_debug) { Serial.println("State 9"); } + if (COMPILE_TIME_DEBUG && getDebug()) { Serial.println("State 9"); } // end cycle SPI_Disable(AD7190_SLAVE_ID); @@ -310,28 +333,28 @@ void Thermometer::exec() { break; case 10: - if (COMPILE_TIME_DEBUG && m_debug) { Serial.println("State 10"); } + if (COMPILE_TIME_DEBUG && getDebug()) { Serial.println("State 10"); } SPI_Disable(AD7190_SLAVE_ID); for (unsigned int i = 0; i < NUM_OF_CHANNELS; i++) { - if (COMPILE_TIME_DEBUG && m_debug) { Serial.print("i="); Serial.print(i); Serial.print(", "); } - if (COMPILE_TIME_DEBUG && m_debug) { Serial.print("m_n="); Serial.print(m_n[i]); Serial.print(", "); } - if (COMPILE_TIME_DEBUG && m_debug) { Serial.print(""); Serial.print((m_n[i] - ((i == 3) ? 0 : m_n[i + 1]))); Serial.print(", "); } - if (COMPILE_TIME_DEBUG && m_debug) { Serial.print("calibrateFactor="); Serial.print(getCalibrateFactor(i), 6); Serial.print(", "); } + if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print("i="); Serial.print(i); Serial.print(", "); } + if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print("m_n="); Serial.print(m_n[i]); Serial.print(", "); } + if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print(""); Serial.print((m_n[i] - ((i == 3) ? 0 : m_n[i + 1]))); Serial.print(", "); } + if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print("calibrateFactor="); Serial.print(getCalibrateFactor(i), 6); Serial.print(", "); } float r = (((float)(m_n[i] - ((i == 3) ? 0 : m_n[i + 1]))) / ((float)N_MAX)) * R_REF * getCalibrateFactor(i); - if (COMPILE_TIME_DEBUG && m_debug) { Serial.print("r="); Serial.print(r); Serial.print(", "); } + if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print("r="); Serial.print(r); Serial.print(", "); } float t = pt1000(r); - if (COMPILE_TIME_DEBUG && m_debug) { Serial.print("t="); Serial.print(t); Serial.print(", "); } - if (COMPILE_TIME_DEBUG && m_debug) { Serial.println(); } + if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print("t="); Serial.print(t); Serial.print(", "); } + if (COMPILE_TIME_DEBUG && getDebug()) { Serial.println(); } setTemperature(i, t); } - if (COMPILE_TIME_DEBUG && m_debug) { Serial.print("timeOuts="); Serial.print(m_timeOutFailureCnt); Serial.print(", "); } - if (COMPILE_TIME_DEBUG && m_debug) { Serial.print("cycles="); Serial.print(m_cylceCnt); Serial.print(", "); } - if (COMPILE_TIME_DEBUG && m_debug) { Serial.println(); } + if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print("timeOuts="); Serial.print(m_timeOutFailureCnt); Serial.print(", "); } + if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print("cycles="); Serial.print(m_cylceCnt); Serial.print(", "); } + if (COMPILE_TIME_DEBUG && getDebug()) { Serial.println(); } - if (COMPILE_TIME_DEBUG && m_debug) { Serial.println("Switching to state 11"); } + if (COMPILE_TIME_DEBUG && getDebug()) { Serial.println("Switching to state 11"); } state = 11; break; diff --git a/thermometer.h b/thermometer.h index a6048f8..096cac6 100644 --- a/thermometer.h +++ b/thermometer.h @@ -98,6 +98,10 @@ private: float getAlpha(); void setCalibrateFactor(unsigned int i, float c); float getCalibrateFactor(unsigned int i); + void setDebug(bool b); + bool getDebug(); + void setInfo(bool b); + bool getInfo(); };