This service is responsible for communicating with the second PIC (to play some audio or control some LEDs).
# Constants
Create_constant HI as 1
Create_constant LO as 0
Create_constant DELAY_SHORT as 35 # milliseconds
Create_constant DELAY_LONG as 70 # milliseconds
Create_constant DELAY_IN_BETWEEN as 20 # milliseconds
Create_constant DELAY_BEFER_RECALL as 150 # milliseconds
# Variables
Declare CurrentState as State
Declare DeferralQueue as DeferQueue_t
Instantiate OutputSignal as SIGNAL_SENDING_LAT
Instantiate CurrentOutputStatus as SIGNAL_SENDING_PORT
No specific functions for this service.
Function InitSignalSendingService:
# Setup the communication Pin
Call PortSetup_ConfigureDigitalOutputs(_Port_B, _Pin_5)
# Init the Deferral queue
Call Initialize(DeferralQueue)
# Post the initial transition event
Set CurrentState to InitSignalSendingState
Post_to SignalSendingService ES_INIT
Endfunction
Function RunSignalSendingService(event):
Instantiate NextState to CurrentState;
Declare SignalNumber as uint8_t
Switch CurrentState:
case InitSignalSendingState:
If type of event is ES_INIT:
Set NextState to WritingSignals
Endif
case WritingSignals:
If type of event is ES_PLAY_AUDIO:
If parameter of event is 1:
Set SignalNumber to 1
Set OutputSignal to HI
Start_timer SignalSendingTimer with duration DELAY_SHORT # timer for 1st pulse
Set NextState to SendingSignals
ElseIf parameter of event > 1:
Set SignalNumber to parameter of event
Set OutputSignal to HI
Start_timer SignalSendingTimer with duration DELAY_LONG # timer for 1st pulse
Set NextState to SendingSignals
Endif
Endif
case Delaying:
If type of event is ES_TIMEOUT and parameter is SignalSendingTimer:
Set NextState to WritingSignals
Call RecallEvents(DeferralQueue)
Endif
case SendingSignals:
If type of event is ES_PLAY_AUDIO:
Add event to DeferralQueue
ElseIf type of event is ES_TIMEOUT and parameter is SignalSendingTimer:
If CurrentOutputStatus is HI:
Set OutputSignal to LO
Start_timer SignalSendingTimer with duration DELAY_IN_BETWEEN
Else:
If SignalNumber > 2:
Set OutputSignal to LO
Set SignalNumber to SignalNumber - 1
Start_timer SignalSendingTimer with duration DELAY_IN_BETWEEN
ElseIf SignalNumber is 2:
Set OutputSignal to LO
Set SignalNumber to SignalNumber - 1
Start_timer SignalSendingTimer with duration DELAY_SHORT
Else:
Set NextState to Delaying
Start_timer SignalSendingTimer with duration DELAY_BEFER_RECALL
Endif
Endif
Endif
Endswitch
Set CurrentState to NextState
Endfunction