Changeset - 7d17184d77c2
[Not reviewed]
default
0 1 0
Ethan Zonca - 10 years ago 2014-07-14 23:01:15
ez@ethanzonca.com
Initial state machine implementation
1 file changed with 196 insertions and 0 deletions:
main.c
196
0 comments (0 inline, 0 general)
main.c
Show inline comments
 
@@ -15,12 +15,14 @@
 
 
 
static __IO uint32_t TimingDelay;
 
 
void init_gpio();
 
void init_spi();
 
void process();
 
void machine();
 
 
int main(void)
 
{
 
 
    // Init clocks
 
    SystemInit();
 
@@ -47,12 +49,19 @@ int main(void)
 
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
 
 
    init_spi();
 
 
    while(1)
 
   {  
 
        // Process sensor inputs [TODO: 5hz?]
 
        process();
 
 
        // Run state machine [TODO: 50hz?]
 
        machine(); // this argument is sooo wrong
 
        // probably just passed the actual port
 
 
        // TODO: Grab buttonpresses with interrupts
 
        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);
 
@@ -68,12 +77,199 @@ RCC_APB2PeriphClockCmd(RCC_APB2Periph_SP
 
        Delay(150);
 
        GPIO_ResetBits(LED_POWER);
 
        Delay(150);
 
    }
 
}
 
 
 
 
int32_t temp = 0;
 
int32_t setpoint = 0;
 
int32_t p = 1;
 
int32_t i = 1;
 
int32_t d = 1;
 
 
// Process things
 
void process()
 
{
 
    // Read MAX temp sensor
 
    temp = 0;
 
 
    // Perform PID calculations
 
 
    // Write output to SSR
 
}
 
 
 
 
enum state {
 
    STATE_IDLE = 0,
 
    STATE_SETP,
 
    STATE_SETI,
 
    STATE_SETD,
 
 
    STATE_PREHEAT_BREW,
 
    STATE_MAINTAIN_BREW,
 
    STATE_PREHEAT_STEAM,
 
    STATE_MAINTAIN_STEAM,
 
};
 
 
 
uint8_t state = STATE_IDLE;
 
 
// State machine
 
void machine()
 
{
 
    
 
    switch(state)
 
    {
 
        // Idle state
 
        case STATE_IDLE:
 
        {
 
            // Write text to OLED
 
            // [ therm :: idle ]
 
 
            // Button handler
 
            if(GPIO_ReadInputDataBit(SW_BTN)) {
 
                state = STATE_SETP;
 
            }
 
 
            // Event Handler
 
            // N/A
 
 
        } break;
 
 
        case STATE_SETP:
 
        {
 
            // Write text to OLED
 
            // [ therm :: set p ]
 
            // [ p = 12         ]
 
 
            // Button handler
 
            if(GPIO_ReadInputDataBit(SW_BTN)) {
 
                state = STATE_IDLE;
 
            }
 
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 
 
        case STATE_SETI:
 
        {
 
            // Write text to OLED
 
            // [ therm :: set i ]
 
            // [ i = 12         ]
 
 
            // Button handler
 
            if(GPIO_ReadInputDataBit(SW_BTN)) {
 
                state = STATE_IDLE;
 
            }
 
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 
 
        case STATE_SETD:
 
        {
 
            // Write text to OLED
 
            // [ therm :: set d ]
 
            // [ d = 12         ]
 
 
            // Button handler
 
            if(GPIO_ReadInputDataBit(SW_BTN)) {
 
                state = STATE_IDLE;
 
            }
 
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 
 
        case STATE_PREHEAT_BREW:
 
        {
 
            // Write text to OLED
 
            // [ therm : preheating brew ]
 
            // [ 30 => 120 C             ]
 
 
            // Button handler
 
            if(GPIO_ReadInputDataBit(SW_BTN)) {
 
                state = STATE_IDLE;
 
            }
 
 
            // 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           ]
 
 
 
            // Button handler
 
            if(GPIO_ReadInputDataBit(SW_BTN)) {
 
                state = STATE_IDLE;
 
            }
 
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 
 
        case STATE_PREHEAT_STEAM:
 
        {
 
            // Write text to OLED
 
            // [ therm : preheating steam ]
 
            // [ 30 => 120 C           ]
 
 
            // Button handler
 
            if(GPIO_ReadInputDataBit(SW_BTN)) {
 
                state = STATE_IDLE;
 
            }
 
 
            // 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            ]
 
 
            // Button handler
 
            if(GPIO_ReadInputDataBit(SW_BTN)) {
 
                state = STATE_IDLE;
 
            }
 
 
            // Event Handler
 
            // N/A
 
 
 
        } break;
 
 
 
        // Something is terribly wrong
 
        deault:
 
        {
 
            state = STATE_IDLE;
 
 
        } break;
 
            
 
    }
 
}
 
 
 
/**
 
  * @brief  Inserts a delay time.
 
  * @param  nTime: specifies the delay time length, in 1 ms.
 
  * @retval None
 
  */
 
void Delay(__IO uint32_t nTime)
0 comments (0 inline, 0 general)