/* Transition function */
typedef void (* const HANDLER_FNP)(EVENT_T e);
typedef struct
{
EVENT_T event; /* Event ID */
HANDLER_FNP handler; /* Pointer to corresponding handler */
} TRANSITION_T;
static const TRANSITION_T STATE_ONE[] = {
{EVENT_ONE, handler_one},
{EVENT_TWO, handler_two},
...
{EVENT_MAX, NULL}
};
struct FSM
{
const TRANSITION_T** currentState;
const TRANSITION_T* stateOne;
const TRANSITION_T* stateTwo;
...
};
// Instantiation
struct FSM fsm = {
NULL, // currentState
STATE_ONE,
STATE_TWO,
...
};
void OnEvent(EVENT_T event)
{
const TRANSITION_T* current = fsm->current;
// stop searching when a NULL handler is found
for (i = 0; currentState[i].handler != NULL ; i++)
{
if (currentState[i].event == event)
{
currentState[i].handler(event);
break;
}
}
}