This service is responsible for decoding the data transmitted on the communication pin. It detects when a new message starts when it ends and reads the data transferred. Once a message is fully received, an event is posted to the Signal Interpreting Service in order to do the queried actions.
# Constants
Create_constant InputSignal as DECODING_SIGNAL_PORT
Create_constant DELAY_SHORT as 35 # milliseconds
Create_constant DELAY_LONG as 70 # milliseconds
Create_constant TOLERANCE as 5 # milliseconds
# Variables
Declare CurrentState as State
Declare LastInputSignal as uint8_t
Declare CurrentInputSignal as uint8_t
No specific functions for this service.
Function InitSignalDecoding:
# Set the readin pin as an input and read its current value
PortSetup_ConfigureDigitalInputs(_Port_A, _Pin_4)
Set LastInputSignal as InputSignal
# Post the initial transition event
Set CurrentState to InitSignalDecodingState
Post_to SignalDecodingService ES_INIT
Endfunction
Function RunSignalDecoding(event):
Instantiate NextState to CurrentState
Instantiate PulseNumber to 0
Instantiate LastSignal to false
Declare RisingTime as uint32_t
Declare FallingTime as uint32_t
Switch CurrentState:
case InitSignalDecodingState:
If type of event is ES_INIT:
Set NextState to WaitingforDecoding
Endif
case WaitingforDecoding:
Switch type of event:
case ES_RISING:
Set RisingTime to parameter of event
case ES_FALLING:
Set FallingTime to parameter of event
# If it is a pulse of duration DELAY_LONG, then it is the first pulse
If FallingTime-RisingTime>DELAY_LONG-TOLERANCE and FallingTime-RisingTime<DELAY_LONG+TOLERANCE:
Set PulseNumber to 1
# If it is a pulse of duration DELAY_SHORT, then it is the last pulse
ElseIf FallingTime-RisingTime>DELAY_SHORT-TOLERANCE and FallingTime-RisingTime<DELAY_SHORT+TOLERANCE:
Set LastSignal to true
Set PulseNumber to PulseNumber + 1
Start_timer SignalDecodingTimer with duration DELAY_SHORT - TOLERANCE
# Otherwise it is a data pulse
Else:
Set PulseNumber to PulseNumber + 1
# Initialize a timer, if within the timer there is no rising edge, trigger the audio service
Start_timer SignalDecodingTimer with duration DELAY_SHORT - TOLERANCE
Endif
case ES_TIMEOUT:
If InputSignal is LO and LastSignal:
Set LastSignal to false
Post_to SignalInterpretingService ES_NEW_SIGNAL with parameter PulseNumber
Set PulseNumber to 0
Endif
Endswitch
Endswitch
Set CurrentState to NextState
Endfunction