/*
 * power_out.h
 *
 *  Created on: 2025年10月16日
 *      Author: LHYe200
 */

#ifndef CODE_POWER_OUT_H_
#define CODE_POWER_OUT_H_

#include "zf_common_typedef.h"
#include "filter.h"


#define POWER_OUT_NUM    8
#define POWER_OUT_READ_TIME_MS   1

#define POWER_OUT_FUNCTION_DISABLE   -1

#define POWER_OUT_VOT_ADC_CHANNEL   {ADC0_CH0_A0, ADC0_CH1_A1, ADC0_CH2_A2, ADC0_CH3_A3, ADC0_CH4_A4, ADC0_CH5_A5, ADC0_CH6_A6, ADC0_CH7_A7}
#define POWER_OUT_AMP_ADC_CHANNEL   {ADC2_CH3_A35, ADC2_CH4_A36, ADC2_CH5_A37, ADC2_CH6_A38, ADC2_CH7_A39, ADC2_CH10_A44, ADC2_CH11_A45, ADC2_CH12_A46}
#define POWER_OUT_VOT_PWM_CHANNEL   {POWER_OUT_FUNCTION_DISABLE, POWER_OUT_FUNCTION_DISABLE, POWER_OUT_FUNCTION_DISABLE, POWER_OUT_FUNCTION_DISABLE, ATOM1_CH0_P02_0, ATOM1_CH1_P02_1, ATOM1_CH2_P02_2, ATOM1_CH3_P02_3}
#define POWER_OUT_ENABLE_PIN        {P33_13, P33_12, P33_11, P33_10, P33_7, P33_6, P33_5, P33_4}

#define POWER_OUT_VOT_ADC_K         {{0.0015, -0.0437}, {0.0015, -0.045}, {0.0015, -0.0451}, {0.0015, -0.0451}, {0.0049, 0.0297}, {0.0049, -0.0292}, {0.0049, -0.0179}, {0.0049, 0.0397}}
#define POWER_OUT_AMP_ADC_K         {{0.0029, -0.1382}, {0.002868, -0.1325}, {0.0029, -0.1335}, {0.0029, -0.1294}, {0.0029, -0.1083}, {0.0029, -0.1174}, {0.0028, -0.0563}, {0.002831, -0.0786}}

#define VOT_PWM_FREQUENCY_HZ   50000

#define VOT_ADJUST_MAX         20.0f
#define VOT_ADJUST_MIN         3.0f
#define AMP_ADJUST_MAX         15.0f

#define VOT_ADJUST_DEF         5.0f
#define AMP_ADJUST_DEF         1.0f
#define POW_ADJUST_DEF         300.0f

#define VOT_PROTECT_K          {0.9f,1.1f}  // 电压保护系数
#define AMP_PROTECT_K          1.1f        // 电流保护系数

typedef struct
{
    float voltage_V;
    float current_A;
    float power_W;
    float energy_J;
    float quantity_C;
    float past_voltage_V[10];
    float past_current_A[10];
} Power_Out_Status_t;

typedef struct
{
    int16 voltage_read_channel;
    int16 current_read_channel;
    float votK[2]; // 电压校准系数
    float ampK[2]; // 电流校准系数
    low_pass_filter_t vot_filter;
    low_pass_filter_t amp_filter;
    low_pass_filter_t vot_adc_filter;
    float v_adc;
    int16 enable_pin;
    uint8 can_adjust; // 是否允许调节输出
    int16 vot_pwm_channel;
} Power_Out_Config_t;

typedef struct
{
    Power_Out_Config_t config;
    Power_Out_Status_t status;
    float set_voltage_V;
    float set_current_A;
    float max_power_W;
    uint8 enabled;
} Power_Out_t;

extern Power_Out_t power_outs[POWER_OUT_NUM];


void Power_Out_Single_Init(Power_Out_t *power_out, int16 vot_channel, int16 amp_channel, float votK[2], float ampK[2], int16 enable_pin, uint8 can_adjust, int16 vot_pwm_channel);
void Power_Out_Init(void);
void Power_Out_Enable(Power_Out_t *power_out, uint8 enable);
void Power_Out_Set_PWM(Power_Out_t *power_out, uint32 duty_cycle);
void Power_Out_Single_Read(Power_Out_t *power_out);
void Power_Out_Read(void);

#endif /* CODE_POWER_OUT_H_ */
