Pseudoleaf
Pseudoleaf
Pseudo-code for the Leaf module (a service that implements a state machine)
InitializeLeaf
Takes a priority number, returns True.
CheckLeafEvents
Takes no parameters, returns True if an event was posted
Local ReturnVal = False, CurrentInputState
LocalVariables: NextState
CurrentState is InitLeafState
If ThisEvent is ES_INIT
Close LEAF servo
Post event to LEDStrip to change lights for leaf actions
Set NextState to NoLeaf
EndIf
If ThisEvent is ES_RESET_BUTTON
Close LEAF servo
Post event to LEDStrip to change lights for leaf actions
EndIf
End InitLeaf block
CurrentState is NoLeaf
If ThisEvent is ES_LEAF_INSERTED
Post event to LEDStrip for LeafInserted
Start ACTIVITY_TIMER to 30s
Set NextState to LeafNotDone
EndIf
End NoLeaf block
CurrentState is LeafNotDone
If ThisEvent is ES_LEAF_REMOVED
If TurnTableSM is in Motion
Post LEDStrip event for LeafRemovedEarly
Post ES_GAME_LOST to List1
Set NextState to InitLeafState
Else
Post LEDStrip event for initial leaf
Set NextState to NoLeaf
Stop ACTIVITY_TIMER
Post ES_RESET_BUTTON to List 1 to simulate a reset
EndIf
EndIf
If ThisEvent is ES_GAME_LOST
Start LEAF_SERVO_TIMER to LEAF_SERVO_DELAY
Set NextState to LeafDone
EndIf
If ThisEvent is ES_GAME_WON
Start LEAF_SERVO_TIMER to LEAF_SERVO_DELAY
Set NextState to LeafDone
EndIf
If ThisEvent is ES_RESET_BUTTON
Post to LEDStrip LeafRemovedEarly
Set NextState to InitLeafState
EndIf
If ThisEvent is ES_TIMEOUT and parameter is ACTIVITY_TIMER
Post ES_GAME_LOST event to List1
Set LED strip to ActivityTimeout
Set NextState to InitLeafState
EndIf
End LeafNotDone block
CurrentState is LeafDone
If ThisEvent is ES_LEAF_REMOVED
Post to LEDStrip a RESET_NEEDED_LIGHT_TYPE event with param
RESET_NEEDED LIGHT_COLOR
Set NextState to NoLeaf
EndIf
If ThisEvent is ES_RESET_BUTTON
Post LEDStrip RESET_NEEDED_LIGHT_TYPE with param
RESET_NEEDED_LIGHT_COLOR
Set NextState to InitLeafState
EndIf
If ThisEvent is ES_TIMEOUT and parameter is LEAF_SERVO_TIMER
Open Leaf servo
EndIf
End LeafDone block
Return ReturnEvent
End of RunLeafSM