Changeset - 280a8ff82f9b
[Not reviewed]
default
0 1 0
Ethan Zonca - 10 years ago 2014-08-26 21:39:57
ez@ethanzonca.com
Added rising edge detection for some buttonpresses
1 file changed with 41 insertions and 10 deletions:
main.c
41
10
0 comments (0 inline, 0 general)
main.c
Show inline comments
 
@@ -145,12 +145,13 @@ uint32_t last_led = 0;
 
int32_t setpoint = 0;
 
uint16_t k_p = 1;
 
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()
 
{
 
    update_temp(); // Read MAX31855
 
 
    // TODO: Add calibration offset (linear)
 
@@ -187,16 +188,34 @@ void draw_setpoint() {
 
}
 
 
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
 
        case STATE_IDLE:
 
        {
 
            // Write text to OLED
 
@@ -226,13 +245,13 @@ void machine()
 
                {
 
                    ssd1306_DrawString("-> setup    ", 1, 40);
 
                } break;
 
            }
 
 
            // Button handler
 
            if(!GPIO_ReadInputDataBit(SW_BTN)) {
 
            if(SW_BTN_PRESSED) {
 
                switch(goto_mode) {
 
                    case 2:
 
                        state = STATE_PREHEAT_BREW;
 
                        break;
 
                    case 1:
 
                        state = STATE_SETP;
 
@@ -241,16 +260,16 @@ void machine()
 
                        state = STATE_SETP;
 
                        break;
 
                    default:
 
                        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--;
 
            }
 
 
 
            // Event Handler
 
            // N/A
 
@@ -260,23 +279,24 @@ void machine()
 
        case STATE_SETP:
 
        {
 
            // Write text to OLED
 
            // [ therm :: set p ]
 
            // [ p = 12         ]
 
            ssd1306_DrawString("Proportional", 0, 40);
 
            ssd1306_drawlogo();
 
 
            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(!GPIO_ReadInputDataBit(SW_BTN)) {
 
            if(SW_BTN_PRESSED) {
 
                state = STATE_SETI;
 
            }
 
            else if(!GPIO_ReadInputDataBit(SW_UP)) {
 
                k_p++;
 
            }
 
            else if(!GPIO_ReadInputDataBit(SW_DOWN) && k_p > 0) {
 
@@ -291,23 +311,24 @@ void machine()
 
        case STATE_SETI:
 
        {
 
            // Write text to OLED
 
            // [ therm :: set i ]
 
            // [ i = 12         ]
 
            ssd1306_DrawString("Integral", 0, 40);
 
            ssd1306_drawlogo();
 
 
            char tempstr[6];
 
            itoa(k_i, tempstr);
 
            ssd1306_DrawString("I=", 1, 45);
 
            ssd1306_DrawString("    ", 1, 57);
 
            ssd1306_DrawString(tempstr, 1, 57);
 
 
            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)) {
 
                k_i++;
 
            }
 
            else if(!GPIO_ReadInputDataBit(SW_DOWN) && k_i > 0) {
 
@@ -323,23 +344,24 @@ void machine()
 
        case STATE_SETD:
 
        {
 
            // Write text to OLED
 
            // [ therm :: set d ]
 
            // [ d = 12         ]
 
            ssd1306_DrawString("Derivative", 0, 40);
 
            ssd1306_drawlogo();
 
 
            char tempstr[6];
 
            itoa(k_d, tempstr);
 
            ssd1306_DrawString("D=", 1, 45);
 
            ssd1306_DrawString("    ", 1, 57);
 
            ssd1306_DrawString(tempstr, 1, 57);
 
 
            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)) {
 
                k_d++;
 
            }
 
            else if(!GPIO_ReadInputDataBit(SW_DOWN) && k_d > 0) {
 
@@ -354,16 +376,17 @@ void machine()
 
        case STATE_PREHEAT_BREW:
 
        {
 
            // Write text to OLED
 
            // [ 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)) {
 
               setpoint++; 
 
            }
 
            else if(!GPIO_ReadInputDataBit(SW_DOWN) && setpoint > 0) {
 
@@ -381,16 +404,17 @@ void machine()
 
        case STATE_MAINTAIN_BREW:
 
        {
 
            // Write text to OLED
 
            // [ 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)) {
 
               setpoint++; 
 
            }
 
            else if(!GPIO_ReadInputDataBit(SW_DOWN) && setpoint > 0) {
 
@@ -406,16 +430,17 @@ void machine()
 
        case STATE_PREHEAT_STEAM:
 
        {
 
            // Write text to OLED
 
            // [ 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)) {
 
               setpoint++; 
 
            }
 
            else if(!GPIO_ReadInputDataBit(SW_DOWN) && setpoint > 0) {
 
@@ -433,16 +458,17 @@ void machine()
 
        case STATE_MAINTAIN_STEAM:
 
        {
 
            // Write text to OLED
 
            // [ 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)) {
 
               setpoint++; 
 
            }
 
            else if(!GPIO_ReadInputDataBit(SW_DOWN) && setpoint > 0) {
 
@@ -465,12 +491,17 @@ void machine()
 
    }
 
 
    if(last_state != state) {
 
        // 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;
 
}
 
 
 
// Delay a number of systicks
 
void delay(__IO uint32_t nTime)
 
{
0 comments (0 inline, 0 general)