#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);
@@ -87,37 +95,37 @@ void display_process(therm_settings_t* s
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
} break;
case 2:
status->state = STATE_PREHEAT_BREW;
status->state = STATE_PREHEAT;
break;
case 1:
status->state = STATE_SETP;
case 0:
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
@@ -327,125 +335,76 @@ void display_process(therm_settings_t* s
else {
user_input_signed(&set->val.temp_offset);
case STATE_PREHEAT_BREW:
case STATE_PREHEAT:
// Write text to OLED
// [ 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;
// Button handler
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);
char tempstr[6];
itoa(status->tc_errno, tempstr, 10);
ssd1306_drawstring(tempstr, 0, 57);
if(status->tc_errno == 1)
@@ -494,25 +453,27 @@ void display_process(therm_settings_t* s
// 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) {
#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
@@ -64,25 +64,25 @@ int main(void)
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);
@@ -37,21 +37,19 @@ enum tempunits {
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: