Skip to content

Instantly share code, notes, and snippets.

@michaellin
Created December 1, 2015 03:44
Show Gist options
  • Save michaellin/fc5216bed956119decd8 to your computer and use it in GitHub Desktop.
Save michaellin/fc5216bed956119decd8 to your computer and use it in GitHub Desktop.
Pseudo-code for the Audio2 module (a service that implements a state machine to play audio for player 2)
Data private to the module: MyPriority (number), CurrentState (state), inARMedMode, inPipeOut, inCelebration, Variables for frequencies of each note, Arrays of frequencies and delays of each tune
InitAudio2
Takes a priority number, returns True
Initialize the MyPriority variable with the passed in parameter
Initialize the port line to write to speaker 2
Set CurrentState to Waiting2Play
End of InitAudio2 (return True)
PostAudio2
Takes an event to post, returns True if event was posted
End of PostAudio2
RunAudio2
Implements 2-state state machine for player 2’s audio
The EventType of ThisEvent will be ES_PLAY_TUNE, ES_START_BUTTON_DB_DOWN, ES_PIPE_RECOVERED, ES_TIMEOUT,
ES_GAINED_POINT, or ES_GAMEOVER; the EventParam will be ARMedMode,
StartButton, SuccessWrench, SuccessBucket, FailBucket, PipeOut,
PipeIn, CelebrationMode, FailureMode, PIPE_TIMER, or AUDIO2_TIMER
Local Variables: NextState (state)
Static Local Variable: TuneFreqs (array)
Static Local Variable: TuneDelays (array)
Static Local Variable: NumberOfNotes (number)
Static Local Variable: CurrentNote (number)
Set NextState to CurrentState
Based on the state of the CurrentState variable, choose one of the following blocks of code:
CurrentState is Waiting2Play
If ThisEvent is ES_PLAY_TUNE
Set InARMedMode, inPipeOut, and inCelebration
to zero
Set TuneFreqs to frequency array corresponding
to Event Parameter
Set TuneDelays to delay array corresponding to
Event Parameter
Set NumberOfNotes to the length of TuneFreqs
Play first note of TuneFreqs
Set audio timer for length of first note in
TuneDelays
Set CurrentNote to 1
Set NextState to Playing
Endif
End Waiting2Play block
CurrentState is Playing
If (ThisEvent is ES_START_BUTTON_DBDOWN) or (ThisEvent is ES_PIPE_RECOVERED) or (ThisEvent is ES_TIMEOUT and Event Parameter is PIPE_TIMER and inCelebration is 0) or (ThisEvent is ES_GAINED_POINT) or (ThisEvent is ES_GAMEOVER)
Set NextState to Waiting2Play
Play nothing
EndIf
If ThisEvent is ES_TIMEOUT and Event Parameter is
AUDIO2_TIMER
If CurrentNote equals NumberOfNotes
If inArmedMode equals 1
Post event ES_PLAY_TUNE to Audio2
with event parameter ARMedMode
Endif
If inPipeOut equals 1
Post event ES_PLAY_TUNE to Audio2
with event parameter PipeOut
Endif
Play nothing
Set NextState to Waiting2Play
Endif
If CurrentNote < NumberOfNotes
Play note of TuneFreqs with index
CurrentNote
Set audio timer for length of note in
TuneDelays with index CurrentNote
Increment CurrentNote
Endif
Endif
End Playing block
Set CurrentState to NextState
Return ES_NO_EVENT
End of RunAudio2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment