#include "Sys_Scheduler_Lib.h"
#include "Dio.h"
#include "COM_CAN.h"
#include "SchM_Can.h"
#include "Port.h"
#include "CAN_CH0_CAN_Communication_Matrix.h"
#include "Adc.h"
#include "SGM4591Q.h"
#include "MC33984EHFK.h"
#include "SGMCD1020Q.h"
#include "PC_Control.h"


void Sys_50us_Tasks(void)
{
    COM_TX_Process();
    // Port_SetToDioMode();
    // Port_SetToAlternateMode();
    // Dio_FlipChannel(DioConf_DioChannel_P56DioChannel);
}

void Sys_2ms_Tasks(void)
{
    
}

void Sys_5ms_Tasks(void)
{
    COM_RX_Process();
}

void Sys_10ms_Tasks(void)
{
    
}

void Sys_20ms_Tasks(void)
{
    // Can_MainFunction_Read();
}
uint32_t u32IOConvert;
uint8_t u8test;
void Sys_50ms_Tasks(void)
{
    u8test = Get_ID_18FF30E1_Sig_ECAS_Decrease();

    u32IOConvert++;
    // if (u32IOConvert > 100)
    // {
        // Port_SetPinDirection(Port_Config_PortGroup24_PortPin13,PORT_PIN_IN);
        // Dio_WriteChannel(DioConf_DioChannel_P24_13,STD_LOW);
    // }
}
uint16_t g_u16ADCValue[16] = {0};
uint8_t g_u8secstatus = 0U;

uint16_t ADC_Input_Voltage_Calc(uint16_t u16ADCSample, uint16_t u16ADCRes, uint16_t u16VRef);
uint16_t ADC_Input_Voltage_Calc(uint16_t u16ADCSample, uint16_t u16ADCRes, uint16_t u16VRef)
{
    uint32_t u32ADCVoltage;
    uint16_t u16Result;

    if ( u16ADCRes == 0U )
    {
        u16Result = 0x0000U;
    }
    else if ( u16ADCRes <= u16ADCSample )
    {
        u16Result = u16VRef;
    }
    else
    {
        u32ADCVoltage = ( uint32_t )u16ADCSample;
        u32ADCVoltage *= ( uint32_t )u16VRef;
        u32ADCVoltage /= ( uint32_t )u16ADCRes;

        u16Result = ( uint16_t )u32ADCVoltage;
    }

    return u16Result;
}
uint32_t time_test=0;
uint8_t aaaaaaData = 0U;
uint8_t bcvcb = 0U;
// uint8 GaaByteAccfdfss[100] = { 0 };
void Sys_100ms_Tasks(void)
{
  time_test++;
    // Dio_FlipChannel(DioConf_DioChannel_P56DioChannel);
    // Dio_FlipChannel(DioConf_DioChannel_P24_13);
    // g_u8secstatus++;

        // Dio_FlipChannel(DioConf_DioChannel_P02_08);

        /* Starts the conversion of group */
        Adc_StartGroupConversion(AdcConf_AdcGroup_AdcGroup_000);

        while (ADC_STREAM_COMPLETED != Adc_GetGroupStatus(AdcConf_AdcGroup_AdcGroup_000))
          ;

        Adc_ReadGroup(AdcConf_AdcGroup_AdcGroup_000, g_u16ADCValue);

        for (uint8_t i = 0; i < 16; i++)
        {
          g_u16ADCValue[i] = ADC_Input_Voltage_Calc(g_u16ADCValue[i], 4096, 5000);
          if (i==4)
          {
            ADC_4=g_u16ADCValue[i];
          }          
        }
    // SGM4591Q_Control();
    
  // Port_SetPinDirection(Port_Config_PortGroup4_PortPin8, PORT_PIN_OUT);
  Dio_WriteChannel(DioConf_DioChannel_P04_08, STD_LOW);
    MC33984EHFK_Read();

  // if (bcvcb == 1)
  // {
  //   Dio_WriteChannel(DioConf_DioChannel_P02_05, STD_HIGH);
  //   Dio_WriteChannel(DioConf_DioChannel_P02_01, STD_HIGH);
  //   bcvcb = 0;
  // }
  // if (bcvcb == 2)
  // {
  //   Dio_WriteChannel(DioConf_DioChannel_P02_05, STD_LOW);
  //   Dio_WriteChannel(DioConf_DioChannel_P02_01, STD_LOW);
  //   bcvcb = 0;
  // }

  // if (aaaaaaData < 99)
  // {
  //   aaaaaaData++;
  // }
  // else
  // {
  //   aaaaaaData = 0;
  // }

  // GaaByteAccfdfss[aaaaaaData] = Dio_ReadChannel(Port_Config_PortGroup2_PortPin1);
  // abbaaaData= Dio_ReadChannel(Port_Config_PortGroup22_PortPin3);
  // Port_SetPinDirection(Port_Config_PortGroup22_PortPin3, PORT_PIN_IN);
    
  // Port_SetPinDirection(Port_Config_PortGroup22_PortPin4, PORT_PIN_IN);
  
  // Port_SetPinDirection(Port_Config_PortGroup22_PortPin4, PORT_PIN_OUT);
  
  // Port_SetPinDirection(Port_Config_PortGroup22_PortPin3, PORT_PIN_OUT);
  SGMCD1020Q_MainFunction();
  PC_Control_U16();
  PC_Control_U48();
}