#include "adc.h"
#include "soft_adc.h"

uint16_t ADC_value = 0;
float aver_value = 0;
uint8_t adc_cnt = 0;
uint32_t ADC_flag = 0;
/*********************AD中断回调*******************/
void HAL_ADC_ConvCpltCallback( ADC_HandleTypeDef *hadc )
{
    ADC_flag = 1;
}

/**
  * @file    buf_sort
  * @brief   冒泡 从小到大
  * @param   arr, length
  * @details 
  * @author  Zhang Sir 
 **/
void buf_sort( float *arr, uint32_t length )
{
    if ( length < 2 )
    {
        return;
    }
    uint32_t num = 0, num1 = 0;
    float tmp = 0;
    for ( num = length - 1; num >= 1; num-- )
    {
        for ( num1 = 0; num1 <= num - 1; num1++ )
        {
            if ( *( arr + num1 ) > *( arr + num1 + 1 ) )
            {
                tmp = *( arr + num1 );
                *( arr + num1 ) = *( arr + num1 + 1 );
                *( arr + num1 + 1 ) = tmp;
            }
        }
    }
}
/**
  * @file    Get_ADCChannelValue
  * @brief   获取不同通道ad采集
  * @param   None
  * @details 
  * @author  Zhang Sir 
 **/
uint16_t Get_ADCChannelValue(ADC_HandleTypeDef *hadc, uint32_t channel)
{
	uint16_t temp_adc = 0;
	ADC_ChannelConfTypeDef ADC_ChanConf;

	ADC_ChanConf.Channel = channel;
	ADC_ChanConf.Rank = ADC_REGULAR_RANK_1;
	ADC_ChanConf.SamplingTime = ADC_SAMPLETIME_71CYCLES_5;
	if (HAL_ADC_ConfigChannel(hadc, &ADC_ChanConf) != HAL_OK)
	{
		Error_Handler();
	}

	HAL_ADC_Start_IT(hadc);
	HAL_ADC_PollForConversion(hadc, 1);

	temp_adc = HAL_ADC_GetValue(hadc);
	return  temp_adc;
}

/**
  * @file    adc_gather_hz
  * @brief   AD电压采集
  * @param   None
  * @details 
  * @author  Zhang Sir 
 **/
static float ADC_buf[20];
float ADC_temp = 0.0f;
void adc_gather_hz()
{
    static uint32_t adc_time = 0;
    if ( HAL_GetTick() - adc_time > 20 )
    {
        ADC_buf[adc_cnt] = Get_ADCChannelValue(&hadc1, ADC_CHANNEL_0);
        HAL_ADC_Stop(&hadc1);

        adc_cnt++;
        if ( adc_cnt >= 20 )
        {
            adc_cnt = 0;
        }
        adc_time = HAL_GetTick();
    }
}


/**
  * @file    ADC_gather
  * @brief   AD采集转换成电压
  * @param   None
  * @details 
  * @author  Zhang Sir 
 **/
float ADC_gather( void )
{
    float real_value = 0;
    if ( ADC_flag == 1 )
    {
        ADC_flag = 0;
        buf_sort( ADC_buf, 20 );
        for ( uint8_t ib = 1; ib < 19; ib++ )
        {
            real_value += ADC_buf[ib];
        }
        //aver_value = real_value / 4096 * 3.3f / 18 * 28 * 1000; //Mv  4096*3 真实电压 , 取18个值 , pmu28分频
        //12bit 4095 超过94V 28分频不够
        aver_value = real_value / 4096 * 3.226f / 18 * 31 * 1000; //Mv  4096*3 真实电压 , 取18个值 , pmu31分频
    }

    return aver_value;
}


/**
  * @file    ADC_read_temptrue
  * @brief   ad温度采集
  * @param   None
  * @details 
  * @author  Zhang Sir 
 **/
uint16_t ADC_read_temptrue()
{
	uint16_t mcu_temp = 0;
    float mcu_temp1 = 0;

    mcu_temp = Get_ADCChannelValue(&hadctemp, ADC_CHANNEL_16);
    HAL_ADC_Stop(&hadctemp);

    mcu_temp1 = ( int )( ( 1.43f - mcu_temp * 3.3f / 4096 ) / 0.00043f + 250 ) / 10.0f;

    return mcu_temp1;
}
