Pseudo Code
Pseudo Code
ButtonService.c
Declare module variable MyPriority
Define module buttonstate variable CurrentState = InitButton
Set module variable DelayTime
Three Functions:
InitButtonService
Initializes Button Service during ES_Init
Input:
Priority of the service
Output:
Boolean. False only when there is initialization error
Set ES_Event as This Event, and set MyPriority = Priority
Post the Initialization event
PostButtonService
Input:
ES_Event This Event
Output:
Boolean. False when queue operation failed
Return ES_PostToService
End PostButtonService
RunButtonService
Input:
ES_Event This Event
Output:
ES_Event
Set ReturnEvent as ES_NO_EVENT
If CurrentState is InitButton
Set CurrentState as Debouncing
Call ES_Timer_InitTimer with DEBOUNCE_TIMER and DelayTime
Else if CurrentState is Debouncing
If the EventType is ES_TIMEOUT
Set CurrentState as Ready2Check
Else if CurrentState is Ready2Check
Call ES_Timer_InitTimer with DEBOUNCE_TIMER and DelayTime
Set CurrentSTate as Debouncing
Endif
Return ReturnEvent
End RunButtonService
ButtonSM.c
Priority
Boolean. False when error in initialization
LightService.c
QueryGameBrainSM
Return CurrentState
QueryGameBrainScoreSM
Return Score
Pseudo-code for the GuitarHeroSM.c
Data Private to GuitarHeroSM: CurrentState, MyPriority, Lowest16Bits, Highest16B
its, Highest8Bits
Array Private to GuitarHeroSM: GuitarNotes[9], GuitarNotesQueue[8], GuitarScreen
Characters[3], WinningSequence[], LosingSequence[]
InitGuitarHeroSM
Set MyPriority to value passed in
Set CurrentState to GuitarInactive
Pass in GUITAR_FLASH_TIMER, GUITAR_FLASH_DELAY to ES_Timer
RunGuitarHeroSM
Main EventType in this SM: ES_TIMEOUT, STRUM_OCCURED, GAME_OVER, GuitarInactive,
GameOverAnimation
Switch Block on CurrentState:
If CurrentState is GuitarActive
If ThisEvent.EventType is ES_TIMEOUT
If ThisEvent.EventParam is NOTE_TIMER
Shift and add notes
Set ES_TIMER_InitTimer
End if
If ThisEvent.EventParam is INACTIVE_TIMER
Post Event IncorrectStrum
End
End if
If CurrentState is STRUM_OCCURED
If ThisEvent.EventParam is GuitarNotesQueue[0]
Shift and Add Notes
Post Event Correct_Strum
Else
Post Event Incorrect_Strum
Post Event PlayEffect
End if
If CurrentState is GAME_OVER
Set CurrentState to GameOverAnimation
Set ES_TIMER to RESETTING
If CurrentState is GuitarInactive
If EventType is ES_TIMEOUT
If EventParam is GUITAR_FLASH_TIMER
Shift and Add Notes
Post NewWheelSegment Event
Post New target Event
End if
End if
If CurrentState is GAME_STARTED
For loop 8 times
Shift and add notes
InitStrumDebounceSM
Set MyPriority to the value passed in.
Activate Port E for Strum, Port D for Guitar Buttons
Activate Port F
Post the initial transition event
Set timer
CheckStrumEvents
Set ReturnVal to be false
Set CurrentButtonState
If CurrentButtonState not equal to LastStrumState
Set ReturnVal to be true
If CurrentButtonState is not equal to 0
Post event STRUM_DOWN
Else
Post STRUM_UP Event
End if
Set LastStrumState to be CurrentButtonState
Return ReturnVal
RunStrumDebounceSM
Event Types: Debouncing, Ready2Sample
Switch Block of CurrentState
If CurrentState is Debouncing
If Event type is ES_TIMEOUT
If EventParam is STRUM_TIMER
Set CurrentState tobe Ready2Sample
End if
End if
If CurrentState is Ready2Sample
If Event Type is STRUM_UP
Set StartTimer to be STRUM_TIMER
Set CurrentState to be Debouncing.
End if
If Event Type is STRUM_DOWN
Set StartTimer to be STRUM_TIMER
Set CurrentState to be Debouncing.
Post event STRUM_OCCURED
End
End Switch Block
Return ReturnEvent
QueryStrumDebounceSM
Return CurrentState
ReadGuitarButtons
Set ButtonVals to 0
Read ButtonVals from port