#include #include //#include #include typedef enum { STATE_ZERO, STATE_ONE } state; typedef enum { EV_ZERO, EV_ONE, EV_TWO } event_id_t; typedef void *event_t; // typedef uint8_t size_t; // der Container für die Events struct eventTransTab_s { void (*transFunc)(event_t *const e); /* transition function */ const uint16_t eventId; const uint16_t nextState; }; // der Container für die States struct fsm_state_data_s { /* pointer auf ein Array */ const struct eventTransTab_s (*const eventTable)[]; const uint16_t eventTableSize; const uint16_t state; }; // und die FSM struct fsm { const struct fsm_state_data_s (*const stateTable)[]; const size_t stateTableSize; uint16_t currState; }; #define ADD_EVENT(evNr, nextStNr, cbFunc) {.eventId = (event_id_t)evNr, .nextState = (uint16_t)nextStNr, .transFunc = cbFunc } #define CREATE_STATE(stateNr, ...) \ { .state = stateNr, .eventTable = &((struct eventTransTab_s[]){__VA_ARGS__}), \ .eventTableSize = sizeof(((struct eventTransTab_s[]){__VA_ARGS__})) / sizeof(struct eventTransTab_s)} #define CREATE_FSM_TABLE(...) \ .stateTableSize = sizeof((struct fsm_state_data_s[]){__VA_ARGS__}) / sizeof(struct fsm_state_data_s), \ .stateTable = &((struct fsm_state_data_s[]){__VA_ARGS__ }) static struct fsm myFSM = { CREATE_FSM_TABLE( CREATE_STATE(STATE_ZERO, ADD_EVENT(EV_ZERO, STATE_ONE, NULL), ADD_EVENT(EV_ONE, STATE_ONE, NULL), ), CREATE_STATE(STATE_ONE, ADD_EVENT(EV_TWO, STATE_ZERO, NULL), ), ), .currState = STATE_ZERO, }; int main() { printf("\n Hello World \n"); printf("Size of myFSM is %zu", sizeof(myFSM)); return 0; }