/*******************************************************************************
 * the includes
 ******************************************************************************/
#include "ad_button1.h"


/*******************************************************************************
 * the defines
 ******************************************************************************/

#define DMA_CHANNEL                 (0)
#define ADC_DEMO_LAST_CHANNLE       (2u)
#define ADC_DEMO_CHANNLE_NUMBER     (4u)

/*ad_value = v * 1024 / 3.3
        v  = ad_value * 3.3 / 1024
*/	 
	 
#define AD1_1         467
#define AD1_2         653
#define AD2_1         467
#define AD2_2         653

#define AD3_1         467
#define AD3_2         653
#define AD3_3         0

#define AD_tolerance 50
#define Key_Delay    10

/*******************************************************************************
 * the typedefs
 ******************************************************************************/

/*******************************************************************************
 * the globals
 ******************************************************************************/

volatile int32_t adcRawValue[ADC_DEMO_CHANNLE_NUMBER] = {0,0,0,0};
uint16_t switch_count[7];
unsigned char ad_button_state=0;

/* Instance of MCU */
extern McuType mcu;



/*******************************************************************************
 * the functions
 ******************************************************************************/


void adc_init(void)
{
	PinsDrv_SetMuxModeSel(&mcu.ptc, 2, PINSDRV_PIN_DISABLED);	/*ntc_ad*/
    PinsDrv_SetMuxModeSel(&mcu.ptc, 3, PINSDRV_PIN_DISABLED);	/*ad3*/
    PinsDrv_SetMuxModeSel(&mcu.ptc, 4, PINSDRV_PIN_DISABLED);	/*ad2*/
    PinsDrv_SetMuxModeSel(&mcu.ptc, 5, PINSDRV_PIN_DISABLED);	/*ad1*/

    /* Configure ADC module */
    AdcDrv_ConfigType adcCfg;
    AdcDrv_GetDefaultConfig(&adcCfg);
	
    adcCfg.trgSrcCfg.hwTrgSrc                      = ADCDRV_HW_TRGSRC_PDB;          /* Use PDB output for trigger source */
    adcCfg.trgSrcCfg.pretrgSrc                     = ADCDRV_PRETRG_PDB_PRETRIGGER;  /* Use PDB Pre-Trigger for ADC Pre-Trigger sources */
    adcCfg.avgEnable                               = true;                          /* Enable hardware average function */
    adcCfg.avgSamplesSel                           = ADCDRV_AVERAGE_32;             /* Select 32 samples average */
    adcCfg.continuousMode                          = ADCDRV_ONESHOT;                /* Select one-shot mode */
	
	adcCfg.chnCfg[0].pCh                           = ADCDRV_PCH_TK_ADC18;
    adcCfg.chnCfg[0].intEnable                     = true;
	
    adcCfg.chnCfg[1].pCh                           = ADCDRV_PCH_TK_ADC19;
    adcCfg.chnCfg[1].intEnable                     = true;
    adcCfg.chnCfg[2].pCh                           = ADCDRV_PCH_TK_ADC20;
    adcCfg.chnCfg[2].intEnable                     = true;
    adcCfg.chnCfg[3].pCh                           = ADCDRV_PCH_TK_ADC21;
    adcCfg.chnCfg[3].intEnable                     = true;
        
    /* Enable ADC interrupts */
    IrqDrv_EnableIrq(ADC_IRQn);

    AdcDrv_Configure(&mcu.adcDrv, &adcCfg);

    /* Configure PDB module */
    uint32_t pdbFreq;
    ClockDrv_GetFreq(&mcu.clockDrv, CLOCKDRV_PDB, &pdbFreq);

    PdbDrv_ConfigType pdbCfg;
    PdbDrv_GetDefaultConfig(&pdbCfg);
	
    pdbCfg.trgInSel                                = PDBDRV_TRGSEL_TRGGER_SOFTWARE; /* Use software trigger source */
    pdbCfg.cMode                                   = PDBDRV_CONT_CONTINUOUS;        /* PDB operation in One-Shot mode */
    pdbCfg.prescalerFactor                         = PDBDRV_PRESCALER_128MULT;      /* Counting uses the peripheral clock divided by MULT (the multiplication factor) */
    pdbCfg.mult                                    = PDBDRV_MULT_FACTOR_40;         /* Multiplication factor is 40 */
	pdbCfg.preTrgCfg.mode[0][0]					   = PDBDRV_PRETRG_DELAY_MODE;		/* The first Pre-Trigger channel start with bypassed mode */
    pdbCfg.preTrgCfg.mode[0][1]                    = PDBDRV_PRETRG_BB_MODE;      
    pdbCfg.preTrgCfg.mode[0][2]                    = PDBDRV_PRETRG_BB_MODE;         /* The others use Back-to-Back operation */
    pdbCfg.preTrgCfg.mode[0][3]                    = PDBDRV_PRETRG_BB_MODE;
    pdbCfg.dlyCfg.modCnt                           = pdbFreq / 128 / 40 / 100;          /* Periodic triggering PDB in 1s*/
    pdbCfg.dlyCfg.dlyCnt[0][0]                     = 0;                             /* first channel don't need delay time*/
    PdbDrv_Configure(&mcu.pdbDrv, &pdbCfg);

    /* Enable PDB */
    PdbDrv_EnablePdb(&mcu.pdbDrv);

    /* Trigger PDB*/
    PdbDrv_SwtrigCmd(&mcu.pdbDrv);
}


void ad_key_scan(void)
{
    AD_switch_detect(adcRawValue[1],AD3_1,1,&switch_count[0]);//s11
    AD_switch_detect(adcRawValue[1],AD3_2,2,&switch_count[1]);//s12
	AD_switch_detect(adcRawValue[1],AD3_3,3,&switch_count[2]);//s13
	
    AD_switch_detect(adcRawValue[2],AD2_1,4,&switch_count[3]);//s9
    AD_switch_detect(adcRawValue[2],AD2_2,5,&switch_count[4]);//s10
	
    AD_switch_detect(adcRawValue[3],AD1_1,6,&switch_count[5]);//s7
    AD_switch_detect(adcRawValue[3],AD1_2,7,&switch_count[6]);//s8
	
    
}

void AD_switch_detect(int32_t adcRawValue,int32_t AD_mid1,uint8_t index,uint16_t *switch_count)
{
    if(adcRawValue<=AD_mid1+AD_tolerance && adcRawValue>=AD_mid1-AD_tolerance)
	{
        (*switch_count)++;
        if((*switch_count)>=Key_Delay)//短按10改为20，5ms*20=100ms;
        {
            ad_button_state = 0x01 << (index-1);
        }
    }
    else
	{
        *switch_count=0;
        ad_button_state &= ~(1<< (index-1) );
    }
}


