@@ -43,11 +43,12 @@
#define DEFAULT_K_I 1
#define DEFAULT_K_D 1
#define DEFAULT_TEMP_OFFSET 0
#define DEFAULT_IGNORE_ERROR 0
#define DEFAULT_SETPOINT_BREW 70
#define DEFAULT_SETPOINT_STEAM 70
#define DEFAULT_HYSTERESIS 1
#endif
// vim:softtabstop=4 shiftwidth=4 expandtab
@@ -22,13 +22,13 @@ static uint8_t sw_right_last = 0;
// States
static uint8_t trigger_drawsetpoint = 1;
static int16_t last_temp = 21245;
static int16_t last_temp_frac = 21245;
static int16_t last_state = STATE_IDLE;
static int16_t last_state = 123;
static uint8_t goto_mode = MODE_HEAT;
static uint8_t reset_mode = RESET_REBOOT;
// Display state machine
@@ -187,13 +187,13 @@ void display_process(therm_settings_t* s
// Button handler
if(SW_BTN_PRESSED) {
if(set->val.control_mode == MODE_PID)
status->state = STATE_SETP;
else
status->state = STATE_SETBOOTTOBREW;
status->state = STATE_SETHYSTERESIS;
}
else if (!HAL_GPIO_ReadPin(SW_UP)) {
set->val.plant_type = PLANT_COOLER;
else if(!HAL_GPIO_ReadPin(SW_DOWN)) {
set->val.plant_type = PLANT_HEATER;
@@ -201,12 +201,41 @@ void display_process(therm_settings_t* s
// Event Handler
// N/A
} break;
case STATE_SETHYSTERESIS:
{
// Write text to OLED
ssd1306_drawstring("Hysteresis", 0, 40);
ssd1306_drawlogo();
char tempstr[6];
itoa(set->val.hysteresis, tempstr, 10);
ssd1306_drawstring("H=", 1, 45);
ssd1306_drawstring(" ", 1, 57);
ssd1306_drawstring(tempstr, 1, 57);
ssd1306_drawstring("Press to accept", 3, 40);
else {
user_input((uint16_t*)&set->val.hysteresis);
case STATE_SETP:
// [ therm :: set p ]
// [ p = 12 ]
ssd1306_drawstring("Proportional", 0, 40);
@@ -37,12 +37,13 @@ void flash_load_defaults(therm_settings_
torestore->val.k_i = DEFAULT_K_I;
torestore->val.k_d = DEFAULT_K_D;
torestore->val.temp_offset = DEFAULT_TEMP_OFFSET;
torestore->val.ignore_error = DEFAULT_IGNORE_ERROR;
torestore->val.setpoint_brew = DEFAULT_SETPOINT_BREW;
torestore->val.setpoint_steam = DEFAULT_SETPOINT_STEAM;
torestore->val.hysteresis = DEFAULT_HYSTERESIS;
static void __flash_write(therm_settings_t* tosave)
// Erase mem
HAL_FLASH_Unlock();
@@ -89,12 +89,14 @@ int main(void)
uint32_t last_vcp_tx = 0;
uint32_t last_led = 0;
uint32_t last_pid = 0;
uint32_t last_thermostat = 0;
int16_t ssr_output = 0; // Duty cycle of ssr, 0 to SSR_PERIOD
uint8_t thermostat_plant_on = 0;
// Main loop
while(1)
// Process sensor inputs
if(HAL_GetTick() - last_led > 400)
@@ -177,22 +179,26 @@ int main(void)
// TODO: Migrate this FxP conversion to the readtemp code or similar
int8_t temp_frac = status.temp_frac > 9 ? status.temp_frac / 10 : status.temp_frac;
temp_frac = status.temp > 0 ? temp_frac : temp_frac * -1;
int32_t temp = (status.temp * 10) + temp_frac;
uint8_t plant_on = 0;
// EMZ FIXME: This could be way simpler
if(set.val.plant_type == PLANT_HEATER && status.setpoint * 10 < temp)
plant_on = 1;
else if(set.val.plant_type == PLANT_COOLER && status.setpoint * 10 > temp)
if(set.val.plant_type == PLANT_HEATER && status.setpoint * 10 < temp - set.val.hysteresis * 10)
thermostat_plant_on = 1;
else if(set.val.plant_type == PLANT_HEATER && status.setpoint * 10 > temp + set.val.hysteresis * 10)
thermostat_plant_on = 0;
if(set.val.plant_type == PLANT_COOLER && status.setpoint * 10 > temp + set.val.hysteresis * 10)
else if(set.val.plant_type == PLANT_COOLER && status.setpoint * 10 < temp - set.val.hysteresis * 10)
// EMZ: TODO: Refactor to output_enabled or something
if(status.pid_enabled && plant_on)
if(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
itoa(ssr_output, tempstr, 10);
@@ -26,12 +26,13 @@ typedef union
int32_t temp_offset;
uint32_t ignore_error;
int32_t setpoint_brew;
int32_t setpoint_steam;
uint32_t control_mode;
uint32_t plant_type;
uint32_t hysteresis;
} val;
uint16_t data[128];
} therm_settings_t;
enum tempunits {
@@ -41,12 +42,13 @@ enum tempunits {
enum state {
STATE_IDLE = 0,
STATE_SETMODE,
STATE_SETPLANTTYPE,
STATE_SETHYSTERESIS,
STATE_SETP,
STATE_SETI,
STATE_SETD,
STATE_SETSTEPS,
STATE_SETWINDUP,
STATE_SETBOOTTOBREW,
Status change: