#include   "SysDiagDetect.h"
#include   "FunctionState.h"       
#include "OsekCom/OsekCom.h"
#include "RTE.h"
#include   "forcedetect.h"
#include   "forceSnsr.h"



static uint8 Bat_Rng = 0;
static void Battery_Volt_Diag_Task(void)
{
    if(Fuction_State != Function_State_A)
     {
        Bat_Rng = Bat_Out_Range;
        InitMessage(SIG_DIAGCFAILRTOUCHPANSWTRCMNFLTSTS, &Bat_Rng);

     }
     else
     {
       Bat_Rng = Bat_In_Range;
       InitMessage(SIG_DIAGCFAILRTOUCHPANSWTRCMNFLTSTS, &Bat_Rng);

     }
}

uint8 Force_Sens_Err = 0;
static void Force_Sensor_Diag_Task(void)
{
    uint16 Diag_Press_baseline;
    sint16 Diag_Convert_baseline;
    uint16 CurrentDiag_Press_signal;
    static uint16 LastDiag_Press_signal = 0;
    static uint16 Press_NoChange_Count = 0;
    
    //basline + - convert 
    Diag_Press_baseline=Get_forcedetect_basline_value();
    if((Diag_Press_baseline >> 11) == 1)
    {
      Diag_Convert_baseline = Diag_Press_baseline - 4096;
    }
    else
    {
      Diag_Convert_baseline = Diag_Press_baseline;
    }
    
    CurrentDiag_Press_signal=Get_forcedetect_force_value();
    
    //Force sensor Diag start
    if(forcesnsr_i2c_process_state_u8 == 1)
    {
      Force_Sens_Err = 1;
    }
    else
    {
        Force_Sens_Err = 0;
        if(CurrentDiag_Press_signal == LastDiag_Press_signal)
        {
          Press_NoChange_Count++;
          if(Press_NoChange_Count>=800) //4 seconds
          {
            Force_Sens_Err = 1;
            Press_NoChange_Count = 800;
          }
        }
        else
        {
          Press_NoChange_Count = 0;
        }
        
        if(Diag_Convert_baseline < -2040 || Diag_Convert_baseline > 1000 )
        {
           Force_Sens_Err = 1;
        }
    }
   
   //Force_Sens_Err = 1; //just for test,to set sensor error
   LastDiag_Press_signal = CurrentDiag_Press_signal; 
   InitMessage(SIG_DIAGCFAILRTOUCHPANSWTRSNSRFLTSTS, &Force_Sens_Err);
}


//Touch_Sensor_Sts00;
static uint8 Touch_Sensor_Fault = 0;
static void Touch_Sensor_Diag_Task(void)
{
  if(Touch_Sensor_Sts00<100 && Touch_Sensor_Sts01<100 && Touch_Sensor_Sts02<100 && Touch_Sensor_Sts03<100 && Touch_Sensor_Sts04<100 && 
     Touch_Sensor_Sts05<200 && Touch_Sensor_Sts06<200 && Touch_Sensor_Sts07<200 && Touch_Sensor_Sts08<200 && Touch_Sensor_Sts09<200 && 
     Touch_Sensor_Sts10<300 && Touch_Sensor_Sts11<300 && Touch_Sensor_Sts12<300 && Touch_Sensor_Sts13<300 && Touch_Sensor_Sts14<300)
  {
    Touch_Sensor_Fault = Touch_NoFault;
    InitMessage(SIG_DIAGCFAILRTOUCHPANSWTRTOUCHDFLTSTS, &Touch_Sensor_Fault);
  }
  else
  {
    Touch_Sensor_Fault = Touch_Out_Range;
    InitMessage(SIG_DIAGCFAILRTOUCHPANSWTRTOUCHDFLTSTS, &Touch_Sensor_Fault);
  }
}


static uint8 Vibra_Fault = 0;
static void Vibration_Diag_Task(void)
{
   //AD_data=voltage * 4096/3.3V   normal:0-0.01(15)  short: 0.15(185)-0.36(448)  0.6(742)-0.7(870)  0.95(1177)-1.05(1305) 1.27(1575)-1.39(1727)  open:1.55(1921) - 1.72(2136)
   
    Vibra_Fault = Vibration_Normal;
    InitMessage(SIG_DIAGCFAILRTOUCHPANSWTRVIBRATIONFLTSTS, &Vibra_Fault);

    /*
    if(LD_AD_Val>=0 && LD_AD_Val<=15)  //normal
    {
        Vibra_Fault = Vibration_Normal;
        InitMessage(SIG_DIAGCFAILRTOUCHPANSWTRVIBRATIONFLTSTS, &Vibra_Fault);
    }
    else if((LD_AD_Val>=185 && LD_AD_Val<=448)||(LD_AD_Val>=742 && LD_AD_Val<=870)||(LD_AD_Val>=1177 && LD_AD_Val<=1305)||(LD_AD_Val>=1575 && LD_AD_Val<=1727))
    {
        Vibra_Fault = Vibration_Short;
        InitMessage(SIG_DIAGCFAILRTOUCHPANSWTRVIBRATIONFLTSTS, &Vibra_Fault);
    }
    else if(LD_AD_Val>=1921 && LD_AD_Val<=2136)
    {
        Vibra_Fault = Vibration_open;
        InitMessage(SIG_DIAGCFAILRTOUCHPANSWTRVIBRATIONFLTSTS, &Vibra_Fault);
    }
    */
}



void Sys_Diag_Detcet_Task(void)
{
   Battery_Volt_Diag_Task();
   if (Bat_Rng == Bat_In_Range)
   {
        Force_Sensor_Diag_Task();
        Touch_Sensor_Diag_Task();
        Vibration_Diag_Task();
   }
}


