
#include "bl_chargControl.h"
#include "parameter.h"
#include "comm_types.h"
#include "FM_GPIO.h"
#include "task.h"
#include "SOE.h"


static BOOL stopChargConditions(void);
static BOOL floatChargConditions(void);
static BOOL mpptChargConditions(void);
static BOOL constantVChargConditions(void);
static void mpptCharge(void);
static void constantVoltageCharge(void);
static void floatCharge(void);
static void mppt_constantVoltage(float InVoltage);
static void mppt_constantVoltageNoBatteryO(float OutVoltage);
static void mppt_constantVoltageO(float OutVoltage);

static void judgeYNBattery(void);
static void chargControlMode(void);
static void BatteryChargControl(void);
static void noBatteryChargControl(void);

static void setPIControlStep(float *PI_step);


static BOOL chargControlFlag = FALSE;
// static BOOL getChargControlFlag(void);
void setChargControlFlag(BOOL state);

void setPIControlStep(float *PI_step)
{
    if (*PI_step > PI_CONTROL_MAX) {
        *PI_step = PI_CONTROL_MAX;
    }

    else if (*PI_step < PI_CONTROL_MIN) {
        *PI_step = PI_CONTROL_MIN;
    }
}

/**
  * @brief  恒定输入电压
  * @param  InVoltage 需要控制到的输入电压
  * @retval
  *
  */
void mppt_constantVoltage(float InVoltage)
{
    static float kp = 0.0005f;
    // static float ki = 0.00001;
    // static float ki = 0.1f;
    static float ki = 10.0f;
    // static float solarInCircuitVoltage;
    static float error;
    static float stepPwm;

    // solarInCircuitVoltage = getSolarInCircuitVoltage();
//    error = InVoltage - getSolarInCircuitVoltage();    
    error = getSolarInCircuitVoltage() - InVoltage;

    // static float Ierror;
    // Ierror += error;

    // stepPwm = kp * error + ki * getSolarInCircuitVoltage();
    stepPwm = kp * error + ki * error * 0.00001f;

    setPIControlStep(&stepPwm);

    setDutyRatio((getDutyRatio() + stepPwm));
    // if (getMosTemperState() == mosTemperEnd) {
    //     setDutyRatio((getDutyRatio() + stepPwm - 0.1));
    // } else {
    //     setDutyRatio((getDutyRatio() + stepPwm));
    // }
}

/**
  * @brief  恒定输出电压(无电池)
  * @param
  * @retval
  *
  */
void mppt_constantVoltageNoBatteryO(float OutVoltage)
{
    static float kp = 0.002f;
    // static float ki = 0.00001;
    // static float ki = 0.1f;
    static float ki = 10.0f;
    static float outVolt;
    static float error;
    static float stepPwm;

    outVolt = getOutputVoltage();
    error = OutVoltage - outVolt;

    // static float Ierror;
    // Ierror += error;
    // stepPwm = kp * error + ki * outVolt;
    stepPwm = kp * error + ki * error * 0.00001f;
    setPIControlStep(&stepPwm);
    
    setDutyRatio((getDutyRatio() + stepPwm));
}

/**
  * @brief  恒定输出电压（输出检测端）
  * @param
  * @retval
  *
  */
