Complete the code to define the initial state of the state machine.
typedef enum {STATE_IDLE, STATE_RUNNING, STATE_STOPPED} State;
State current_state = [1];The initial state of the state machine is usually set to STATE_IDLE to indicate it is waiting for events.
Complete the code to declare the event type for the state machine.
typedef enum {EVENT_START, EVENT_STOP, [1] Event;EVENT_RESET is a common event used to reset the state machine to its initial state.
Fix the error in the state transition function to handle the STOP event.
void handle_event(Event event) {
switch(current_state) {
case STATE_RUNNING:
if(event == [1]) {
current_state = STATE_STOPPED;
}
break;
default:
break;
}
}The EVENT_STOP triggers the transition from STATE_RUNNING to STATE_STOPPED.
Fill both blanks to complete the state transition for the RESET event.
void handle_event(Event event) {
switch(current_state) {
case STATE_STOPPED:
if(event == [1]) {
current_state = [2];
}
break;
default:
break;
}
}The EVENT_RESET event resets the state machine back to STATE_IDLE.
Fill all three blanks to implement the event-driven state machine loop.
while(1) { Event event = get_next_event(); switch(current_state) { case STATE_IDLE: if(event == [1]) { current_state = [2]; } break; case STATE_RUNNING: if(event == [3]) { current_state = STATE_STOPPED; } break; default: break; } }
The machine starts running on EVENT_START from STATE_IDLE, and stops on EVENT_STOP from STATE_RUNNING.