diff --git a/thermometer.cpp b/thermometer.cpp index e033270..493fe39 100644 --- a/thermometer.cpp +++ b/thermometer.cpp @@ -2,7 +2,6 @@ #include "thermometer.h" #include "fatal.h" #include "AD7190.h" -#include "Streaming.h" #include "Config.h" #include "Resources.h" @@ -62,7 +61,7 @@ String ThermValues::exec(String params) { m_stream->print(getResource(ALPHA_2_SPACES_KEY)); m_stream->print(m_thermometer->getAlpha()); m_stream->println(); for (unsigned int i = 0; i < NUM_OF_CHANNELS; i++) { - m_stream->print(getResource(INDEX_KEY)); m_stream->print(i); m_stream->print(", "); + m_stream->print(getResource(INDEX_KEY)); m_stream->print(i); m_stream->print(getResource(COMMA_SPACE_KEY)); m_stream->print(getResource(CAL_KEY)); m_stream->print(m_thermometer->getCalibrateFactor(i), 6); m_stream->print(getResource(COMMA_SPACE_KEY)); m_stream->print(getResource(T_KEY)); m_stream->print(m_thermometer->m_temperature[i]); m_stream->print(getResource(COMMA_SPACE_KEY)); m_stream->print(getResource(TS_KEY)); m_stream->print(m_thermometer->m_smoothedTemperature[i]); @@ -92,12 +91,12 @@ String ThermCalibrate::exec(String params) { } else if (params.startsWith("disable")) { m_enabled = false; } else if (params.startsWith("help")) { - *m_stream << getResource(CALI_HELP_1_KEY) << endl; - *m_stream << getResource(CALI_HELP_2_KEY) << endl; - *m_stream << getResource(CALI_HELP_3_KEY) << endl; - *m_stream << getResource(CALI_HELP_4_KEY) << endl; - *m_stream << getResource(CALI_HELP_5_KEY) << endl; - *m_stream << getResource(CALI_HELP_6_KEY) << endl; + m_stream->println(getResource(CALI_HELP_1_KEY)); + m_stream->println(getResource(CALI_HELP_2_KEY)); + m_stream->println(getResource(CALI_HELP_3_KEY)); + m_stream->println(getResource(CALI_HELP_4_KEY)); + m_stream->println(getResource(CALI_HELP_5_KEY)); + m_stream->println(getResource(CALI_HELP_6_KEY)); } else if (params.startsWith("start ") && (space != -1) && m_enabled) { m_channel = atoi(pb1); if ((m_channel + 1) > NUM_OF_CHANNELS) { @@ -110,15 +109,15 @@ String ThermCalibrate::exec(String params) { } else if (params.startsWith("r ") && (space != -1)) { m_r_cal = atof(pb1); } else if (params.equalsIgnoreCase("show")) { - *m_stream << getResource(CALI_SHOW_1_KEY) << m_enabled << endl; - *m_stream << getResource(CALI_SHOW_2_KEY) << m_r_cal << endl; + m_stream->print(getResource(CALI_SHOW_1_KEY)); m_stream->print(m_enabled); m_stream->println(); + m_stream->print(getResource(CALI_SHOW_2_KEY)); m_stream->print(m_r_cal); m_stream->println(); } else { res = "subcommand not found"; } return res; } -Thermometer::Thermometer() : m_period(DEFAULT_PERIOD), m_periodMillis(DEFAULT_PERIOD), +Thermometer::Thermometer() : m_periodMillis(DEFAULT_PERIOD), m_debug(DEBUG), m_info(INFO), thermConfig(this), thermValues(this), thermCalibrate(this), m_timeOutFailureCnt(0), m_cylceCnt(0), m_alpha(1.0), @@ -134,7 +133,6 @@ Thermometer::Thermometer() : m_period(DEFAULT_PERIOD), m_periodMillis(DEFAULT_PE void Thermometer::setPeriodMeasure(unsigned long p) { Config::setULong(Config::THERMOMETER_PERIOD, p); m_periodMillis = p; - m_period = Metro(p); } unsigned long Thermometer::getPeriodMeasure() { @@ -256,7 +254,7 @@ void Thermometer::begin(CmdServer *cmdServer) { if (! Config::isInitialized()) { - Serial << getResource(THERMOMETER_BEGIN_1_KEY) << endl; + Serial.println(getResource(THERMOMETER_BEGIN_1_KEY)); Config::setFloat(Config::THERMOMETER_ALPHA, 1.0); Config::setULong(Config::THERMOMETER_PERIOD, 1000); for (int i = 0; i < 4; i++) { @@ -293,6 +291,9 @@ void Thermometer::exec() { float r; static unsigned int channelCnt; + static unsigned long lastMillis = 0; + unsigned long currentMillis; + switch (state) { case 0: @@ -344,7 +345,7 @@ void Thermometer::exec() { m_timeOutFailureCnt++; if (getDebug() || getInfo()) { - Serial << getResource(STATE_9_DEBUG_1) << m_timeOutFailureCnt << endl; + Serial.print(getResource(STATE_9_DEBUG_1)); Serial.print(m_timeOutFailureCnt); Serial.println(); } state = 0; @@ -356,13 +357,13 @@ void Thermometer::exec() { SPI_Disable(AD7190_SLAVE_ID); if (m_calibrationMode || thermCalibrate.m_start_calibration) { - Serial << getResource(CALIBRATION_MODE_HINT_KEY) << endl; + Serial.println(getResource(CALIBRATION_MODE_HINT_KEY)); state = 20; } else { for (unsigned int i = 0; i < NUM_OF_CHANNELS; i++) { if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print("i="); Serial.print(i); Serial.print(getResource(COMMA_SPACE_KEY)); } if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print("m_n="); Serial.print(m_n[i]); Serial.print(getResource(COMMA_SPACE_KEY)); } - if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print(""); Serial.print((m_n[i] - ((i == 3) ? 0 : m_n[i + 1]))); Serial.print(getResource(COMMA_SPACE_KEY)); } + if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print((m_n[i] - ((i == 3) ? 0 : m_n[i + 1]))); Serial.print(getResource(COMMA_SPACE_KEY)); } if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print("calibrateFactor="); Serial.print(getCalibrateFactor(i), 6); Serial.print(getResource(COMMA_SPACE_KEY)); } float r = (((float)(m_n[i] - ((i == 3) ? 0 : m_n[i + 1]))) / ((float)N_MAX)) * R_REF * getCalibrateFactor(i); if (COMPILE_TIME_DEBUG && getDebug()) { Serial.print("r="); Serial.print(r); Serial.print(getResource(COMMA_SPACE_KEY)); } @@ -382,8 +383,11 @@ void Thermometer::exec() { break; case 11: - if (m_period.check() == 1) + currentMillis = millis(); + if (currentMillis >= (lastMillis + getPeriodMeasure())) { + lastMillis = currentMillis; state = 0; + } break; case 20: @@ -392,22 +396,22 @@ void Thermometer::exec() { // do the calibration stuff for (unsigned int i = 0; i < NUM_OF_CHANNELS; i++) { float r = (((float)(m_n[i] - ((i == 3) ? 0 : m_n[i + 1]))) / ((float)N_MAX)) * R_REF; - Serial << "t=" << thermCalibrate.m_turn << ", c=" << i << ", r=" << _FLOAT(r, 6) << ", n=" << m_n[i] << endl; + Serial.print("t="); Serial.print(thermCalibrate.m_turn); Serial.print(", c="); Serial.print(i); + Serial.print(", r="); Serial.print(r, 6); Serial.print(", n="); Serial.print(m_n[i]); if (i == thermCalibrate.m_channel) { thermCalibrate.m_r_sum += r; } } (thermCalibrate.m_turn)++; float r_avg = thermCalibrate.m_r_sum / thermCalibrate.m_turn; - Serial << getResource(STATE_20_MSG_1_KEY) << thermCalibrate.m_channel << ": " << _FLOAT(r_avg, 6) << endl; + Serial.print(getResource(STATE_20_MSG_1_KEY)); Serial.print(thermCalibrate.m_channel); Serial.print(getResource(COLON_SPACE_KEY)); Serial.print(r_avg, 6); float calFact = thermCalibrate.m_r_cal / r_avg; - Serial << getResource(STATE_20_MSG_2_KEY) << _FLOAT(calFact, 6) << endl; + Serial.print(getResource(STATE_20_MSG_2_KEY)); Serial.print(calFact, 6); Serial.println(); } else { thermCalibrate.m_preserved_alpha = m_alpha; m_alpha = 1; thermCalibrate.m_preserved_period = m_periodMillis; m_periodMillis = 500; - m_period = Metro(m_periodMillis); thermCalibrate.m_r_sum = 0.0; thermCalibrate.m_turn = 0; m_calibrationMode = true; @@ -417,11 +421,11 @@ void Thermometer::exec() { m_calibrationMode = false; m_alpha = thermCalibrate.m_preserved_alpha; m_periodMillis = thermCalibrate.m_preserved_period; - m_period = Metro(m_periodMillis); - Serial << getResource(STATE_20_MSG_3_KEY) << endl; + Serial.println(getResource(STATE_20_MSG_3_KEY)); float r_avg = thermCalibrate.m_r_sum / thermCalibrate.m_turn; float calFact = thermCalibrate.m_r_cal / r_avg; - Serial << getResource(STATE_20_MSG_4_KEY) << thermCalibrate.m_channel << getResource(COLON_SPACE_KEY) << _FLOAT(calFact, 6) << endl; + Serial.print(getResource(STATE_20_MSG_4_KEY)); Serial.print(thermCalibrate.m_channel); + Serial.print(getResource(COLON_SPACE_KEY)); Serial.print(calFact, 6); Serial.println(); setCalibrateFactor(thermCalibrate.m_channel, calFact); } diff --git a/thermometer.h b/thermometer.h index 8e14b80..86aec88 100644 --- a/thermometer.h +++ b/thermometer.h @@ -8,7 +8,6 @@ #ifndef THERMOMETER_H_ #define THERMOMETER_H_ -#include #include "cmd.h" #include "Resources.h" @@ -75,7 +74,6 @@ public: friend class ThermValues; private: - Metro m_period; unsigned long m_periodMillis; ThermConfig thermConfig; ThermValues thermValues;