State Machine Design and Implementation in C

State machines are extremely important in C programming, particularly in our field of programming. They provide a systematic approach to solving complex state transition logic problems. Key concepts in state machines include: State, Event, State Transition, Action.

State Machine Design and Implementation in C

1. State Definition

// Abstract definition of states
typedef enum {
    STATE_IDLE,        // Idle state
    STATE_PROCESSING,  // Processing state
    STATE_WAITING,     // Waiting state
    STATE_ERROR        // Error state
} system_state_t;

2. Event Definition

// Event definition
typedef enum {
    EV_START,          // Start event
    EV_DATA_READY,     // Data ready
    EV_TIMEOUT,        // Timeout event
    EV_ERROR,          // Error event
    EV_COMPLETE        // Complete event
} event_t;
// Events can carry data
typedef struct {
    event_t type;
    void* data;
    size_t data_size;
} event_data_t;

3. State Transition

// Definition of transition rules
typedef struct {
    system_state_t current_state;
    event_t trigger_event;
    system_state_t next_state;
    void (*transition_action)(void* data);
    bool (*guard_condition)(void* context);
} transition_rule_t;

4. Action Definition

// Classification of action types
typedef enum {
    ACTION_ENTRY,      // Executed when entering the state
    ACTION_EXIT,       // Executed when leaving the state
    ACTION_DO,         // Executed during the state
    ACTION_TRANSITION  // Executed during state transition
} action_type_t;

5. C Language Demo

State Machine Design and Implementation in C

#include <stdio.h>
// State enumeration
typedef enum {
    STATE_IDLE,
    STATE_RUNNING,
    STATE_PAUSED,
    STATE_EXIT
} State;
// Event enumeration
typedef enum {
    EV_START,
    EV_PAUSE,
    EV_RESUME,
    EV_STOP
} Event;
// State machine type
typedef struct {
    State current_state;
} StateMachine;
// State handler function type
typedef void (*StateHandler)(StateMachine *);
// State handler function declarations
void handle_idle(StateMachine *);
void handle_running(StateMachine *);
void handle_paused(StateMachine *);
// Array of state handler function pointers
StateHandler state_handlers[] = {
    handle_idle,
    handle_running,
    handle_paused
};
// State transition table
State transition_table[][4] = {
    // EV_START, EV_PAUSE, EV_RESUME, EV_STOP
    {STATE_RUNNING, STATE_IDLE,  STATE_IDLE,  STATE_EXIT}, // IDLE
    {STATE_RUNNING, STATE_PAUSED, STATE_RUNNING, STATE_IDLE}, // RUNNING
    {STATE_PAUSED,  STATE_PAUSED, STATE_RUNNING, STATE_IDLE}  // PAUSED
};
// Event processing function
void process_event(StateMachine *sm, Event ev) {
    State next_state = transition_table[sm->current_state][ev];
    if (next_state != sm->current_state) {
        // State transition, can execute exit action first, then entry action
        // For simplicity, directly transition state here
        sm->current_state = next_state;
    }
    // Execute the current state's handler function
    state_handlers[sm->current_state](sm);
}
// State handler function implementations
void handle_idle(StateMachine *sm) {
    printf("In IDLE state\n");
}
void handle_running(StateMachine *sm) {
    printf("In RUNNING state\n");
}
void handle_paused(StateMachine *sm) {
    printf("In PAUSED state\n");
}
int main() {
    StateMachine sm = {STATE_IDLE};
    // Process a series of events
    process_event(&sm, EV_START);  // From IDLE to RUNNING
    process_event(&sm, EV_PAUSE);  // From RUNNING to PAUSED
    process_event(&sm, EV_RESUME); // From PAUSED to RUNNING
    process_event(&sm, EV_STOP);   // From RUNNING to IDLE
    return 0;
}

That’s all for now. If you don’t understand, follow me! This is a skill that can be mastered; don’t think programming is too difficult. 😏

State Machine Design and Implementation in C

Leave a Comment