@@ -20,24 +20,34 @@ extern __IO uint32_t length ;
uint8_t Send_Buffer[64];
uint32_t packet_sent=1;
uint32_t packet_receive=1;
// Globalish setting vars
uint8_t boottobrew = 0;
#define WINDUP_GUARD_GAIN 100
uint16_t windup_guard = WINDUP_GUARD_GAIN;
uint16_t k_p = 1;
uint16_t k_i = 1;
uint16_t k_d = 1;
// ISR ticks var TODO: Double check functionality after volatilazation... needed on ARM?
volatile uint32_t ticks = 0;
// Increase on each press, and increase at a fast rate after duration elapsed of continuously holding down... somehow...
uint32_t change_time_reset = 0;
#define CHANGE_PERIOD_MS 100
#define CHANGE_ELAPSED (ticks - change_time_reset) > CHANGE_PERIOD_MS
#define CHANGE_RESET change_time_reset = ticks
int16_t setpoint_brew = 0;
int16_t setpoint_steam = 0;
// State definition
enum state {
STATE_IDLE = 0,
STATE_SETP,
STATE_SETI,
STATE_SETD,
STATE_SETWINDUP,
STATE_SETBOOTTOBREW,
@@ -206,25 +216,24 @@ int16_t update_pid(uint16_t k_p, uint16_
if(result > 1000)
result = 1000;
else if(result < -1000)
result = -1000;
// Return feedback
return result;
}
uint32_t ticks = 0;
uint32_t last_ssr_on = 0;
uint32_t last_led = 0;
int32_t setpoint = 0;
int16_t ssr_output = 0; // Duty cycle of ssr, 0 to SSR_PERIOD
uint8_t pid_enabled = 0;
// Process things
void process()
{
update_temp(); // Read MAX31855
// TODO: Add calibration offset (linear)
@@ -356,24 +365,42 @@ void restore_settings()
while(FLASH_GetStatus()==FLASH_BUSY);
k_d = (*(__IO uint32_t*)EEPROM_ADDR_K_D);
setpoint_brew = (*(__IO uint32_t*)EEPROM_ADDR_BREWTEMP);
setpoint_steam = (*(__IO uint32_t*)EEPROM_ADDR_STEAMTEMP);
DATA_EEPROM_Lock();
void user_input(uint16_t* to_modify)
if(CHANGE_ELAPSED) {
// TODO: Make function that takes reference to a var and increase/decreases it based on buttonpress
if(!GPIO_ReadInputDataBit(SW_UP) ) {
CHANGE_RESET;
(*to_modify)++;
else if(!GPIO_ReadInputDataBit(SW_DOWN) && (*to_modify) > 0) {
(*to_modify)--;
void machine()
uint8_t last_state = state;
uint8_t sw_btn = !GPIO_ReadInputDataBit(SW_BTN);
uint8_t sw_up = !GPIO_ReadInputDataBit(SW_UP);
uint8_t sw_down = !GPIO_ReadInputDataBit(SW_DOWN);
uint8_t sw_left = !GPIO_ReadInputDataBit(SW_LEFT);
uint8_t sw_right = !GPIO_ReadInputDataBit(SW_RIGHT);
switch(state)
@@ -450,29 +477,26 @@ void machine()
char tempstr[6];
itoa(k_p, tempstr);
ssd1306_DrawString("P=", 1, 45);
ssd1306_DrawString(" ", 1, 57);
ssd1306_DrawString(tempstr, 1, 57);
ssd1306_DrawString("Press to accept", 3, 40);
// Button handler
if(SW_BTN_PRESSED) {
state = STATE_SETI;
else if(!GPIO_ReadInputDataBit(SW_UP)) {
k_p++;
else if(!GPIO_ReadInputDataBit(SW_DOWN) && k_p > 0) {
k_p--;
else {
user_input(&k_p);
// Event Handler
// N/A
} break;
case STATE_SETI:
// Write text to OLED
// [ therm :: set i ]
// [ i = 12 ]
@@ -482,31 +506,27 @@ void machine()
itoa(k_i, tempstr);
ssd1306_DrawString("I=", 1, 45);
state = STATE_SETD;
k_i++;
user_input(&k_i);
else if(!GPIO_ReadInputDataBit(SW_DOWN) && k_i > 0) {
k_i--;
case STATE_SETD:
// [ therm :: set d ]
// [ d = 12 ]
ssd1306_DrawString("Derivative", 0, 40);
@@ -515,29 +535,26 @@ void machine()
itoa(k_d, tempstr);
ssd1306_DrawString("D=", 1, 45);
state = STATE_SETWINDUP;
k_d++;
else if(!GPIO_ReadInputDataBit(SW_DOWN) && k_d > 0) {
k_d--;
user_input(&k_d);
case STATE_SETWINDUP:
// [ therm :: set windup ]
// [ g = 12 ]
@@ -547,29 +564,26 @@ void machine()
itoa(windup_guard, tempstr);
ssd1306_DrawString("G=", 1, 45);
state = STATE_SETBOOTTOBREW;
windup_guard++;
else if(!GPIO_ReadInputDataBit(SW_DOWN) && windup_guard > 0) {
windup_guard--;
user_input(&windup_guard);
case STATE_SETBOOTTOBREW:
@@ -593,108 +607,94 @@ void machine()
boottobrew = 1;
else if(!GPIO_ReadInputDataBit(SW_DOWN)) {
boottobrew = 0;
case STATE_PREHEAT_BREW:
// [ therm : preheating brew ]
// [ 30 => 120 C ]
ssd1306_DrawString("Preheating...", 0, 40);
ssd1306_drawlogo();
draw_setpoint();
pid_enabled = 1;
setpoint = setpoint_brew;
save_setpoints(); // TODO: Check for mod
state = STATE_IDLE;
setpoint_brew++;
user_input(&setpoint_brew);
else if(!GPIO_ReadInputDataBit(SW_DOWN) && setpoint_brew > 0) {
setpoint_brew--;
if(temp >= setpoint) {
state = STATE_MAINTAIN_BREW;
case STATE_MAINTAIN_BREW:
// [ therm : ready to brew ]
ssd1306_DrawString("Ready to Brew!", 0, 40);
case STATE_PREHEAT_STEAM:
// [ therm : preheating steam ]
setpoint = setpoint_steam;
setpoint_steam++;
else if(!GPIO_ReadInputDataBit(SW_DOWN) && setpoint_steam > 0) {
setpoint_steam--;
user_input(&setpoint_steam);
state = STATE_MAINTAIN_STEAM;
case STATE_MAINTAIN_STEAM:
@@ -702,31 +702,27 @@ void machine()
ssd1306_DrawString("Ready to Steam!", 0, 40);
// Something is terribly wrong
default:
pid_enabled = 0;
Status change: