#include <stdint.h>
#include "common_include.h"
#include "api_capture.h"

extern FREQUENCE Frequeice; 
extern FREQUENCE BackLight;
uint16_t BackLight_List_L[5],BackLight_List_H[5],uer_count_1,uer_count_2;
uint16_t Frequeice_List[5], uer_count, Frequeice_Value;
volatile uint16_t  BackLight_TimeH;
volatile uint16_t  BackLight_TimeL;
void WheelSpeedSensor1(uint8_t channel) //上升沿触发
{
    GET_BUTTON_BackLight_LEVEL;
    if(BUTTON_BackLigth_IN == 1)
    {
      BackLight.Flag = 1;
      BackLight.Time = 0;
      
      BackLight_List_L[ uer_count_1 ] = BackLight.TimeL;
      uer_count_1 ++;
      uer_count_1 = (uer_count_1 + 1) % 6;
      App_SortShort(BackLight_List_L,6);

      if((BackLight_TimeL > (BackLight_List_L[ 2 ])) || ((BackLight_TimeL ) < BackLight_List_L[ 2 ]))
      {
        BackLight_TimeL = BackLight_List_L[ 4 ] ;
      }
    }
    if( BUTTON_BackLigth_IN == 0)
    {
      BackLight.Flag = 0;
      BackLight.TimeL = 0;
      BackLight_List_H[ uer_count_2 ] = BackLight.Time;
      uer_count_2 ++;
      uer_count_2 = (uer_count_2 + 1) % 6;
      App_SortShort(BackLight_List_H,6);
      if((BackLight_TimeH > (BackLight_List_H[ 2 ])) || ((BackLight_TimeH ) < BackLight_List_H[ 2 ]))
      {
        BackLight_TimeH = BackLight_List_H[ 4 ] ;
      }
    }
    
    BackLight.Value = BackLight_TimeL * 1000 / (BackLight_TimeH + BackLight_TimeL);
}
void WheelSpeedSensor0(uint8_t channel) //上升沿触发
{
    if ( Frequeice.Flag == 0 ) 
    { //第一个上升触发
        Frequeice.Time = 0;
        Frequeice.Flag = 1;
    } 
    else if ( Frequeice.Flag == 1  ) 
    {
       
        Frequeice.Count = Frequeice.Time;
        
        Frequeice.Time = 0;
        
        Frequeice_List[ uer_count ] = Frequeice.Count;
        uer_count ++;

        uer_count = (uer_count + 1) % 6;

        App_SortShort(Frequeice_List,6);

        if((Frequeice_Value > (Frequeice_List[ 2 ] + 3)) || ((Frequeice_Value + 3) < Frequeice_List[ 2 ]))
        {
          Frequeice_Value = Frequeice_List[ 2 ] ;
        }
        
        Frequeice.Value = 100000/Frequeice_Value;
    } 
}



void ICU_IO_Initialize(void)
{
    //模拟外部中断 
     //模拟外部中断 
    stc_eic_config_t      stcEicConfig;       //设置外部中断
    // EIC Configuration
    //stcEicConfig.enRequestEvent               = EicAnyEdge;             // Detect rising edge
    stcEicConfig.enRequestEvent               = EicRisingEdge;
    stcEicConfig.bInputNoiseFilterEnable      = FALSE;                  // Enable the noise filter
    stcEicConfig.bDmaRequestEnable            = FALSE;                  // Disable the DMA request
    stcEicConfig.pfnCallback                  = &WheelSpeedSensor0;     // using interrupts       
    
    Port_SelectInputPort(&RIC.unRESIN265, PortInputPortE);   //车速频率
    Eic_Init((stc_eic00_t *) &EIC00, 14, &stcEicConfig);  
    Eic_Enable((stc_eic00_t *) &EIC00, 14);
    
    stcEicConfig.enRequestEvent               = EicAnyEdge;
    stcEicConfig.bInputNoiseFilterEnable      = FALSE;                  // Enable the noise filter
    stcEicConfig.bDmaRequestEnable            = FALSE;                  // Disable the DMA request
    stcEicConfig.pfnCallback                  = &WheelSpeedSensor1;     // using interrupts  
    
    Port_SelectInputPort(&RIC.unRESIN264, PortInputPortE);   //ILL频率
    Eic_Init((stc_eic00_t *) &EIC00, 13, &stcEicConfig);  
    Eic_Enable((stc_eic00_t *) &EIC00, 13);

    Port_EnableInput();    
    
}
 /*
void Frequency_Task(FREQUENCE *Argument)
{
    if (1 == Argument->Statu) {      
        Argument->Value = 10000 / Argument->Count;//Argument->Sensor0time; 
   
      //  Argument->Sensor0Count = 0;
      //  Argument->Sensor0time = 0;
      //  Argument->Sensor0TimeOut = 0;
        Argument->Sensor0Valid = 0;
    }  
}
*/
