Implement setting fan speed manually

This commit is contained in:
BlackMark 2021-02-12 16:38:41 +01:00
parent 7aa98a8ebd
commit 5ec4a5441a
3 changed files with 50 additions and 3 deletions

View File

@ -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);
}

View File

@ -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();

View File

@ -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<const char *>(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<uint8_t>(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;