/**************************************************************************** Function StartMasterSM Parameters ES_Event CurrentEvent Returns nothing Description Does any required initialization for this state machine Notes Author J. Edward Carryer, 02/06/12, 22:15 ****************************************************************************/ void StartMasterSM ( ES_Event CurrentEvent ) { // if there is more than 1 state to the top level machine you will need // to initialize the state variable CurrentState = WAITING; //Initialize the Ports B0-2 on the Tiva as a digital input pins //to indicate which kart is ours at the start of the race HWREG(SYSCTL_RCGCGPIO) |= (SYSCTL_RCGCGPIO_R1); puts("\r\nPort B activated"); int Dummy = HWREG(SYSCTL_RCGCGPIO); /* set as a digital ports */ HWREG(GPIO_PORTB_BASE+GPIO_O_DEN) |= (LED1_BIT|LED2_BIT|LED3_BIT); Dummy = HWREG(SYSCTL_RCGCGPIO); /* set as input ports*/ HWREG(GPIO_PORTB_BASE+GPIO_O_DIR) &= ~(LED1_BIT|LED2_BIT|LED3_BIT); //decide which Kart is ours and assign names KartA and KartB to opponent Karts //this happens once per race initializeKartStatus(); printf("\r\n We are Kart %u", our_kart.kartNumber); // now we need to let the Run function init the lower level state machines // use LocalEvent to keep the compiler from complaining about unused var RunMasterSM(CurrentEvent); return; }
void main(void) { (void)printf("Start of E128 program\r\n"); InitPorts(); InitTimer(); InitMotors(); InitSide(); InitBeacons(); EnableInterrupts; StartMasterSM(); while(1) { // Repeat State Machine Forever (void)RunMasterSM(CheckEvents()); } }
/**************************************************************************** Function RunMasterSM Parameters ES_Event: the event to process Returns ES_Event: an event to return Description the run function for the top level state machine Notes uses nested switch/case to implement the machine. Author J. Edward Carryer, 02/06/12, 22:09 ****************************************************************************/ ES_Event RunMasterSM( ES_Event CurrentEvent ) { bool MakeTransition = false;/* are we making a state transition? */ ComState_t NextState = CurrentState; ES_Event EntryEventKind = { ES_ENTRY, 0 };// default to normal entry to new state ES_Event ReturnEvent = { ES_NO_EVENT, 0 }; // assume no error switch ( CurrentState ) { case WAITING : // If current state is state one // Execute During function for state one. ES_ENTRY & ES_EXIT are // processed here allow the lowere level state machines to re-map // or consume the event CurrentEvent = DuringWaitingState(CurrentEvent); //process any events if ( CurrentEvent.EventType != ES_NO_EVENT ) //If an event is active { switch (CurrentEvent.EventType) { case ES_TIMEOUT : //If event is event one if(CurrentEvent.EventParam == COM_TIMER) { // Execute action function for state one : event one NextState = PROCESSING;//Decide what the next state will be // for internal transitions, skip changing MakeTransition MakeTransition = true; //mark that we are taking a transition // if transitioning to a state with history change kind of entry // EntryEventKind.EventType = ES_ENTRY_HISTORY; // optionally, consume or re-map this event for the upper // level state machine ReturnEvent.EventType = ES_NO_EVENT; } break; // repeat cases as required for relevant events } } break; case PROCESSING: // Do the processing // Execute During function for state one. ES_ENTRY & ES_EXIT are // processed here allow the lowere level state machines to re-map // or consume the event CurrentEvent = DuringProcessingState(CurrentEvent); //process any events if ( CurrentEvent.EventType != ES_NO_EVENT ) //If an event is active { switch (CurrentEvent.EventType) { case DONE_PROCESSING : //If event is event one // Execute action function for state one : event one NextState = WAITING;//Decide what the next state will be // for internal transitions, skip changing MakeTransition MakeTransition = true; //mark that we are taking a transition // if transitioning to a state with history change kind of entry // EntryEventKind.EventType = ES_ENTRY_HISTORY; // optionally, consume or re-map this event for the upper // level state machine ReturnEvent.EventType = ES_NO_EVENT; break; // repeat cases as required for relevant events } } break; // repeat state pattern as required for other states } // If we are making a state transition if (MakeTransition == true) { // Execute exit function for current state CurrentEvent.EventType = ES_EXIT; RunMasterSM(CurrentEvent); CurrentState = NextState; //Modify state variable // Execute entry function for new state // this defaults to ES_ENTRY RunMasterSM(EntryEventKind); } // in the absence of an error the top level state machine should // always return ES_NO_EVENT, which we initialized at the top of func return(ReturnEvent); }