/*****************************************************************************
 * file    Inc/ADC_collect.h 
 * author  YuLiang
 * version 1.0.0
 * date    20-Jun-2022
 * brief   This file provides all the headers of ADC collect function.
 ******************************************************************************
 * Attention
 *
 * <h2><center>&copy; COPYRIGHT(c) 2022 LandPower</center></h2>
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *   1. Redistributions of source code must retain the above copyright notice,
 *      this list of conditions and the following disclaimer.
 *   2. Redistributions in binary form must reproduce the above copyright notice,
 *      this list of conditions and the following disclaimer in the documentation
 *      and/or other materials provided with the distribution.
 *   3. Neither the name of WTOE nor the names of its contributors may be used to 
 *      endorse or promote products derived from this software without specific
 *      prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 ******************************************************************************/

#ifndef __ADC_COLLECT_H__
#define __ADC_COLLECT_H__

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "FreeRTOS.h"
#include "task.h"
#include "cmsis_os.h"

/* Define --------------------------------------------------------------------*/
#define ADC_COLLECT_CNT 2000                // ADC 每个通道采样次数.
#define ADC_CNT_SUM 2000                    // ADC 计数总数.
#define ADC_PER_CNT 5                       // ADC 预采样次数, 预采样不参与计算.
#define ADC_SPI_TIMEOUT 10                  // ADC SPI 通讯超时时间.

#define ADCi_COLLECT_CNT 10                 // ADCi 每个通道采样次数.
#define ADCi_COLLECT_SUM 40                 // ADCi 总共采样次数, ADCi_COLLECT_CNT * ADCi_CH_CNT.

/* Exported types ------------------------------------------------------------*/
/* ADCi 通道定义. */
typedef enum
{
    ADCi_CH_TEMP = 0,
    ADCi_CH_VIN,
    ADCi_CH_VSC,
    ADCi_CH_VBAT,
    ADCi_CH_CNT
} ADCi_CH_E;

/* ADC 全局结构体. */
typedef struct
{
    osThreadId_t ADCHandle;                 // ADC 任务句柄.
    TIM_HandleTypeDef *ADC_tim;             // ADC 使用的定时器句柄.
    SPI_HandleTypeDef *ADC_spi;             // ADC 使用的 SPI 句柄.
    uint16_t ADC_cnt;                       // ADC 采样计数.
    uint8_t ADC_state;                      // ADC 采样状态.
    uint8_t ADC_err_cnt;                    // ADC 采样失败次数.
    uint8_t collect_finish;                 // 采集数据是否完成 BOOL.
    uint8_t continue_mode;                  // 是否是持续采样模式.
    int16_t ADC_index;                      // ADC 采样当前采样 index.
    int16_t ADC_buf[CFG_ADC_CH_CNT];        // ADC 采样临时 buffer.
    int16_t reserve1[CFG_ADC_CH_CNT];
    int16_t ADC_value[CFG_ADC_CH_CNT][ADC_COLLECT_CNT];     // ADC 采样原始值.
    int16_t ADC_value_elec[CFG_ADC_CH_CNT][ADC_COLLECT_CNT];// ADC 采样计算电流值.
    uint32_t ADC_elec[CFG_ADC_CH_CNT];      // ADC 采样计算后的电流有效值 mA.
    ADC_HandleTypeDef *ADCi_adc;            // ADCi 使用硬件句柄.
    uint16_t ADCi_value[ADCi_COLLECT_SUM];  // ADCi 采样原始值.
    uint32_t ADCi_param1;                   // ADCi 温度传感器校准系数 1.
    uint16_t ADCi_param2;                   // ADCi 温度传感器校准系数 2.
    int16_t ADCi_temp;                      // ADCi 温度传感器计算值, 单位: 0.1℃.
    uint16_t ADCi_vbat;                     // ADCi 电池电压通道计算值, 单位: mv.
    uint16_t ADCi_vin;                      // ADCi 输入电压通道计算值, 单位: mv.
    uint16_t ADCi_vsc;                      // ADCi 超级电容电压通道计算值, 单位: mv.
    uint16_t ADCi_cnt;                      // ADCi 采样计数.
    uint8_t ADCi_state;                     // ADCi 采样状态.
    uint8_t ADCi_err_cnt;                   // ADCi 采样失败次数.
    uint8_t energy_mode;                    // 设备工作模式 0 - 正常 1 - 休眠, 这个参数放这里, 主要是考虑到模式是由电流电压确定的.
    uint8_t up_finish;                      // 上传是否完成 BOOL.
    uint32_t collect_time;                  // 上次采集结束时间.
    uint8_t is_energy_first;                // 用于判断是否第一次判断节能模式, 因为第一次判断节能模式后需要立即反应, 后续判断模式在 wireless 里反应.
    uint8_t is_ADC_thr;                     // ADC 采用超过工频阈值.
    uint8_t is_force_col;                   // ADC 主动采集.
} ADC_ctrl_t;

/* Exported macro ------------------------------------------------------------*/
/* Extern global variables ---------------------------------------------------*/
extern ADC_ctrl_t ADC_ctrl;

/* Extern functions ----------------------------------------------------------*/
extern void ADC_init_os(void);
extern int32_t ADCi_vbat_auto(uint32_t base);
extern void ADCi_show(void);
extern HAL_StatusTypeDef ADC_base_auto(uint8_t ch_bitmap, uint8_t index);
extern void ADC_restart_force(void);
extern void ADC_show(void);
extern void ADC_value_show(uint32_t ch);
extern void ADC_adj_show_ch(uint8_t ch);
extern void ADC_adj_show(void);

#endif
/******************* (C) COPYRIGHT LandPower ***** END OF FILE ****************/

