#include "VolumeTransducer.h"
#include "define.h"
#include "BoardCfg.h"
#include "timer.h"
#include <stdio.h>
#include "Syslog.h"

int mDebouncingBureau = false;
int mDebouncingCuisine = false;
int mBureauVolume = 0;
int mCuisineVolume = 0;

char VolDebug[100];

int VolumeTransducerInit()
{
    //Bureau volume transducer
    IEC0bits.INT1IE = 0;
    IFS0bits.INT1IF = 0;
    if(BUREAU_VOL_POT_CCW_PIN == 1)
    {
        INTCONbits.INT1EP = 0;  //Falling edge
    }
    else
    {
        INTCONbits.INT1EP = 1;  //Rising edge
    }
    IPC1bits.INT1IP = 2;
    IPC1bits.INT1IS = 1;
    IEC0bits.INT1IE = 1;
    
    
    //Cuisine volume transducer
    IEC0bits.INT3IE = 0;
    IFS0bits.INT3IF = 0;
    if(CUISINE_VOL_POT_CCW_PIN == 1)
    {
        INTCONbits.INT3EP = 0;  //Falling edge
    }
    else
    {
        INTCONbits.INT3EP = 1;  //Rising edge
    }
    IPC3bits.INT3IP = 2;
    IPC3bits.INT3IS = 2;
    IEC0bits.INT3IE = 1;
    
    
    mDebouncingBureau = false;
    mDebouncingCuisine = false;
    
    return RET_OK;
}


int VolumeTransducerTick()
{
    if(mDebouncingBureau == true)
    {
        if(IsTimerExpired(BUREAU_VOLUME_TRANSDUCER_TIMER) == 1)
        {
            TimerStop(BUREAU_VOLUME_TRANSDUCER_TIMER);
            if(BUREAU_VOL_POT_CCW_PIN == BUREAU_VOL_POT_CW_PIN)
            {
                mBureauVolume += (-1 * VOLUME_TRANSDUCER_INCREMENT);
             //   mBureauChange = 0;
                
                sprintf(VolDebug,"Bureau: %d\n",mBureauVolume);
                SyslogNewString(VolDebug);
        //        mBureauChange = -1;
            }
            else
            {
                mBureauVolume += (VOLUME_TRANSDUCER_INCREMENT);
            //    mBureauChange = 0;
                
                sprintf(VolDebug,"Bureau: %d\n",mBureauVolume);
                SyslogNewString(VolDebug);
     //           mBureauChange = 1;
            }
            if(BUREAU_VOL_POT_CCW_PIN == 1)
            {
                INTCONbits.INT1EP = 0;  //Falling edge
            }
            else
            {
                INTCONbits.INT1EP = 1;  //Rising edge
            }
            IFS0bits.INT1IF = 0;
            IEC0bits.INT1IE = 1;
            mDebouncingBureau = false;
        }
    }
    
    
    if(mDebouncingCuisine == true)
    {
        if(IsTimerExpired(CUISINE_VOLUME_TRANSDUCER_TIMER) == 1)
        {
            TimerStop(CUISINE_VOLUME_TRANSDUCER_TIMER);
            if(CUISINE_VOL_POT_CCW_PIN == CUISINE_VOL_POT_CW_PIN)
            {
                mCuisineVolume += (-1 * VOLUME_TRANSDUCER_INCREMENT);
             //   mBureauChange = 0;
                
                sprintf(VolDebug,"Cuisine: %d\n",mCuisineVolume);
                SyslogNewString(VolDebug);
        //        mBureauChange = -1;
            }
            else
            {
                mCuisineVolume += (VOLUME_TRANSDUCER_INCREMENT);
            //    mBureauChange = 0;
                
                sprintf(VolDebug,"Cuisine: %d\n",mCuisineVolume);
                SyslogNewString(VolDebug);
     //           mBureauChange = 1;
            }
            if(CUISINE_VOL_POT_CCW_PIN == 1)
            {
                INTCONbits.INT3EP = 0;  //Falling edge
            }
            else
            {
                INTCONbits.INT3EP = 1;  //Rising edge
            }
            IFS0bits.INT3IF = 0;
            IEC0bits.INT3IE = 1;
            mDebouncingCuisine = false;
        }
    }
}


void __ISR(_EXTERNAL_1_VECTOR, ipl2) BureauTransducerInterrupt(void)
{
    IEC0bits.INT1IE = 0;
    
    TimerStart(BUREAU_VOLUME_TRANSDUCER_TIMER,VOLUME_TRANSDUCER_DEBOUNCE_TIMEOUT);
    mDebouncingBureau = true;
    
	IFS0bits.INT1IF = 0;	//Clear interrupt flag	
}

void __ISR(_EXTERNAL_3_VECTOR, ipl2) CuisineTransducerInterrupt(void)
{
    IEC0bits.INT3IE = 0;
    
    TimerStart(CUISINE_VOLUME_TRANSDUCER_TIMER,VOLUME_TRANSDUCER_DEBOUNCE_TIMEOUT);
    mDebouncingCuisine = true;
    
	IFS0bits.INT3IF = 0;	//Clear interrupt flag	
}