/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2024-05-11     LJ       the first version
 */
#include <adc.h>

rt_adc_device_t adc1_dev;
rt_adc_device_t adc2_dev;
rt_adc_device_t adc3_dev;
ADC_Data ADC;

/**
 * ADC初始化函数
 * @return
 */
rt_uint8_t adc_init()
{
    //查找设备
    adc1_dev = (rt_adc_device_t) rt_device_find("adc1");
    if (adc1_dev == RT_NULL)
    {
        rt_kprintf("adc sample run failed! can't find %s device!\n", "adc1");
        return RT_ERROR;
    }

    adc2_dev = (rt_adc_device_t) rt_device_find("adc2");
    if (adc2_dev == RT_NULL)
    {
        rt_kprintf("adc sample run failed! can't find %s device!\n", "adc3");
        return RT_ERROR;
    }

    adc3_dev = (rt_adc_device_t) rt_device_find("adc3");
    if (adc3_dev == RT_NULL)
    {
        rt_kprintf("adc sample run failed! can't find %s device!\n", "adc3");
        return RT_ERROR;
    }

    //使能设备
    rt_adc_enable(adc1_dev, ADC1_CHANNEL_0);
    rt_adc_enable(adc2_dev, ADC2_CHANNEL_4);
    rt_adc_enable(adc3_dev, ADC3_CHANNEL_10);
    rt_adc_enable(adc3_dev, ADC3_CHANNEL_11);

    return RT_EOK;

}

/**
 * ADC读取数据
 */
void adc_read()
{
    /**
     *    根据电路推导下面公式
     * 0-10v  vout = vin*2/6.8 + 0.2v
     * 外部电压  = （vout - 0.2）* 6.8 / 2
     *
     * 4-20ma vin = i * 150
     *   外部电流 = （vout）* 6.8 / 2 / 150
     *
     *
     */

    ADC.ADC1_0_value = rt_adc_read(adc1_dev, ADC1_CHANNEL_0); //读取采样值
    ADC.ADC1_0_vol = ADC.ADC1_0_value * REFER_VOLTAGE / CONVERT_BITS; //转换为对应电压值
    // h_vol = (((float)vol - 0.2) * 6.8 ) / 2 ;
    ADC.ADC1_0_h_vol = (rt_uint16_t) (((float) ADC.ADC1_0_vol / 100 - 0.2) * 340);

    if (ADC.ADC1_mode == 0)  //0:0-10V  1:4-20ma
    {
        // 0-10V ->  0 - 100 cN
        if (ADC.ADC1_0_h_vol < 30) {
            ADC.ADC1_0_h_vol = 0;
        } else if (ADC.ADC1_0_h_vol > 1000) {
            ADC.ADC1_0_h_vol = 1000;
        } else if (ADC.ADC1_0_h_vol >= 600 && ADC.ADC1_0_h_vol < 1000) {
            ADC.ADC1_0_h_vol = ADC.ADC1_0_h_vol - 145;
        } else if (ADC.ADC1_0_h_vol >= 300 && ADC.ADC1_0_h_vol < 600) {
            ADC.ADC1_0_h_vol = ADC.ADC1_0_h_vol - 130;
        } else if (ADC.ADC1_0_h_vol >= 200 && ADC.ADC1_0_h_vol < 300) {
            ADC.ADC1_0_h_vol = ADC.ADC1_0_h_vol - 120;
        } else if (ADC.ADC1_0_h_vol >= 100 && ADC.ADC1_0_h_vol < 200) {
            ADC.ADC1_0_h_vol = ADC.ADC1_0_h_vol - 100;
        }


//        ADC.ADC1_0_ZL_H = (ADC.ADC1_0_h_vol >> 8) & 0xFF;
//        ADC.ADC1_0_ZL_L = ADC.ADC1_0_h_vol & 0xFF;
        ADC.ADC1_0_ZL_H = ADC.ADC1_0_h_vol / 10;
        ADC.ADC1_0_ZL_L = ADC.ADC1_0_h_vol % 10;

        if (ADC.log)
        {
            rt_kprintf("ADC1_ZL is :%d.%d \n", ADC.ADC1_0_ZL_H,ADC.ADC1_0_ZL_L);
        }
    }
    else if (ADC.ADC1_mode == 1)
    {
        ADC.ADC1_0_Current = (rt_uint16_t) (((float) ADC.ADC1_0_vol / 100 ) / 150 * 340);
    }

    if (ADC.log)
    {
        rt_kprintf("ADC1_CHANNEL_0 value is :%d voltage is :%d.%02d \n", ADC.ADC1_0_value, ADC.ADC1_0_vol / 100, ADC.ADC1_0_vol % 100);
        rt_kprintf("ADC1_CHANNEL_0 0-10V voltage is :%d.%02d , Current is :%d.%02d\n", ADC.ADC1_0_h_vol / 100, ADC.ADC1_0_h_vol % 100,
                ADC.ADC1_0_Current / 100, ADC.ADC1_0_Current % 100);
    }

    ADC.ADC2_4_value = rt_adc_read(adc2_dev, ADC2_CHANNEL_4);
    ADC.ADC2_4_vol = ADC.ADC2_4_value * REFER_VOLTAGE / CONVERT_BITS;
    ADC.ADC2_4_h_vol = (rt_uint16_t) (((float) ADC.ADC2_4_vol / 100 - 0.2) * 340);
    if (ADC.ADC2_mode == 0)
    {
        // 0-10V ->  0 - 100 cN
        if (ADC.ADC2_4_h_vol < 30) {
            ADC.ADC2_4_h_vol = 0;
        } else if (ADC.ADC2_4_h_vol > 1000) {
            ADC.ADC2_4_h_vol = 1000;
        }

//        ADC.ADC1_0_ZL_H = (ADC.ADC1_0_h_vol >> 8) & 0xFF;
//        ADC.ADC1_0_ZL_L = ADC.ADC1_0_h_vol & 0xFF;
        ADC.ADC2_4_ZL_H = ADC.ADC2_4_h_vol / 10;
        ADC.ADC2_4_ZL_L = ADC.ADC2_4_h_vol % 10;

        if (ADC.log)
        {
            rt_kprintf("ADC2_ZL is :%d.%d \n", ADC.ADC2_4_ZL_H,ADC.ADC2_4_ZL_L);
        }
    }
    else if (ADC.ADC2_mode == 1)
    {
        ADC.ADC2_4_Current = (rt_uint16_t) (((float) ADC.ADC2_4_vol / 100 ) / 150 * 340);
    }

    if (ADC.log)
    {
        rt_kprintf("ADC2_CHANNEL_4 value is :%d voltage is :%d.%02d \n", ADC.ADC2_4_value, ADC.ADC2_4_vol / 100, ADC.ADC2_4_vol % 100);
        rt_kprintf("ADC2_CHANNEL_4 0-10V voltage is :%d.%02d , Current is :%d.%02d\n", ADC.ADC2_4_h_vol / 100, ADC.ADC2_4_h_vol % 100,
                ADC.ADC2_4_Current / 100, ADC.ADC2_4_Current % 100);
    }

    ADC.ADC3_10_value = rt_adc_read(adc3_dev, ADC3_CHANNEL_10);
    ADC.ADC3_10_vol = ADC.ADC3_10_value * REFER_VOLTAGE / CONVERT_BITS;
    if (ADC.log)
    {
        rt_kprintf("ADC3_CHANNEL_10 value is :%d  voltage is :%d.%02d \n", ADC.ADC3_10_value, ADC.ADC3_10_vol / 100, ADC.ADC3_10_vol % 100);

    }
    ADC.ADC3_11_value = rt_adc_read(adc3_dev, ADC3_CHANNEL_11);
    ADC.ADC3_11_vol = ADC.ADC3_11_value * REFER_VOLTAGE / CONVERT_BITS;
    if (ADC.log)
    {
        rt_kprintf("ADC3_CHANNEL_11 value is :%d voltage is :%d.%02d \n", ADC.ADC3_11_value, ADC.ADC3_11_vol / 100, ADC.ADC3_11_vol % 100);
    }

}


