GameSM State Machine
GameSM Pseudocode
START OF GameSM
/******************************************************************************/
GAME RUNNING MODULE STATE MACHINE PSEUDO-CODE
InitializeGameSM
Takes a priority number, returns True
Initialize the MyPriority variable with the passed in parameter
Initialize GoTimer
Initialize ServoTimer
Set CurrentState to be InitGameSM
post Event ES_INIT to InitGameSM (this service)
End
RunGameSM (implements the state machine for the Marathon Beekeeping Game)
The EventType field of ThisEvent will be of one: ES_INIT, ES_STOMPRISE, ES_BATONFALL, ES_STING, ES_FSRPINCH, ES_PLAYERLOST, ES_PLAYERWIN,
ES_HONEYWIN, ES_RESET
List of States: InitGamePState, WaitingForGo, Racing, PullingStinger, PlayerCelebrate, PlayerLose
Returns ES_NO_EVENT
Local Variables: NextState
beeCounter
servoCounter
Set NextState to CurrentState
Based on the state of the CurrentState variable, choose one of the following blocks of code:
CurrentState is InitGamePState
IF ThisEvent is ES_INIT
Set NextState to WaitingForGo
ENDIF
END
CurrentState is WaitingForGo
IF ThisEvent is ES_TIMEOUT
IF ThisEvent.EventParam is number for GoTimer
Turn off yellow LED
Turn on Green LED
Set NextState to Racing
post event to honeyClock service to start honey clock
ENDIF
IF ThisEvent is ES_RESET
call reset function
Set NextState to WaitingForGo
ENDIF
CurrentState is Racing
IF ThisEvent is ES_STOMPRISE
IF servoCount is equal to servoWinPosition
post ES_PLAYERWIN to RunGameSM
send player lose message to other c32, post ES_PLAYERWIN to comm module
Set NextState to PlayerCelebrate
ELSE
Advance player servo 1 step
Set NextState to Racing
IF ThisEvent is ES_BATONFALL
increment beeCounter
Set NextState to Racing
IF beeCounter is equal to beeThreshold
send STING message, post ES_STING to communication module
reset beecounter = 0
ENDIF
IF ThisEvent is ES_STING
Turn on stinger LEDs
Turn on vibration motor
Set NextState to PullingStinger
If ThisEvent is ES_PLAYERLOST
Move player servo to start position
Turn on vibration motor
Set NextState to PlayerLose
IF ThisEvent is ES_RESET
call reset function
Set NextState to WaitingForGo
ENDIF
CurrentState is PullingStinger
IF ThisEvent is ES_FSRPINCH
Turn off the stinger LEDS
Turn of the vibration motor
Set NextState to Racing
IF ThisEvent is ES_RESET
call reset function
Set NextState to WaitingForGo
ENDIF
CurrentState is PlayerCelebrate
IF ThisEvent is ES_PLAYERWIN
run the celebrate function
IF ThisEvent is ES_TIMEOUT
IF ThisEvent.Param is celebrate timer number and celebrate counter is less than threshold
move servo
toggle LED
toggle vibration motor
ENDIF
IF ThisEvent is ES_RESET
call reset function
Set NextState to WaitingForGo
ENDIF
CurrentState is PlayerLose
IF ThisEvent is ES_RESET
call reset function
Set NextState to WaitingForGo
ENDIF
Set CurrentState to NextState
Return ES_NO_EVENT
END of RunGameSM
/******************************************************************************/
Private Functions
/******************************************************************************/
void RunGameReset(void)
Purpose: resets the game components to their initial state
Turn off all LEDs
Move all servos back to starting position
Turn off vibration motor
post reset to communication module
post ES_RESET to InitSM
void celebrate(void)
make the servo move back and forth
pulse the leds
pulse the vibration motor
post ES_RESET to all modules, post to comm module to send reset message