void APP_Tasks (void) { /* Check the application's current state. */ switch ( appData.state ) { /* Application's initial state. */ case APP_STATE_INIT: { bool appInitialized = true; if (appInitialized) { // appData.state = APP_STATE_SERVICE_TASKS; /* This System Timer object causes APP_TMR_Callback1 to be executed every APP_SWITCH_READ_DELAY milliseconds. APP_TMR_Callback1 polls for a switch press. */ appData.tmrObj1 = SYS_TMR_ObjectCreate(APP_SWITCH_READ_DELAY, 1, APP_TimerCallback1, SYS_TMR_FLAG_PERIODIC); /* Stay in this state until System Timer object created */ if(SYS_TMR_HANDLE_INVALID != appData.tmrObj1) { /* Initialize state machine to blink LEDs after reset. The following variable settings force the state machine into the APP_STATE_BLINK_LED_NO_PRESS_DETECT state. */ appData.pressDetected = true; appData.switchIsPressed = true; appData.state = APP_STATE_LED_OFF_NO_PRESS_DETECT; } break; } break; } /* LED is off while waiting for a switch press. */ case APP_STATE_LED_OFF_NO_PRESS_DETECT: { if (appData.pressDetected) { /* This System Timer object causes APP_TMR_Callback2 to be executed every APP_LED_BLINK_DELAY milliseconds. APP_TMR_Callback2 blinks the LED and debounces the switch */ appData.tmrObj2 = SYS_TMR_ObjectCreate(APP_LED_BLINK_DELAY, 1, APP_TimerCallback2, SYS_TMR_FLAG_PERIODIC); /* Stay in this state until System Timer object created */ if(SYS_TMR_HANDLE_INVALID != appData.tmrObj2) { /* Reset debounce counters to test for switch press */ appData.debounceCounter = 0; appData.timeoutCounter = 0; appData.state = APP_STATE_LED_OFF_DEBOUNCE_PRESS; } break; } break; } /* LED is off and switch press has been detected. Wait for it to be confirmed. */ case APP_STATE_LED_OFF_DEBOUNCE_PRESS: { /* Test if debounce timeout counter expired (press detected but failed to be confirmed). */ if (appData.timeoutCounter < APP_DEBOUNCE_TIMEOUT) { /* If switch press confirmed. */ if (appData.switchIsPressed) { /* Reset debounce counter to test for switch release */ appData.debounceCounter = 0; appData.state = APP_STATE_BLINK_LED_DEBOUNCE_RELEASE; } } /* Switch press not confirmed in time. Revert to previous state. */ else { SYS_TMR_ObjectDelete(appData.tmrObj2); appData.pressDetected = false; appData.state = APP_STATE_LED_OFF_NO_PRESS_DETECT; } break; } /* Blink LED while testing for switch release (no press). */ case APP_STATE_BLINK_LED_DEBOUNCE_RELEASE: { if (!appData.switchIsPressed) { appData.pressDetected = false; appData.state = APP_STATE_BLINK_LED_NO_PRESS_DETECT; } break; } /* Blink LED while waiting for a press to be detected. */ case APP_STATE_BLINK_LED_NO_PRESS_DETECT: { if (appData.pressDetected) { /* Reset debounce counters to test for switch press */ appData.debounceCounter = 0; appData.timeoutCounter = 0; appData.state = APP_STATE_BLINK_LED_DEBOUNCE_PRESS; } break; } /* Continue to blink LED while switch press is confirmed. */ case APP_STATE_BLINK_LED_DEBOUNCE_PRESS: { /* Test if debounce timeout counter expired (press detected but failed to be confirmed). */ if (appData.timeoutCounter < APP_DEBOUNCE_TIMEOUT) { /* If switch press confirmed. */ if (appData.switchIsPressed) { /* Turn off LED. */ BSP_LEDOff(APP_BSP_LED); /* Reset debounce counters to test for switch release */ appData.debounceCounter = 0; appData.state = APP_STATE_LED_OFF_DEBOUNCE_RELEASE; } } /* Switch press not confirmed in time. Revert to previous state. */ else { appData.pressDetected = false; appData.state = APP_STATE_BLINK_LED_NO_PRESS_DETECT; } break; } /* Keep LED off while waiting for switch release (no press). */ case APP_STATE_LED_OFF_DEBOUNCE_RELEASE: { if (!appData.switchIsPressed) { /* This System Timer object is deleted because you don't need this delay in the next state (no switch to debounce and no LED blink delay). Delete to conserve resources. */ SYS_TMR_ObjectDelete(appData.tmrObj2); appData.pressDetected = false; appData.state = APP_STATE_LED_OFF_NO_PRESS_DETECT; } break; } /* The default state should never be executed. */ default: { /* TODO: Handle error in application's state machine. */ break; } } }