void mppt_constantVoltageO(float OutVoltage)
{
    // static float lastVolt = 0;
    // static float lastStepPwm = 0;
    static float lastDutyRatio = 0;
    static float kp = 0.002f;
    // static float ki = 0.00001;
    // static float ki = 0.1f;
    static float ki = 10.0f;
    static float outVolt;
    static float error;
    static float StepPwm;

    outVolt = getOutputVoltage();
    error = OutVoltage - outVolt;
    // static float Ierror;
    // Ierror += error;
    // StepPwm = kp * error + ki * outVolt;
    StepPwm = kp * error + ki * error * 0.00001f;
    setPIControlStep(&StepPwm);

    /* 当有电池时，输出电压的曲线是先上升后下降 */
    if (lastDutyRatio >= getDutyRatio()) {
//        if (lastVolt >= outVolt) {
        setDutyRatio((getDutyRatio() + StepPwm));
        // if (getMosTemperState() == mosTemperEnd) {
        //     setDutyRatio((getDutyRatio() + StepPwm - 0.1));
        // } else {
        //     setDutyRatio((getDutyRatio() + StepPwm));
        // }
//        } else {
//            g_controlParameter.dutyRatio -= StepPwm;
//        }
    } else {    
//        if (lastVolt >= outVolt) {
//            g_controlParameter.dutyRatio -= StepPwm;
//        } else {
//            g_controlParameter.dutyRatio += StepPwm;
//        }
        setDutyRatio((getDutyRatio() - StepPwm));        
        // if (getMosTemperState() == mosTemperEnd) {        
        //     setDutyRatio((getDutyRatio() + StepPwm - 0.1));
        // } else {
        //     setDutyRatio((getDutyRatio() + StepPwm));
        // }
    }

    // lastVolt = outVolt;
    // lastStepPwm = StepPwm;
    lastDutyRatio = getDutyRatio();
}

/**
  * @brief  通过扰动干扰法追踪最大功率点
  * @param
  * @retval
  *
  */
