/**
    @copyright None
    @file      led.c
    @author    Comment Vivre
    @date      2025-12-19
    @brief     None
*/

#include <MyProject.h>
LED_State_t ledState = LED_STATE_OFF;
LED_Count_t ledCount;


/**
    @function     LED_GPIO_Init
    @brief        LED GPIO初始化
    @date         2025-12-19
*/
void LED_GPIO_Init(void)
{
    SetBit(LED_PXIN, LED_PINX);
    LED_OFF;
}

/**
    @function     LED_State_Display
    @brief        指示运行与故障代码
    @param[in]    Xn0: [输入/出] 故障代码
    @date         2025-05-30
*/
void LED_State_Display(unsigned char Xn0)
{
    switch (ledState)
    {
        // 关闭状态
        case LED_STATE_OFF:
        
            // 存在故障 切入故障状态 清零计数
            if (Xn0)
            {
                ledState = LED_STATE_BLINK_FAULT;
                ledCount.PauseDelayCnt = 0;
                ledCount.BlinkCnt = 0;
            }
            // 开启状态且无故障 切入开启状态并打开LED
            else if (isCtrlPowOn)
            {
                ledState = LED_STATE_ON;
                LED_ON;
            }
            
            break;
            
        // 开启状态
        case LED_STATE_ON:
        
            // 故障或停止时 关闭LED并切回关闭状态
            if (!isCtrlPowOn || Xn0)
            {
                ledState = LED_STATE_OFF;
                LED_OFF;
            }
            
            break;
            
        // 故障指示状态
        case LED_STATE_BLINK_FAULT:
            ledCount.PauseDelayCnt ++;
            
            // 延时等待
            if (ledCount.PauseDelayCnt >= 500)
            {
                ledCount.PauseDelayCnt = 0;
                LEDPinONOFF;
                
                // 灭灯时计数
                if (LED_PIN)
                {
                    ledCount.BlinkCnt ++;
                    
                    // 完成一个周期的闪烁 切换到延时等待模式
                    if (ledCount.BlinkCnt >= Xn0)
                    { ledState = LED_STATE_POST_FAULT_DELAY; }
                }
            }
            
            // 故障清除后 立刻回到熄灭状态
            if (!Xn0)
            {
                LED_OFF;
                ledState = LED_STATE_OFF;
            }
            
            break;
            
        // 延迟等待
        case LED_STATE_POST_FAULT_DELAY:
            ledCount.PauseDelayCnt ++;
            
            // 延时满足或故障清除回到关闭状态
            if ((ledCount.PauseDelayCnt >= 1000) || (!Xn0))
            { ledState = LED_STATE_OFF; }
            
            break;
    }
}
