#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 "TCA9539.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;
void Sys_50ms_Tasks(void)
{
    uint8_t u8test;
    u8test = Get_CAN_CH0_ID_361_Sig_PRES_DSG();
    u8test++;
    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;
uint8_t g_u8cnt = 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;
}

uint8_t aaaaaaData;
uint8_t abbaaaData;
uint8_t sdfa=0;
void Sys_100ms_Tasks(void)
{
    // Dio_FlipChannel(DioConf_DioChannel_P56DioChannel);
    // Dio_FlipChannel(DioConf_DioChannel_P24_13);
    // g_u8secstatus++;
    // if (g_u8secstatus == 10)
    // {
    //     g_u8secstatus = 0;
        g_u8cnt++;
    // }
    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 (sdfa==0)
{
  sdfa=1;
      TCA9539_IIC_Send(0xEA,0x06,0);
      TCA9539_IIC_Send(0xEA,0x02,0xFF);
}
    // aaaaaaData= Dio_ReadChannel(Port_Config_PortGroup22_PortPin4);
    // 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);
}