#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();
}
uint8_t u8test;
void Sys_50ms_Tasks(void)
{
  u8test = Get_ID_18FF30E1_Sig_ECAS_Decrease();
}
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;
void Sys_100ms_Tasks(void)
{
  time_test++;

  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_U16 = g_u16ADCValue[i];
    }
    if (i == 0)
    {
      ADC_U48 = g_u16ADCValue[i];
    }
  }
  Dio_WriteChannel(DioConf_DioChannel_P04_08, STD_LOW);
  MC33984EHFK_Read();

  SGMCD1020Q_MainFunction();
  PC_Control_U16();
  PC_Control_U48();
}