Files @ c1b2840961f0
Branch filter:

Location: therm-ng/src/thermostat.c

Ethan Zonca <ethanzonca>
Hacking thermostatic mode into operation. This code needs some love.
//
// Thermostat: Thermostatic controller
//

#include "thermostat.h"
#include "gpio.h"

// Thermostatic control implementation
static uint8_t thermostat_plant_on = 0;

float thermostat_process(void)
{

    //            #ifdef MAX31865_RTD_SENSOR
    //            max31865_readtemp(spi_get(), &set, &status);
    //			#else
    //			max31855_readtemp(spi_get(), &set, &status); // Read MAX31855
    //			#endif


    // TODO: Migrate this FxP conversion to the readtemp code or similar
    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)
    {
    	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)
    {
        // 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(thermostat_plant_on, tempstr, 10);
        ssd1306_drawstring(tempstr, 0, 90);



		HAL_GPIO_WritePin(SSR, 1);
        HAL_GPIO_WritePin(LED, 1);
    }
    else
    {
		HAL_GPIO_WritePin(SSR, 0);
		HAL_GPIO_WritePin(LED, 0);
    }

    if(thermostat_plant_on)
    	return 1000.0f;
    else
    	return 0.0f;
}