void mppt_readJust(void)
{
    /* 调节占空比 */
//    static float_t step1 = 0.01;
//    static float_t step2 = 0.003;
//    static float_t tempV = 0.2;
//    static float_t i = 0.005;
//    static uint16_t flag = 0;
//    static float_t lastSolarInCircuitVoltage = 0;
//    static float_t lastPower = 0;
//    flag++;
//    if (flag < 500) {
//        return;
//    }
//    flag = 0;
//
//    float_t SolarInCircuitVoltage = get_PV1_VOLT_IN();
//    float_t power = g_otherParameter.Output_Voltage * g_otherParameter.Charg_Current;
//
//    float_t voltageDifference = SolarInCircuitVoltage - lastSolarInCircuitVoltage;
//
//    /* 输出电压随占空比增加电压减小 */
//    if (power <= lastPower) {
//        if (lastSolarInCircuitVoltage <= SolarInCircuitVoltage) {
//            if (voltageDifference > tempV) {
//                g_controlParameter.dutyRatio += step2 + voltageDifference / i;
//            } else {
//                g_controlParameter.dutyRatio += step1 + voltageDifference / i;
//            }
//        } else {
//            if (voltageDifference < -tempV) {
//                g_controlParameter.dutyRatio -= step2 + voltageDifference / i;
//            } else {
//                g_controlParameter.dutyRatio -= step1 + voltageDifference / i;
//            }
//        }
//    } else {
//        if (lastSolarInCircuitVoltage <= SolarInCircuitVoltage) {
//            if (voltageDifference > tempV) {
//                g_controlParameter.dutyRatio -= step2 - voltageDifference / i;
//            } else {
//                g_controlParameter.dutyRatio -= step1 - voltageDifference / i;
//            }
//        } else {
//            if (voltageDifference < -tempV) {
//                g_controlParameter.dutyRatio += step2 - voltageDifference / i;
//            } else {
//                g_controlParameter.dutyRatio += step1 - voltageDifference / i;
//            }
//        }
//    }
//
//    lastPower = power;
//    lastSolarInCircuitVoltage = SolarInCircuitVoltage;
//
//    Set_duty_ratio(&g_controlParameter.dutyRatio);


    /* 调节电压，变步长调节 */
//    static float_t Power3 = 0;                     //上上次功率
//    static float_t Power2 = 0;                     //上次功率
//    static float_t Power1 = 0;                     //当前功率
//    static float_t power23 = 0;                    //上次和上上次功率的绝对值
//    static float_t power12 = 0;                    //当前功率和上次功率的绝对值
////    static float_t SolarInCircuitVoltage3 = 0;     //上上次太阳能板电压
//    static float_t SolarInCircuitVoltage2 = 0;     //上次太阳能板电压
//    static float_t SolarInCircuitVoltage1 = 0;     //当前太阳能板电压
//    static float_t SolarInCircuitVoltage12 = 0;    //当前太阳能板电压和上次太阳能板电压的绝对值
//    SolarInCircuitVoltage1 = get_PV1_VOLT_IN();
//    Power1 = g_otherParameter.Output_Voltage * g_otherParameter.Charg_Current;
//    static float_t power12Abs = 0;
//    static float_t power23Abs = 0;
//    static float_t SolarInCircuitVoltage12Abs = 0;
//    static float_t dk = 0;                              //变步长因子
//    static float_t stepV = 0;
//    static float_t SolarInCircuitV = 18;           //控制太阳能板的输出电压稳定在该值
//
//    static float_t kp = 0.005;
//    static float_t ki = 0.00001;
//
//    /* 延时一段时间才判断 */
//    static uint16_t flag = 0;
//    flag++;
//    if (flag < 1000) {
////        float_t pv1Volt = g_otherParameter.Solar_In_Circuit_Voltage;
//        float_t pv1Volt = SolarInCircuitVoltage1;
//        float_t error = pv1Volt - SolarInCircuitV;
//        float_t stepPwm = kp * error + ki * pv1Volt;
//
//        g_controlParameter.dutyRatio += stepPwm;
//
//        /* 过温保护 */
//        if (g_otherParameter.overTemperature == 0) {
//
//        } else if (g_otherParameter.overTemperature == 1) {
//            g_controlParameter.dutyRatio -= 0.1;
//        } else if (g_otherParameter.overTemperature == 2) {
//            g_controlParameter.dutyRatio -= 0.2;
//        } else if (g_otherParameter.overTemperature == 3) {
//            g_controlParameter.dutyRatio -= 0.3;
//        }
//
//        Set_duty_ratio(&g_controlParameter.dutyRatio);
//
//        return;
//    }
//    flag = 0;
//
//    power23 = Power2 - Power3;
//    if (power23 < 0) {
//        power23Abs = -power23;
//    } else {
//        power23Abs = power23;
//    }
//
//    power12 = Power1 - Power2;
//    if (power12 < 0) {
//        power12Abs = -power12;
//    } else {
//        power12Abs = power12;
//    }
//
////    SolarInCircuitVoltage23 = SolarInCircuitVoltage2 - SolarInCircuitVoltage3;
//
//    SolarInCircuitVoltage12 = SolarInCircuitVoltage1 - SolarInCircuitVoltage2;
//
//    dk = power12Abs / power23Abs;
//    stepV = dk * SolarInCircuitVoltage12Abs;
//
////    printf(" dk : %d/10000 \n",  (int)(dk * 10000));
//
//    if (power12 > 0) {
//        if (SolarInCircuitVoltage12 > 0) {
//            SolarInCircuitV = SolarInCircuitVoltage1 + stepV;
//        } else {
//            SolarInCircuitV = SolarInCircuitVoltage1 - stepV;
//        }
//    } else {
//        if (SolarInCircuitVoltage12 > 0) {
//            SolarInCircuitV = SolarInCircuitVoltage1 - stepV;
//        } else {
//            SolarInCircuitV = SolarInCircuitVoltage1 + stepV;
//        }
//    }
//
//    printf(" SolarInCircuitV : %d/100 \n",  (int)(SolarInCircuitV * 100));
//
//    if (SolarInCircuitV > 21) {
//        SolarInCircuitV = 21;
//    }
//    else if (SolarInCircuitV < 15) {
//        SolarInCircuitV = 15;
//    }
//
//    printf(" SolarInCircuitV : %d/100 \n",  (int)(SolarInCircuitV * 100));
//
//    Power3 = Power2;
//    Power2 = Power1;
////    SolarInCircuitVoltage3 = SolarInCircuitVoltage2;
//    SolarInCircuitVoltage2 = SolarInCircuitVoltage1;
//
////      float_t pv1Volt = g_otherParameter.Solar_In_Circuit_Voltage;
//    float_t pv1Volt = SolarInCircuitVoltage1;
//    float_t error = pv1Volt - SolarInCircuitV;
//    float_t stepPwm = kp * error + ki * pv1Volt;
//
//    g_controlParameter.dutyRatio += stepPwm;
//
//    /* 过温保护 */
//    if (g_otherParameter.overTemperature == 0) {
//
//    } else if (g_otherParameter.overTemperature == 1) {
//        g_controlParameter.dutyRatio -= 0.1;
//    } else if (g_otherParameter.overTemperature == 2) {
//        g_controlParameter.dutyRatio -= 0.2;
//    } else if (g_otherParameter.overTemperature == 3) {
//        g_controlParameter.dutyRatio -= 0.3;
//    }
//
//    Set_duty_ratio(&g_controlParameter.dutyRatio);
//
//    return;

    /* 调节电压，两个电压步调节 */
    static float stepV1 = 0.2;
    static float stepV2 = 0.05;
    static float Power = 0;
    // static float totalPower = 0;
    // static float powerData[50] = {0};
    // static uint8_t powerIndex = 0;

    static float totalChargeCurr = 0;
    static float chargeCurrData[50] = {0};
    static uint8_t chargeCurrIndex = 0;

    /* 获取50次值的和 */
    totalChargeCurr -= chargeCurrData[chargeCurrIndex];
    chargeCurrData[chargeCurrIndex] = getChargCurrent();
    totalChargeCurr += chargeCurrData[chargeCurrIndex];
    chargeCurrIndex++;
    if (chargeCurrIndex >= 50) {
        chargeCurrIndex = 0;
    }

    // totalPower -= powerData[powerIndex];
    // powerData[powerIndex] = getOutputVoltage() * getChargCurrent();
    // totalPower += powerData[powerIndex];
    // powerIndex++;
    // if (powerIndex >= 50) {
    //     powerIndex = 0;
    // }

    static float lPower = 0;
    static float lLPower = 0;
//    static float lLLPower = 0;

    static float SolarInCircuitV = 17;           //控制太阳能板的输出电压稳定在该值,初始为17V
    // static float kp = 0.005;
    // static float ki = 0.00001;

    static uint8_t flag1 = 0;       //表明上次运算是加还是减

    /* 延时一段时间才判断 */
    static uint16_t flag = 0;
    flag++;
    if (flag < 250) {
        // float pv1Volt = getSolarInCircuitVoltage();
        // float error = pv1Volt - SolarInCircuitV;
        // float stepPwm = kp * error + ki * pv1Volt;

        // setDutyRatio((getDutyRatio() + stepPwm));
        // set_pwmDutyRatio(getDutyRatio());

        mppt_constantVoltage(SolarInCircuitV);

        return;
    }

    if (getMosTemperState() == mosTemperReduce) {
        SolarInCircuitV = g_cfgParameter.MPPTReduceConstantVoltage;
        mppt_constantVoltage(SolarInCircuitV);
        return;
    }

    flag = 0;
    // Power = totalPower / 30.0f;
    // Power = totalPower;
    Power = totalChargeCurr * getOutputVoltage();

    static float powerT = 0;
    powerT = Power - lPower;
    if (powerT < 0) {
        powerT = -powerT;
    }

    /* 滞环值 */
    float hysteresisValue1;
    float hysteresisValue2;
    /* 一段时间内电流都很小则固定电压输出 */
    static uint8_t currMinFlag = 0;
    static uint8_t currMinFlag1 = 0;

    // if (getChargCurrent() < 0.8f) {
    if (totalChargeCurr < 200) {
        // hysteresisValue1 = getChargCurrent() * 1.7f;
        // hysteresisValue2 = getChargCurrent() * 12;
        currMinFlag++;
        if (currMinFlag == 5) {
            currMinFlag = 0;
            SolarInCircuitV = g_cfgParameter.MPPTConstantVoltage;
            currMinFlag1 = 1;
        }

        return;
    }

    // else if (getChargCurrent() < 3 && currMinFlag1) {
    else if (totalChargeCurr < 250 && currMinFlag1) {
        // currMinFlag1 = 0;
        currMinFlag = 0;
        return;
    }

    // else if (getChargCurrent() < 5) {
    else if (totalChargeCurr < 300) {
        currMinFlag1 = 0;
        currMinFlag = 0;
        // hysteresisValue1 = getChargCurrent() * 1.1f;
        // hysteresisValue2 = getChargCurrent() * 10;
        hysteresisValue1 = totalChargeCurr / 25.0f;
        hysteresisValue2 = totalChargeCurr / 2.5f;
    }
    
    // else if (getChargCurrent() < 13) {
    else if (totalChargeCurr < 650) {
        currMinFlag1 = 0;
        currMinFlag = 0;
        // hysteresisValue1 = getChargCurrent() * 1.1f;
        // hysteresisValue2 = getChargCurrent() * 10;
        hysteresisValue1 = totalChargeCurr / 35.0f;
        hysteresisValue2 = totalChargeCurr / 3.5f;
    }

    // else if (getChargCurrent() < 20) {
    else if (totalChargeCurr < 1000) {
        currMinFlag1 = 0;
        currMinFlag = 0;
        // hysteresisValue1 = getChargCurrent() * 0.7f;
        // hysteresisValue2 = getChargCurrent() * 7;
        hysteresisValue1 = totalChargeCurr / 60.0f;
        hysteresisValue2 = totalChargeCurr / 6.0f;
    } 

    // else if (getChargCurrent() < 25) {
    else if (totalChargeCurr < 1250) {
        currMinFlag1 = 0;
        currMinFlag = 0;
        // hysteresisValue1 = getChargCurrent() * 0.5f;
        // hysteresisValue2 = getChargCurrent() * 5;
        hysteresisValue1 = totalChargeCurr / 90.0f;
        hysteresisValue2 = totalChargeCurr / 9.0f;
    } 

    else {
        currMinFlag1 = 0;
        currMinFlag = 0;
        // hysteresisValue1 = getChargCurrent() * 0.3f;
        // hysteresisValue2 = getChargCurrent() * 3;
        hysteresisValue1 = totalChargeCurr / 140.0f;
        hysteresisValue2 = totalChargeCurr / 14.0f;
    }

    // else {
    //     currMinFlag1 = 0;
    //     currMinFlag = 0;
    //     hysteresisValue1 = 120;
    //     hysteresisValue2 = 100;
    // }
    

    static uint8_t numFlag = 0;
    //  if ((lPower + 0.8f < Power) && (lLPower + 0.8f < Power) && (lLLPower + 0.8f < Power)) {
    // if ((lPower + 0.1f < Power) && (lLPower + 0.1f < Power)) {
    
    if ((lPower + hysteresisValue1 < Power) && (lLPower + hysteresisValue1 < Power)) {
        numFlag = 0;
    // if ((lPower + 0.3f < Power)) {
        if (powerT > hysteresisValue2) {
            if (flag1) {
                SolarInCircuitV += stepV1;
                flag1 = 1;
            } else {
                SolarInCircuitV -= stepV1;
                flag1 = 0;
            }
        } else {
            if (flag1) {
                SolarInCircuitV += stepV2;
                flag1 = 1;
            } else {
                SolarInCircuitV -= stepV2;
                flag1 = 0;
            }
        }
    // } else if ((lPower - 0.8f > Power) && (lLPower - 0.8f > Power) && (lLLPower - 0.8f > Power)) {
    } else if ((lPower - hysteresisValue1 > Power) && (lLPower - hysteresisValue1 > Power)) {
    // } else if ((lPower - 0.3f > Power)) {
        numFlag = 0;
        if (powerT > hysteresisValue2) {
            numFlag = 0;
            if (flag1) {
                SolarInCircuitV -= stepV1;
                flag1 = 0;
            } else {
                SolarInCircuitV += stepV1;
                flag1 = 1;
            }
        } else {
            if (flag1) {
                SolarInCircuitV -= stepV2;
                flag1 = 0;
            } else {
                SolarInCircuitV += stepV2;
                flag1 = 1;
            }
        }
    } else {
        numFlag++;
    }

    /* 一段时间内都未调节 */
    if (numFlag == 10) {
        if (Power < 300) {
            SolarInCircuitV = g_cfgParameter.MPPTConstantVoltage;
        }
        else if (flag1) {
            SolarInCircuitV -= stepV2;
            flag1 = 0;
        } 
        else {
            SolarInCircuitV += stepV2;
            flag1 = 1;
        }
    }


    if (SolarInCircuitV > 19.0f) {
        SolarInCircuitV = 19.0f;
    }

    else if (SolarInCircuitV < 16.0f) {
        SolarInCircuitV = 16.0f;
    }

    // lLLPower = lLPower;
    lLPower = lPower;
    lPower = Power;

    mppt_constantVoltage(SolarInCircuitV);
}

