Vous êtes sur la page 1sur 3

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

Pseudo-code for the Wheel module (a service that implements a state machine)

InitializeWheel
Takes a priority number, returns True.

Initialize the MyPriority variable with the passed in parameter.


Initialize EncoderCount and PictureCount
Sample port line to initialize last difficulty reading
Sample port line to initialize LastWheelPicState
Post Event ES_INIT to this service
End of InitializeWheel

CheckDifficultyDialEvents
Set ReturnVal to false
Sample line to get CurrentDifficultyReading
If CurrentDifficultyReading has changed
Set CurrentAnswerTimeout to appropriate value based on reading
Reset ACTIVITY_TIMER to ACTIVITY_TIME
Set ReturnVal to true
EndIf
Return ReturnVal
End of CheckDifficultyDialEvents

CheckWheelPicture
Set ReturnVal to false
Sample port line to get CurrentWheelPicState
Check if CurrentWheelPicState has changed
If CurrentWheelPicState is low
Increment EncoderCount
If EncoderCount mod ECONDER_PULSES_PER_STOP is 0
If PictureCount is equal to number of pictures
Set PictureCount to 0
EndIf
Set NewPicType using PictureCount from WheelPicTypes array
Increment PictureCount
Post ES_NEW_PICTURE with EventParam NewPicType to Game and Wheel
Set ReturnVal to true
EndIf
EndIf
Set LastWheelPicState to CurrentWheelPicState
Return ReturnVal
End of CheckWheelPicture
RunWheelSM
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 NotSpinningWheel
If ThisEvent is ES_INIT
Stop Wheel Motor
EndIf
If ThisEvent is ES_LEAF_INSERTED
Initialize EncoderCount and PictureCount
Set NextState to WaitingToPlayWheel
EndIf
End NotSpinningWheel block

CurrentState is WaitingToPlayWheel
If ThisEvent is ES_DOOR_CLOSED
Start motor at correct speed
Set NextState to SpinningWheel
Post to LEDStrip WHEEL_SPINNING_LIGHT_TYPE event with EventParam
WHEEL_SPINNING_LIGHT_COLOR
EndIf
If ThisEvent is ES_RESET_BUTTON
Set NextState to NotSpinningWheel
EndIf
If ThisEvent is ES_GAME_LOST
Set NextState to NotSpinningWheel
EndIf
End WaitingToPlayWheel block

CurrentState is SpinningWheel
If ThisEvent is ES_GAME_LOST
Stop motion
Set NextState to NotSpinningWheel
EndIf
If ThisEvent is ES_GAME_WON
Stop motion
Set NextState to NotSpinningWheel
EndIf
If ThisEvent is ES_RESET_BUTTON
Stop motion
Set NextState to NotSpinningWheel
EndIf
If ThisEvent is ES_NEW_PICTURE
Stop motion
Set WHEEL_PAUSE_TIMER to CurrentAnswerTimeout
Set NextState to PausingWheel
Post to LEDStrip WHEEL_PAUSED_LIGHT_TYPE with EventParam
WHEEL_PAUSED_LIGHT_COLOR
EndIf
End SpinningWheel block

CurrentState is PausingWheel
If ThisEvent is ES_TIMEOUT and EventParam is WHEEL_PAUSE_TIMER
Start motor to correct speed
Post LEDStrip to WHEEL_SPINNING_LIGHT_TYPE with EventParam
WHEEL_SPINNING_LIGHT_COLOR
Set NextState to SpinningWheel
If ThisEvent is ES_GAME_LOST
Stop motion
Set NextState to NotSpinningWheel
EndIf
If ThisEvent is ES_GAME_WON
Stop motion
Set NextState to NotSpinningWheel
EndIf
If ThisEvent is ES_RESET_BUTTON
Stop motion
Set NextState to NotSpinningWheel
EndIf
End PausingWheel block

Set CurrentState to NextState

Return ReturnValue
End RunWheelSM

Vous aimerez peut-être aussi