0
0
Embedded Cprogramming~10 mins

State machine for protocol handling 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 protocol.

Embedded C
enum State { IDLE, RECEIVING, PROCESSING, [1] };
Drag options to blanks, or click blank then click option'
ADONE
BWAITING
CERROR
DSENDING
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing a state that does not represent sending data.
Using a state that is not part of the protocol flow.
2fill in blank
medium

Complete the code to transition to the PROCESSING state when data is received.

Embedded C
if (event == DATA_RECEIVED) {
    current_state = [1];
}
Drag options to blanks, or click blank then click option'
AIDLE
BPROCESSING
CSENDING
DRECEIVING
Attempts:
3 left
💡 Hint
Common Mistakes
Setting the state to IDLE instead of PROCESSING.
Using RECEIVING which is the previous state.
3fill in blank
hard

Fix the error in the state transition to IDLE after sending is done.

Embedded C
if (event == SEND_COMPLETE) {
    current_state = [1];
}
Drag options to blanks, or click blank then click option'
AIDLE
BRECEIVING
CERROR
DPROCESSING
Attempts:
3 left
💡 Hint
Common Mistakes
Setting the state to PROCESSING or RECEIVING incorrectly.
Using ERROR state without an error event.
4fill in blank
hard

Fill both blanks to complete the state machine switch-case for handling events.

Embedded C
switch (current_state) {
    case IDLE:
        if (event == START_RECEIVE) {
            current_state = [1];
        }
        break;
    case RECEIVING:
        if (event == DATA_RECEIVED) {
            current_state = [2];
        }
        break;
}
Drag options to blanks, or click blank then click option'
ARECEIVING
BPROCESSING
CIDLE
DSENDING
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the states in the blanks.
Using IDLE or SENDING incorrectly in these transitions.
5fill in blank
hard

Fill all three blanks to complete the dictionary-like state transition table.

Embedded C
State next_state(State current, Event event) {
    if (current == [1] && event == START_SEND) {
        return [2];
    } else if (current == [3] && event == SEND_COMPLETE) {
        return IDLE;
    }
    return current;
}
Drag options to blanks, or click blank then click option'
APROCESSING
BIDLE
CSENDING
DRECEIVING
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the states for sending and idle.
Returning wrong states on events.