0
0
Embedded Cprogramming~10 mins

Hierarchical state machine concept 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_PROCESSING, STATE_ERROR } State;

State currentState = [1];
Drag options to blanks, or click blank then click option'
ASTATE_IDLE
BSTATE_UNKNOWN
CSTATE_ERROR
DSTATE_PROCESSING
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing a state that represents an error or processing as the initial state.
2fill in blank
medium

Complete the code to declare a nested (sub) state inside the PROCESSING state.

Embedded C
typedef enum { SUBSTATE_READ, SUBSTATE_WRITE } ProcessingSubState;

ProcessingSubState currentSubState = [1];
Drag options to blanks, or click blank then click option'
ASUBSTATE_WRITE
BSUBSTATE_READ
CSUBSTATE_IDLE
DSUBSTATE_ERROR
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing a substate that does not exist or is unrelated.
3fill in blank
hard

Fix the error in the transition function to correctly change the state.

Embedded C
void transitionToProcessing(State *state) {
    *state = [1];
}
Drag options to blanks, or click blank then click option'
ASTATE_PROCESSING
BSTATE_ERROR
CSTATE_IDLE
DPROCESSING_STATE
Attempts:
3 left
💡 Hint
Common Mistakes
Using an undefined state name or a wrong state.
4fill in blank
hard

Fill both blanks to complete the hierarchical state check.

Embedded C
if (currentState == [1]) {
    if (currentSubState == [2]) {
        // Handle read substate
    }
}
Drag options to blanks, or click blank then click option'
ASTATE_PROCESSING
BSTATE_IDLE
CSUBSTATE_READ
DSUBSTATE_WRITE
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up main states and substates in the condition.
5fill in blank
hard

Fill all three blanks to complete the hierarchical state machine transition.

Embedded C
switch (currentState) {
    case [1]:
        switch (currentSubState) {
            case [2]:
                currentSubState = [3];
                break;
        }
        break;
}
Drag options to blanks, or click blank then click option'
ASTATE_PROCESSING
BSUBSTATE_READ
CSUBSTATE_WRITE
DSTATE_IDLE
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong state or substate names in the switch cases.