/*
 * task.c
 *
 *  Created on: 2024年6月24日
 *      Author: psx
 */

#include "task.h"


#include "tim.h"
#include "pwm.h"
#include "adc.h"
#include "rs485.h"
#include "gpio.h"
#include "flash.h"


#include "sl_protocol.h"
#include "mppt_control.h"
#include "inflash.h"
#include <stdlib.h>
#include "collect_Conversion.h"

/**
  * @brief  运行指示灯
  * @param
  * @retval
  */
STR_TimeSliceOffset m_runled;
void Task_RunLED(void)
{
    static uint8_t flag = RESET;
    flag = !flag;
    GPIO_WriteBit(RUN_LED_GPIO, RUN_LED_PIN, flag);
    return;
}


/**
  * @brief  mppt控制
  * @param
  * @retval
  */
STR_TimeSliceOffset m_mpptControl;
void Task_mpptControl(void)
{
//    uart_dev_write(g_gw485_uart4_handle, "hello world \n", sizeof("hello world \n"));
    test();

    return;
}

/**
  * @brief 串口数据解析和处理
  * @param
  * @retval
  */
STR_TimeSliceOffset m_usart;
uint8_t RegistrationRequestFlag = 0;
void Task_usart(void)
{
//    printf(" task3******** \n");
    read_and_process_uart_data(g_gw485_uart4_handle);
    return;
}

/**
  * @brief 看门狗
  * @param
  * @retval
  */
STR_TimeSliceOffset m_wdi;
void Task_wdi(void)
{
    static uint32_t temp = 0;
    if (wdi_RESET == temp++) {
        temp = 0;
        NVIC_SystemReset();
    }
//    GPIO_WriteBit(WDI_INPUT_GPIO, WDI_INPUT_PIN, SET);
//    GPIO_WriteBit(WDI_INPUT_GPIO, WDI_INPUT_PIN, RESET);
    return;
}

/**
  * @brief 接收到广播指令后,发送数据给网关，未接收到指令再次发送，最多三次，间隔时间3s
  * @param
  * @retval
  */
uint8_t recvbroadcast_flag = 0;
device_handle g_recvBroadcastDevice;
uint8_t g_recvBroadcastRegisterNumber;
STR_TimeSliceOffset m_recvbroadcast;
void Task_recvbroadcast(void)
{
    m_recvbroadcast.count = recvbroadcast_reloadVal;
    static uint8_t run_number = 0;
    /* 超过三次，不再发送 */
    if (run_number++ == 3 || run_number > 3) {
        RegistrationRequestFlag = 0;
        run_number = 0;
        TimeSliceOffset_Unregister(&m_recvbroadcast);
        return;
    }
//    if (g_Mppt_Para.Registration_Status == REGISTER_SUCCESS) {
//        TimeSliceOffset_Unregister(&m_recvbroadcast);
//        return;
//    }
    SL_Mppt_RegistrationRequest_pack recvpack = {0};

    /* 起始标志 */
    recvpack.start_Flag[0] = g_slConfigInfo.start_Flag[0];
    recvpack.start_Flag[1] = g_slConfigInfo.start_Flag[1];

    /* ID */
    recvpack.address[0] = 0xFF;
    recvpack.address[1] = 0xFF;
    recvpack.address[2] = 0xFF;
    recvpack.address[3] = 0xFF;
    recvpack.address[4] = 0xFF;
    recvpack.address[5] = 0xFF;
    recvpack.address[6] = 0xFF;

    /* 功能码 */
    recvpack.function_Code = SL_Function_Code_Registration_request;

    /* 寄存器长度 */
    recvpack.register_Length_H =  g_recvBroadcastRegisterNumber >> 8;
    recvpack.register_Length_L =  g_recvBroadcastRegisterNumber;

    /* 注册状态 */
    recvpack.registration_Status_H = g_Mppt_Para.Registration_Status >> 8;
    recvpack.registration_Status_L = g_Mppt_Para.Registration_Status;

    /* 接入节点ID */
    recvpack.access_Node_ID[0] = g_slConfigInfo.address[0];
    recvpack.access_Node_ID[1] = g_slConfigInfo.address[1];
    recvpack.access_Node_ID[2] = g_slConfigInfo.address[2];
    recvpack.access_Node_ID[3] = g_slConfigInfo.address[3];
    recvpack.access_Node_ID[4] = g_slConfigInfo.address[4];
    recvpack.access_Node_ID[5] = g_slConfigInfo.address[5];
    recvpack.access_Node_ID[6] = g_slConfigInfo.address[6];

    /* 接入节点类型 */
    recvpack.access_Node_Type_H = g_Mppt_Para.Access_Node_Type >> 8;
    recvpack.access_Node_Type_L = g_Mppt_Para.Access_Node_Type;

    /* 校验位 */
//    uint8_t *rpack_buf = (uint8_t *)&recvpack;
    uint16_t crc = CheckFunc((uint8_t *)&recvpack, SL_MPPT_REGISTRATIONREQUEST_PACK_SIZE - 3);

    recvpack.check_Bit_H = crc >> 8;
    recvpack.check_Bit_L = crc;
    /* 结束标志 */
    recvpack.end_Flag = g_slConfigInfo.end_Flag;
    /* 校验位 */
    for (uint8_t var = 0; var < 10; ++var) {
        Delay_Ms(randomDelay());
        if (!Check_485_bus_busy(g_recvBroadcastDevice)) {
            if (recvbroadcast_flag == 1) {
                recvbroadcast_flag = 0;
                run_number = 0;
                return;
            }
            uart_dev_write(g_recvBroadcastDevice, (uint8_t *)&recvpack, SL_MPPT_REGISTRATIONREQUEST_PACK_SIZE + 1);

            if (g_recvBroadcastDevice == g_bat485_uart3_handle) {
                USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
            } else {
                USART_ITConfig(USART4, USART_IT_RXNE, ENABLE);
            }

            break;
        }
    }
}

