Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h> // define the header file
- #include <stdint.h>
- void zle()
- {
- static uint8_t state = 0;
- uint8_t flag = 0;
- switch (state)
- {
- case 0:
- if (flag)
- {
- state = 1;
- // init_vars_in_state_1();
- }
- break;
- case 1:
- /* code */
- break;
- case 2:
- if (flag)
- {
- state = 1;
- // again_init_vars_in_state_1();
- }
- break;
- default:
- break;
- }
- }
- void lepiej()
- {
- static uint8_t state = 0;
- uint8_t flag = 0;
- uint8_t stateChanged = 0;
- switch (state)
- {
- case 0:
- if (flag)
- {
- state = 1;
- stateChanged = 1;
- }
- break;
- case 1:
- if (stateChanged)
- {
- // init_vars_in_state_1();
- stateChanged = 0;
- }
- break;
- case 2:
- if (flag)
- {
- state = 1;
- stateChanged = 1;
- }
- break;
- default:
- break;
- }
- }
- typedef struct FSM_td FSM_t;
- typedef void (*fsm_cb)(FSM_t *fsm);
- typedef enum
- {
- FSM_STATE_RUN,
- FSM_STATE_ENTERED,
- FSM_STATE_LEAVE
- } FSM_STATE;
- struct FSM_td
- {
- fsm_cb currentStateCb; // callback for current state
- fsm_cb nextStateCb; // callback for next state
- FSM_STATE executionStep; // state status for callback
- void *userParam; // not used by library
- } FSM_td;
- // void fsmTask_old(FSM_t* FSM){//
- // if (FSM->nextStateCb != NULL) {
- // FSM->executionStep = FSM_STATE_LEAVE;
- // if (FSM->currentStateCb != NULL){
- // FSM->currentStateCb(FSM);
- // }
- // FSM->currentStateCb = FSM->nextStateCb;
- // FSM->nextStateCb = NULL;
- // FSM->executionStep = FSM_STATE_ENTERED;
- // FSM->currentStateCb(FSM);
- // FSM->executionStep = FSM_STATE_RUN;
- // }else{
- // if (FSM->currentStateCb != NULL){
- // FSM->currentStateCb(FSM);
- // }
- // }
- // }
- /*
- call only one callback per execution
- if state change in FSM_STATE_ENTERED, next call will be FSM_STATE_LEAVE
- */
- void fsmRun(FSM_t *FSM)
- {
- if (FSM->nextStateCb != NULL)
- { // pendind state change
- if (FSM->currentStateCb != NULL)
- { // current state not terminated, telave it
- FSM->executionStep = FSM_STATE_LEAVE;
- }
- else
- {
- FSM->executionStep = FSM_STATE_ENTERED; // current state == NULL, init next state
- }
- }
- else
- {
- FSM->executionStep = FSM_STATE_RUN; // no pending state change
- }
- switch (FSM->executionStep)
- {
- case FSM_STATE_RUN:
- if (FSM->currentStateCb != NULL)
- { // current state should be initialized to null
- FSM->currentStateCb(FSM);
- }
- break;
- case FSM_STATE_ENTERED:
- FSM->currentStateCb = FSM->nextStateCb; // current state terminated, get new state and init
- FSM->nextStateCb = NULL;
- FSM->currentStateCb(FSM);
- break;
- case FSM_STATE_LEAVE:
- FSM->currentStateCb(FSM);
- FSM->currentStateCb = NULL; // terminate current state
- break;
- default:
- break;
- }
- }
- /*
- run all state changes until normal execution occurs, no timeout
- */
- void fsmRunUntinState_RUN(FSM_t *FSM)
- {
- printf("->");
- do
- {
- fsmRun(FSM);
- } while (FSM->executionStep != FSM_STATE_RUN || FSM->nextStateCb != NULL);
- }
- // init fsm or change state
- void fsmGo(FSM_t *FSM, fsm_cb nextStateClb)
- {
- FSM->nextStateCb = nextStateClb;
- }
- FSM_t fsm;
- void state1(FSM_t *fsm);
- void state2(FSM_t *fsm);
- void state1(FSM_t *fsm)
- {
- static int cnt = 0;
- if (fsm->executionStep == FSM_STATE_ENTERED)
- {
- printf(" state1 entered\n");
- cnt = 0;
- }
- if (fsm->executionStep == FSM_STATE_LEAVE)
- printf(" state1 Leave\n");
- if (fsm->executionStep == FSM_STATE_RUN)
- {
- printf(" state1 run\n");
- if (++cnt >= 5)
- fsmGo(fsm, state2);
- }
- }
- void state2(FSM_t *fsm)
- {
- if (fsm->executionStep == FSM_STATE_ENTERED)
- printf(" state2 entered\n");
- if (fsm->executionStep == FSM_STATE_LEAVE)
- printf(" state2 Leave\n");
- if (fsm->executionStep == FSM_STATE_RUN)
- printf(" state2 run\n");
- fsmGo(fsm, state1);
- }
- /*
- run log:
- state1 entered
- state1 run
- state1 run
- state1 run
- state1 run
- state1 run
- state1 Leave
- state2 entered !!!
- state2 Leave !!!
- state1 entered
- state1 run
- end
- */
- int main() // define the main function
- {
- fsmRun(&fsm);
- fsmRun(&fsm);
- fsmRun(&fsm);
- fsmRun(&fsm);
- fsmGo(&fsm, state1);
- for (size_t v = 0; v <= 10; v += 1)
- {
- // fsmRunUntinState_RUN(&fsm);
- fsmRun(&fsm);
- }
- printf("end"); // print the statement.
- return 0;
- }
Add Comment
Please, Sign In to add comment