0
0
Embedded Cprogramming~10 mins

Event-driven state machine in Embedded C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Event-driven state machine
Start
Wait for Event
Event Received?
NoWait for Event
Yes
Process Event
Change State if Needed
Perform Actions
Back to Wait for Event
The state machine waits for events, processes them, changes state if needed, performs actions, then waits for the next event.
Execution Sample
Embedded C
typedef enum {IDLE, RUNNING, ERROR} State;
State current_state = IDLE;

void handle_event(int event) {
  switch(current_state) {
    case IDLE:
      if(event == 1) current_state = RUNNING;
      break;
    case RUNNING:
      if(event == 2) current_state = ERROR;
      break;
    case ERROR:
      if(event == 0) current_state = IDLE;
      break;
  }
}
This code changes the state based on events: from IDLE to RUNNING on event 1, RUNNING to ERROR on event 2, and ERROR to IDLE on event 0.
Execution Table
StepCurrent StateEventCondition CheckedState ChangeNew State
1IDLE1event == 1 (true)current_state = RUNNINGRUNNING
2RUNNING2event == 2 (true)current_state = ERRORERROR
3ERROR0event == 0 (true)current_state = IDLEIDLE
4IDLE2event == 1 (false)no changeIDLE
5IDLE0event == 1 (false)no changeIDLE
💡 No more events to process; state machine waits for next event.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
current_stateIDLERUNNINGERRORIDLEIDLEIDLE
eventN/A12020
Key Moments - 2 Insights
Why does the state not change when event 2 is received in IDLE state (Step 4)?
Because in IDLE state, only event 1 triggers a state change. Step 4 shows event 2 does not meet the condition event == 1, so state stays IDLE.
How does the state machine return to IDLE from ERROR state?
At Step 3, when event 0 is received in ERROR state, the condition event == 0 is true, so current_state changes back to IDLE.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the current_state after Step 2?
AERROR
BIDLE
CRUNNING
DUndefined
💡 Hint
Check the 'New State' column at Step 2 in the execution_table.
At which step does the state machine change from ERROR to IDLE?
AStep 1
BStep 3
CStep 4
DStep 5
💡 Hint
Look for the row where current_state is ERROR and new state is IDLE.
If event 1 is received while in RUNNING state, what happens?
ANo state change
BState changes to IDLE
CState changes to ERROR
DState machine resets
💡 Hint
Refer to the switch-case logic and see which events cause state changes in RUNNING state.
Concept Snapshot
Event-driven state machine in embedded C:
- Define states as enum.
- Use a variable to track current state.
- On event, check current state and event.
- Change state if conditions match.
- Repeat waiting for next event.
Full Transcript
An event-driven state machine waits for events and changes its state based on the current state and the event received. The example code uses an enum to define states and a switch-case to decide state transitions. The execution table shows step-by-step how the state changes with each event. Variables track the current state and event values. Key moments clarify why some events do not cause state changes and how the machine returns to the initial state. The quiz tests understanding of state transitions and event handling.