/**
  * @brief  停止充电，并打开启动任务
  * @param
  * @retval
  *
  */
void endChargWork(void)
{
    setChargControlFlag(FALSE);
    setDutyRatioToZero();
    setMPPT_Mode(noWork);
    beginStartControlTask();
    EN_PWMOUT_Diseable();
}

/**
  * @brief  停止充电，不开启启动任务
  * @param
  * @retval
  *
  */
void stopChargWork(void)
{
    setChargControlFlag(FALSE);
    setDutyRatioToZero();
    setMPPT_Mode(noWork);
    EN_PWMOUT_Diseable();
}

/**
  * @brief  启动充电，开启启动任务
  * @param
  * @retval
  *
  */
void beginChargWork(void)
{
    beginStartControlTask();
    EN_PWMOUT_Eable();
}

/**
  * @brief  启动充电，直接软启动
  * @param
  * @retval
  *
  */
void startChargWork(void)
{
    beginSoftStartTask();
    EN_PWMOUT_Eable();
}


/**
  * @brief  判断达到停止充电的条件
  * @param
  * @retval TRUE    达到停止充电
  *         FALSE   未达到
  *
  */
BOOL stopChargConditions(void)
{
    if (getSolarInCircuitVoltage() < g_cfgParameter.stopSolarOutputCircuitV
            && getChargCurrent() < 1) {
        // log_info("in stopChargConditions stopChargWork");
        return TRUE;
    }

    /* 异常情况关闭充电 */
    static uint16_t flag = 0;
    if ((getSolarInCircuitVoltage() < (g_cfgParameter.stopSolarOutputCircuitV + 1) 
        // || getSolarInCircuitVoltage() > (g_cfgParameter.startSolarOpenCircuitV))
        || getSolarInCircuitVoltage() > 18.5f)
        && getChargCurrent() < 0.1f
        && getMPPT_Mode() == MPPT) {
        // return TRUE;
        flag++;
    }
    // if ((getSolarInCircuitVoltage() < 17.8f 
    //     || getSolarInCircuitVoltage() > 20)
    //     && getChargCurrent() < 0.1f
    //     && getMPPT_Mode() == MPPT) {
    //     // return TRUE;
    //     flag++;
    // }
     else {
         flag = 0;
     }

    if (flag > 10000) {
        flag = 0;
        insertEventsOrderRecord(abnormalControl);
        return TRUE;
    }

    return FALSE;
}

