Professional Documents
Culture Documents
Definitions
"ES_Configure.h" /* gets us event definitions */
"ES_Types.h"
/* gets bool type for returns */
"ES_Framework.h"
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
//used for gpio
#include "ES_Port.h"
#include "termio.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "inc/hw_sysctl.h"
#include "driverlib/gpio.h"
#define ALL_BITS (0xff<<2)
// Event
#include
#include
#include
Definitions
"ES_Configure.h" /* gets us event definitions */
"ES_Types.h"
/* gets bool type for returns */
"ES_Framework.h"
}
/****************************************************************************
Function
PostGameFSM
Parameters
ES_Event ThisEvent , the event to post to the queue
Returns
boolean False if the Enqueue operation failed, True otherwise
Description
Posts an event to this state machine's queue
Notes
Final Edit: Dongao Yang 11/25/14
****************************************************************************/
bool PostGameFSM( ES_Event ThisEvent )
{
return ES_PostToService( MyPriority, ThisEvent);
}
/****************************************************************************
Function
RunGameFSM
Parameters
ES_Event : the event to process
Returns
ES_Event, ES_NO_EVENT if no error ES_ERROR otherwise
Description
Runs main state machine for the whole game
Final Edit: Dongao Yang 11/25/14
****************************************************************************/
ES_Event RunGameFSM( ES_Event ThisEvent )
{
// Event to return at the end, ES_NO_EVENT
ES_Event ReturnEvent;
ReturnEvent.EventType = ES_NO_EVENT; // assume no errors
// Event to post to countdown SM
ES_Event TimerEvent;
// Event to post to Broom SM
ES_Event BroomEvent;
// Set Next state to current state
GameState_t NextState = CurrentState;
// variable to track the current score
static uint8_t score;
/*In this switch statement, the possible states are: TerrorState,
WaitForTiltInput, WaitForScore, WaitForSnitchPress, and HappyState*/
// switch on current state
switch ( CurrentState )
{
break;
// If current state is wait for tilt
case WaitForTiltInput :
// switch on event type
switch ( ThisEvent.EventType )
{
//if the event is LEFT_TILT_DETECTED
case LEFT_TILT_DETECTED :
//set red target LEDs status to left
redLEDStatus = leftRedLED;
//light new arrangement on target LED
LightTargetLED(redLEDStatus | greenLEDStatus);
//If left is the right tilt
if(openHoop == LEFT_HOOP)
{
//Set next state to wait for score
NextState = WaitForScore;
}
break;
//if the event is CENTER_TILT_DETECTED
case CENTER_TILT_DETECTED :
//set red target LEDs status to center
redLEDStatus = centerRedLED;
//light new arrangement on target LED
LightTargetLED(redLEDStatus | greenLEDStatus);
//If center is the right tilt
if(openHoop == CENTER_HOOP)
{
//Set next state to wait for score
NextState = WaitForScore;
}
break;
//if the event is RIGHT_TILT_DETECTED
case RIGHT_TILT_DETECTED :
//set red target LEDs status to right
redLEDStatus = rightRedLED;
//light new arrangement on target LED
LightTargetLED(redLEDStatus | greenLEDStatus);
//If right is the right tilt
if(openHoop == RIGHT_HOOP)
{
//Set next state to wait for score
NextState = WaitForScore;
}
break;
//If event is ES_TIMEOUT
case ES_TIMEOUT :
//switch on event param (timer)
switch(ThisEvent.EventParam)
{
//If main timer expired
case GAME_TIMER:
//Post STOP_COUNT to countdown module
TimerEvent.EventType = STOP_COUNT;
PostCountdownFSM(TimerEvent);
//Light all timer LEDs
LightAllTimerLEDs();
//Close all hoops
MoveHoopDown(BIT0HI);
MoveHoopDown(BIT1HI);
MoveHoopDown(BIT2HI);
//Start the reset timer, 15S
ES_Timer_InitTimer(HAPPY_TIMER, 15000);
//Post CAPTURE_SNITCH_READY event to Broom
BroomEvent.EventType = CAPTURE_SNITCH_READY;
PostBroomFSM( BroomEvent);
//Set next state to HappyState
NextState = HappyState;
break;
//If snitch timer expired
case SNITCH_TIMER:
//Set snitch key value to 1
SnitchTimerRunning = 1;
break;
//If Green LED timer expired
case GLED_TIMER:
//Set all green light to 0
greenLEDStatus = 0;
//light new arrangement on target LED
LightTargetLED(redLEDStatus | greenLEDStatus);
//Shut down the vibration
VibrationOff();
break;
}// end switch on timer
break;
default :
;
}// end switch on event type
break;
// If the current state is wait for score
case WaitForScore :
// Switch on event type
switch(ThisEvent.EventType)
{
//If the event is LEFT_TILT_DETECTED
case LEFT_TILT_DETECTED :
//set red target LEDs status to left
redLEDStatus = leftRedLED;
//light new arrangement on target LED
LightTargetLED(redLEDStatus | greenLEDStatus);
//if left is not right tilt
if(openHoop != LEFT_HOOP)
{
//Set next state to wait for tilt
NextState = WaitForTiltInput;
}
break;
//If the event is CENTER_TILT_DETECTED
case CENTER_TILT_DETECTED :
//set red target LEDs status to center
redLEDStatus = centerRedLED;
//light new arrangement on target LED
LightTargetLED(redLEDStatus | greenLEDStatus);
ES_Timer_InitTimer(BUTTONBLINK_TIMER, 1000);
//Set Next state to TerrorState
NextState = TerrorState;
}
}
break;
default :
;
} // end switch on current state
// set current state to next state
CurrentState = NextState;
// return ES_NO_EVENT
return ReturnEvent;
}
/***************************************************************************
private functions
***************************************************************************/
/****************************************************************************
Function
PickNextHoop
Parameters
nothing
Returns
nothing
Description
Randomly pick a hoop, can't choose repeat hoop
Final Edit: Dongao Yang 11/25/14
****************************************************************************/
void PickNextHoop(void)
{
//Generate a random number from 0 to 1
double random = (double)rand() / (double)RAND_MAX ;
//If current hoop is left
if (openHoop == LEFT_HOOP)
{
//if random number is less than 0.5
if (random<0.5)
// next hoop is center hoop
openHoop = CENTER_HOOP;
else
// next hoop is right hoop
openHoop = RIGHT_HOOP;
}
//If current hoop is center
else if(openHoop == CENTER_HOOP)
{
//if random number is less than 0.5
if (random<0.5)
// next hoop is left hoop
openHoop = LEFT_HOOP;
else
// next hoop is right hoop
openHoop = RIGHT_HOOP;
}
//If current hoop is right
else if(openHoop== RIGHT_HOOP)
{
//if random number is less than 0.5
if (random<0.5)
// next hoop is center hoop
openHoop = CENTER_HOOP;
else
// next hoop is left hoop
openHoop = LEFT_HOOP;
}
}