0
0
Embedded Cprogramming~10 mins

Event-driven state machine in Embedded C - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define the initial state of the state machine.

Embedded C
typedef enum {STATE_IDLE, STATE_RUNNING, STATE_STOPPED} State;

State current_state = [1];
Drag options to blanks, or click blank then click option'
ASTATE_IDLE
BSTATE_UNKNOWN
CSTATE_STOPPED
DSTATE_RUNNING
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing a running or stopped state as initial state.
2fill in blank
medium

Complete the code to declare the event type for the state machine.

Embedded C
typedef enum {EVENT_START, EVENT_STOP, [1] Event;
Drag options to blanks, or click blank then click option'
AEVENT_PAUSE
BEVENT_RUN
CEVENT_IDLE
DEVENT_RESET
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing events that are not typical for resetting state machines.
3fill in blank
hard

Fix the error in the state transition function to handle the STOP event.

Embedded C
void handle_event(Event event) {
    switch(current_state) {
        case STATE_RUNNING:
            if(event == [1]) {
                current_state = STATE_STOPPED;
            }
            break;
        default:
            break;
    }
}
Drag options to blanks, or click blank then click option'
AEVENT_START
BEVENT_RESET
CEVENT_STOP
DEVENT_PAUSE
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong event name for stopping the machine.
4fill in blank
hard

Fill both blanks to complete the state transition for the RESET event.

Embedded C
void handle_event(Event event) {
    switch(current_state) {
        case STATE_STOPPED:
            if(event == [1]) {
                current_state = [2];
            }
            break;
        default:
            break;
    }
}
Drag options to blanks, or click blank then click option'
AEVENT_RESET
BEVENT_START
CSTATE_IDLE
DSTATE_RUNNING
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing the reset event or state with start or running states.
5fill in blank
hard

Fill all three blanks to implement the event-driven state machine loop.

Embedded C
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;
    }
}
Drag options to blanks, or click blank then click option'
AEVENT_START
BSTATE_RUNNING
CEVENT_STOP
DSTATE_IDLE
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up event names or states in the transitions.