/**
  * @brief  判断达到浮充充电的条件
  * @param
  * @retval TRUE    达到
  *         FALSE   未达到
  *
  */
BOOL floatChargConditions(void)
{
    if ((g_cfgParameter.constantVoltageChargeV < getBatteryVoltage() && g_cfgParameter.floatI > getChargCurrent())) {
        return TRUE;
    }    
    
    return FALSE;
}

/**
  * @brief  判断达到最大功率充电的条件
  * @param
  * @retval TRUE    达到
  *         FALSE   未达到
  *
  */
BOOL mpptChargConditions(void)
{
    if (((g_cfgParameter.constantVoltageChargeV - 0.2f) > getBatteryVoltage())
            && (getChargCurrent() > 0.05f)) {
        return TRUE;
    }

    return FALSE;
}

/**
  * @brief  判断达到恒压充电的条件
  * @param
  * @retval TRUE    达到
  *         FALSE   未达到
  *
  */
BOOL constantVChargConditions(void)
{
    if ((g_cfgParameter.constantVoltageV < getBatteryVoltage())
           && ((g_cfgParameter.floatI + 0.1f) <= getChargBatteryCurrent())) {
        return TRUE;
    }
    
    return FALSE;
}

/**
  * @brief  判断充电控制的模式
  * @param
  * @retval      
  *
  */
