Macros and classes for writing Finite State Machines. An FSM is defined with the FSM_Switch() macro.
Classes | |
class | cFSM |
Store the state of an FSM. This class is used in conjunction with the FSM_Switch() and other FSM_ macros. More... | |
Macros | |
#define | FSM_MAXT 64 |
After this many transitions without reaching a steady state we assume the FSM is in an infinite loop. More... | |
#define | FSM_Switch(fsm) |
Implements a Finite State Machine. FSM state is stored in an object of type cFSM. More... | |
#define | FSM_Transient(state) |
Declares a transient state; to be used in enum which declares states. More... | |
#define | FSM_Steady(state) |
Declares a steady state; to be used in enum which declares states. More... | |
#define | FSM_Enter(state) |
Within an FSM_Switch() case branch, declares code to be executed on entering the given state. More... | |
#define | FSM_Exit(state) |
Within an FSM_Switch() case branch, declares code to be executed on exiting the given state. More... | |
#define | FSM_Goto(fsm, state) |
To be used in state exit code, to transition to another state. More... | |
#define FSM_MAXT 64 |
After this many transitions without reaching a steady state we assume the FSM is in an infinite loop.
#define FSM_Switch | ( | fsm | ) |
Implements a Finite State Machine. FSM state is stored in an object of type cFSM.
There are two kinds of states: transient and steady. At each execution of the FSM_Switch() statement, the FSM transitions out of the current (steady) state, potentially undergoes a series of state changes to transient states, and arrives at another steady state.
The actual FSM is embedded in an FSM_Switch(), which has cases for entering and leaving each state:
FSM_Switch(fsm) { case FSM_Exit(state1): //... break; case FSM_Enter(state1): //... break; case FSM_Exit(state2): //... break; case FSM_Enter(state2): //... break; //... }
States are declared in enums, using the FSM_Transient() and FSM_Steady() macros.
State transitions are done via calls to FSM_Goto(), which simply stores the new state in the cFSM object.
#define FSM_Transient | ( | state | ) |
Declares a transient state; to be used in enum which declares states.
Example:
enum { INIT = 0, SLEEP = FSM_Steady(1), ACTIVE = FSM_Steady(2), SEND = FSM_Transient(1), };
The numbers in parens must be unique within the state type and they are used for constructing numeric IDs for the states.
#define FSM_Steady | ( | state | ) |
Declares a steady state; to be used in enum which declares states.
See example in FSM_Transient.
#define FSM_Enter | ( | state | ) |
Within an FSM_Switch() case branch, declares code to be executed on entering the given state.
Calls to FSM_Goto() are not allowed within a state's Enter block.
#define FSM_Exit | ( | state | ) |
Within an FSM_Switch() case branch, declares code to be executed on exiting the given state.
#define FSM_Goto | ( | fsm, | |
state | |||
) |
To be used in state exit code, to transition to another state.
Uses stringize (#state), so it only works correctly if 'state' is the enum name itself and not some variable that contains the state code.