This state machine manages the progress of the game, advancing in level when each goal has been reached and lighting the proper indicator LEDs to prompt the user to move the ball to the next goal.
State Diagram
Pseudocode
Pseudo-code for the ping pong game progress module (a service that implements a state machine)
Data private to the module: CurrentState
InitGame
Takes a priority number, returns True if no errors
Initialize the MyPriority variable with the passed in parameter
Call GameInitialize
Set CurrentState to GameStandby
Post the initial transition event
End of InitGame
PostGame
Posts an event to this state machine's queue (false if the enqueue operation failed, true otherwise)
Return post given event with parameters MyPriority and ThisEvent
End of PostGame
RunGame
Takes an event ThisEvent with EventType that is one of ES_INIT, ES_TIMEOUT, SPINNER_START, POS_MIDDLE, POS_TOP, or END_POTATO
Returns ES_NO_EVENT if no error ES_ERROR otherwise
If ThisEvent is not ES_TIMEOUT
Reinitialize the idle timer
End
Based on the state of the CurrentState variable choose one of the following blocks of code:
CurrentState is GameStandby
If ThisEvent is SPINNER_START
Light the middle LEDs
Stop all audio
Change CurrentState to Level1
End GameStandby block
CurrentState is Level1
If ThisEvent is POS_MIDDLE
Turn off the middle LEDs
Light the top LEDs
Play the level up audio clip
Change CurrentState to Level2
Else If ThisEvent is ES_TIMEOUT from the audio timer
Stop all audio
Else If ThisEvent is END_POTATO
Turn off all LEDs
Change CurrentState to GameStandby
End
End Level1 block
CurrentState is Level2
If ThisEvent is POS_TOP
Turn off the top LEDs
Light the middle LEDs
Play the level up audio clip
Change CurrentState to Level3
Else If ThisEvent is ES_TIMEOUT from the audio timer
Stop all audio
Else If ThisEvent is END_POTATO
Turn off all LEDs
Change CurrentState to GameStandby
End
End Level2 block
CurrentState is Level3
If ThisEvent is POS_MIDDLE
Light middle and top LEDs
Play the level up audio clip
Post PP_COMPLETED to all services
Change CurrentState to PingPong_Completed
Else If ThisEvent is ES_TIMEOUT from the audio timer
Stop all audio
Else If ThisEvent is END_POTATO
Turn off all LEDs
Change CurrentState to GameStandby
End
End Level3 block
CurrentState is PingPong_Completed
If ThisEvent is ES_TIMEOUT from the audio timer
Stop all audio
Else If ThisEvent is ES_TIMEOUT from the game timer
Turn off all LEDs
Else If ThisEvent is END_POTATO
Turn off all LEDs
Stop all audio
Change CurrentState to GameStandby
End
End PingPong_Completed block
End of RunGame
QueryGame
Return the CurrentState
End of QueryGame
GameInitialize
return
End of GameInitialize
LEDMiddle
Takes an on/off setting and toggles the middle LEDs
If setting is 0
Turn off the middle LEDs
Else
Turn on the middle LEDs
End of LEDMiddle
LEDTop
Takes an on/off setting and toggles the top LEDs
If setting is 0
Turn off the top LEDs
Else
Turn on the top LEDs
End of LEDTop
LEDSuccess
Takes an on/off setting and toggles the middle and top LEDs
If setting is 0
Turn off the middle LEDs
Turn off the top LEDs
Else
Turn on the middle LEDs
Turn on the top LEDs
End of LEDSuccess
Data private to the module: CurrentState
InitGame
Takes a priority number, returns True if no errors
Initialize the MyPriority variable with the passed in parameter
Call GameInitialize
Set CurrentState to GameStandby
Post the initial transition event
End of InitGame
PostGame
Posts an event to this state machine's queue (false if the enqueue operation failed, true otherwise)
Return post given event with parameters MyPriority and ThisEvent
End of PostGame
RunGame
Takes an event ThisEvent with EventType that is one of ES_INIT, ES_TIMEOUT, SPINNER_START, POS_MIDDLE, POS_TOP, or END_POTATO
Returns ES_NO_EVENT if no error ES_ERROR otherwise
If ThisEvent is not ES_TIMEOUT
Reinitialize the idle timer
End
Based on the state of the CurrentState variable choose one of the following blocks of code:
CurrentState is GameStandby
If ThisEvent is SPINNER_START
Light the middle LEDs
Stop all audio
Change CurrentState to Level1
End GameStandby block
CurrentState is Level1
If ThisEvent is POS_MIDDLE
Turn off the middle LEDs
Light the top LEDs
Play the level up audio clip
Change CurrentState to Level2
Else If ThisEvent is ES_TIMEOUT from the audio timer
Stop all audio
Else If ThisEvent is END_POTATO
Turn off all LEDs
Change CurrentState to GameStandby
End
End Level1 block
CurrentState is Level2
If ThisEvent is POS_TOP
Turn off the top LEDs
Light the middle LEDs
Play the level up audio clip
Change CurrentState to Level3
Else If ThisEvent is ES_TIMEOUT from the audio timer
Stop all audio
Else If ThisEvent is END_POTATO
Turn off all LEDs
Change CurrentState to GameStandby
End
End Level2 block
CurrentState is Level3
If ThisEvent is POS_MIDDLE
Light middle and top LEDs
Play the level up audio clip
Post PP_COMPLETED to all services
Change CurrentState to PingPong_Completed
Else If ThisEvent is ES_TIMEOUT from the audio timer
Stop all audio
Else If ThisEvent is END_POTATO
Turn off all LEDs
Change CurrentState to GameStandby
End
End Level3 block
CurrentState is PingPong_Completed
If ThisEvent is ES_TIMEOUT from the audio timer
Stop all audio
Else If ThisEvent is ES_TIMEOUT from the game timer
Turn off all LEDs
Else If ThisEvent is END_POTATO
Turn off all LEDs
Stop all audio
Change CurrentState to GameStandby
End
End PingPong_Completed block
End of RunGame
QueryGame
Return the CurrentState
End of QueryGame
GameInitialize
return
End of GameInitialize
LEDMiddle
Takes an on/off setting and toggles the middle LEDs
If setting is 0
Turn off the middle LEDs
Else
Turn on the middle LEDs
End of LEDMiddle
LEDTop
Takes an on/off setting and toggles the top LEDs
If setting is 0
Turn off the top LEDs
Else
Turn on the top LEDs
End of LEDTop
LEDSuccess
Takes an on/off setting and toggles the middle and top LEDs
If setting is 0
Turn off the middle LEDs
Turn off the top LEDs
Else
Turn on the middle LEDs
Turn on the top LEDs
End of LEDSuccess
Header
ES_Configure.h
Source Code
Download
game.h | |
File Size: | 0 kb |
File Type: | h |
game.c | |
File Size: | 8 kb |
File Type: | c |