#include "PushBtnMgr.h"
#include "timer.h"
#include "Syslog.h"

BtnState_t mLinkZ2BtnState, mMainZonePwrBtn, mZ2PwrBtn;




void PushBtnMgrInit()
{
    mLinkZ2BtnState.Debouncing = false;
    mMainZonePwrBtn.Debouncing = false;
    mZ2PwrBtn.Debouncing = false;
    
    mLinkZ2BtnState.BtnState = AUDIO_CONSOLE_LINK_Z2_BTN;
    mMainZonePwrBtn.BtnState = AUDIO_CONSOLE_MAIN_PWR_BTN;
    mZ2PwrBtn.BtnState = AUDIO_CONSOLE_Z2_PWR_BTN;
    
 
    
    TimerStart(PUSHBTN_DEBOUNCE_TIMER,PUSHBTN_DEBOUNCE_TIMEOUT);
}


void PushButtonMgrTick()
{
    if(IsTimerExpired(PUSHBTN_DEBOUNCE_TIMER) == true)
    {
        //Check link Z2 volume with main zone btn
        if(mLinkZ2BtnState.BtnState != AUDIO_CONSOLE_LINK_Z2_BTN)
        {
            mLinkZ2BtnState.BtnState = AUDIO_CONSOLE_LINK_Z2_BTN;
            mLinkZ2BtnState.Debouncing = true;
        }
        else if(mLinkZ2BtnState.Debouncing == true)
        {
            //signal is debounced
            mLinkZ2BtnState.Debouncing = false;
            if(AUDIO_CONSOLE_LINK_Z2_BTN == 0)
            {
                LinkZ2BtnPressed();
                ONBOARD_LED7_PIN = LED_ON;
                SyslogNewString("Link Z2 Btn ON\n");
            }
            else
            {
                ONBOARD_LED7_PIN = LED_OFF;
                SyslogNewString("Link Z2 Btn OFF\n");
            }
        }
        
        //Check Main zone power toggle btn
        if(mMainZonePwrBtn.BtnState != AUDIO_CONSOLE_MAIN_PWR_BTN)
        {
            mMainZonePwrBtn.BtnState = AUDIO_CONSOLE_MAIN_PWR_BTN;
            mMainZonePwrBtn.Debouncing = true;
        }
        else if(mMainZonePwrBtn.Debouncing == true)
        {
            //signal is debounced
            mMainZonePwrBtn.Debouncing = false;
            if(AUDIO_CONSOLE_MAIN_PWR_BTN == 0)
            {
                MainZonePwrBtnPressed();
                ONBOARD_LED7_PIN = LED_ON;
                SyslogNewString("Main Zone Pwr ON\n");
            }
            else
            {
                ONBOARD_LED7_PIN = LED_OFF;
                SyslogNewString("Main Zone Pwr OFF\n");
            }
        }
        
        //Check Main zone power toggle btn
        if(mZ2PwrBtn.BtnState != AUDIO_CONSOLE_Z2_PWR_BTN)
        {
            mZ2PwrBtn.BtnState = AUDIO_CONSOLE_Z2_PWR_BTN;
            mZ2PwrBtn.Debouncing = true;
        }
        else if(mZ2PwrBtn.Debouncing == true)
        {
            //signal is debounced
            mZ2PwrBtn.Debouncing = false;
            if(AUDIO_CONSOLE_Z2_PWR_BTN == 0)
            {
                Zone2PwrBtnPressed();
                ONBOARD_LED7_PIN = LED_ON;
                SyslogNewString("Zone 2 Pwr ON\n");
            }
            else
            {
                ONBOARD_LED7_PIN = LED_OFF;
                SyslogNewString("Zone 2 Pwr OFF\n");
            }
        }
        
        
        TimerStart(PUSHBTN_DEBOUNCE_TIMER,PUSHBTN_DEBOUNCE_TIMEOUT);
    }
    
    

    
    
    
}