A C State Machine Generator
This program reads the finite state machine variable definition and state transfer definition files and generates the C implementation of such a state machine.
- C
- A C example of state machine
- Python
- C State Machine Generator implemented in Python
- Rust
- C State Machine Generator implemented in Rust (In Progress)
The State Transfer Matrix. State transition conditions are defined from each row to each column.
From\To, A, B
A, a_loop, cond_a_to_b
B, cond_b_to_a, b_loop
State Variable Definition.
a, int
b, float
state_machine.h
#ifndef __STATE_MACHINE_H__
#define __STATE_MACHINE_H__
typedef struct state_machine_var
{
void (* current_state)(struct state_machine_var * var);
/* State Machine Variables Begin */
/* State Machine Variables End */
} state_machine_var_def;
extern state_machine_var state_machine_var;
#endif
state_machine.c
#include "state_machine.h"
/* State Definition Begin */
void state_0(state_machine_var * var);
void state_1(state_machine_var * var);
/* State Definition End */
/* State Variable Begin */
state_machine_var_def state_machine_var = {state_0};
/* State Variable End */
/* State Implementation Begin */
void state_0(state_machine_var_def * var)
{
/* State Transition Begin */
if(1)
{
var->current_state = state_1;
return;
}
/* State Transition End */
}
void state_1(state_machine_var_def * var)
{
/* State Transition Begin */
if(1)
{
var->current_state = state_0;
return;
}
/* State Transition End */
}
/* State Implementation End */
main.c
#include "state_machine.h"
#include <stdio.h>
void main()
{
while(1)
{
my_var.current_state(&my_var);
printf("State addr: %x.\n", (long)my_var.current_state);
}
}