From 5ec4a5441a67b2387bbb1731d3835d142875d300 Mon Sep 17 00:00:00 2001 From: BlackMark Date: Fri, 12 Feb 2021 16:38:41 +0100 Subject: [PATCH] Implement setting fan speed manually --- fantemp/controller.cpp | 5 ++++- fantemp/controller.hpp | 1 + fantemp/terminal.hpp | 47 ++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/fantemp/controller.cpp b/fantemp/controller.cpp index 015687b..bfa8bda 100644 --- a/fantemp/controller.cpp +++ b/fantemp/controller.cpp @@ -8,6 +8,7 @@ double Controller::m_resistance; double Controller::m_temperature; uint8_t Controller::m_fanSpeed; bool Controller::m_dataAvailable = false; +bool Controller::m_autoMode = true; volatile uint32_t Controller::m_adcSampleSum; volatile bool Controller::m_adcSampleReady = false; @@ -28,7 +29,9 @@ void Controller::callback() m_resistance = m_thermistor.getResistance(m_adcSample); m_temperature = m_thermistor.getTemperature(m_resistance); - m_fanSpeed = mapTemperature(m_temperature); + + if (m_autoMode) + m_fanSpeed = mapTemperature(m_temperature); pwm::setDuty(m_fanSpeed); } diff --git a/fantemp/controller.hpp b/fantemp/controller.hpp index fd6d78b..f4785c1 100644 --- a/fantemp/controller.hpp +++ b/fantemp/controller.hpp @@ -13,6 +13,7 @@ class Controller { static double m_temperature; static uint8_t m_fanSpeed; static bool m_dataAvailable; + static bool m_autoMode; static void init(); diff --git a/fantemp/terminal.hpp b/fantemp/terminal.hpp index 2f92453..430f0d4 100644 --- a/fantemp/terminal.hpp +++ b/fantemp/terminal.hpp @@ -22,11 +22,13 @@ GF(MONITOR_CMD, "monitor"); GF(BOOTLOADER_CMD, "bootloader"); GF(UPTIME_CMD, "uptime"); GF(STATISTICS_CMD, "statistics"); -GF(RESET_CMD, "reset"); GF(HISTOGRAM_CMD, "histogram"); +GF(RESET_CMD, "reset"); +GF(SET_CMD, "set"); +GF(AUTO_CMD, "auto"); GF(VERSION_CMD, "version"); -GF(VERSION, "1.7"); +GF(VERSION, "1.8"); static inline bool substringEquals(const char *str, const ::detail::FlashString *flashStr, const size_t &size) { @@ -128,6 +130,26 @@ class Terminal { return false; } + static uint8_t parseFanSpeed() + { + const auto setCmdLen = strlen_P(reinterpret_cast(detail::SET_CMD)); + + if (m_inputSize > setCmdLen && substringEquals(m_inputBuffer, detail::SET_CMD, setCmdLen) && + m_inputSize < INPUT_BUFFER_SIZE) { + m_inputBuffer[m_inputSize] = '\0'; // Null terminate to be parsable by stdlib + const auto *fanSpeedStr = m_inputBuffer + setCmdLen; + auto *fanSpeedStrEnd = m_inputBuffer + setCmdLen; + const auto fanSpeed = strtol(fanSpeedStr, &fanSpeedStrEnd, 10); + + if (fanSpeedStrEnd != fanSpeedStr && *fanSpeedStr != '\0' && *fanSpeedStrEnd == '\0') { + if (fanSpeed >= 0 && fanSpeed <= 100) + return static_cast(fanSpeed); + } + } + + return 0xFF; + } + static void parseInput() { if (m_inputSize) { @@ -152,6 +174,10 @@ class Terminal { printHistogram(); } else if (stringEquals(m_inputBuffer, detail::RESET_CMD, m_inputSize)) { handleReset(); + } else if (uint8_t targetFanSpeed = parseFanSpeed(); targetFanSpeed <= 100) { + handleSet(targetFanSpeed); + } else if (substringEquals(m_inputBuffer, detail::AUTO_CMD, m_inputSize)) { + handleAuto(); } else if (substringEquals(m_inputBuffer, detail::VERSION_CMD, m_inputSize)) { printVersion(); } else { @@ -183,6 +209,8 @@ class Terminal { m_serial << detail::STATISTICS_CMD << F(" .: prints overall statistics like min and max temp") << detail::ENDL; m_serial << detail::HISTOGRAM_CMD << F(" ..: prints a histogram of the temperature") << detail::ENDL; m_serial << detail::RESET_CMD << F(" ......: resets statistics to 0 in EEPROM and RAM") << detail::ENDL; + m_serial << detail::SET_CMD << F(" ........: sets the fan speed to the provided value") << detail::ENDL; + m_serial << detail::AUTO_CMD << F(" .......: turns on automatic fan control") << detail::ENDL; m_serial << detail::VERSION_CMD << F(" ....: displays firmware version") << detail::ENDL; } @@ -306,6 +334,21 @@ class Terminal { m_serial << F("Reset statistics") << detail::ENDL; } + static void handleSet(uint8_t targetFanSpeed) + { + m_serial << F("Setting fan speed to "); + m_serial.txNumber(targetFanSpeed); + m_serial << detail::ENDL; + Controller::m_autoMode = false; + Controller::m_fanSpeed = targetFanSpeed; + } + + static void handleAuto() + { + m_serial << F("Turning on automatic fan control") << detail::ENDL; + Controller::m_autoMode = true; + } + static void printVersion() { m_serial << F("FanTemp v") << detail::VERSION << detail::ENDL;