#include "hv_mw_AmbientLight.h"
#include "Common/hv_comm_DataType.h"
#include "hv_vos_Comm.h"
#include "hv_comm_Assert.h"
#include "hv_comm_Event.h"
#include "hv_chip_Memory.h"
#include "hv_chip_Config.h"
#include "hv_drv_Gpio.h"
#include "hv_cal_mprt.h"
#include "hv_comm_Timer.h"


VOID Hv_Mw_AmbientLight_Start(VOID)
{

    return;
}

VOID Hv_Mw_AmbientLight_Stop(VOID)
{

    return;
}

typedef struct
{
    HV_TIMER_S *pstAmblDutyPeriodTimer;               /* 氛围灯调节占空比timer */
}AmbientLightTimer;

static AmbientLightTimer s_stAmbientLightTimer = {0};

#define AMBL_TIMER_EACH_PERIOD              2      //s，单色灯从灭亮灭一个周期的用时
#define AMBL_PWM_LIGHT1_PWM0                0       //port
#define AMBL_PWM_LIGHT2_PWM7                7       //port
#define AMBL_PWM_LIGHT3_PWM6                6       //port
#define AMBL_PWM_FRQ_TOTOAL                 65332   //频率3000hz
#define AMBL_TIMER_TUNE_DUTY_PERIOD         10      //ms，多长时间调一次占空比

static VOID AmbientLightPwmInit(VOID)
{
    //使用mode3 freerun pwm
    Hv_Cal_MPRT_PwmSetMode(AMBL_PWM_LIGHT1_PWM0, 3);
    Hv_Cal_MPRT_PwmSetMode(AMBL_PWM_LIGHT2_PWM7, 3);
    Hv_Cal_MPRT_PwmSetMode(AMBL_PWM_LIGHT3_PWM6, 3);
    //频率设置200hz
    Hv_Cal_MPRT_PwmSetMult(AMBL_PWM_LIGHT1_PWM0, 0);
    Hv_Cal_MPRT_PwmSetMult(AMBL_PWM_LIGHT2_PWM7, 0);
    Hv_Cal_MPRT_PwmSetMult(AMBL_PWM_LIGHT3_PWM6, 0);
    //初始占空比10%，65332*0.1
    Hv_Cal_MPRT_PwmSetValue(AMBL_PWM_LIGHT1_PWM0, 6533);
    Hv_Cal_MPRT_PwmSetStart(AMBL_PWM_LIGHT1_PWM0, AMBL_PWM_FRQ_TOTOAL - 6533);
    Hv_Cal_MPRT_PwmSetValue(AMBL_PWM_LIGHT2_PWM7, 6533);
    Hv_Cal_MPRT_PwmSetStart(AMBL_PWM_LIGHT2_PWM7, AMBL_PWM_FRQ_TOTOAL - 6533);
    Hv_Cal_MPRT_PwmSetValue(AMBL_PWM_LIGHT3_PWM6, 6533);
    Hv_Cal_MPRT_PwmSetStart(AMBL_PWM_LIGHT3_PWM6, AMBL_PWM_FRQ_TOTOAL - 6533);

    Hv_Cal_MPRT_PwmEnable(AMBL_PWM_LIGHT1_PWM0, 0);
    Hv_Cal_MPRT_PwmEnable(AMBL_PWM_LIGHT2_PWM7, 0);
    Hv_Cal_MPRT_PwmEnable(AMBL_PWM_LIGHT3_PWM6, 0);
    return;
}

static VOID AmbientLightPwmLoop(VOID* pvArgument, USHORT16 usTimerId)
{
    HV_UNUSED(pvArgument);
    HV_UNUSED(usTimerId);

    UINT32 auiLightPin[] = {AMBL_PWM_LIGHT1_PWM0, AMBL_PWM_LIGHT2_PWM7, AMBL_PWM_LIGHT3_PWM6};
    static BOOL bIsIncreasing = 1;
    static UINT32 uiPinNextCount = 0;
    static UINT32 uiCurrentTunePin = AMBL_PWM_LIGHT1_PWM0;
    USHORT16 uiDuty10Percent = AMBL_PWM_FRQ_TOTOAL * 0.1;
    USHORT16 uiDuty90Percent = AMBL_PWM_FRQ_TOTOAL * 0.9;
    USHORT16 uiDutySetpVale = (AMBL_PWM_FRQ_TOTOAL * 0.8) / (AMBL_TIMER_EACH_PERIOD * 1000 / AMBL_TIMER_TUNE_DUTY_PERIOD);

    static USHORT16 usDutyCurrentValue = 6533;

    Hv_Cal_MPRT_PwmEnable(uiCurrentTunePin, 1);
    Hv_Cal_MPRT_PwmSetValue(uiCurrentTunePin, usDutyCurrentValue);
    Hv_Cal_MPRT_PwmSetStart(uiCurrentTunePin, AMBL_PWM_FRQ_TOTOAL - usDutyCurrentValue);

    if (bIsIncreasing) 
    {
        usDutyCurrentValue += uiDutySetpVale;
        if (usDutyCurrentValue >= uiDuty90Percent) 
        {
            bIsIncreasing = 0;
        }
    }
    else 
    {
        usDutyCurrentValue -= uiDutySetpVale;
        if (usDutyCurrentValue <= uiDuty10Percent) 
        {
            bIsIncreasing = 1;
            Hv_Cal_MPRT_PwmEnable(uiCurrentTunePin, 0);
            uiPinNextCount++;
        }
    }
    if (uiPinNextCount > 2)
    {
        uiPinNextCount = 0;
        uiCurrentTunePin = auiLightPin[uiPinNextCount];
    }
    else
    {
        uiCurrentTunePin = auiLightPin[uiPinNextCount];
    }
    //HV_LOG_INFO(PM, "pin:%u, val:%u, Pin+%u", uiCurrentTunePin, usDutyCurrentValue, uiPinNextCount);
    Hv_Vos_Timer_Rstart(s_stAmbientLightTimer.pstAmblDutyPeriodTimer,0);
    return;
}

