This service is one of the three action services. It gets activated by the Master Service and when activated it waits for the infrared beam in the mouth of the cat to be broken (meaning that a coin has been inserted). When this happens, it sends back an event to confirm that the feeding has been completed. After a certain time, if the user did not feed the cat, the Master Service will disable the Feeding Service.
# Constants
Create_constant DURATION_BLINK as 250 # milliseconds
Create_constant DURATION_MAX_TO_BE_FED as 8000 # milliseconds
Create_constant DELAY_RESET as 200 # milliseconds
# Variables
Declare CurrentState as State
Declare LedOn as bool
# Blinks the LEDs of the mouth
Function BlinkLED:
Set LedOn to 1 - LedOn
Set LED_LAT to LedOn
Endfunction
# Turns off the LEDs of the mouth
Function TurnOffLED:
Set LedOn to false
Set LED_LAT to 0
Endfunction
Function InitFeedingService:
# Initialize variables
Set CurrentState to InitFeedingState
Set LedOn to false
# Sets the LED as digital output
Set LED_TRIS to 0 # Output
Set LED_ANS to 0 # Digital
# Sets the IR receiver as digital input
Set FEEDING_IR_TRIS to 1 # Input
Set FEEDING_IR_ANS to 0 # Digital
# Posts initial transition
Post_to FeedingService ES_INIT
Endfunction
Function RunFeedingService:
Set NextState to CurrentState
Switch CurrentState:
case InitFeedingState
If type of event is ES_INIT:
Set NextState to InactiveFeeding
Call TurnOffLED()
Endif
case InactiveFeeding:
If type of event is ES_FEEDING_ACTIVATE:
Set NextState to WaitingToBeFed
Start_timer FeedingBlinkLEDTimer to DURATION_BLINK
Call BlinkLED()
ElseIf type of event is ES_FEEDING_BEAM_BROKEN:
Call PlayAudio(AUDIO_NOT_HUNGRY)
Endif
case WaitingToBeFed:
If type of event is ES_FEEDING_BEAM_BROKEN:
Call PlayAudio(AUDIO_MIAM)
Post_to MasterService ES_FEEDING_COMPLETED
Set NextState to Resetting
Start_timer FeedingBlinkLEDTimer to DELAY_RESET
Call TurnOffLED()
ElseIf type of event is ES_FAIL:
Set NextState to InactiveFeeding
Call TurnOffLED()
ElseIf type of event ES_TIMEOUT and parameter is FeedingBlinkLEDTimer:
Start_timer FeedingBlinkLEDTimer to DURATION_BLINK
Call BlinkLED()
Endif
case Resetting:
If type of event ES_TIMEOUT and parameter is FeedingBlinkLEDTimer:
Set NextState to InactiveFeeding
Endif
Endswitch
Set CurrentState to NextState
Endfunction