State machine generator, based on statemachine library
In many microcontroler programs, without the help of tiny OS (like FreeRTOS), we need to implement a non-blocking state machine. In many programs we just can see the easy way, which will be quite difficult to maintain afterwards : a switch/case called each time in the main loop. This generator build state machine using the library statemachine
run __install.bat or python -m pip install -r requirements.txt
A state is described by 3 actions:
Switching from a state to another is managed by do_job regarding which event is recieved.
An event is identified by an integer value and sometimes has attached data. For example, an event can be “button pushed” and the attached data can be “the button identifier”
This lib integrate a generator to build code basis to build your own state machine.
The input is a YAML file that describe the machine.
The output files are :
These files are stored into generator/output directory
{
"machine" : "", # your state machine name
"entry" : "", # the entry point state name
"global" : # global state action
{
"actions" : [], #see states
"enter" : "", #see states
"exit" : "", #see states
}
# the states list
states :
[
{
"name" : "", # sthe state name
"comment" : "", # some information on the state
# describes state change and/or action on a event
"actions" :
[
# an action
{
"to" : "", # destination state name
"conds":
[ # the list of conditions triggering state change
{
"event": "", #the event name
"cond": "", #optional specific condition
"comment": "", # optional event description. You can set only one time the event comment
},
#another condition
{
...
}
],
"job" : "" # what to do on this event
},
# another action
{
...
}
],
},
# an other state
{
...
}
]
}
Call the generator like this from the generator directory :
python StateMachineGenerator.py -i {path_to_your YAML file}
It stored automaticaly output files with the base name of yout yaml file
You can set a custom output name with the ‘-o’ option
python SMGene.py -i {path_to_your YAML file} -o {your_custom_name}
You can describe templates to build the state machine.
You will find exemples in templates directory :
Availables templates are for source and header files.
filename to use : template_source.c
In the source you may put :
filename to use : template_header.c
In the source you may put :
Put templates in a directory and start generation with :
python SMGene.py -i {path_to_your YAML file} -o {your_custom_name} -t {your_template_directory}
You can find the state machine example here :