2018年8月8日 星期三

[FSM] The sample code of two dimension FSM


#include <stdio.h>
//Different state of ATM machine
typedef enum {
    Idle_State,
    Card_Inserted_State,
    Pin_Eentered_State,
    Option_Selected_State,
    Amount_Entered_State,
    last_State
} eSystemState;

//Different type events
typedef enum {
    Card_Insert_Event,
    Pin_Enter_Event,
    Option_Selection_Event,
    Amount_Enter_Event,
    Amount_Dispatch_Event,
    last_Event
} eSystemEvent;

//typedef of 2d array
typedef eSystemState (*const afEventHandler[last_State][last_Event])(void);

//typedef of function pointer
typedef eSystemState (*pfEventHandler)(void);

//function call to dispatch the amount and return the ideal state
eSystemState AmountDispatchHandler(void)
{
    return Idle_State;
}

//function call to Enter amount and return amount enetered state
eSystemState EnterAmountHandler(void)
{
    return Amount_Entered_State;
}

//function call to option select and return the option selected state
eSystemState OptionSelectionHandler(void)
{
    return Option_Selected_State;
}

//function call to enter the pin and return pin entered state
eSystemState EnterPinHandler(void)
{
    return Pin_Eentered_State;
}

//function call to processing track data and return card inserted state
eSystemState InsertCardHandler(void)
{
    return Card_Inserted_State;
}

eSystemEvent ReadEvent()
{

}

int main(int argc, char *argv[])
{
    eSystemState eNextState = Idle_State;
    eSystemEvent eNewEvent;
// Table to define valid states and event of finite state machine
    static afEventHandler StateMachine = {
  /* Idle_State: 0 */              [Idle_State]            =       {[Card_Insert_Event]= InsertCardHandler,NULL,NULL,NULL,NULL},
  /* Card_Inserted_State 1*/       [Card_Inserted_State]   =       {NULL,[Pin_Enter_Event] = EnterPinHandler,NULL,NULL,NULL },
  /* Pin_Eentered_State 2 */       [Pin_Eentered_State]    =       {NULL,NULL,[Option_Selection_Event] = OptionSelectionHandler,NULL,NULL},
  /* Option_Selected_State: 3 */   [Option_Selected_State] =       {NULL,NULL,NULL,[Amount_Enter_Event] = EnterAmountHandler,NULL},
  /* Amount_Entered_State 4 */     [Amount_Entered_State]  =       {NULL,NULL,NULL,NULL,[Amount_Dispatch_Event] = AmountDispatchHandler},
    };
    while(1) {
// assume api to read the next event
        eSystemEvent eNewEvent = ReadEvent();
        if( ( eNextState < last_State) && (eNewEvent < last_Event) && StateMachine[eNextState][eNewEvent]!= NULL) { //Check NULL pointer and array boundary
// function call as per the state and event and return the next state of the finite state machine
            eNextState = (*StateMachine[eNextState][eNewEvent])();
        } else {
//Invalid
        }
    }
    return 0;
}

Method 2
#include <stdio.h>
//Different state of ATM machine
typedef enum {
    Idle_State,
    Card_Inserted_State,
    Pin_Eentered_State,
    Option_Selected_State,
    Amount_Entered_State,
    last_State
} eSystemState;

//Different type events
typedef enum {
    Card_Insert_Event,
    Pin_Enter_Event,
    Option_Selection_Event,
    Amount_Enter_Event,
    Amount_Dispatch_Event,
    last_Event
} eSystemEvent;

//typedef of 2d array
typedef eSystemState (*const afEventHandler[last_State][last_Event])(void);

//typedef of function pointer
typedef eSystemState (*pfEventHandler)(void);

//function call to dispatch the amount and return the ideal state
eSystemState AmountDispatchHandler(void)
{
    return Idle_State;
}

//function call to Enter amount and return amount enetered state
eSystemState EnterAmountHandler(void)
{
    return Amount_Entered_State;
}

//function call to option select and return the option selected state
eSystemState OptionSelectionHandler(void)
{
    return Option_Selected_State;
}

//function call to enter the pin and return pin entered state
eSystemState EnterPinHandler(void)
{
    return Pin_Eentered_State;
}

//function call to processing track data and return card inserted state
eSystemState InsertCardHandler(void)
{
    return Card_Inserted_State;
}

eSystemEvent ReadEvent()
{
    /* [Hardware detection] User insert the card
     * if (Card Insert == True)
     *      return Card_Insert_Event;
     * 
     * [Hardware detection] User start to press button
     * else if (User press button == True)
     *      return Pin_Enter_Event;
     * */
}

// Table to define valid states and event of finite state machine
const static afEventHandler StateMachine = {
/* Idle_State: 0 */              {/*Card_Insert_Event*/ InsertCardHandler, /*Pin_Enter_Event*/ NULL,             /*Option_Selection_Event*/ NULL,                  /*Amount_Enter_Event*/ NULL,              /*Amount_Dispatch_Event*/ NULL},
/* Card_Inserted_State 1*/       {/*Card_Insert_Event*/ NULL,              /*Pin_Enter_Event*/ EnterPinHandler,  /*Option_Selection_Event*/ NULL,                  /*Amount_Enter_Event*/ NULL,              /*Amount_Dispatch_Event*/ NULL},
/* Pin_Eentered_State 2 */       {/*Card_Insert_Event*/ NULL,              /*Pin_Enter_Event*/ NULL,             /*Option_Selection_Event*/ OptionSelectionHandler,/*Amount_Enter_Event*/ NULL,              /*Amount_Dispatch_Event*/ NULL},
/* Option_Selected_State: 3 */   {/*Card_Insert_Event*/ NULL,              /*Pin_Enter_Event*/ NULL,             /*Option_Selection_Event*/ NULL,                  /*Amount_Enter_Event*/ EnterAmountHandler,/*Amount_Dispatch_Event*/ NULL},
/* Amount_Entered_State 4 */     {/*Card_Insert_Event*/ NULL,              /*Pin_Enter_Event*/ NULL,             /*Option_Selection_Event*/ NULL,                  /*Amount_Enter_Event*/ NULL,              /*Amount_Dispatch_Event*/ AmountDispatchHandler},
};

int main(int argc, char *argv[])
{
    eSystemState eNextState = Idle_State;
    eSystemEvent eNewEvent;

    while(1) {
// assume api to read the next event
        eSystemEvent eNewEvent = ReadEvent();
        if( ( eNextState < last_State) && (eNewEvent < last_Event) && StateMachine[eNextState][eNewEvent]!= NULL) { //Check NULL pointer and array boundary
// function call as per the state and event and return the next state of the finite state machine
            eNextState = (*StateMachine[eNextState][eNewEvent])();
        } else {
//Invalid
        }
    }
        
    return 0;
}

0 意見:

張貼留言