Below are the Event Checkers. They are responsible for checking regularly the value of an input pin and posting an event when the pin changes in some way (Falling or Rising edge for example). They are similar to interrupts.
The 4 event checkers are quite similar but still present a few differences (debouncing of the button for example).
Create_constant DELAY_MIN_DEBOUNCE as 200 # milliseconds
Instantiate lastPost to 0
Instantiate prevState to HI
# Returns true if the state of the button has changed.
# Only posts events every DELAY_MIN_DEBOUNCE max (to debounce and avoid spamming services)
# Post ES_BUTTON_RELEASED on a falling edge
# Post ES_BUTTON_PRESSED on a rising edge
Function Check4Button:
Instantiate returnValue to false
Instantiate curState to LO
If BUTTON_PORT is 1:
Set curState to HI
Endif
If prevState is not curState:
Set returnValue to true
Instantiate dt to ES_Timer_GetTime() - last_post
If curState is LO and dt >= DELAY_MIN_DEBOUNCE:
Post_to MasterService ES_BUTTON_RELEASED with parameter ES_Timer_GetTime()
Set last_post to ES_Timer_GetTime()
ElseIf curState is HI and dt >= DELAY_MIN_DEBOUNCE:
Post_to MasterService ES_BUTTON_PRESSED with parameter ES_Timer_GetTime()
Set last_post to ES_Timer_GetTime()
Endif
Endif
Set prevState to curState
Return returnValue
Endfunction
Instantiate prevState as HI
# Returns true if the state of the IR receiver has changed
# Post ES_FEEDING_BEAM_BROKEN on a falling edge
Function FeedingIRChanged:
Instantiate returnValue to false
Instantiate curState to LO
If FEEDING_IR_PORT is 1:
Set curState to HI
Endif
If prevState is not curState:
Set returnValue to true
If curState is LO:
Post_to FeedingService ES_FEEDING_BEAM_BROKEN with parameter ES_Timer_GetTime()
Endif
Endif
Set prevState to curState
Return returnValue;
Endfunction
Instantiate prevState as HI
# Returns true if the state of the IR receiver has changed
# Post ES_PAWMOVING_BEAM_BROKEN on a falling edge
# Post ES_PAWMOVING_BEAM_RESTORED on a rising edge
Function HandIRChanged:
Instantiate returnValue to false
Instantiate curState to LO
If PAW_DETECTING_PORT is 1:
Set curState to HI
Endif
If prevState is not curState:
Set returnValue to true
If curState is LO:
Post_to PawMovingService ES_PAWMOVING_BEAM_BROKEN with parameter ES_Timer_GetTime()
ElseIf curState is HI:
Post_to PawMovingService ES_PAWMOVING_BEAM_RESTORED with parameter ES_Timer_GetTime()
Endif
Endif
Set prevState to curState
Return returnValue;
Endfunction
Instantiate prevState as HI
# Returns true if the state of the communication pin has changed
# Post ES_FALLING on a falling edge
# Post ES_RISING on a rising edge
Function SignalHasChanged:
Instantiate returnValue to false
Instantiate curState to LO
If INPUT_SIGNAL_PORT is 1:
Set curState to HI
Endif
If prevState is not curState:
Set returnValue to true
If curState is LO:
Post_to SignalDecodingService ES_FALLING with parameter ES_Timer_GetTime()
ElseIf curState is HI:
Post_to SignalDecodingService ES_RISING with parameter ES_Timer_GetTime()
Endif
Endif
Set prevState to curState
Return returnValue;
Endfunction