/**
  * @brief 给泵电路电容充电
  * @param
  * @retval
  */
STR_TimeSliceOffset m_CapCharg;
void Task_CapCharg(void)
{
    if (0.1 > get_CHG_CURR()) {
//        TIM_Cmd(TIM4, DISABLE);
//
//        TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Disable);
//        TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
//
//        TIM_Cmd(TIM4, ENABLE);
//        Delay_Us(200);
//
//        TIM_Cmd(TIM4, DISABLE);
//        TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Disable);
//        TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);
//        TIM_Cmd(TIM4, ENABLE);

//        TIM_Cmd(TIM4, DISABLE);
//        TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Disable);
        Set_duty_ratio(0);
//        GPIO_WriteBit(GPIOB, GPIO_Pin_9, RESET);
        for (uint8_t var = 0; var < 8; ++var) {
            GPIO_WriteBit(CAPCHARG_GPIO, CAPCHARG_PIN, SET);
            Delay_Us(1);
            GPIO_WriteBit(CAPCHARG_GPIO, CAPCHARG_PIN, RESET);
            Delay_Us(1);
        }
//        TIM_Cmd(TIM4, ENABLE);
//        TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);
        duty_ratio = 0.9;
        Set_duty_ratio(duty_ratio);

//        Set_duty_ratio(0);
//        GPIO_WriteBit(GPIOB, GPIO_Pin_9, SET);


        printf("in test \n");
        return;
    }
//    Set_duty_ratio(0);
//    GPIO_WriteBit(CAPCHARG_GPIO, CAPCHARG_PIN, SET);
//    Delay_Us(1);
//    GPIO_WriteBit(CAPCHARG_GPIO, CAPCHARG_PIN, RESET);

//    Set_duty_ratio(0.5);
    return;
}
/**
  * @brief  初始化各个寄存器
  * @param
  * @retval
  */
void g_Mppt_Para_Init(void)
{
    g_Mppt_Para.Registration_Status = UNREGISTER;
    g_Mppt_Para.address[0] = g_slConfigInfo.address[0];
    g_Mppt_Para.address[1] = g_slConfigInfo.address[1];
    g_Mppt_Para.address[2] = g_slConfigInfo.address[2];
    g_Mppt_Para.address[3] = g_slConfigInfo.address[3];
    g_Mppt_Para.address[4] = g_slConfigInfo.address[4];
    g_Mppt_Para.address[5] = g_slConfigInfo.address[5];
    g_Mppt_Para.address[6] = g_slConfigInfo.address[6];
    g_Mppt_Para.Access_Node_Type = g_slConfigInfo.Access_Node_Type;
    g_Mppt_Para.Communication_Methods = g_slConfigInfo.Communication_Methods;
    g_Mppt_Para.Battery_Voltage = 0;
    g_Mppt_Para.Battery_temperature = 0;
    g_Mppt_Para.Remaining_Battery_Bower = 0;
    g_Mppt_Para.Solar_Open_Circuit_Voltage1 = 0;
    g_Mppt_Para.Solar_Open_Circuit_Voltage2 = 0;
}

/**
  * @brief  初始化各个任务
  * @param
  * @retval
  */
void task_Init(void)
{
    read_config_info();
    g_Mppt_Para_Init();
//
    TimeSliceOffset_Register(&m_runled, Task_RunLED, runled_reloadVal, runled_offset);
//    TimeSliceOffset_Register(&m_mpptControl, Task_mpptControl, mpptControl_reloadVal, mpptControl_offset);
    TimeSliceOffset_Register(&m_usart, Task_usart, usart_reloadVal, usart_offset);
    TimeSliceOffset_Register(&m_wdi, Task_wdi, wdi_reloadVal, wdi_offset);
    TimeSliceOffset_Register(&m_CapCharg, Task_CapCharg, CapCharg_reloadVal, CapCharg_offset);
//
    TimeSliceOffset_Start();    /* 启动时间片轮询 */

//    while(1)
//    {
//        Task_RunLED();
//        Delay_Ms(1000);
//    }
}


/**
  * @brief  初始化所需外设和寄存器中的变量
  * @param
  * @retval
  */
void hardware_Init(void)
{
    TIM3_Init();
    uart_dev_init();
    PWM_TIM_Configuration();
    ADC_all_Init();
    RUN_LED_Init();
    G_FFMOS_CON_Init();
    WDI_INPUT_Init();
    BEEP_Init();
    POW_OUT_CON_Init();
    DSG_PROT_Init();
    SPI_Flash_Init();

    Set_duty_ratio(0.5);
//
    CAPCHARG_Init();
}