void chargControlMode(void)
{
    if (stopChargConditions()) {
        endChargWork();
    }

    else if (getBatteryState() == FALSE) {
        setMPPT_Mode(noBattery);
    }

    else if (floatChargConditions()) {
        setMPPT_Mode(floatCharg);
    }

    else if (constantVChargConditions()) {
        setMPPT_Mode(constantVoltage);
    }

    else if (mpptChargConditions()) {
        setMPPT_Mode(MPPT);
    }    
}

/**
  * @brief  判断有无电池
  * @param
  * @retval
  *
  */
void judgeYNBattery(void)
{
    if (getBatteryVoltage() > 16 || getBatteryVoltage() < 10) {
        setBatteryState(FALSE);
        return;
    }
    // if (getOutputVoltage() > 16 || getOutputVoltage() < 10) {
    //     setBatteryState(FALSE);
    //     return;
    // }
}

/**
  * @brief  无电池时控制
  * @param
  * @retval      
  *
  */
void noBatteryChargControl(void)
{
    mppt_constantVoltageNoBatteryO(g_cfgParameter.FloatChargeV);
}

/**
  * @brief  最大功率充电
  * @param
  * @retval      
  *
  */
void mpptCharge(void)
{
    mppt_readJust();
}

/**
  * @brief  恒压充电
  * @param
  * @retval      
  *
  */
