#include "bsp_beep_led_emv.h"

#include "bsp_uart.h"
#include "log.h"

#include "bsp_beep_led_emv.h"
#include "bsp_valve.h"

#include "SLEEP.h"

#include "bsp_adc.h"

#undef LOG_ENABLE
#define LOG_ENABLE 1

#undef LOG_TAG
#define LOG_TAG "led"

tmosTaskID led_task_id = INVALID_TASK_ID;




void ShowLed()
{
    // gValveData.bat = BSP_ReadVbat();
    // BSP_RequestBoost();

    // 电压过低就关阀
    // if (gValveData.bat <= LOW_VABAT_ALARM_VALUE)
    if (bat_mv <= LOW_VABAT_CLOSE_MV)
    {
        LED_ALL_OFF_DEINIT;
        LED_Y_ON;

        // BEEP_ON;
        logDebug("LED_Y_ON");
    }
    else if (gValveData.switch_status == kUnknown || gValveData.switch_status == kClosed)
    {
        LED_ALL_OFF_DEINIT;
        LED_R_ON;

        logDebug("LED_R_ON");
    }
    else if (gValveData.switch_status == kOpened)
    {
        LED_ALL_OFF_DEINIT;

        LED_G_ON;

        logDebug("LED_G_ON");
    }
}

static tmosEvents LED_Task_ProcessEvent(tmosTaskID taskID, tmosEvents event)
{
    //event 处理
    if(event & LED_SHOW_START_EVT)
    {
        logDebug("LED_SHOW_START_EVT");
        BSP_BlockSleep();
        BSP_RequestBoost();
        // DelayMs(3);
        ShowLed();
        tmos_stop_task(led_task_id, LED_SHOW_END_EVT);
        // tmos_start_task(led_task_id, LED_SHOW_ONCE_EVT, MS1_TO_SYSTEM_TIME(10));
        // TODO:这里短点功耗低
        tmos_start_task(led_task_id, LED_SHOW_END_EVT, MS1_TO_SYSTEM_TIME(5));
        return (event ^ LED_SHOW_START_EVT);
    }
    if (event & LED_SHOW_ONCE_EVT)
    {
        logDebug("LED_SHOW_ONCE_EVT");

        LED_ALL_OFF_DEINIT;
        BEEP_OFF_DEINIT;

        tmos_set_event(led_task_id, LED_SHOW_END_EVT);

        return (event ^ LED_SHOW_ONCE_EVT);
    }
    if(event & LED_SHOW_END_EVT)
    {
        logDebug("LED_SHOW_END_EVT");

        LED_ALL_OFF_DEINIT;
        BEEP_OFF_DEINIT;

        BSP_NoNeedBoost();
        BSP_RequestSleep();

        // BEEP_OFF_DEINIT;
        // EMV_CHARGE_OFF_DEINIT;
        // EMV_OFF_DEINIT;

        return (event ^ LED_SHOW_END_EVT);
    }
    if (event & LOW_VBAT_EVT)
    {
        logDebug("LOW_VBAT_EVT");
        LED_Y_ON;
        return (event ^ LOW_VBAT_EVT);
    }
    if (event & CONNECT_BEEP_START_EVT)
    {
        logDebug("CONNECT_BEEP_START_EVT");
        BSP_BlockSleep();
        BSP_RequestBoost();
        // BEEP_ON;
        LED_G_ON;

        tmos_start_task(led_task_id, CONNECT_BEEP_END_EVT, MS1_TO_SYSTEM_TIME(50));

        return (event ^ CONNECT_BEEP_START_EVT);
    }
    if(event & CONNECT_BEEP_END_EVT)
    {
        logDebug("CONNECT_BEEP_END_EVT");

        LED_ALL_OFF_DEINIT;
        BEEP_OFF_DEINIT;

        BSP_NoNeedBoost();
        BSP_RequestSleep();

        // BEEP_OFF_DEINIT;
        // EMV_CHARGE_OFF_DEINIT;
        // EMV_OFF_DEINIT;

        return (event ^ CONNECT_BEEP_END_EVT);
    }

}


void BSP_BEEP_LED_EMV_Init(void)
{
    BOOST_OFF_DEINIT;
    BEEP_OFF_DEINIT;
    LED_ALL_OFF_DEINIT;
    EMV_CHARGE_OFF_DEINIT;
    EMV_OFF_DEINIT;

    led_task_id = TMOS_ProcessEventRegister(LED_Task_ProcessEvent);
    logDebug("BSP_BEEP_LED_EMV_Init");
}

