/*-----------------Module constant variables------------------*/ ONE_SEC 1000 HALF_SEC (ONE_SEC / 2) TWO_SEC (ONE_SEC * 2) FIVE_SEC (ONE_SEC * 5) //define sensors in port C, leds in port D SENSOR_1 GPIO_PIN_4 SENSOR_2 GPIO_PIN_5 SENSOR_3 GPIO_PIN_6 SENSOR_4 GPIO_PIN_7 LED_1 GPIO_PIN_2 LED_2 GPIO_PIN_3 LED_3 GPIO_PIN_6 LED_4 GPIO_PIN_7 SENSOR_1_HI BIT4HI SENSOR_2_HI BIT5HI SENSOR_3_HI BIT6HI SENSOR_4_HI BIT7HI LED_1_HI BIT2HI LED_2_HI BIT3HI LED_3_HI BIT6HI LED_4_HI BIT7HI SENSOR_1_LO BIT4LO SENSOR_2_LO BIT5LO SENSOR_3_LO BIT6LO SENSOR_4_LO BIT7LO LED_1_LO BIT2LO LED_2_LO BIT3LO LED_3_LO BIT6LO LED_4_LO BIT7LO //Set time for length of time for lights to turn back on TIMER1 3000 TIMER2 5000 TIMER3 6000 TIMER4 1000 //Set number of points each light is worth LIGHT_POINTSVAL 1 //Set up inactivity timer INACTIVITY_TIMER_NUMBER 1 ONE_SEC 1000 INACTIVITY_TIMER_TIME (30*ONE_SEC) /*-----------------------Module Functions-----------------*/ FlashLEDs(void) to flash LEDs in celebration mode ES_Event_t WhackKeyMapper(ES_Event_t) for testing state transitions /*-----------------------Module Code----------------------*/ bool InitializeWhackALight(uint_t Priority){ ES_Event_t ThisEvent to be posted MyPriorirty is Priority passed in through parameter Set up DeferralQueue for events to be processed Set up I/O lines enable the clock to PortC for sensors and PortD for LEDs Kill a few cycles to let the peripheral clock get going Enable pins for digital I/O setting, Port C as inputs and port D as outputs. Write all four LEDs high Set LastInputState as current pin states set CurrentState as Welcome Post This.Event as ES_INIT If posting succeeds, return true, else return false. } bool PostWhackALight(ES_Event_t ThisEvent){ return ES_PostTOSErvice with MyPriority and parameter event } ES_Event_t RunWhackALightSM(ES_Event_t ThisEvent){ Define ReturnEvent as ES_NO_EVENT, PointEvent as POST_POINTS, and a Next WhackALightState If the current state is Welcome, if ThisEvent is ES_Init, initialize all LEDs as High if ThisEvent is GAME_START, set NextState as WhackMe if the current state is WhackMe, if LED1 is covered if LED 1 was high set LED1 low. Set and start LED 1 timer Post scored points Stop, reset, and start inactivity timer if LED2 is covered if LED 2 was high set LED 2 low. Set and start LED 2 timer Post scored points Stop, reset, and start inactivity timer if LED 3 is covered if LED 3 was high set LED 3 low. Set and start LED 3 timer Post scored points Stop, reset, and start inactivity timer if LED 4 is covered if LED 4 was high set LED 4 low. Set and start LED 4 timer Post scored points Stop, reset, and start inactivity timer if LED 1 timer timeout Set LED 1 high if LED 2 timer timeout Set LED 2 high if LED 3 timer timeout Set LED 3 high if LED 4 timer timeout Set LED 4 high if Reset, Set next state as Welcome if the current state is Celebration, Initialize and start timer for flashing LEDs if flashing LED timer times out, reinitialize flashing LED timer and call FlashLEDs if reset, Set next state as Welcome Set current state as next state return ReturnEvent } static void FlashLEDs(){ Set state of LEDs as different from its last state (ex high to low, low to high) using XOR } //to be commented out when integration with other modules begin static ES_Event_t WhackKeyMapper(ES_Event_t ThisEvent){ ES_Event_t ReturnEvent; if key hit was 'w', ReturnEvent type is set to GAME_START if key hit was 'r', ReturnEvent type is set to RESET if key hit was 'c', ReturnEvent type is set to GAME_OVER if key hit was '1', ReturnEvent type is set to LED_1_COVERED if key hit was '2', ReturnEvent type is set to LED_2_COVERED if key hit was '3', ReturnEvent type is set to LED_3_COVERED if key hit was '4', ReturnEvent type is set to LED_4_COVERED return ReturnEvent } //Event checker function for WhackALight Module bool CheckWhackALight(void){ Set ReturnVal as false CurrentInputState as state of all pins LastPin1Input is the LastInputState masked with Sensor_1_HI isolating Sensor 1 value; CurrentPin1Input is CurrentInputState masked with Sensor_1_HI isolating Sensor 1 value; LastPin2Input is the LastInputState masked with Sensor_2_HI isolating Sensor 2 value CurrentPin2Input is CurrentInputState masked with Sensor_2_HI isolating Sensor 2 value LastPin3Input is the LastInputState masked with Sensor_3_HI isolating Sensor 3 value CurrentPin3Input is CurrentInputState masked with Sensor_3_HI isolating Sensor 3 value LastPin4Input is the LastInputState masked with Sensor_4_HI isolating Sensor 4 value CurrentPin4Input is CurrentInputState masked with Sensor_4_HI isolating Sensor 4 value if CurrentPin1Input is different from LastPin1Input and the CurrentPin1Input is high Post event LED_1_COVERED Set ReturnVal as true CurrentPin2Input is high Post event LED_2_COVERED Set ReturnVal as true CurrentPin3Input is high Post event LED_3_COVERED Set ReturnVal as true CurrentPin4Input is high Post event LED_4_COVERED Set ReturnVal as true Set LastInputState as CurrentInputState return ReturnVal; }