From 226d1ba6c9af427d6abeb5c3dec058ff6d5e7bcf Mon Sep 17 00:00:00 2001 From: BlackMark Date: Wed, 1 Apr 2020 03:11:03 +0200 Subject: [PATCH] Refactor code from main to controller class --- fantemp/controller.cpp | 36 ++++++++++++++++++++++++ fantemp/controller.hpp | 39 ++++++++++++++++++++++++++ fantemp/fantemp.cppproj | 6 ++++ fantemp/main.cpp | 61 ++++------------------------------------- fantemp/terminal.hpp | 33 +++++++++++++++++++--- 5 files changed, 116 insertions(+), 59 deletions(-) create mode 100644 fantemp/controller.cpp create mode 100644 fantemp/controller.hpp diff --git a/fantemp/controller.cpp b/fantemp/controller.cpp new file mode 100644 index 0000000..40b4f83 --- /dev/null +++ b/fantemp/controller.cpp @@ -0,0 +1,36 @@ +#include "controller.hpp" + +#define ADC_INT_VECTOR +#include "adc/adc.hpp" + +double Controller::m_adcSample; +double Controller::m_resistance; +double Controller::m_temperature; +uint8_t Controller::m_fanSpeed; + +void Controller::init() +{ + m_adcPin.init(); + pwm::init(); + pwm::setDuty(100); +} + +void Controller::callback() +{ + sample(); + mapTemperature(); + pwm::setDuty(m_fanSpeed); +} + +void Controller::sample() +{ + m_adcSample = m_thermistor.sampleAdc(m_adcPin, 1000); + m_resistance = m_thermistor.getResistance(m_adcSample); + m_temperature = m_thermistor.getTemperature(m_resistance); +} + +void Controller::mapTemperature() +{ + double fanSpeed = (10 * m_temperature - 200) / 3; + m_fanSpeed = clamp(fanSpeed, 0, 100); +} diff --git a/fantemp/controller.hpp b/fantemp/controller.hpp new file mode 100644 index 0000000..74dd7db --- /dev/null +++ b/fantemp/controller.hpp @@ -0,0 +1,39 @@ +#pragma once + +#include "adc/adc.hpp" +#include "io/io.hpp" + +#include "pwm.hpp" +#include "thermistor.hpp" + +class Controller { + public: + static double m_adcSample; + static double m_resistance; + static double m_temperature; + static uint8_t m_fanSpeed; + + static void init(); + + static void callback(); + + private: + using adc_conf = adc::Config; + static adc::Adc m_adcPin; + + static Thermistor m_thermistor; + + static void sample(); + + template + static T clamp(double value, T lower, T upper) + { + if (value < lower) + return lower; + if (value > upper) + return upper; + return static_cast(value); + } + + static void mapTemperature(); +}; diff --git a/fantemp/fantemp.cppproj b/fantemp/fantemp.cppproj index 35bbaeb..ae268db 100644 --- a/fantemp/fantemp.cppproj +++ b/fantemp/fantemp.cppproj @@ -203,6 +203,12 @@ compile + + compile + + + compile + compile diff --git a/fantemp/main.cpp b/fantemp/main.cpp index ec0f3f6..d37fafa 100644 --- a/fantemp/main.cpp +++ b/fantemp/main.cpp @@ -1,75 +1,26 @@ #include "clock.hpp" -#include - -#include "flash/flash.hpp" -#include "io/io.hpp" #include "uart/uart.hpp" -#define ADC_INT_VECTOR -#include "adc/adc.hpp" - #define UART0_INT_VECTORS #include "uart/hardware0.hpp" -#include "pwm.hpp" +#include "controller.hpp" #include "terminal.hpp" -#include "thermistor.hpp" - -template -T clamp(double value, T lower, T upper) -{ - if (value < lower) - return lower; - if (value > upper) - return upper; - return static_cast(value); -} - -uint8_t temperatureCurve(double temperature) -{ - double fanSpeed = (10 * temperature - 200) / 3; - return clamp(fanSpeed, 0, 100); -} int main() { - uart::Uart0> serial; - Terminal terminal; + using serial = uart::Uart0>; + Terminal terminal; terminal.init(); - using adc_conf = adc::Config; - - adc::Adc adcPin; - adcPin.init(); - - Thermistor temp; - - pwm::init(); - pwm::setDuty(100); + Controller controller; + controller.init(); while (true) { - const auto adcSample = temp.sampleAdc(adcPin, 1000); - const auto fantempResistance = temp.getResistance(adcSample); - const auto temperature = temp.getTemperature(fantempResistance); - const auto fanSpeed = temperatureCurve(temperature); - - /*char floatBuffer[16]; - - sprintf(floatBuffer, "%.2f", adcSample); - serial << F("Read ADC: ") << floatBuffer << F("\r\n"); - sprintf(floatBuffer, "%.2f", fantempResistance); - serial << F("Resistance: ") << floatBuffer << F("\r\n"); - sprintf(floatBuffer, "%.2f", temperature); - serial << F("Temperature: ") << floatBuffer << F(" C \r\n"); - serial << F("Fan speed: ") << fanSpeed << F("%\r\n");*/ - - pwm::setDuty(fanSpeed); - + controller.callback(); terminal.callback(); } - serial.flushTx(); - return 0; } diff --git a/fantemp/terminal.hpp b/fantemp/terminal.hpp index a0fd3a6..33208f1 100644 --- a/fantemp/terminal.hpp +++ b/fantemp/terminal.hpp @@ -2,12 +2,17 @@ #include #include +#include #include #include "flash/flash.hpp" +#include "controller.hpp" + #define ENDL F("\r\n") +#define HELP_CMD F("help") +#define SHOW_CMD F("show") template class Terminal { @@ -79,15 +84,33 @@ class Terminal { static void parseInput() { - if (m_inputSize && strncmp_P(m_inputBuffer, reinterpret_cast(F("help")), m_inputSize) == 0) { - printHelp(); + if (m_inputSize) { + if (strncmp_P(m_inputBuffer, reinterpret_cast(HELP_CMD), m_inputSize) == 0) { + printHelp(); + } else if (strncmp_P(m_inputBuffer, reinterpret_cast(SHOW_CMD), m_inputSize) == 0) { + showState(); + } } } static void printHelp() { - m_serial << F("Help: ") << ENDL; - m_serial << F("help .: print this help message") << ENDL; + m_serial << F("FanTemp command overview: ") << ENDL; + m_serial << HELP_CMD << F(" .: print this help message") << ENDL; + m_serial << SHOW_CMD << F(" .: shows current temperature and fan speed") << ENDL; + } + + static void showState() + { + char floatBuffer[16]; + + sprintf(floatBuffer, "%.2f", Controller::m_adcSample); + m_serial << F("Read ADC: ") << floatBuffer << F("\r\n"); + sprintf(floatBuffer, "%.2f", Controller::m_resistance); + m_serial << F("Resistance: ") << floatBuffer << F("\r\n"); + sprintf(floatBuffer, "%.2f", Controller::m_temperature); + m_serial << F("Temperature: ") << floatBuffer << F(" C \r\n"); + m_serial << F("Fan speed: ") << Controller::m_fanSpeed << F("%\r\n"); } }; @@ -98,3 +121,5 @@ template uint16_t Terminal::m_inputSize = 0; #undef ENDL +#undef HELP_CMD +#undef SHOW_CMD