Files
ESP32/Thermostat/src/thermostat.cpp
2026-03-11 12:28:44 +01:00

51 lines
1.6 KiB
C++

#include "thermostat.h"
Thermostat::Thermostat() : mode(MODE_OFF), targetTemp(0), heating(false) {
presetTemps[MODE_CONFORT] = 20.0;
presetTemps[MODE_ECO] = 17.0;
presetTemps[MODE_BOOST] = 22.0;
presetTemps[MODE_HORS_GEL] = 7.0;
presetTemps[MODE_OFF] = 0.0;
}
/**
* Set the thermostat mode. This will automatically update the target temperature to the preset for that mode.
*/
void Thermostat::setMode(ThermostatMode m) {
mode = m;
targetTemp = presetTemps[mode];
}
/**
* Manually set the target temperature. This does not change the mode or presets, but will override the preset for the
* current mode until the mode is changed again.
*/
void Thermostat::setTemperature(float temp) {
targetTemp = temp;
}
/**
* Set the preset temperature for a specific mode. If the current mode is the one being updated, also update the
* target temperature.
*/
void Thermostat::setPresetTemp(ThermostatMode m, float temp) {
presetTemps[m] = temp;
if (mode == m) targetTemp = temp;
}
/**
* Update the heating state based on the current temperature and target temperature.
* this is real simple logic: if current temp is below target, turn on heating, otherwise turn it off. In a real system, you would want to add some hysteresis to prevent rapid on/off cycling.
*/
void Thermostat::update(float currentTemp) {
if (mode == MODE_OFF) {
heating = false;
} else {
heating = (currentTemp < targetTemp);
}
}
bool Thermostat::isHeating() const { return heating; }
ThermostatMode Thermostat::getMode() const { return mode; }
float Thermostat::getTargetTemp() const { return targetTemp; }