0
0
Embedded Cprogramming~5 mins

Hierarchical state machine concept in Embedded C

Choose your learning style9 modes available
Introduction

A hierarchical state machine helps organize complex behaviors by grouping related states inside bigger states. It makes programs easier to understand and manage.

When a device has many modes that share common behaviors.
When you want to avoid repeating code for similar states.
When you need to handle events differently depending on a broader context.
When designing embedded systems like washing machines or traffic lights.
When you want clearer structure in your state management.
Syntax
Embedded C
typedef enum {
    STATE_TOP,
    STATE_A,
    STATE_A1,
    STATE_A2,
    STATE_B
} State;

void handle_event(State *current_state, Event event) {
    switch (*current_state) {
        case STATE_TOP:
            // handle top-level events
            break;
        case STATE_A:
            // handle events common to A and its substates
            break;
        case STATE_A1:
            // handle events specific to A1
            break;
        case STATE_A2:
            // handle events specific to A2
            break;
        case STATE_B:
            // handle events for B
            break;
    }
}

States are organized in a tree-like structure, where some states contain other states.

Events can be handled at different levels, allowing shared behavior.

Examples
Defines states where STATE_A1 and STATE_A2 are inside STATE_A.
Embedded C
typedef enum {
    STATE_TOP,
    STATE_A,
    STATE_A1,
    STATE_A2
} State;
Handles events differently for sub-states A1 and A2.
Embedded C
switch (current_state) {
    case STATE_A1:
        // handle A1 events
        break;
    case STATE_A2:
        // handle A2 events
        break;
}
Sample Program

This program shows a simple hierarchical state machine with states inside STATE_A. It moves between states based on events and prints the current state changes.

Embedded C
#include <stdio.h>

typedef enum {
    STATE_TOP,
    STATE_A,
    STATE_A1,
    STATE_A2,
    STATE_B
} State;

typedef enum {
    EVENT_GO_A1,
    EVENT_GO_A2,
    EVENT_GO_B,
    EVENT_BACK
} Event;

void handle_event(State *current_state, Event event) {
    switch (*current_state) {
        case STATE_TOP:
            if (event == EVENT_GO_A1) {
                *current_state = STATE_A1;
                printf("Moved to STATE_A1\n");
            } else if (event == EVENT_GO_B) {
                *current_state = STATE_B;
                printf("Moved to STATE_B\n");
            } else {
                printf("No transition from STATE_TOP\n");
            }
            break;
        case STATE_A1:
            if (event == EVENT_GO_A2) {
                *current_state = STATE_A2;
                printf("Moved to STATE_A2\n");
            } else if (event == EVENT_BACK) {
                *current_state = STATE_TOP;
                printf("Returned to STATE_TOP\n");
            } else {
                printf("No transition from STATE_A1\n");
            }
            break;
        case STATE_A2:
            if (event == EVENT_BACK) {
                *current_state = STATE_TOP;
                printf("Returned to STATE_TOP\n");
            } else {
                printf("No transition from STATE_A2\n");
            }
            break;
        case STATE_B:
            if (event == EVENT_BACK) {
                *current_state = STATE_TOP;
                printf("Returned to STATE_TOP\n");
            } else {
                printf("No transition from STATE_B\n");
            }
            break;
    }
}

int main() {
    State current_state = STATE_TOP;

    handle_event(&current_state, EVENT_GO_A1); // Move to A1
    handle_event(&current_state, EVENT_GO_A2); // Move to A2
    handle_event(&current_state, EVENT_BACK);  // Back to TOP
    handle_event(&current_state, EVENT_GO_B);  // Move to B
    handle_event(&current_state, EVENT_BACK);  // Back to TOP

    return 0;
}
OutputSuccess
Important Notes

Hierarchical states reduce repeated code by sharing behavior in parent states.

Transitions can happen between sub-states or back to parent states.

Keep the state tree simple to avoid confusion.

Summary

Hierarchical state machines group states inside bigger states for clarity.

They help manage complex behaviors by sharing common actions.

Use events to move between states and sub-states.