diff --git a/src/thermostat.c b/src/thermostat.c --- a/src/thermostat.c +++ b/src/thermostat.c @@ -3,8 +3,10 @@ // #include "thermostat.h" +#include "gpio.h" -// PID implementation +// Thermostatic control implementation +static uint8_t thermostat_plant_on = 0; float thermostat_process(void) { @@ -15,23 +17,28 @@ float thermostat_process(void) // max31855_readtemp(spi_get(), &set, &status); // Read MAX31855 // #endif - float ssr_output = 0.0; - uint8_t thermostat_plant_on = 0; // TODO: Migrate this FxP conversion to the readtemp code or similar - float temp = runtime_status()->temp; - + float temp = runtime_status()->temp; // (scale to whole degrees) + uint32_t hyst = flash_getsettings()->val.hysteresis; // EMZ FIXME: This could be way simpler - if(flash_getsettings()->val.plant_type == PLANT_HEATER && runtime_status()->setpoint * 10 < temp - flash_getsettings()->val.hysteresis * 10) - thermostat_plant_on = 1; - else if(flash_getsettings()->val.plant_type == PLANT_HEATER && runtime_status()->setpoint * 10 > temp + flash_getsettings()->val.hysteresis * 10) - thermostat_plant_on = 0; - if(flash_getsettings()->val.plant_type == PLANT_COOLER && runtime_status()->setpoint * 10 > temp + flash_getsettings()->val.hysteresis * 10) - thermostat_plant_on = 1; - else if(flash_getsettings()->val.plant_type == PLANT_COOLER && runtime_status()->setpoint * 10 < temp - flash_getsettings()->val.hysteresis * 10) - thermostat_plant_on = 0; + if(flash_getsettings()->val.plant_type == PLANT_HEATER) + { + if(temp < runtime_status()->setpoint - hyst) + thermostat_plant_on = 1; + if(temp > runtime_status()->setpoint + hyst) + thermostat_plant_on = 0; + } + + if(flash_getsettings()->val.plant_type == PLANT_COOLER) + { + if(temp < runtime_status()->setpoint - hyst) + thermostat_plant_on = 0; + if(temp > runtime_status()->setpoint + hyst) + thermostat_plant_on = 1; + } // EMZ: TODO: Refactor to output_enabled or something if(runtime_status()->pid_enabled && thermostat_plant_on) @@ -40,21 +47,24 @@ float thermostat_process(void) // put ssr output on display ssd1306_drawstring(" ", 0, 90); //fixme: this is bad, but I can't get the old digits to clear otherwise char tempstr[6]; - itoa(ssr_output, tempstr, 10); + itoa(thermostat_plant_on, tempstr, 10); ssd1306_drawstring(tempstr, 0, 90); - // HAL_GPIO_WritePin(SSR_PIN, 1); + HAL_GPIO_WritePin(SSR, 1); HAL_GPIO_WritePin(LED, 1); } else { - // HAL_GPIO_WritePin(SSR_PIN, 0); - HAL_GPIO_WritePin(LED, 0); + HAL_GPIO_WritePin(SSR, 0); + HAL_GPIO_WritePin(LED, 0); } - return ssr_output; + if(thermostat_plant_on) + return 1000.0f; + else + return 0.0f; }