/**************************************************************************** Module MasterSM.c 2.0.1 Description This is a template for the top level Hierarchical state machine Notes History When Who What/Why -------------- --- -------- 02/16/14 bc created ****************************************************************************/ /*----------------------------- Include Files -----------------------------*/ /* include header files for this state machine as well as any machines at the next lower level in the hierarchy that are sub-machines to this machine */ #include "ES_Configure.h" #include "ES_Framework.h" #include "MasterSM.h" #include "JSR.h" #include "GameSM.h" #include "DriveMotors.h" #include "Pins.h" #include "LauncherControl.h" /*----------------------------- Module Defines ----------------------------*/ /*---------------------------- Module Functions ---------------------------*/ static ES_Event DuringMaster( ES_Event Event); /*---------------------------- Module Variables ---------------------------*/ // everybody needs a state variable, though if the top level state machine // is just a single state container for orthogonal regions, you could get // away without it // static MasterState_t CurrentState; // the top level is just a single state // container, so we don't need a state variable // with the introduction of Gen2, we need a module level Priority var as well static uint8_t MyPriority; /*------------------------------ Module Code ------------------------------*/ /**************************************************************************** Function InitMasterSM Parameters uint8_t : the priorty of this service Returns boolean, False if error in initialization, True otherwise Description Saves away the priority, and starts the top level state machine Notes Author J. Edward Carryer, 02/06/12, 22:06 ****************************************************************************/ bool InitMasterSM ( uint8_t Priority ) { ES_Event ThisEvent; MyPriority = Priority; // save our priority ThisEvent.EventType = ES_ENTRY; // Start the Master State machine StartMasterSM( ThisEvent ); return true; } /**************************************************************************** Function PostMasterSM Parameters ES_Event ThisEvent , the event to post to the queue Returns boolean False if the post operation failed, True otherwise Description Posts an event to this state machine's queue Notes Author J. Edward Carryer, 10/23/11, 19:25 ****************************************************************************/ bool PostMasterSM( ES_Event ThisEvent ) { return ES_PostToService( MyPriority, ThisEvent); } /**************************************************************************** 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 ****************************************************************************/ // make recursive call warning into info #pragma MESSAGE INFORMATION C1855 ES_Event RunMasterSM( ES_Event CurrentEvent ) { // start by passing events to the lower level machines // this is the 'during' function for this machine CurrentEvent = DuringMaster(CurrentEvent); // there is only 1 state at this level and it is // running the GameSM state machines. // we only process 1 event at this level, the QUERY_TIMER ES_TIMEOUT // and this is an internal transition /**************************/ /*Always IN GAMEPLAY state*/ if(CurrentEvent.EventType == EV_END_MATCH){ //pass on ES_EXIT event and run the during function CurrentEvent.EventType = ES_EXIT; RunMasterSM(CurrentEvent); //re-enters the GAMEPLAY state CurrentEvent.EventType = ES_ENTRY; RunMasterSM(CurrentEvent); //Turn off LED PORT_STATUS_LED &= ~PIN_STATUS_LED; } /***************************/ // in the absence of an error the top level state machine should // always return ES_NO_EVENT CurrentEvent.EventType = ES_NO_EVENT; return(CurrentEvent); } /**************************************************************************** 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 ) { // local variable to get debugger to display the value of CurrentEvent volatile ES_Event LocalEvent = CurrentEvent; // if there is more than 1 state to the top level machine you will need // to initialize the state variable // CurrentState = StateOne; // 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(LocalEvent); return; } /*************************************************************************** private functions ***************************************************************************/ static ES_Event DuringMaster( ES_Event Event ) { // process ES_ENTRY & ES_EXIT events if ( Event.EventType == ES_ENTRY) { // implement any entry actions required for this state machine // after that start any lower level machines that run in this state StartGameSM(Event); }else if ( Event.EventType == ES_EXIT) { // on exit, give the lower levels a chance to clean up first RunGameSM(Event); // Turn Off all motors StopMotor(); TurnOffLauncher(); }else // do the 'during' function for this state { // run any lower level state machine RunGameSM(Event); } return(Event); }