You are on page 1of 5

MusicSequence.

c
\\ Implements SM that controls music, hand backlight, indicator LEDs and reads the
headphone state

Module level variables: MyPriority, LastHeaphoneState, LastButtonStates, NoteTones


(array of PWM freqeuncy for creating tones), NoteDurations (array of times that
notes should be played), Fingers (array of which button is correct), Buttons (array
of buttons to press)NoteIndex, MAXNOTES, ResetEarly, HEADPHONE_LED, PLACE_CAR_LED,
HAND_Backlight, FINGER1_LED, FINGER2_LED, FINGER3_LED, FINGER4_LED, FINGER5_LED,
HIT1_LED, HIT2_LED, HIT3_LED, HAND_UP_LED, HAND_DOWN_LED, NoteIndex

Module defines: ONE_SEC, HALF_SEC, THREE_SEC, FIVE_SEC, AUDIO_DELAY, QUARTER, EIGHTH,


SIXTEENTH, B3F, C4, D4, E4F, F4, G4, A4, B4F, HEADPHONE_DELAY, MAX_NOTE, MUSIC_DUTY,
MUSIC_CHANNEL, MUSIC_GROUP, BUTTON1, BUTTON2, BUTTON3, BUTTON4, BUTTON5,
BUTTON_PINS, HEADPHONE_PIN, LED_DATA, LED_SCLK, LED_RCLK, AUDIO_DATA, AUDIO_SCLK ,
AUDIO_RCLK, AUDIO_WELCOME, AUDIO_FIGHT, AUDIO_DEFEAT, AUDIO_VICTORY
******************************************************************************
****************************
InitializeMusicSequence
Takes a priority number, returns True

Set the priority of this service.


Initialize the hardware for this service (Headphone IR pin, button pins, LED register,
Audio
register, Tiva TONE)
Initialize LastHeadphoneState by reading IR pin (mask for only this pin)
Initialize LastButtonStates by reading port and masking for only relevant pins

Set CurrentState to be InitMusicSequence


Post Event ES_INIT to MusicSequence queue (this service)
End of InitializeMusicSequence

PostMusicSequence
Takes Event, returns True

Posts an event to MusicSequence service


END of PostMusicSequence

RunMusicSequence (implements the state machine for MusicSequence)


Event Types: ES_INIT, HEADPHONES_LIFTED, HEADPHONES_PLACED, CAR_PLACED, RIGHT_NOTE,
VICTORY, ES_TIMEOUT, CAR_END, EARLY_RESET
Returns: ES_NO_EVENT

Switch based on CurrentState


CurrentState is InitializeMusicSequence
If ThisEvent is ES_INIT
Write headphone LED ON
Set CurrentState to WelcomeState
EndIf
break
EndBlock

CurrentState is WelcomeState
If ThisEvent is HEADPHONES_LIFTED
Write headphone LED OFF
Set timer for welcome clip
Set CurrentState to FirstDebounceHeadphones
EndIf
break
EndBlock

CurrentState is FirstDebounceHeadphones:
If ThisEvent is ES_TIMEOUT and parameter is IntroDelayTimer
Write the Welcome audio clip
Set timer for length of audio LO pulse
ElseIf ThisEvent is ES_TIMEOUT and parameter is AudioPulseTimer
Set all triggers back HI
PostEvent WELCOME_AUDIO to Car service
Turn on car LED
Set CurrentState to WaitForCar
ElseIf ThisEvent is HEADPHONES_PLACED
PostEvent EARLY_RESET to all services
Set ResetEarly to 1
Set CurrentSTate to WaitForHeadphonesMusic
EndIf
Break
EndBlock

CurrentState is WaitForCar
If ThisEvent is CAR_PLACED
Write CarMount LED OFF, HandBackLight LED ON and ButtonLED[NoteIndex] ON
Set CurrentState to WaitForNote

ElseIf ThisEvent is ES_TIMEOUT and EventParam is MainTimer


Turn on headphone LED only
Write Audio register to play defeat clup
Set timer for length of audio LO pulse
Set CurrentState to WaitForHeadphonesMusic

ElseIf ThisEvent is HEADPHONES_PLACED and ResetEarly equals 0


PostEvent EARLY_RESET to all services
Set ResetEarly to 1
Set CurrentState to WaitForHeadphonesMusic
break
EndBlock

