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
 
@@ -139,24 +139,25 @@ void update_temp() {
 
    GPIO_SetBits(MAX_CS);
 
}
 
 
uint32_t ticks = 0;
 
uint32_t last_ssr_on = 0;
 
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)
 
 
    if(ticks - last_led > 400) 
 
    {
 
        GPIO_ToggleBits(LED_POWER);
 
        last_led = ticks;
 
    }
 
@@ -181,28 +182,46 @@ void draw_setpoint() {
 
    //ssd1306_DrawString("        ", 3, 40);
 
    ssd1306_DrawString(tempstr, 3, 40);
 
    ssd1306_DrawString("-> ", 3, 80);
 
    itoa(setpoint, tempstr);
 
    ssd1306_DrawString("    ", 3, 95);
 
    ssd1306_DrawString(tempstr, 3, 95);
 
}
 
 
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
 
            // [ therm :: idle ]
 
            ssd1306_DrawString("therm :: idle ", 0, 40);
 
 
            char tempstr[6];
 
            itoa_fp(temp, temp_frac, tempstr);
 
            ssd1306_DrawString("Temp: ", 3, 40);
 
@@ -220,235 +239,242 @@ void machine()
 
                case 1:
 
                {
 
                    ssd1306_DrawString("-> set P/I/D", 1, 40);
 
                } break;
 
 
                case 0:
 
                {
 
                    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;
 
                        break;
 
                    case 0:
 
                        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
 
 
        } break;
 
 
        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) {
 
                k_p--;
 
            }
 
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 
 
        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) {
 
                k_i--;
 
            }
 
 
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 
 
        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) {
 
                k_d--;
 
            }
 
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 
 
        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) {
 
                setpoint--;
 
            }
 
 
 
            // Event Handler
 
            if(temp >= setpoint) {
 
                state = STATE_MAINTAIN_BREW;
 
            }
 
 
 
        } break;
 
 
        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) {
 
                setpoint--;
 
            }
 
 
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 
 
        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) {
 
                setpoint--;
 
            }
 
 
 
            // Event Handler
 
            if(temp >= setpoint) {
 
                state = STATE_MAINTAIN_STEAM;
 
            }
 
 
 
        } break;
 
 
        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) {
 
                setpoint--;
 
            }
 
 
 
            // Event Handler
 
            // N/A
 
@@ -459,24 +485,29 @@ void machine()
 
        default:
 
        {
 
            state = STATE_IDLE;
 
 
        } break;
 
            
 
    }
 
 
    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)
 
{
 
  TimingDelay = nTime;
 
  while(TimingDelay != 0);
 
}
 
 
// ISR-triggered decrement of delay and increment of tickcounter
 
void TimingDelay_Decrement(void)
0 comments (0 inline, 0 general)