/******************************************************************************
  Function:
    void APP_Tasks ( void )

  Remarks:
    See prototype in app.h.
 */

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;
                appData.switchHasBeenPressed = true;
                appData.state = APP_STATE_TIMER_OBJECT_CREATE;
            }
            break;
        }

//        case APP_STATE_SERVICE_TASKS:
//        {

//            break;
//        }

        /* TODO: implement your application state machine.*/

        /* State to create the timer object for periodic alarm */
        case APP_STATE_TIMER_OBJECT_CREATE:
        {
            appData.tmrObj = SYS_TMR_ObjectCreate(APP_LED_BLINK_DELAY, 1, APP_TimerCallBack, SYS_TMR_FLAG_PERIODIC);
            if(SYS_TMR_HANDLE_INVALID != appData.tmrObj)
            {
                appData.state = APP_STATE_DEBOUNCE_START;
            }
            break;
        }

        case APP_STATE_DEBOUNCE_START:
        {
            if (appData.timeoutCounter < APP_DEBOUNCE_TIMEOUT)
            {
                if (appData.switchHasBeenPressed)
                {
                    appData.changeNoticed = false;
                    appData.state = APP_STATE_LED_BLINKING;
                }
            }
            else
            {
                SYS_TMR_ObjectDelete(appData.tmrObj);            
                appData.state = APP_STATE_IDLE;
            }
            break;
        }

        case APP_STATE_LED_BLINKING:
        {
            if (appData.changeNoticed)
            {
                appData.changeNoticed           = false;
                appData.switchHasBeenPressed    = false;
                appData.debounceCounter         = 0;
                appData.timeoutCounter          = 0;
                appData.state = APP_STATE_DEBOUNCE_STOP;
            }
            break;
        }

        case APP_STATE_DEBOUNCE_STOP:
        {
            if (appData.timeoutCounter < APP_DEBOUNCE_TIMEOUT)
            {
                if (appData.switchHasBeenPressed)
                {
                    appData.changeNoticed = false;
                    BSP_LEDOff(APP_BSP_LED);
                    SYS_TMR_ObjectDelete(appData.tmrObj);            
                    appData.state = APP_STATE_IDLE;
                }
            }
            else
            {
                appData.changeNoticed           = false;
                appData.switchHasBeenPressed    = false;
                appData.state = APP_STATE_LED_BLINKING;
            }
            break;
        }

        case APP_STATE_IDLE:
        default:
        {
            if (appData.changeNoticed)
            {
                /* Start timer and prepare to detect switch press. */
                appData.changeNoticed           = false;
                appData.switchHasBeenPressed    = false;
                appData.debounceCounter         = 0;
                appData.timeoutCounter          = 0;
                appData.state   = APP_STATE_TIMER_OBJECT_CREATE;
            }
            break;
        }

        /* The default state should never be executed. */
//        default:
//        {
//            /* TODO: Handle error in application's state machine. */
//            break;
//        }
    }
}