CurrentState is WaitForNote
If ThisEvent is CORRECT_NOTE[NoteIndex != MAXNOTES]
Write PWM Tone channel to 50% duty cycle with frequency of NoteTones[NoteIndex]
Set NoteTimer with duration NoteDurations[NoteIndex]
Turn off last button
PostEvent CORRECT_NOTE to Car service
NoteIndex++

ElseIf ThisEvent is ES_TIMEOUT and EventParam is NoteTimer


Write PWM Tone channel to 0% duty cycle
Write ButtonLED[NoteIndex] ON

ElseIf ThisEvent is CORRECT_NOTE[NoteIndex == MAXNOTES]


Write PWM Tone channel to 50% duty cycle with frequency of NoteTones[NoteIndex]
Set NoteTimer with duration NoteDurations[NoteIndex]
Write ButtonLED[NoteIndex] OFF
PostEvent LAST_NOTE to Attack service
PostEvent CORRECT_NOTE to Car service
Set CurrentState to WaitAttack

ElseIf ThisEvent is ES_TIMEOUT and EventParam is Main


Write Headphone LED ON and all others off (including hand)
Write Audio register to play defeat clip
Set timer for length of audio LO pulse
Set CurrentState to WaitForHeadphonesMusic

ElseIf This Event is HEADPHONES_PLACED and ResetEarly equals 0


PostEvent EARLY_RESET to all services
Set ResetEarly to 1
Set CurrentState to WaitForHeadphonesMusic
EndIf
break
EndBlock

CurrentState is WaitForAttack
If ThisEvent is ES_TIMEOUT and EventParam is NoteTimer
Write PWM Tone channel to 0 duty cycle

ElseIf ThisEvent is VICTORY


Write Audio register to play celebration sound
Set timer for length of audio LO pulse
Set CurrentState to WaitForHeadphonesMusic

ElseIf ThisEvent is ATTACK


Play fight sound
Set timer for length of audio LO pulse

ElseIf ThisEvent is ES_TIMEOUT and EventParam is MainTimer


Write Headphone LED ON and all others off (including hand)
Write Audio register to play defeat clip
Set timer for length of audio LO pulse
Set CurrentState to WaitForHeadphonesMusic

ElseIf ThisEvent is ES_TIMEOUT and EventParam is AudioPulseTimer


Set all triggers back HI
ElseIf ThisEvent is HEADPHONES_PLACED and ResetEarly equals 0
PostEvent EARLY_RESET to all services
Set ResetEarly to 1.
Set CurrentState to WaitForHeadphonesMusic
EndIf
Break
EndBlock

CurrentState is WaitForHeadphonesMusic
If ThisEvent is HEADPHONES_PLACED or ResetEarly equals 1
Set HeadphoneDelay Timer
Set CurrentState to DebounceHeadphones
ElseIf ThisEvent is ES_TIMEOUT and EventPAram is AudioPulseTimer
Set all triggers back HI
End if
break
EndBlock

CurrentState is DebounceHeadphones
If EventType is ES_TIMEOUT and EventParam is HeadphoneTimer
If ResetEarly is zero
PostEvent HEADPHONES_RESET to all services
EndIf
Set ResetEarly to zero
Set CurrentState to WelcomeState
Turn on headphone LED
Set all triggers back HI
Set NoteIndex to zero
End If

break
EndBlock

END of RunMusicSequence
Check4Headphones
Takes no parameters, returns True if an event was posted
LocalVariables: ReturnValue = False, CurrentHeadphoneState

Read the CurrentHeadphoneState from IR pin

If the state of the Headphone line has changed


If the current state of the input line is high
PostEvent HEADPHONES_LIFTED

Else (current input state is low)


PostEvent HEADPHONES_PLACED

End if
Set ReturnValue = True
End if

Set LastHeadphoneState to CurrentHeadphoneState


Return ReturnValue

END of Check4Headphones

Check4Notes (switch our wiring to normally low so easier to think about)


Takes no parameters, returns True if an event was posted
LocalVariables: ReturnValue = False, CurrentButtonStates

Read the CurrentButtonStates from port


Mask so only looking at pins related to Buttons

If CurrentButtonStates is not equal to LastButtonStates

If CurrentButtonStates == Buttons[NoteIndex]
PostEvent CORRECT_NOTE to MusicSequence Service and Idle Service
ReturnValue = True
EndIf
EndIf

Set LastButtonStates to CurrentButtonStates


Return ReturnValue

END of Check4Notes

You might also like