//#include <proc/p32mx440f256h.h>

#include "KnobEncoderCtrl.h"
#include "BoardCfg.h"
#include "LedLightCtrl.h"
#include "timer.h"

bool KnobDebounce;
int KnobReadSM = KEC_WAIT_FOR_TRIGGER_STATE;
#define PIN_ACTIVE 0
#define PIN_INACTIVE 1

//bool KnobDebounce;
//void __ISR(_EXTERNAL_1_VECTOR , ipl2) KnobEncoderInterrupt(void)
//{
//    if(KnobDebounce == false)
//    {
//        int dir = KNOB_PH_B_PIN;
//       
//         KnobDebounce = true;
//         TimerStart(KNOB_DEBOUNCE_TIMER,KNOB_DEBOUNCE_TIMEOUT);
//         IEC0bits.INT1IE = 0;
//        
//        if(dir == 1)
//        {
//            LedLightIncrease();
//        }
//        else
//        {
//            LedLightDecrease();
//        }
//    }
//    
//    IFS0bits.INT1IF = 0;
//}

void KnobDebounceCtrlSM()
{
    switch(KnobReadSM)
    {
        case KEC_CHECK_BTN_STATE:
        {
            if(KNOB_TGLE_BTN_PIN == PIN_ACTIVE)
            {
                LedLightONOFFBtnPressed();
                KnobReadSM = KEC_DEBOUNCE_BTN_STATE;
                TimerStart(KNOB_DEBOUNCE_TIMER,250);
            }
            else
            {
                KnobReadSM = KEC_WAIT_FOR_TRIGGER_STATE;
            }
            break;
        }
        case KEC_DEBOUNCE_BTN_STATE:
        {
            if(IsTimerExpired(KNOB_DEBOUNCE_TIMER))
            {
                KnobReadSM = KEC_WAIT_FOR_BTN_RST_STATE;
            }
        
            break;
        }
        case KEC_WAIT_FOR_BTN_RST_STATE:
        {
            if(KNOB_TGLE_BTN_PIN == PIN_INACTIVE)
            {
                KnobReadSM = KEC_WAIT_FOR_TRIGGER_STATE;
            }
            break;
        }
        case KEC_WAIT_FOR_TRIGGER_STATE:
        {
            if(KNOB_PH_A_PIN == PIN_ACTIVE)
            {
                if(KNOB_PH_B_PIN == PIN_ACTIVE)
                {
                    LedLightIncrease();
                }
                else
                {
                    LedLightDecrease();
                }
                TimerStart(KNOB_DEBOUNCE_TIMER,KNOB_DEBOUNCE_TIMEOUT);
                KnobReadSM = KEC_DEBOUNCE_STATE;
            }
            else
            {
                KnobReadSM = KEC_CHECK_BTN_STATE;
            }
            break;
        }
        case KEC_DEBOUNCE_STATE:
        {
            if(IsTimerExpired(KNOB_DEBOUNCE_TIMER))
            {
                KnobReadSM = KEC_WAIT_FOR_RESET_STATE;
            }
            break;
        }
        case KEC_WAIT_FOR_RESET_STATE:
        {
             if((KNOB_PH_A_PIN == PIN_INACTIVE))
            {
                if(KNOB_PH_B_PIN == PIN_INACTIVE)
                {
                    TimerStart(KNOB_DEBOUNCE_TIMER,KNOB_DEBOUNCE_TIMEOUT);
                    KnobReadSM = KEC_WAIT_A_LITTLE_MORE_STATE;
                }
            }
              
            break;
        }
        case KEC_WAIT_A_LITTLE_MORE_STATE:
        {
            if(IsTimerExpired(KNOB_DEBOUNCE_TIMER))
            {
                KnobReadSM = KEC_CHECK_BTN_STATE;
            }
            break;
        }
    }
}