This service is responsible for interpreting signals. The Signal Decoding Service will post data received so that this service can take action. Actions include: playing certain audio, stopping all audio, toggling on and off the State LED, lighting up the next progress LED, stopping all audio from playing, and turning off all LEDs.
# Constants
Create_constant HI as 1
Create_constant LO as 0
Create_constant DELAY_TURN_OFF as 200 # milliseconds
Create_constant DELAY_BLINK as 250 # milliseconds
Create_constant COUNT_BLINK as 10
# Variables
Declare CurrentState as State
Declare LastAudioStatus as uint8_t
Declare uint8_t CurrentAudioStatus as uint8_t
Instantiate currentProgress to 0
Instantiate count to 0
Instantiate state to true
# Stops playing all sounds
Function DisableAllSounds:
For i from 0 to 10:
Set SOUNDTRACK_LAT[i] to HI
Endfor
Endfunction
# Turns off all LEDs
Function TurnAllLEDOff:
Set currentProgress to 0
For i from 0 to 5:
Set LED_PROGRESS_LAT[i] to LO
Endfor
Set LED_STATE_LAT to 0
Endfunction
# Blinks all LEDs
Function Blink:
If count < COUNT_BLINK:
For i from 0 to 5:
Set LED_PROGRESS_LAT[i] to state
Endfor
Set state to not state
Set count to count + 1
Start_timer BlinkTimer with duration DELAY_BLINK
ES_Timer_InitTimer(Blink_TIMER, DELAY_BLINK);
Endif
Endfunction
# Turns on the next LED, if all LEDs are already turned on, starts to blink
Function IncreaseProgress:
Set currentProgress to currentProgress + 1
Set count to COUNT_BLINK
# Checks if all LEDs are already lit up
If currentProgress is 6:
Set count to 0
Call Blink()
Set currentProgress to 0
Else:
Set LED_PROGRESS_LAT[currentProgress-1] to HI
Endif
Endfunction
Function InitSignalInterpretingService:
# PIC Com setup
PortSetup_ConfigureDigitalInputs(_Port_B, _Pin_15)
Set LastAudioStatus to AUDIO_ACT_PORT
# Soundtracks
PortSetup_ConfigureDigitalOutputs(_Port_B, _Pin_10) # soundtrack 1
PortSetup_ConfigureDigitalOutputs(_Port_B, _Pin_9) # soundtrack 2
PortSetup_ConfigureDigitalOutputs(_Port_B, _Pin_8) # soundtrack 3
PortSetup_ConfigureDigitalOutputs(_Port_B, _Pin_5) # soundtrack 4
PortSetup_ConfigureDigitalOutputs(_Port_B, _Pin_11) # soundtrack 5
PortSetup_ConfigureDigitalOutputs(_Port_B, _Pin_2) # soundtrack 6
PortSetup_ConfigureDigitalOutputs(_Port_B, _Pin_3) # soundtrack 7
PortSetup_ConfigureDigitalOutputs(_Port_A, _Pin_2) # soundtrack 8
PortSetup_ConfigureDigitalOutputs(_Port_A, _Pin_3) # soundtrack 9
PortSetup_ConfigureDigitalOutputs(_Port_B, _Pin_4) # soundtrack 10
# LEDs
PortSetup_ConfigureDigitalOutputs(_Port_B, _Pin_6) # led 1
PortSetup_ConfigureDigitalOutputs(_Port_B, _Pin_7) # led 2
PortSetup_ConfigureDigitalOutputs(_Port_B, _Pin_12) # led 3
PortSetup_ConfigureDigitalOutputs(_Port_B, _Pin_13) # led 4
PortSetup_ConfigureDigitalOutputs(_Port_B, _Pin_14) # led 5
PortSetup_ConfigureDigitalOutputs(_Port_A, _Pin_1) # state led
# Reset
DisableAllSounds()
TurnAllLEDOff()
# Post the initial transition event
Set CurrentState to InitInterpretingState
Post_to SignalInterpretingService ES_INIT
Endfunction
Function RunSignalInterpretingService(event):
Instantiate NextState to CurrentState
Switch CurrentState:
case InitInterpretingState:
If type of event is ES_INIT)
Set NextState to WaitingNewSignal
Endif
case WaitingNewSignal:
If type of event is ES_NEW_SIGNAL:
# Play some audio
If parameter of event <= 10:
Call DisableAllSounds()
SOUNDTRACK_LAT[parameter of event - 1] = LO # To play a sound its pin must be pulled down
Start_timer StopTimer to DELAY_TURN_OFF
# Light up next LED
Else if parameter of event is 11
Call IncreaseProgress()
# Turn on state LED
Else if parameter of event is 12
Set LED_STATE_LAT to HI
# Turn off state LED
Else if parameter of event is 13
Set LED_STATE_LAT to LO
# Stops all sounds
Else if parameter of event is 14
Call DisableAllSounds()
# Turn off all LEDs
Else if parameter of event is 15
Call TurnAllLEDOff()
Endif
ElseIf type of event is ES_TIMEOUT:
If parameter of event is StopTimer:
Call DisableAllSounds()
ElseIf parameter of event is BlinkTimer:
Call Blink()
Endif
Endif
Endswitch
Set CurrentState to NextState
Endfunction