Escolar Documentos
Profissional Documentos
Cultura Documentos
MarioMovementSM
States:
PseudoState_MarioMovementSM, //PseudoState
MarioMoving,
MarioNotMoving
Intaking Events:
ES_TIMEOUT
MOVE_MARIO (from ControlSM when we have START_BUTTON_PRESSED event from Setup
to Ingame)
Posting Events:
*/
/* 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 "SR_HillAndMotor.h"
#include "ControlSM.h"
#include "MarioMovementSM.h"
// Hardware
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "inc/hw_sysctl.h"
// readability defines
/****************************************************************************
Function
InitMarioMovementSM
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 InitMarioMovementSM(uint8_t Priority)
{
MyPriority = Priority;
printf("\r \n Initializing MarioMovementSM");
ES_Event_t ThisEvent;
ThisEvent.EventType = ES_INIT;
if (ES_PostToService(MyPriority, ThisEvent) == true)
{
return true;
}
else
{
return false;
}
}
/****************************************************************************
Function
PostMarioMovementSM
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 PostMarioMovementSM(ES_Event_t ThisEvent)
{
return ES_PostToService(MyPriority, ThisEvent);
}
/****************************************************************************
Function
RunMarioMovementSM
Parameters
ES_Event_t : the event from its service queue to process in this State
Machine
Types of ES_Event_t: VISUAL_BLAST_OFF, VISUAL_BLAST_ON, GLOW_RANDOM_PIPE,
ES_TIMEOUT
Returns
ES_Event_t, ES_NO_EVENT if no error ES_ERROR otherwise
Description
add your description here
Notes
Author
****************************************************************************/
switch (CurrentState)
{
case PseudoState_MarioMovementSM: // 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 th
//switching state
CurrentState = MarioNotMoving;
}
}
break;
case MarioNotMoving:
{
//guard conditions. Should not move to MarioMoving if Mario is already at
the end.
//Should also make movements only when ControlSM is in InGame state
if ((!(HWREG(POSITION_GPIO_BASE + (GPIO_O_DATA + ALL_BITS)) &
MARIO_END_PIN_HI)) && (getControlSM_State() == ControlSM_GameState))
{
if ((ThisEvent.EventType == ES_TIMEOUT) || (ThisEvent.EventType ==
MOVE_MARIO))
{
//Restart timer for 1 sec movement
ES_Timer_InitTimer(MarioMovementTimer, HALF_SEC);
//switch state
CurrentState = MarioMoving;
}
}
}
break;
case MarioMoving:
{
if (ThisEvent.EventType == ES_TIMEOUT)
{
//Restart timer for 9 sec pause
ES_Timer_InitTimer(MarioMovementTimer, NINE_SEC);
//switching state
CurrentState = MarioNotMoving;
}
}
break;
}
return ReturnEvent;
}