Skip to content

Instantly share code, notes, and snippets.

@michaellin
Created December 1, 2015 03:59
Show Gist options
  • Save michaellin/6c935a4acdb19e8d54c6 to your computer and use it in GitHub Desktop.
Save michaellin/6c935a4acdb19e8d54c6 to your computer and use it in GitHub Desktop.
Pseudo-code for the PipeSegment module (a service that implements a state machine to control the movement of the two pipe segments)
Data private to the module: MyPriority, CurrentState
InitPipeSegment
Takes a priority number, returns True
Initialize the MyPriority variable with the passed in parameter
Initialize the port lines to write to PipeServo1 and PipeServo2
Initialize the port lines to write to PipeLED1 and PipeLED2
Start both pipes in the “pipes in” position
Set CurrentState to BothPipesIn
End of InitializePipeSegment (return True)
PostPipeSegment
Takes an event to post, returns True if event was posted
End of PostPipeSegment
RunPipeSegment (implements a 3-state state machine for the pipe segments)
The EventType field of ThisEvent will be one of: ES_PIPE1_BUTTON_DBDOWN, ES_PIPE2_BUTTON_DBDOWN, ES_TIMEOUT, ES_GAMEOVER, ES_PIPE_DOWN_LOST
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 BothPipesIn
If EventType is ES_TIMEOUT and parameter is pipe
timer number
Post event ES_PLAY_TUNE to Audio1 and Audio2
with event parameter PipeOut
Calculate a random integer: either 0 or 1
If random integer is 0:
Move pipe 1 to the out position
Set NextState to Pipe1Out
Turn on pipe 1 button LED
EndIf
If random integer is 1:
Move pipe 2 to the out position
Set NextState to Pipe2Out
Turn on pipe 2 button LED
EndIf
EndIf
If EventType is ES_GAMEOVER
Set NextState to InCelebration
EndIf
End BothPipesIn block
CurrentState is Pipe1Out
If (EventType is ES_PIPE_DOWN_LOST) or (EventType is
ES_PIPE1_BUTTON_DBDOWN)
Turn off Pipe button LEDs
Post Event ES_PLAY_TUNE to Audio1 and Audio2
with event parameter PipeIn
Post Event ES_PIPE_RECOVERED to Bucket Service,
Wrench Service, Audio1, and Audio2
Move Pipe1 to the in position
Set NextState to BothPipesIn
Start pipe timer for 16 seconds
EndIf
If EventType is ES_GAMEOVER
Turn off pipe button LEDs
Move pipe 1 back to the in position
Set NextState to InCelebration
EndIf
End Pipe1Out Block
CurrentState is Pipe2Out
If (EventType is ES_PIPE_DOWN_LOST) or (EventType is
ES_PIPE2_BUTTON_DBDOWN)
Turn off Pipe button LEDs
Post Event ES_PLAY_TUNE to Audio1 and Audio2
with event parameter PipeIn
Post Event ES_PIPE_RECOVERED to Bucket Service,
Wrench Service, Audio1, and Audio2
Move Pipe2 to the in position
Set NextState to BothPipesIn
Start pipe timer for 16 seconds
EndIf
If EventType is ES_GAMEOVER
Turn off pipe button LEDs
Move pipe 2 back to the in position
Set NextState to InCelebration
EndIf
End Pipe2Out Block
CurrentState is InCelebration
If EventType is ES_START_BUTTON_DBDOWN
Set NextState to BothPipesIn
EndIf
End InCelebration Block
Set CurrentState to NextState
Return ES_NO_EVENT
End of RunPipeSegment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment