/**
 * @file led.c
 * @brief led interface of Project.
 *
 * @verbatim
 * ==============================================================================
 *                        ##### How to use #####
 * ==============================================================================
 *
 *
 @ endverbatim
 *
 * @author HiView SoC Software Team
 * @version 1.0.0
 * @date 2023-05-11
 */
#include "hv_vos_Comm.h"
#include "hv_comm_Define.h"
#include "hv_comm_DataType.h"
#include "hv_comm_DataBase.h"
#include "hv_comm_Assert.h"

#include "hv_drv_Gpio.h"

#include "hv_mw_EventHub.h"
#include "hv_mw_SystemManager.h"

static HV_TIMER_S*   pstLedBlinkTimer;

VOID Hv_Mw_Led_SetLedPowerMode(UCHAR8 bMode)
{
    return;
}

VOID Hv_Mw_Led_SetRedLedMode(UCHAR8 ucMode)
{
    HV_VOS_QUEUE_S *pstEventHubQueue = Hv_Mw_System_GetEventHubQueue();
    HV_ASSERT_VALID_PTR_VOID(pstEventHubQueue);
    Event stEvent = {0};
    stEvent.usEventId = EVENT_HUB_REV_MSG_ID_PMLED_OFF + ucMode;
    stEvent.usValue = 0;
    HV_ASSERT_TRUE_VOID(Hv_Vos_QueueSend(pstEventHubQueue, &stEvent));

    return;
}

VOID Hv_Mw_Led_SetRedLed(BOOL bEn)
{
    #if 0
    HV_VOS_QUEUE_S *pstEventHubQueue = Hv_Mw_System_GetEventHubQueue();
    HV_ASSERT_VALID_PTR_VOID(pstEventHubQueue);
    Event stEvent = {0};
    stEvent.usEventId = bEn ? EVENT_HUB_REV_MSG_ID_PMLED_ON : EVENT_HUB_REV_MSG_ID_PMLED_OFF ;
    stEvent.usValue = 0;
    HV_ASSERT_TRUE_VOID(Hv_Vos_QueueSend(pstEventHubQueue, &stEvent));
    #endif
    return;
}

#define CONFIG_LED_PWM_TOTAL_CNT 7656
VOID Hv_Mw_Led_SetBlueLedBright(UCHAR8 ucBrightness)
{
    USHORT16 usValue = 0;
    #if HV_BOARD_CONFIG_LED_B_POL == 0
        ucBrightness = 255 - ucBrightness;
    #endif
    #ifdef HV_BOARD_CONFIG_LED_B_PWM
        usValue = CONFIG_LED_PWM_TOTAL_CNT/0xff*ucBrightness;
        Hv_Drv_MPRT_SetOutHighFreqPwmDuty(HV_BOARD_CONFIG_LED_B_PWM, CONFIG_LED_PWM_TOTAL_CNT-usValue, usValue);
    #endif
    return;
}

VOID Hv_Mw_Led_SetBlueLedNoData(BOOL bEn)
{
#ifdef HV_BOARD_CONFIG_GPIO_LED_B
    #ifdef HV_BOARD_CONFIG_LED_B_PWM
        Hv_Mw_Led_SetBlueLedBright(bEn?0xff:0);
    #else
        #if HV_BOARD_CONFIG_LED_B_POL == 0
            Hv_Drv_Gpio_SetPinLevel(HV_BOARD_CONFIG_GPIO_LED_B, (bEn ? GPIO_LEVEL_LOW:GPIO_LEVEL_HIGH));
        #else
            Hv_Drv_Gpio_SetPinLevel(HV_BOARD_CONFIG_GPIO_LED_B, (bEn ? GPIO_LEVEL_HIGH:GPIO_LEVEL_LOW));
        #endif
        Hv_Drv_Gpio_SetDirection(HV_BOARD_CONFIG_GPIO_LED_B, GPIO_DIR_OUTPUT);
    #endif
#endif
    return;
}
VOID Hv_Mw_Led_SetBlueLed(BOOL bEn)
{
    HV_COMMON_DATABASE_SET(UserData, ucLightEffect, bEn);
    Hv_Mw_Led_SetBlueLedNoData(bEn);
    return;
}


static VOID FactoryBlinkLed(VOID *pvArg, USHORT16 usTimerId)
{
    static UCHAR8 ucLoop = 0;

    ucLoop++;
    if (ucLoop%2)
    {
        Hv_Mw_Led_SetRedLedMode(HV_TRUE);
        Hv_Mw_Led_SetBlueLedNoData(HV_FALSE);
    }
    else
    {
        Hv_Mw_Led_SetRedLedMode(HV_FALSE);
        Hv_Mw_Led_SetBlueLedNoData(HV_TRUE);
    }

    return;
}

static HV_TIMER_INIT_PARAMS stLedBlinkTimerParam = \
{"LedBlinkTimer", 1000, HV_TRUE, NULL, FactoryBlinkLed, 0};

static VOID Hv_Mw_Led_BlinkTimerStart( VOID )
{
    if (pstLedBlinkTimer == NULL)
    {
        pstLedBlinkTimer = Hv_Vos_Timer_Create(&stLedBlinkTimerParam);
    }

    HV_ASSERT_VALID_PTR_VOID(pstLedBlinkTimer);

    Hv_Vos_Timer_Start(pstLedBlinkTimer);

    return;
}

static VOID Hv_Mw_Led_BlinkTimerStop(VOID)
{
    if (pstLedBlinkTimer != NULL)
    {
        Hv_Vos_Timer_Stop(pstLedBlinkTimer);
    }
    return;
}

VOID Hv_Mw_Led_SetLedMode(LedMode eMode)
{
    HV_LOGI("Hv_Mw_Led_SetLedMode %d\n", eMode);
    switch (eMode)
    {
        case LED_POWERSTANDBY:
        {
            Hv_Mw_Led_BlinkTimerStop();
            Hv_Mw_Led_SetRedLedMode(HV_TRUE);
            Hv_Mw_Led_SetBlueLedNoData(HV_FALSE);
            break;
        }
        case LED_POWEROFF:
        {
            Hv_Mw_Led_BlinkTimerStop();
            Hv_Mw_Led_SetRedLedMode(HV_FALSE);
            Hv_Mw_Led_SetBlueLedNoData(HV_FALSE);
            break;
        }
        case LED_BURN_IN:
        {
            Hv_Mw_Led_BlinkTimerStart();
            break;
        }
        case LED_BURN_OUT:
        {
            Hv_Mw_Led_BlinkTimerStop();
            //pass through to LED_POWERON
        }
        case LED_POWERON:
        {
            Hv_Mw_Led_SetRedLedMode(HV_FALSE);
            if (HV_COMMON_DATABASE_GET(UserData, ucLightEffect))
            {
                Hv_Mw_Led_SetBlueLedNoData(HV_TRUE);
            }
            else 
            {
                Hv_Mw_Led_SetBlueLedNoData(HV_FALSE);
            }
            break;
        }
        default:
        {
            break;
        }
    }
    return;
}