This state machine manages interactions with the microphone, tracking the duration of the user's blowing into it and lights up LEDs in sequence.
State Diagram
Pseudocode
Pseudo-code for the microphone blower module (a service that implements a state machine)
Data private to the module: CurrentState, LastMicState
InitBlower
Takes a priority number, returns True if no errors
Initialize the MyPriority variable with the passed in parameter
Call BlowerInitialize
Set CurrentState to BlowerStandby
Set LastMicState to the current state of the microphone
Post the initial transition event
End of InitBlower
PostBlower
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 PostBlower
RunSpinner
Takes an event ThisEvent with EventType that is one of ES_INIT, ES_TIMEOUT, SPINNER_START, SPINNER_STOP, BLOWING_START, or PP_COMPLETED
Returns ES_NO_EVENT if no error ES_ERROR otherwise
Initialize an unsigned integer increment as 0
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 BlowerStandby
If ThisEvent is PP_COMPLETED
Change CurrentState to NotBlowing
Else If ThisEvent is SPINNER_START
Check the state of the Game state machine
If GameState is PingPong_Completed
Set increment to 0
Change CurrentState to NotBlowing
End
End
End BlowerStandby block
CurrentState is NotBlowing
If ThisEvent is SPINNER_STOP
Set increment to 0
Turn off all LEDs
Change CurrentState to BlowerStandby
Else If ThisEvent is BLOWING_START
Initialize the blowing timer
Initialize the next LED timer
Change CurrentState to Blowing
End
End NotBlowing block
CurrentState is Blowing
If ThisEvent is SPINNER_STOP
Turn off all LEDs
Set increment to 0
Change CurrentState to BlowerStandby
Else If ThisEvent is ES_TIMEOUT from the audio timer
Stop all audio
Else If ThisEvent is ES_TIMEOUT from the next LED timer
Increment increment by 1
Stop all audio
If increment is not 4
Play the "pop" audio clip
End
Light the number of LEDs equal to increment
If increment is 4
Play the "success" audio clip
Set increment to 0
Post GAME_COMPLETED to all services
Change CurrentState to BlowerStandby
End
Initialize the next LED timer
Else If ThisEvent is ES_TIMEOUT from the blowing timer
Set increment to 0
Turn off all LEDs
Change CurrentState to NotBlowing
Else If ThisEvent is BLOWING_START
Initialize the blowing timer
End
End Blowing block
End of RunBlower
QueryBlower
Return the CurrentState
End of QueryBlower
BlowerInitialize
Initialize the shift register pins
Set PB2 as a digital input
End of BlowerInitialize
LED_on
Takes an integer num and lights the appropriate number of LEDs
Based on the value of the num variable choose one of the following blocks of code:
num is 0
Turn off all 4 microphone LEDs
End 0 block
num is 1
Turn on the first mic LED
End 1 block
num is 2
Turn on the second mic LED
End 2 block
num is 3
Turn on the third mic LED
End 3 block
num is 4
Turn on the fourth mic LED
End 4 block
End of LED_on
GetMicState
Checks the state of the microphone pin, returns 1 if high, and a 0 if low
If the mic pin is high
Return 1
Else
Return 0
End
End of GetMicState
CheckBlowerEvents
Event checker for the microphone blower
Get the current state of the microphone pin
If the microphone state has changed
Post BLOWING_START to Blower
Set ReturnVal to true
End
Change LastMicState to CurrentMicState
Return ReturnVal
End of CheckBlowerEvents
Data private to the module: CurrentState, LastMicState
InitBlower
Takes a priority number, returns True if no errors
Initialize the MyPriority variable with the passed in parameter
Call BlowerInitialize
Set CurrentState to BlowerStandby
Set LastMicState to the current state of the microphone
Post the initial transition event
End of InitBlower
PostBlower
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 PostBlower
RunSpinner
Takes an event ThisEvent with EventType that is one of ES_INIT, ES_TIMEOUT, SPINNER_START, SPINNER_STOP, BLOWING_START, or PP_COMPLETED
Returns ES_NO_EVENT if no error ES_ERROR otherwise
Initialize an unsigned integer increment as 0
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 BlowerStandby
If ThisEvent is PP_COMPLETED
Change CurrentState to NotBlowing
Else If ThisEvent is SPINNER_START
Check the state of the Game state machine
If GameState is PingPong_Completed
Set increment to 0
Change CurrentState to NotBlowing
End
End
End BlowerStandby block
CurrentState is NotBlowing
If ThisEvent is SPINNER_STOP
Set increment to 0
Turn off all LEDs
Change CurrentState to BlowerStandby
Else If ThisEvent is BLOWING_START
Initialize the blowing timer
Initialize the next LED timer
Change CurrentState to Blowing
End
End NotBlowing block
CurrentState is Blowing
If ThisEvent is SPINNER_STOP
Turn off all LEDs
Set increment to 0
Change CurrentState to BlowerStandby
Else If ThisEvent is ES_TIMEOUT from the audio timer
Stop all audio
Else If ThisEvent is ES_TIMEOUT from the next LED timer
Increment increment by 1
Stop all audio
If increment is not 4
Play the "pop" audio clip
End
Light the number of LEDs equal to increment
If increment is 4
Play the "success" audio clip
Set increment to 0
Post GAME_COMPLETED to all services
Change CurrentState to BlowerStandby
End
Initialize the next LED timer
Else If ThisEvent is ES_TIMEOUT from the blowing timer
Set increment to 0
Turn off all LEDs
Change CurrentState to NotBlowing
Else If ThisEvent is BLOWING_START
Initialize the blowing timer
End
End Blowing block
End of RunBlower
QueryBlower
Return the CurrentState
End of QueryBlower
BlowerInitialize
Initialize the shift register pins
Set PB2 as a digital input
End of BlowerInitialize
LED_on
Takes an integer num and lights the appropriate number of LEDs
Based on the value of the num variable choose one of the following blocks of code:
num is 0
Turn off all 4 microphone LEDs
End 0 block
num is 1
Turn on the first mic LED
End 1 block
num is 2
Turn on the second mic LED
End 2 block
num is 3
Turn on the third mic LED
End 3 block
num is 4
Turn on the fourth mic LED
End 4 block
End of LED_on
GetMicState
Checks the state of the microphone pin, returns 1 if high, and a 0 if low
If the mic pin is high
Return 1
Else
Return 0
End
End of GetMicState
CheckBlowerEvents
Event checker for the microphone blower
Get the current state of the microphone pin
If the microphone state has changed
Post BLOWING_START to Blower
Set ReturnVal to true
End
Change LastMicState to CurrentMicState
Return ReturnVal
End of CheckBlowerEvents
Header
ES_Configure.h
Source Code
Download
blower.h | |
File Size: | 0 kb |
File Type: | h |
blower.c | |
File Size: | 8 kb |
File Type: | c |