static VOID AmbientLightTimerInit(VOID)
{
    HV_TIMER_INIT_PARAMS stAmbientLightDutyTimerParam = \
    {"AmbientLightTimer", AMBL_TIMER_TUNE_DUTY_PERIOD, HV_FALSE, NULL, AmbientLightPwmLoop, EVENT_TIMER_MAX};
    s_stAmbientLightTimer.pstAmblDutyPeriodTimer = Hv_Vos_Timer_Create(&stAmbientLightDutyTimerParam);
    HV_ASSERT_VALID_PTR_VOID(s_stAmbientLightTimer.pstAmblDutyPeriodTimer);
    return;
}

#if 0
#define PWM_PERIOD 5000 // PWM周期为5000us，对应于200Hz的频率
#define DUTY_CYCLE_MIN (PWM_PERIOD / 10) // 最小占空比为10%
#define DUTY_CYCLE_MAX (PWM_PERIOD * 9 / 10) // 最大占空比为90%
#define DUTY_CYCLE_STEP ((DUTY_CYCLE_MAX - DUTY_CYCLE_MIN) / 100) // 占空比每次改变的步长
#define DUTY_CYCLE_PERIOD 2000000 // 占空比改变周期为2000000us，即2秒

void pwm_loop(void) 
{
    UINT32 period = 5000; // 200Hz 的周期为 5000us
    UINT32 highTime10Percent = period * 0.2;
    UINT32 highTime90Percent = period * 0.8;
    UINT32 currentHighTime = highTime10Percent;
    UINT32 stepus = 5;  //40us
	BOOL increasing = 1;
    UINT32 auiLightPin[] = {99, 100, 101};
    UINT32 PinNum = pin[0];
    UINT32 pinSwapNext = 0;
    while (1) 
	{
        // 输出高电平
        //HV_LOG_INFO(PM, "%u",currentHighTime);
        Hv_Drv_Gpio_SetPinLevel(PinNum, GPIO_LEVEL_HIGH);
        Hv_Vos_Delayus(currentHighTime);
        

        // 输出低电平
        Hv_Drv_Gpio_SetPinLevel(PinNum, GPIO_LEVEL_LOW);
        Hv_Vos_Delayus(period - currentHighTime);
        //HV_LOG_INFO(PM, "low %u",PinNum);


        // 根据当前的方向改变占空比
        if (increasing) 
		{
            currentHighTime += stepus;
            if (currentHighTime >= highTime90Percent) 
			{
                increasing = 0;

            }
        }
		else 
		{
            currentHighTime -= stepus;
            if (currentHighTime <= highTime10Percent) 
			{
                increasing = 1;
                pinSwapNext++;
                Hv_Drv_Gpio_SetPinLevel(PinNum, GPIO_LEVEL_LOW);
            }
        }
        if (pinSwapNext != 0)
        {
            PinNum = pinSwapNext % 3;
            PinNum = auiLightPin[PinNum];
            //pinSwapNext = 0;
            //HV_LOG_INFO(PM, "-##########");
        }
    }
    return;
}
#endif
VOID Hv_Mw_AmbientLightTaskHandle(VOID *arg)
{

    for (;;)
    {
        //Hv_Vos_MSleep(1000);

        Hv_Vos_Timer_Rstart(s_stAmbientLightTimer.pstAmblDutyPeriodTimer,0);
        Hv_Vos_TaskYield();
    }

    return;
}

VOID Hv_Mw_AmbientLightPwmInit(VOID)
{
    AmbientLightTimerInit();
    AmbientLightPwmInit();
    return;
}

