//
// Thermostat: Thermostatic controller
//
#include "thermostat.h"
#include "flash.h"
#include "gpio.h"
// PID implementation
float thermostat_process(void)
{
// #ifdef MAX31865_RTD_SENSOR
// max31865_readtemp(spi_get(), &set, &status);
// #else
// 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;
// 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;
// EMZ: TODO: Refactor to output_enabled or something
if(runtime_status()->pid_enabled && thermostat_plant_on)
{
// EMZ TODO: functionalize this
// 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);
ssd1306_drawstring(tempstr, 0, 90);
// HAL_GPIO_WritePin(SSR_PIN, 1);
HAL_GPIO_WritePin(LED, 1);
}
else
{
// HAL_GPIO_WritePin(SSR_PIN, 0);
HAL_GPIO_WritePin(LED, 0);
}
return ssr_output;
}