rt_uint16_t get_ADC1_0_Value()
{
    return ADC.ADC1_0_value;
}

rt_uint16_t get_ADC2_4_Value()
{
    return ADC.ADC2_4_value;
}
/**
 * 获取ADC1张力值
 * @return
 */
rt_uint8_t get_ADC1_0_H_ZL()
{
    return ADC.ADC1_0_ZL_H;
}
rt_uint8_t get_ADC1_0_L_ZL()
{
    return ADC.ADC1_0_ZL_L;
}

/**
 * 获取ADC2张力值
 * @return
 */
rt_uint8_t get_ADC2_4_H_ZL()
{
    return ADC.ADC2_4_ZL_H;
}
rt_uint8_t get_ADC2_4_L_ZL()
{
    return ADC.ADC2_4_ZL_L;
}


/**
 * 获取3V3的值ADC
 * @return
 */
rt_uint16_t get_ADC3_10_vol()
{
    return ADC.ADC3_10_vol;
}

rt_uint16_t get_ADC3_11_vol()
{
    return ADC.ADC3_11_vol;
}
/**
 * 获取0-10vADC值
 * @return
 */
rt_uint16_t get_ADC1_0_h_vol()
{
    return ADC.ADC1_0_h_vol;
}

/**
 * 获取4-10ma电流值
 * @return
 */
rt_uint16_t get_ADC1_0_Current()
{
    return ADC.ADC1_0_Current;
}

/**
 * 获取0-10vADC值
 * @return
 */
rt_uint16_t get_ADC2_4_h_vol()
{
    return ADC.ADC2_4_h_vol;
}

/**
 * 获取4-10ma电流值
 * @return
 */
rt_uint16_t get_ADC2_4_Current()
{
    return ADC.ADC2_4_Current;
}

void set_ADC_log(rt_uint8_t log)
{
    ADC.log = log;
}

/**
 * 设置ADC1的模式0:0-10V  1:4-20ma
 * @param mode
 */
void set_ADC1_mode(rt_uint8_t mode)
{
    ADC.ADC1_mode = mode;
}

/**
 * 设置ADC2的模式1:0-10V  2:4-20ma
 * @param mode
 */
void set_ADC2_mode(rt_uint8_t mode)
{
    ADC.ADC2_mode = mode;
}

/**
 * 获取ADC1的模式1:0-10V  2:4-20ma
 * @param mode
 */
rt_uint8_t get_ADC1_mode()
{
    return ADC.ADC1_mode;
}

/**
 * 获取ADC2的模式1:0-10V  2:4-20ma
 * @param mode
 */
rt_uint8_t get_ADC2_mode()
{
    return ADC.ADC2_mode;
}
