Академический Документы
Профессиональный Документы
Культура Документы
GAME_STATE_MACHINE
This state machine incorporates part of the game except the scoring
States:
PseudoState_Game
Uninitialized
SelectingPipe
WaterFlowing
IRRead
WaitforReset
Intaking Events:
Hardware events:
IR_N0_CONNECTION
IR_CONNECTION
Internal Events:
ACTIVATE_RANDOM_PIPE
TIMEOUT_EVENTS
PIPE_TIMER_TIMEOUT
SCORE_TIMER_TIMEOUT
Posting Events:
*/
/*
****************************************
REMOVE
*/
#include "SR_HillAndMotor.h"
// Hardware
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "inc/hw_sysctl.h"
/* 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
*/
/****************************************************************************
Function
InitGameSM
Parameters
uint8_t : the priorty of this service
Returns
bool, false if error in initialization, true otherwise
Description
Saves away the priority, and does any
other required initialization for this service
Notes
Author
****************************************************************************/
bool InitGameSM(uint8_t Priority)
{
MyPriority = Priority;
printf("Initializing GameSM \n\r");
*/
// set up port C and Port E by enabling the peripheral clock, waiting for the
// peripheral to be ready and setting the direction of PE1-PE5 as input and
PC4 to PC7 as output
//Initializing SRPipe
SRPipe_Init();
ES_Event_t ThisEvent;
ThisEvent.EventType = ES_INIT;
if (ES_PostToService(MyPriority, ThisEvent) == true)
{
return true;
}
else
{
return false;
}
}
/****************************************************************************
Function
PostGameSM
Parameters
ES_Event_t ThisEvent ,the event to post to the queue
Returns
bool false if the Enqueue operation failed, true otherwise
Description
Posts an event to this state machine's queue
Notes
Author
****************************************************************************/
bool PostGameSM(ES_Event_t ThisEvent)
{
return ES_PostToService(MyPriority, ThisEvent);
}
/****************************************************************************
Function
RunGameSM
Parameters
ES_Event_t : the event from its service queue to process in this State
Machine
Types of ES_Event_t:
Returns
ES_Event_t, ES_NO_EVENT if no error ES_ERROR otherwise
Description
Runs InGame state machine
Notes
Author
J. Edward Carryer, 01/15/12, 15:23
****************************************************************************/
ES_Event_t RunGameSM(ES_Event_t ThisEvent)
{
ES_Event_t ReturnEvent;
ReturnEvent.EventType = ES_NO_EVENT; // assume no errors
//if not in the InGame state from ControlSM, no point being here in the GameSM
if (getControlSM_State() != InGame)
{
printf("\r \n Not in Game state for now");
//not posting any reset, just going to the Uninitialized state if not in the
InGame sate in ControlSM
CurrentState = Uninitialized_Game;
}
switch (CurrentState)
{
case PseudoState_Game: // If current state is initial Psedudo State
{
if (ThisEvent.EventType == ES_INIT) // only respond to ES_Init
{
// this is where you would put any actions associated with the
// transition from the initial pseudo-state into the actual
// initial state
case Uninitialized_Game:
{
if (ThisEvent.EventType == START_GAME)
{
//actions
/*
Send ACTIVATE_RANDOM_PIPE back to this service
Set Pipe timer
Set score timer
initialize the random pipe
*/
random_pipe_index = 0;
case SelectingPipe:
{
if (ThisEvent.EventType == ACTIVATE_RANDOM_PIPE) //Leaf removed
{
//actions
/*
Select a Random Pipe and the events now will be based on that
Start Pipe Timer
Start Score Timer
Start Pipe Lights for the selected pipe
*/
random_pipe_index = rand() % 5 + 1; //pipes from 1-5
//random_pipe_index = 4 + 1; checking pipe 5
printf("\r \n %d pipe selected", random_pipe_index);
ES_Timer_InitTimer(PIPE_TIMER, pipeTime);
ES_ShortTimerStart(TIMER_A, SCORE_TIME);
//restarting the water flow timer
ES_Timer_InitTimer(WATER_FLOW_TIMER, WaterFlowTime);
case WaterFlowing:
{
if (ThisEvent.EventType == ES_SHORT_TIMEOUT) //Score timer timeout
{
//actions
/*
Reset score timer -- not required. Will just directly start it
*/
puts("\r \n WaterFlowing --> IRRead");
CurrentState = IRRead;
/*******************
*/
uint8_t Value = SR_HillAndMotor_GetCurrentRegister();
printf("\r \n %d", Value);
//restart timer
ES_Timer_InitTimer(WATER_FLOW_TIMER, WaterFlowTime);
CurrentState = WaterFlowing;
}
else if (ThisEvent.EventType == MARIO_END) //MARIO has reached the end.
Timer off
{
//actions
//Turning off all pipes
ActivatePipeLED(0);
puts("\r \n WaterFlowing--> WaitForReset");
CurrentState = WaitForReset;
}
else if ((ThisEvent.EventType == LEAF_RESET) || (ThisEvent.EventType ==
INACTIVE_RESET))
{
//Turning off all pipes
ActivatePipeLED(0);
puts("\r \n WaterFlowing--> Uninitialized");
CurrentState = Uninitialized_Game;
}
}
break;
case IRRead:
{
if (ThisEvent.EventType == MARIO_END) //Leaf removed
{
//actions
//Turning off all pipes
ActivatePipeLED(0);
puts("\r \n IRRead--> WaitForReset");
CurrentState = WaitForReset;
}
else if ((ThisEvent.EventType == LEAF_RESET) || (ThisEvent.EventType ==
INACTIVE_RESET))
{
//Turning off all pipes
ActivatePipeLED(0);
puts("\r \n IRRead --> Uninitialized");
CurrentState = Uninitialized_Game;
}
else if (ThisEvent.EventType == SCORE_CHECK)
{
//deterministic non-blocking internal check for transitioning back to
the WaterFlowing state
if (CheckIRConnection())
{
//puts("\r \n No Bucket Placed");
//actions
/*
Start score timer
play the bad sound
Reset and start the inactivity timer
*/
//puts("\r \n Posting SCORE_DEC to ScoringSM");
ES_Event_t PostingEvent;
PostingEvent.EventType = SCORE_DEC;
PostScoringSM(PostingEvent);
ES_ShortTimerStart(TIMER_A, SCORE_TIME);
//ES_Timer_InitTimer(INACTIVE_TIMER, THIRTY_SEC);
case WaitForReset:
{
if ((ThisEvent.EventType == LEAF_RESET) || (ThisEvent.EventType ==
INACTIVE_RESET))
{
//actions
return ReturnEvent;
}