diff --git a/main.c b/main.c --- a/main.c +++ b/main.c @@ -148,6 +148,7 @@ uint16_t k_i = 1; uint16_t k_d = 1; uint8_t ssr_output = 0; // Duty cycle of ssr, 0 to SSR_PERIOD + // Process things void process() { @@ -190,10 +191,28 @@ uint8_t state = STATE_IDLE; uint8_t goto_mode = 2; // State machine +uint8_t sw_btn_last = 0; +uint8_t sw_up_last = 0; +uint8_t sw_down_last = 0; +uint8_t sw_left_last = 0; +uint8_t sw_right_last = 0; + +#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) + 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) { // Idle state @@ -229,7 +248,7 @@ void machine() } // Button handler - if(!GPIO_ReadInputDataBit(SW_BTN)) { + if(SW_BTN_PRESSED) { switch(goto_mode) { case 2: state = STATE_PREHEAT_BREW; @@ -244,10 +263,10 @@ void machine() state = STATE_PREHEAT_BREW; } } - else if(!GPIO_ReadInputDataBit(SW_UP) && goto_mode < 2) { + else if(SW_UP_PRESSED && goto_mode < 2) { goto_mode++; } - else if(!GPIO_ReadInputDataBit(SW_DOWN) && k_p > 0 && goto_mode > 0) { + else if(SW_DOWN_PRESSED && k_p > 0 && goto_mode > 0) { goto_mode--; } @@ -263,6 +282,7 @@ void machine() // [ therm :: set p ] // [ p = 12 ] ssd1306_DrawString("Proportional", 0, 40); + ssd1306_drawlogo(); char tempstr[6]; itoa(k_p, tempstr); @@ -273,7 +293,7 @@ void machine() ssd1306_DrawString("Press to accept", 3, 40); // Button handler - if(!GPIO_ReadInputDataBit(SW_BTN)) { + if(SW_BTN_PRESSED) { state = STATE_SETI; } else if(!GPIO_ReadInputDataBit(SW_UP)) { @@ -294,6 +314,7 @@ void machine() // [ therm :: set i ] // [ i = 12 ] ssd1306_DrawString("Integral", 0, 40); + ssd1306_drawlogo(); char tempstr[6]; itoa(k_i, tempstr); @@ -304,7 +325,7 @@ void machine() ssd1306_DrawString("Press to accept", 3, 40); // Button handler - if(!GPIO_ReadInputDataBit(SW_BTN)) { + if(SW_BTN_PRESSED) { state = STATE_SETD; } else if(!GPIO_ReadInputDataBit(SW_UP)) { @@ -326,6 +347,7 @@ void machine() // [ therm :: set d ] // [ d = 12 ] ssd1306_DrawString("Derivative", 0, 40); + ssd1306_drawlogo(); char tempstr[6]; itoa(k_d, tempstr); @@ -336,7 +358,7 @@ void machine() ssd1306_DrawString("Press to accept", 3, 40); // Button handler - if(!GPIO_ReadInputDataBit(SW_BTN)) { + if(SW_BTN_PRESSED) { state = STATE_IDLE; } else if(!GPIO_ReadInputDataBit(SW_UP)) { @@ -357,10 +379,11 @@ void machine() // [ therm : preheating brew ] // [ 30 => 120 C ] ssd1306_DrawString("Preheating...", 0, 40); + ssd1306_drawlogo(); draw_setpoint(); // Button handler - if(!GPIO_ReadInputDataBit(SW_BTN)) { + if(SW_BTN_PRESSED) { state = STATE_IDLE; } else if(!GPIO_ReadInputDataBit(SW_UP)) { @@ -384,10 +407,11 @@ void machine() // [ therm : ready to brew ] // [ 30 => 120 C ] ssd1306_DrawString("Ready to Brew!", 0, 40); + ssd1306_drawlogo(); draw_setpoint(); // Button handler - if(!GPIO_ReadInputDataBit(SW_BTN)) { + if(SW_BTN_PRESSED) { state = STATE_IDLE; } else if(!GPIO_ReadInputDataBit(SW_UP)) { @@ -409,10 +433,11 @@ void machine() // [ therm : preheating steam ] // [ 30 => 120 C ] ssd1306_DrawString("Preheating...", 0, 40); + ssd1306_drawlogo(); draw_setpoint(); // Button handler - if(!GPIO_ReadInputDataBit(SW_BTN)) { + if(SW_BTN_PRESSED) { state = STATE_IDLE; } else if(!GPIO_ReadInputDataBit(SW_UP)) { @@ -436,10 +461,11 @@ void machine() // [ therm : ready to steam ] // [ 30 => 120 C ] ssd1306_DrawString("Ready to Steam!", 0, 40); + ssd1306_drawlogo(); draw_setpoint(); // Button handler - if(!GPIO_ReadInputDataBit(SW_BTN)) { + if(SW_BTN_PRESSED) { state = STATE_IDLE; } else if(!GPIO_ReadInputDataBit(SW_UP)) { @@ -468,6 +494,11 @@ void machine() // Clear screen on state change ssd1306_clearscreen(); } + 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; }