void constantVoltageCharge(void)
{
    mppt_constantVoltageO(g_cfgParameter.constantVoltageChargeV);
}

/**
  * @brief  浮充充电
  * @param
  * @retval      
  *
  */
 void floatCharge(void)
 {
    mppt_constantVoltageO(g_cfgParameter.FloatChargeV);
 }

/**
  * @brief  有电池时控制
  * @param
  * @retval      
  *
  */
void BatteryChargControl(void)
{
    switch(getMPPT_Mode()) {

    case MPPT:
        mpptCharge();
        // mppt_constantVoltage(17.5f);
        // setDutyRatio(0.1f);
        break;

    case constantVoltage:
        constantVoltageCharge();
        break;

    case floatCharg:
        floatCharge();
        break;

    default:
        setMPPT_Mode(noWork);
        // stopChargWork();
        endChargWork();
        break;
    }
}

BOOL getChargControlFlag(void)
{
    return chargControlFlag;
}

/**
  * @brief  设置充电控制的标志位
  * @param  TRUE    启动充电控制
  *         FALSE   关闭充电控制
  * @retval
  *
  */
void setChargControlFlag(BOOL state)
{
    if (state == TRUE || state == FALSE) {
        chargControlFlag = state;
        // debug_printf("chargControlFlag : %d", state);
    }

    if (state == TRUE) {
        chargRunLed(runLedChargMode);
        // debug_printf("setChargControlFlag is true");
    } else if (state == FALSE) {
        chargRunLed(runLedOtherMode);
        // debug_printf("setChargControlFlag is false");
    }
}

/**
  * @brief  完成充电控制
  * @param
  * @retval
  *
  */
void bl_chargControl(void)
{
    setBatteryVoltage();
    
    if (getChargControlFlag() == FALSE) {
        return;
    }

    // getCVData();
    judgeYNBattery();

    chargControlMode();

    if (getMPPT_Mode() == noWork) {
        return;
    }

    if (getBatteryState()) {
        BatteryChargControl();
    } else {
        noBatteryChargControl();
    }    
    // noBatteryChargControl();
}

