From ea6a6bd2188aa74acd76253d9cceae692f6bdce7 Mon Sep 17 00:00:00 2001 From: BlackMark Date: Tue, 7 Apr 2020 21:05:40 +0200 Subject: [PATCH] Add check to prevent printing data before it's available --- fantemp/controller.cpp | 3 +++ fantemp/controller.hpp | 1 + fantemp/terminal.hpp | 20 ++++++++++++-------- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/fantemp/controller.cpp b/fantemp/controller.cpp index 46f02f2..015687b 100644 --- a/fantemp/controller.cpp +++ b/fantemp/controller.cpp @@ -7,6 +7,8 @@ double Controller::m_adcSample; double Controller::m_resistance; double Controller::m_temperature; uint8_t Controller::m_fanSpeed; +bool Controller::m_dataAvailable = false; + volatile uint32_t Controller::m_adcSampleSum; volatile bool Controller::m_adcSampleReady = false; @@ -21,6 +23,7 @@ void Controller::callback() { if (m_adcSampleReady) { m_adcSample = static_cast(m_adcSampleSum) / NUM_ADC_SAMPLES; + m_dataAvailable = true; m_adcSampleReady = false; m_resistance = m_thermistor.getResistance(m_adcSample); diff --git a/fantemp/controller.hpp b/fantemp/controller.hpp index bdd1de4..fd6d78b 100644 --- a/fantemp/controller.hpp +++ b/fantemp/controller.hpp @@ -12,6 +12,7 @@ class Controller { static double m_resistance; static double m_temperature; static uint8_t m_fanSpeed; + static bool m_dataAvailable; static void init(); diff --git a/fantemp/terminal.hpp b/fantemp/terminal.hpp index f7c1e91..b5bad56 100644 --- a/fantemp/terminal.hpp +++ b/fantemp/terminal.hpp @@ -175,15 +175,19 @@ class Terminal { static void showState() { - char floatBuffer[16]; + if (Controller::m_dataAvailable) { + char floatBuffer[16]; - sprintf(floatBuffer, "%.2f", Controller::m_adcSample); - m_serial << F("ADC value ...: ") << floatBuffer << F(" / 1023") << detail::ENDL; - sprintf(floatBuffer, "%.2f", Controller::m_resistance); - m_serial << F("Resistance ..: ") << floatBuffer << F(" Ohm") << detail::ENDL; - sprintf(floatBuffer, "%.2f", Controller::m_temperature); - m_serial << F("Temperature .: ") << floatBuffer << F(" C") << detail::ENDL; - m_serial << F("Fan speed ...: ") << Controller::m_fanSpeed << F("%") << detail::ENDL; + sprintf(floatBuffer, "%.2f", Controller::m_adcSample); + m_serial << F("ADC value ...: ") << floatBuffer << F(" / 1023") << detail::ENDL; + sprintf(floatBuffer, "%.2f", Controller::m_resistance); + m_serial << F("Resistance ..: ") << floatBuffer << F(" Ohm") << detail::ENDL; + sprintf(floatBuffer, "%.2f", Controller::m_temperature); + m_serial << F("Temperature .: ") << floatBuffer << F(" C") << detail::ENDL; + m_serial << F("Fan speed ...: ") << Controller::m_fanSpeed << F("%") << detail::ENDL; + } else { + m_serial << F("No data available yet!") << detail::ENDL; + } } static void printCurve()