#include "stm32f0xx_hal.h"
#include "ssd1306.h"
#include "stringhelpers.h"
#include "display.h"
#include "config.h"
#include "states.h"
#include "syslib.h"
#include "flash.h"
#include "gpio.h"
static uint8_t goto_mode = 2;
// Private function prototypes
static void draw_setpoint(therm_status_t* status);
// State machine
// Button transition variables
static uint8_t sw_btn_last = 0;
static uint8_t sw_up_last = 0;
static uint8_t sw_down_last = 0;
static uint8_t sw_left_last = 0;
static uint8_t sw_right_last = 0;
// Buttonpress macros
#define SW_BTN_PRESSED (sw_btn_last == 0 && sw_btn == 1) // rising edge on buttonpress
#define SW_UP_PRESSED (sw_up_last == 0 && sw_up == 1)
#define SW_DOWN_PRESSED (sw_down_last == 0 && sw_down == 1)
#define SW_LEFT_PRESSED (sw_left_last == 0 && sw_left == 1)
#define SW_RIGHT_PRESSED (sw_right_last == 0 && sw_right == 1)
// States
static uint8_t trigger_drawsetpoint = 1;
static int16_t last_temp = 21245;
// Display state machine
void display_process(therm_settings_t* set, therm_status_t* status)
{
uint8_t last_state = status->state;
uint8_t temp_changed = status->temp != last_temp;
last_temp = status->temp;
uint8_t sw_btn = !HAL_GPIO_ReadPin(SW_BTN);
uint8_t sw_up = !HAL_GPIO_ReadPin(SW_UP);
uint8_t sw_down = !HAL_GPIO_ReadPin(SW_DOWN);
uint8_t sw_left = !HAL_GPIO_ReadPin(SW_LEFT);
uint8_t sw_right = !HAL_GPIO_ReadPin(SW_RIGHT);
switch(status->state)
// Idle state
case STATE_IDLE:
// Write text to OLED
// [ therm :: idle ]
ssd1306_drawstring("therm :: idle ", 0, 40);
status->pid_enabled = 0;
if(temp_changed) {
@@ -75,61 +83,61 @@ void display_process(therm_settings_t* s
case 1:
ssd1306_drawstring("-> setup ", 1, 40);
} break;
case 0:
ssd1306_drawstring("-> reset ", 1, 40);
}
// Button handler
if(SW_BTN_PRESSED) {
switch(goto_mode) {
case 3:
ssd1306_clearscreen();
ssd1306_drawstring("Bootloader Entered", 0, 0);
ssd1306_drawstring("Device won't boot", 2, 0);
ssd1306_drawstring("until reflashed!", 3, 0);
bootloader_enter(); // Resets into bootloader
status->state = STATE_IDLE; // Just in case
case 2:
status->state = STATE_PREHEAT_BREW;
status->state = STATE_PREHEAT;
break;
status->state = STATE_SETP;
status->state = STATE_IDLE;
//flash_erase();
NVIC_SystemReset();
default:
else if(SW_UP_PRESSED && goto_mode < 3) {
goto_mode++;
else if(SW_DOWN_PRESSED && goto_mode > 0) {
goto_mode--;
// Event Handler
// N/A
case STATE_SETP:
// [ therm :: set p ]
// [ p = 12 ]
ssd1306_drawstring("Proportional", 0, 40);
ssd1306_drawlogo();
char tempstr[6];
@@ -315,149 +323,100 @@ void display_process(therm_settings_t* s
itoa(set->val.temp_offset, tempstr, 10);
ssd1306_drawstring("O=", 1, 45);
ssd1306_drawstring(" ", 1, 57);
ssd1306_drawstring(tempstr, 1, 57);
ssd1306_drawstring("Press to accept", 3, 40);
flash_save(&set);
else {
user_input_signed(&set->val.temp_offset);
case STATE_PREHEAT_BREW:
case STATE_PREHEAT:
// [ therm : preheating brew ]
// [ 30 => 120 C ]
ssd1306_drawstring("Preheating...", 0, 0);
//ssd1306_drawlogo();
draw_setpoint(status);
status->pid_enabled = 1;
status->setpoint = set->val.setpoint_brew;
user_input(&set->val.setpoint_brew);
if(status->temp >= status->setpoint) {
status->state = STATE_MAINTAIN_BREW;
status->state = STATE_MAINTAIN;
case STATE_MAINTAIN_BREW:
case STATE_MAINTAIN:
// [ therm : ready to brew ]
ssd1306_drawstring("Preheated!", 0, 0);
case STATE_PREHEAT_STEAM:
// [ therm : preheating steam ]
status->setpoint = set->val.setpoint_steam;
user_input(&set->val.setpoint_steam);
status->state = STATE_MAINTAIN_STEAM;
case STATE_MAINTAIN_STEAM:
// [ therm : ready to steam ]
ssd1306_drawstring("Ready to Steam!", 0, 0);
// Thermocouple error
case STATE_TC_ERROR:
ssd1306_drawstring("Error: ", 0, 0);
itoa(status->tc_errno, tempstr, 10);
ssd1306_drawstring(tempstr, 0, 57);
if(status->tc_errno == 1)
ssd1306_drawstring(" TC Open Circuit", 1, 0);
else if(status->tc_errno == 4)
ssd1306_drawstring(" TC Short to GND", 1, 0);
else if(status->tc_errno == 8)
ssd1306_drawstring(" TC Short to VCC", 1, 0);
else
ssd1306_drawstring("#?, Unknown Error", 1, 0);
ssd1306_drawstring(" ", 2, 0);
ssd1306_drawstring("-> to ignore all or", 2, 0);
ssd1306_drawstring("press to continue", 3, 0);
@@ -482,49 +441,51 @@ void display_process(therm_settings_t* s
if(last_state != status->state) {
// Clear screen on state change
goto_mode = 2;
trigger_drawsetpoint = 1;
// Last buttonpress
sw_btn_last = sw_btn;
sw_up_last = sw_up;
sw_down_last = sw_down;
sw_left_last = sw_left;
sw_right_last = sw_right;
static int32_t temp_last = 43002;
static int32_t setpoint_last = 10023;
void draw_setpoint(therm_status_t* status) {
// Draw current setpoint on display
static void draw_setpoint(therm_status_t* status) {
// FIXME: need to do this when switching modes too
if(status->temp != temp_last || trigger_drawsetpoint) {
char tempstr[3];
itoa_fp(status->temp, status->temp_frac, tempstr);
ssd1306_drawstringbig(" ", 3, 0);
ssd1306_drawstringbig(tempstr, 3, 0);
if(trigger_drawsetpoint)
ssd1306_drawstringbig(">", 3, 74);
if(status->setpoint != setpoint_last || trigger_drawsetpoint) {
itoa(status->setpoint, tempstr, 10);
ssd1306_drawstringbig(" ", 3, 90);
ssd1306_drawstringbig(tempstr, 3, 90);
trigger_drawsetpoint = 0;
setpoint_last = status->setpoint;
temp_last = status->temp;
// vim:softtabstop=4 shiftwidth=4 expandtab
#ifndef DISPLAY_H
#define DISPLAY_H
void display_process(therm_settings_t* set, therm_status_t* status);
void draw_setpoint(therm_status_t* status);
#endif
@@ -52,49 +52,49 @@ int main(void)
ssd1306_init();
// Default settings
set.val.boottobrew = 0;
set.val.temp_units = TEMP_UNITS_FAHRENHEIT;
set.val.windup_guard = 200;
set.val.k_p = 10;
set.val.k_i = 10;
set.val.k_d = 1;
set.val.ignore_tc_error = 0;
set.val.setpoint_brew = 70;
set.val.setpoint_steam = 70;
// Default status
status.temp = 0;
status.temp_frac = 0;
status.state_resume = 0;
status.state = STATE_IDLE;
status.setpoint = 70;
status.pid_enabled = 0;
// Go to brew instead of idle if configured thusly
if(set.val.boottobrew)
status.state = STATE_PREHEAT_BREW;
status.state = STATE_PREHEAT;
// Startup screen
ssd1306_drawstring("therm v0.2", 1, 40);
ssd1306_drawstring("protofusion.org/therm", 3, 0);
HAL_Delay(1000);
// Restore settings from flash memory
flash_restore(&set);
// Soft timers
uint32_t last_ssr_on = 0;
uint32_t last_vcp_tx = 0;
uint32_t last_led = 0;
uint32_t last_pid = 0;
int16_t ssr_output = 0; // Duty cycle of ssr, 0 to SSR_PERIOD
// Main loop
while(1)
// Process sensor inputs
@@ -25,33 +25,31 @@ typedef union
int32_t setpoint_brew;
int32_t setpoint_steam;
} val;
uint16_t data[128];
} therm_settings_t;
enum tempunits {
TEMP_UNITS_CELSIUS = 0,
TEMP_UNITS_FAHRENHEIT,
};
enum state {
STATE_IDLE = 0,
STATE_SETP,
STATE_SETI,
STATE_SETD,
STATE_SETSTEPS,
STATE_SETWINDUP,
STATE_SETBOOTTOBREW,
STATE_SETUNITS,
STATE_SETTEMPOFFSET,
STATE_PREHEAT_BREW,
STATE_MAINTAIN_BREW,
STATE_PREHEAT_STEAM,
STATE_MAINTAIN_STEAM,
STATE_PREHEAT,
STATE_MAINTAIN,
STATE_TC_ERROR
STATE_TC_ERROR,
Status change: