Sie sind auf Seite 1von 3

*************************************************************************************

Psuedo-code for the GameButton module (a service that implements a state machine)

InitializeGameButton
Takes a priority number, returns True.

Initialize the MyPriority variable with the passed in parameter.


Sample port line to initialize LastInputState
Use LastInputState to initialize LastTrashState, LastCompostState, and LastRecyclingState

Set CurrentState to Ready2Sample


Post ES_INIT to this service
End of InitializeEve

CheckButtonEvents
Takes no parameters, returns True if an event was posted
Local ReturnVal = False, CurrentInputState, MultipleDown = False

Get the CurrentInputState from the input line


Get CurrentTrashState from CurrentInputState
Get CurrentCompostState from CurrentInputState
Get CurrentRecyclingState from CurrentInputState
Get CurrentResetState from CurrentInputState

If the input state has changed


If the reset button is down
Post ES_RESET_BUTTON to this service
Set ReturnVal to true
Else If multiple states are high
Post Event ES_MULTIPLE_DOWN to this service
Set ReturnVal to true
Else
If CurrentTrashState has changed and is low
Post ES_TRASH_BUTTON to this service
Set ReturnVal to true
Else If the CurrentCompostState has changed and is low
Post ES_COMPOST_BUTTON to this service
Set ReturnVal to true
Else If the state of the CurrentRecyclingState has changed and is low
Post ES_RECYCLING_BUTTON to this service
Set ReturnVal to True
EndIf
EndIf
EndIf
If ReturnVal is true
Set ACTIVITY_TIMER TO ACTIVITY_TIME
EndIf
Set LastInputState to CurrentInputState
Set LastTrashState to CurrentTrashState
Set LastCompostState to CurrentCompostState
Set LastRecyclingState to CurrentRecyclingState
Return ReturnVal
End of CheckGameEvents

RunGameButtonSM (implements the state machine for GameButton)


Set ReturnEvent to ES_NO_EVENT
Local variables: NextState

Set NextState to CurrentState


Based on the state of the CurrentState variable choose one of the following blocks of code:

CurrentState is Debouncing
If ThisEvent is ES_TIMOUT and EventParam is BUTTON_DEBOUNCE_TIMER
Set NextState to Ready2Sample
EndIf
End of Debouncing Block

CurrentState is Ready2Sample
If ThisEvent is ES_TRASH_BUTTON
Start BUTTON_DEBOUNCE_TIMER to BUTTON_DEBOUNCE_TIME
Post ES_TRASH_BUTTON to Game SM
Set NextState to Debouncing
EndIf
If ThisEvent is ES_COMPOST_BUTTON
Start BUTTON_DEBOUNCE_TIMER to BUTTON_DEBOUNCE_TIME
Post ES_COMPOST_BUTTON to Game SM
Set NextState to Debouncing
EndIf
If ThisEvent is ES_RECYCLING_BUTTON
Start BUTTON_DEBOUNCE_TIMER to BUTTON_DEBOUNCE_TIME
Post ES_RECYCLING_BUTTON to Game SM
Set NextState to Debouncing
EndIf
If ThisEvent is ES_MULTIPLE_DOWN
Start BUTTON_DEBOUNCE_TIMER to BUTTON_DEBOUNCE_TIME
Post ES_MULTIPLE_DOWN to Game SM
Set NextState to Debouncing
EndIf
If ThisEvent is ES_RESET_BUTTON
Start BUTTON_DEBOUNCE_TIMER to BUTTON_DEBOUNCE_TIME
Post ES_RESET_BUTTON to all services
Set NextState to Debouncing
EndIf
End of Ready2Sample Block
Set CurrentState to NextState
Return ReturnEvent
End of RunGameButton
End of GameButtonSM

Das könnte Ihnen auch gefallen