Master Service
This service is responsible for tracking the current state of the rotating spice rack as well as coordinating sensory inputs and outputs. The user opens the cabinet doors, which prompts the start of the Welcome menu on an LED display. This event is detected by hall effect sensors with magnets attached to the back of the door. Upon pressing a capacitive touch switch, the user can start the Ferris Wheel rotation that presents different spices with each rotation. A potentiometer (analog input) can be adjusted by the user to increase or decrease the speed of the rotating Ferris Wheel. When the user gestures in front of a reflective-opto sensor, the Ferris Wheel is stopped, and a “ding” is sounded to indicate the stopped motion. During this process, the Ferris Wheel displays the time that has elapsed since the user initiated the first event. If there is no user input for 20 seconds or more, the motor stops, the timer expires and the entire state machine is reset.
Constants and Module Variables
Module Defines
FerrisWheelState_t CurrentState, static uint8_t MyPriority
Constants
#define TWENTY_SEC 20000
#define HALF_SEC 150
Module Functions
PostSpiceRackFSM
Input: ES_Event_t
Post to service(priority, event)
QueryLEDServiceFSM
No input
Return CurrentState
Service Initialization
InitSpiceRackFSM
Inputs: priority
Call clrScrn()
Initialize LAT,PORT,TRIS and ANSbits for peripherals
Set CurrentState to Idle_FerrisWheel
Post initial ES_INIT
Service Run
RunSpiceRackFSM
Input: Event ThisEvent
Declare ReturnEvent to be ES_NO_EVENT
Declare PostEvent to be ES_NO_EVENT
Switch statement for CurrentState
Case Idle_FerrisWheel:
Set YELLOW_LED to 1
If CurrentEvent’s EventType is ES_DOOR_OPEN:
Declare ES_Event_t newEvent, set EventType to ES_BEGIN
Post newEvent to PostMessageService
Post newEvent to PostSRService
Set currentState to Begin
Set YELLOW_LED to 1
Break;
Case Begin:
Switch statement for CurrentEvent
Case DBButtonDown:
Declare ES_Event_t newEvent, set EventType to ES_MOTOR_INIT
Post newEvent to PostMotorDriverService
Init Timer to TWENTY_SEC
Declare ES_Event_t newEvent, set EventType to ES_STARTING
Post newEvent to PostMessageService
Set CurrentState to Rotating
Set GREEN_LED to 1
Break;
Case ES_OptoDown:
Declare ES_Event_t newEvent, set EventType to ES_MOTOR_INIT
Post newEvent to PostMotorDriverService
Init Timer to TWENTY_SEC
Declare ES_Event_t newEvent, set EventType to ES_STARTING
Post newEvent to PostMessageService
Set CurrentState to Rotating
Set GREEN_LED to 1
Break;
Case ES_DOOR_CLOSE:
Declare ES_Event_t newEvent, set EventType to ES_MOTOR_STOP
Post newEvent to PostMotorDriverService
Declare ES_Event_t newEvent, set EventType to ES_CLR_SCRN
Post newEvent to PostMessageService
Set CurrentState to Idle_FerrisWheel
Set YELLOW_LED to 1
Break;
Case ES_FinishedDisplay:
Declare ES_Event_t newEvent, set EventType to ES_BEGIN
Post newEvent to PostMessageService
Break;
Break;
Case Rotating:
Switch statement for CurrentEvent
Case DBButtonDown:
Set Set_Buzzer_PIN to 0
Init timer for HALF_SEC
Declare ES_Event_t newEvent, set EventType to ES_MOTOR_STOP
Post newEvent to PostMotorDriverService
Init Timer to TWENTY_SEC
Declare ES_Event_t newEvent, set EventType to ES_STOPPING
Post newEvent to PostMessageService
Set CurrentState to Not_Rotating
Set GREEN_LED to 0
Set RED_LED to 0
Break;
Case ES_OptoDown:
Set Set_Buzzer_PIN to 0
Init timer for HALF_SEC
Declare ES_Event_t newEvent, set EventType to ES_MOTOR_STOP
Post newEvent to PostMotorDriverService
Init Timer to TWENTY_SEC
Declare ES_Event_t newEvent, set EventType to ES_STOPPING
Post newEvent to PostMessageService
Set CurrentState to Not_Rotating
Set GREEN_LED to 0
Set RED_LED to 0
Break;
Case ES_SPICE_HIGH:
Declare ES_Event_t newEvent, set EventType to ES_NEW_SPICE
Post newEvent to PostMessageService
Break;
Case ES_DOOR_CLOSE:
Declare ES_Event_t newEvent, set EventType to ES_MOTOR_STOP
Post newEvent to PostMotorDriverService
Declare ES_Event_t newEvent, set EventType to ES_CLR_SCRN
Post newEvent to PostMessageService
Set CurrentState to Idle_FerrisWheel
Set GREEN_LED to 0
Set YELLOW_LED to 1
Break;
Case ES_TIMEOUT:
Declare ES_Event_t newEvent, set EventType to ES_MOTOR_STOP
Post newEvent to PostMotorDriverService
Declare ES_Event_t newEvent, set EventType to ES_RESTARTING
Post newEvent to PostMessageService
Set CurrentState to Begin
Set GREEN_LED to 0
Break;
Case ES_FinishedDisplay:
Declare ES_Event_t newEvent, set EventType to ES_BEGIN
Post newEvent to PostMessageService
Break;
Break;
Case Not_Rotating:
Set Set_Buzzer_PIN to 1
Switch statement for CurrentEvent
Case DBButtonDown:
Declare ES_Event_t newEvent, set EventType to ES_MOTOR_INIT
Post newEvent to PostMotorDriverService
Init Timer to TWENTY_SEC
Declare ES_Event_t newEvent, set EventType to ES_STARTING
Post newEvent to PostMessageService
Set CurrentState to Rotating
Set GREEN_LED to 1
Set RED_LED to 0
Break;
Case ES_OptoDown:
Declare ES_Event_t newEvent, set EventType to ES_MOTOR_INIT
Post newEvent to PostMotorDriverService
Init Timer to TWENTY_SEC
Declare ES_Event_t newEvent, set EventType to ES_STARTING
Post newEvent to PostMessageService
Set CurrentState to Rotating
Set GREEN_LED to 1
Set RED_LED to 0
Break;
Case ES_DOOR_CLOSE:
Declare ES_Event_t newEvent, set EventType to ES_MOTOR_STOP
Post newEvent to PostMotorDriverService
Declare ES_Event_t newEvent, set EventType to ES_CLR_SCRN
Post newEvent to PostMessageService
Set CurrentState to Idle_FerrisWheel
Set GREEN_LED to 0
Set YELLOW_LED to 1
Break;
Case ES_TIMEOUT:
Declare ES_Event_t newEvent, set EventType to ES_MOTOR_STOP
Post newEvent to PostMotorDriverService
Declare ES_Event_t newEvent, set EventType to ES_RESTARTING
Post newEvent to PostMessageService
Set CurrentState to Begin
Set GREEN_LED to 0
Break;
Case ES_FinishedDisplay:
Declare ES_Event_t newEvent, set EventType to ES_BEGIN
Post newEvent to PostMessageService
Break;
Break;
Return ReturnEvent