0
0
Embedded Cprogramming~5 mins

State transition table approach in Embedded C

Choose your learning style9 modes available
Introduction

The state transition table approach helps organize how a system moves from one state to another based on inputs. It makes complex behavior easier to understand and manage.

When you have a device that reacts differently depending on its current mode.
When you want to control a machine with clear rules for each step.
When you need to handle multiple inputs that change the system's behavior.
When debugging state changes in embedded systems.
When designing simple games or user interfaces with different screens.
Syntax
Embedded C
typedef enum {STATE1, STATE2, STATE3} State;
typedef enum {EVENT1, EVENT2, EVENT3} Event;

State state_transition_table[][3] = {
  // EVENT1, EVENT2, EVENT3
  {STATE2, STATE1, STATE3}, // STATE1 transitions
  {STATE1, STATE3, STATE2}, // STATE2 transitions
  {STATE3, STATE2, STATE1}  // STATE3 transitions
};

State current_state = STATE1;

void handle_event(Event e) {
  current_state = state_transition_table[current_state][e];
}

The table rows represent current states.

The columns represent events or inputs.

Examples
This example shows a simple machine with three states and three events.
Embedded C
typedef enum {IDLE, RUNNING, STOPPED} State;
typedef enum {START, STOP, RESET} Event;

State state_table[][3] = {
  {RUNNING, IDLE, IDLE},    // IDLE
  {RUNNING, STOPPED, IDLE}, // RUNNING
  {IDLE, STOPPED, IDLE}     // STOPPED
};
Updates the current state based on the input event using the table.
Embedded C
State current = IDLE;
Event input = START;
current = state_table[current][input];
Sample Program

This program simulates a simple machine changing states based on events using a state transition table.

Embedded C
#include <stdio.h>

typedef enum {IDLE, RUNNING, STOPPED} State;
typedef enum {START, STOP, RESET} Event;

const char* state_names[] = {"IDLE", "RUNNING", "STOPPED"};
const char* event_names[] = {"START", "STOP", "RESET"};

State state_table[][3] = {
  {RUNNING, IDLE, IDLE},    // IDLE
  {RUNNING, STOPPED, IDLE}, // RUNNING
  {IDLE, STOPPED, IDLE}     // STOPPED
};

int main() {
  State current_state = IDLE;
  Event events[] = {START, STOP, RESET, START, STOP};
  int n = sizeof(events) / sizeof(events[0]);

  printf("Initial state: %s\n", state_names[current_state]);

  for (int i = 0; i < n; i++) {
    printf("Event: %s\n", event_names[events[i]]);
    current_state = state_table[current_state][events[i]];
    printf("New state: %s\n", state_names[current_state]);
  }

  return 0;
}
OutputSuccess
Important Notes

Make sure the table covers all states and events to avoid undefined behavior.

Use enums to keep states and events clear and readable.

This approach makes adding new states or events easier by updating the table.

Summary

The state transition table maps current states and events to next states.

It helps organize and simplify complex state changes.

Using enums and tables makes code easier to read and maintain.