State Diagram
Pseudocode
Pseudo-code for the TOT module (a service that implements a state machine)
Data private to the module: CurrentState, LastTOTState, WelcomeIncrement
InitTOT
Takes a priority number, returns True if no errors
Initialize the MyPriority variable with the passed in parameter
Call TOTInitialize
Set CurrentState to NoTOT
Get the current state of the coin sensor
Post the initial transition event
End of InitTOT
PostTOT
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 PostTOT
RunTOT
Takes an event ThisEvent with EventType that is one of ES_INIT, TOT_DETECTED, TOT_REMOVED, GAME_COMPLETED, or RESET
Returns ES_NO_EVENT if no error ES_ERROR otherwise
If ThisEvent is the idle timer timeout
Post a RESET event to all services
Else
Reinitialize the idle timer
Based on the state of the CurrentState variable choose one of the following blocks of code:
CurrentState is NoTOT
If ThisEvent is ES_INIT
Set the WelcomeIncrement to 0
Initialize the welcome timer
Else If ThisEvent is TOT_DETECTED
Set WelcomeIncrement to 0
Turn off all LEDs
Post START_POTATO to all services
Play the welcome audio clip
Change CurrentState to YesTOT
Else If ThisEvent is ES_TIMEOUT from the welcome timer
Light the correct LEDs based on the value of WelcomeIncrement modulo 9
Increment WelcomeIncrement by 1
Initialize the welcome timer
Else If ThisEvent is ES_TIMEOUT from the servo reset timer
Reset the timing servo to its original position
End
End NoTOT block
CurrentState is YesTOT
If ThisEvent is TOT_REMOVED
Post END_POTATO to all services
Initialize the next game timer
Change CurrentState to Waiting4NextGame
Else If ThisEvent is ES_TIMEOUT from the audio timer
Stop playing all audio
Else If ThisEvent is GAME_COMPLETED or RESET or ES_TIMEOUT from the TOT timer
Post END_POTATO to all services
Open the trapdoor to release the TOT
Initialize the next game timer
Change current state to Waiting4NextGame
End
End YesTOT block
CurrentState is Waiting4NextGame
If ThisEvent is ES_TIMEOUT
If EventParam is audio timer
Play the goodbye audio clip
Else If EventParam is the TOT timer
Close the trapdoor
Turn off all audio and LEDs
Set WelcomeIncrement to 0
Reset all timers
Change CurrentState to NoTOT
End
Else If ThisEvent is TOT_DETECTED
Close the trapdoor, turn off all audio and LEDs
Set WelcomeIncrement to 0
Post START_POTATO to all services
Play the welcome audio clip
Initialize the idle timer
Change CurrentState to YesTOT
Else If ThisEvent is TOT_REMOVED
Post END_POTATO to all services
Init the next game timer
End
End Waiting4NextGame block
End of RunTOT
QueryTOT
Return the CurrentState
End of QueryTOT
TOTInitialize
Set PB0, PB1 and PB3 as digital inputs
Set PB3 with an internal pull-up resistor
Initialize the PWM channels for the servos
Initialize the shift registers
End of TOTInitialize
ReleaseTOT
Open the trapdoor servo
End of ReleaseTOT
GetTOTState
Checks the state of the coin sensor, returns 1 if a TOT is inserted, and a 0 if no TOT is inserted
If the coin sensor pin is high
Return 1
Else
Return 0
End
End of GetTOTState
GetResetState
Checks the state of the reset button, returns 1 if a it is being pressed, and a 0 if it is not
If the button pin is high
Return 1
Else
Return 0
End
End of GetResetState
CheckTOTEvents
Event checker for TOT and button events
Get the current state of the TOT and the reset button
If the TOT state has changed
If the current TOT state is high
Post TOT_DETECTED to all services
Set ReturnVal to true
Else
Post TOT_REMOVED to all services
Set ReturnVal to true
End
End
If the reset button state is low
Post RESET to all services
Set ReturnVal to true
End
Change LastTOTState to CurrentTOTState
Return ReturnVal
End of CheckTOTEvents
Data private to the module: CurrentState, LastTOTState, WelcomeIncrement
InitTOT
Takes a priority number, returns True if no errors
Initialize the MyPriority variable with the passed in parameter
Call TOTInitialize
Set CurrentState to NoTOT
Get the current state of the coin sensor
Post the initial transition event
End of InitTOT
PostTOT
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 PostTOT
RunTOT
Takes an event ThisEvent with EventType that is one of ES_INIT, TOT_DETECTED, TOT_REMOVED, GAME_COMPLETED, or RESET
Returns ES_NO_EVENT if no error ES_ERROR otherwise
If ThisEvent is the idle timer timeout
Post a RESET event to all services
Else
Reinitialize the idle timer
Based on the state of the CurrentState variable choose one of the following blocks of code:
CurrentState is NoTOT
If ThisEvent is ES_INIT
Set the WelcomeIncrement to 0
Initialize the welcome timer
Else If ThisEvent is TOT_DETECTED
Set WelcomeIncrement to 0
Turn off all LEDs
Post START_POTATO to all services
Play the welcome audio clip
Change CurrentState to YesTOT
Else If ThisEvent is ES_TIMEOUT from the welcome timer
Light the correct LEDs based on the value of WelcomeIncrement modulo 9
Increment WelcomeIncrement by 1
Initialize the welcome timer
Else If ThisEvent is ES_TIMEOUT from the servo reset timer
Reset the timing servo to its original position
End
End NoTOT block
CurrentState is YesTOT
If ThisEvent is TOT_REMOVED
Post END_POTATO to all services
Initialize the next game timer
Change CurrentState to Waiting4NextGame
Else If ThisEvent is ES_TIMEOUT from the audio timer
Stop playing all audio
Else If ThisEvent is GAME_COMPLETED or RESET or ES_TIMEOUT from the TOT timer
Post END_POTATO to all services
Open the trapdoor to release the TOT
Initialize the next game timer
Change current state to Waiting4NextGame
End
End YesTOT block
CurrentState is Waiting4NextGame
If ThisEvent is ES_TIMEOUT
If EventParam is audio timer
Play the goodbye audio clip
Else If EventParam is the TOT timer
Close the trapdoor
Turn off all audio and LEDs
Set WelcomeIncrement to 0
Reset all timers
Change CurrentState to NoTOT
End
Else If ThisEvent is TOT_DETECTED
Close the trapdoor, turn off all audio and LEDs
Set WelcomeIncrement to 0
Post START_POTATO to all services
Play the welcome audio clip
Initialize the idle timer
Change CurrentState to YesTOT
Else If ThisEvent is TOT_REMOVED
Post END_POTATO to all services
Init the next game timer
End
End Waiting4NextGame block
End of RunTOT
QueryTOT
Return the CurrentState
End of QueryTOT
TOTInitialize
Set PB0, PB1 and PB3 as digital inputs
Set PB3 with an internal pull-up resistor
Initialize the PWM channels for the servos
Initialize the shift registers
End of TOTInitialize
ReleaseTOT
Open the trapdoor servo
End of ReleaseTOT
GetTOTState
Checks the state of the coin sensor, returns 1 if a TOT is inserted, and a 0 if no TOT is inserted
If the coin sensor pin is high
Return 1
Else
Return 0
End
End of GetTOTState
GetResetState
Checks the state of the reset button, returns 1 if a it is being pressed, and a 0 if it is not
If the button pin is high
Return 1
Else
Return 0
End
End of GetResetState
CheckTOTEvents
Event checker for TOT and button events
Get the current state of the TOT and the reset button
If the TOT state has changed
If the current TOT state is high
Post TOT_DETECTED to all services
Set ReturnVal to true
Else
Post TOT_REMOVED to all services
Set ReturnVal to true
End
End
If the reset button state is low
Post RESET to all services
Set ReturnVal to true
End
Change LastTOTState to CurrentTOTState
Return ReturnVal
End of CheckTOTEvents
Header
ES_Configure.h
Source Code
Download
tot.h | |
File Size: | 0 kb |
File Type: | h |
tot.c | |
File Size: | 12 kb |
File Type: | c |