I have implemented a FSM for STM32 using arduino framework. It respond efficienctly to the different events. The output is like:
Starting MY FSM EXAMPLE
Running myTask...
[my_FSM][INFO] Dispatch event FSM_EVENT_INIT_OK to state handler state_init
[my_FSM][INFO] state_init -> state_connecting
[my_FSM][INFO] Dispatch event FSM_EVENT_CONNECTED to state handler state_connecting
[my_FSM][INFO] state_connecting -> state_buffering
[my_FSM][INFO] Dispatch event FSM_EVENT_READY to state handler state_buffering
[my_FSM][INFO] state_buffering -> state_playing
[my_FSM][INFO] Dispatch event FSM_EVENT_SUSPENDED to state handler state_playing
[my_FSM][INFO] state_playing -> state_buffering
[my_FSM][INFO] Dispatch event FSM_EVENT_SUSPENDED to state handler state_buffering
[my_FSM][INFO] Dispatch event FSM_EVENT_READY to state handler state_buffering
[my_FSM][INFO] state_buffering -> state_playing
[my_FSM][INFO] Dispatch event FSM_EVENT_FINISHED to state handler state_playing
[my_FSM][INFO] state_playing -> state_closing
High level description
In sim_events.cpp
it is simulated how send some events that is going to make the system to move from state to state.
When creating a new project is necessary to:
- Modify
my_fsm.h
andmy_fsm.cpp
- Create the
state_*.cpp
with the description about how the system react to different events depending on each state.
C disclaimer
This code is done in C. It is not really worth to move it to C++. It would take some time and doesnt really give bit improvements.