What is the output of this embedded C code simulating a simple event-driven state machine?
#include <stdio.h> typedef enum {STATE_IDLE, STATE_RUN, STATE_STOP} State; typedef enum {EVENT_START, EVENT_STOP, EVENT_RESET} Event; State current_state = STATE_IDLE; void handle_event(Event e) { switch(current_state) { case STATE_IDLE: if (e == EVENT_START) { current_state = STATE_RUN; printf("Started\n"); } break; case STATE_RUN: if (e == EVENT_STOP) { current_state = STATE_STOP; printf("Stopped\n"); } else if (e == EVENT_RESET) { current_state = STATE_IDLE; printf("Reset to Idle\n"); } break; case STATE_STOP: if (e == EVENT_RESET) { current_state = STATE_IDLE; printf("Reset to Idle\n"); } break; } } int main() { handle_event(EVENT_START); handle_event(EVENT_STOP); handle_event(EVENT_RESET); return 0; }
Trace the state changes step by step and note the printed messages.
The state machine starts in STATE_IDLE. EVENT_START moves it to STATE_RUN and prints "Started". EVENT_STOP in STATE_RUN moves it to STATE_STOP and prints "Stopped". EVENT_RESET in STATE_STOP moves it back to STATE_IDLE and prints "Reset to Idle".
In an event-driven state machine, what best describes the role of the event handler function?
Think about how the state machine reacts to events.
The event handler function decides the next state by checking the current state and the event received. It updates the state accordingly.
What error will this code produce when compiled or run?
#include <stdio.h> typedef enum {IDLE, RUNNING} State; typedef enum {START=1, STOP=2} Event; State current_state = IDLE; void handle_event(Event e) { switch(current_state) { case IDLE: if (e = START) { current_state = RUNNING; printf("Running\n"); } break; case RUNNING: if (e == STOP) { current_state = IDLE; printf("Idle\n"); } break; } } int main() { handle_event(START); handle_event(STOP); return 0; }
Look carefully at the if condition inside the IDLE case.
The condition 'if (e = START)' uses assignment '=' instead of comparison '=='. This causes the condition to always be true, which is a logical error.
Which option contains the correct syntax to define an event-driven state machine handler function in embedded C?
Remember the syntax for switch statements in C.
Option B correctly uses 'switch(current_state) { ... }' with braces and colons after case labels. Other options miss braces, colons, or have wrong syntax.
Consider a state machine with states: INIT, READY, PROCESSING, ERROR, DONE. It starts in INIT. The events are: START, FAIL, COMPLETE, RESET. The transitions are:
- INIT + START -> READY
- READY + START -> PROCESSING
- PROCESSING + COMPLETE -> DONE
- PROCESSING + FAIL -> ERROR
- ERROR + RESET -> INIT
- DONE + RESET -> INIT
If the event sequence is START, START, FAIL, RESET, START, COMPLETE, how many times does the state machine enter the READY state?
Trace the states step by step for each event.
The machine starts at INIT. Events:
1) START: INIT->READY (READY entered once)
2) START: READY->PROCESSING
3) FAIL: PROCESSING->ERROR
4) RESET: ERROR->INIT
5) START: INIT->READY (READY entered second time)
6) COMPLETE: READY has no transition on COMPLETE, so state stays READY.
But since READY is not entered again on event 6, total READY entries = 2.
However, event 6 does not cause a transition, so READY is not re-entered. So READY entered twice.