/************************************************************************
*                             ---- 居加居 ----                           
*  文件名:      	Led.c
*  编写人员:    	Kinve JQ
*  说明:         
*  版本:        		V0.1
*  编写日期:    	2018-09-30
*  程序维护:     
*  维护记录:    
*    					2018-09-30.Kinve:1.创建文件
* 免责声明：
* (c) Author Kinve JQ. All rights reserved.
**************************************************************************/

#include "multi_led.h"
#include "led.h"
#include "N76E003.h"

struct Led lRun, lAla, lErr;

void ScanLed ( void )		//100ms
{
    led_ticks();
}

void Set_RUN_GPIO ( bool state )
{
    P12 = state;
}


void Leds_Init ( void )
{
    led_init_Head();
    led_init ( &lRun, Set_RUN_GPIO );

    led_set_state ( &lRun, LED_WORK, 	0, 	LED_STATE2_FLASH, 	1000, 	0, 	0 );

    led_start ( &lRun );
    //make the timer invoking the button_ticks() interval 5ms.
    //This function is implemented by yourself.
    //__timer_start(button_ticks, 0, 100);

}


/*
通用:WorkLed闪动
通信正常: TxLed 亮 (其他功能可共用), GZLed 灭; 传输TxLed 闪
通信故障: TxLed 灭, GZLed 亮:电源故障
通信故障,100s声光提示,声可消除;光保持到,无线模块复位,或故障消除
*/
#if 0

pFault FAULT_State = {.State = Bin ( 00 ) };
pFault FAULT_StateOld = {.State = Bin ( 00 ) };
bool FAULT_State_NoChange = true;
u8 sysLed_Pretreat ( u8 State )	//预处理一下:故障和恢复
{
    u8 Res = State;

    //3G/电源 故障与恢复
    switch ( State )
    {
    case LED_MODE_WORK_3G_OK:
        FAULT_State.Flag._3g = 0;
        break;

    case LED_MODE_WORK_3G_FAIL:
        FAULT_State.Flag._3g = 1;
        break;

    case LED_MODE_WORK_DC_BAT_OK:
        FAULT_State.Flag._dc_bat = 0;
        break;

    case LED_MODE_WORK_DC_BAT_FAIL:
        FAULT_State.Flag._dc_bat = 1;
        break;

    default:
        return State;
    }

    if ( FAULT_State.Flag._3g )				Res = LED_MODE_WORK_3G_FAIL;		//最优先3G
    else if ( FAULT_State.Flag._dc_bat )	Res = LED_MODE_WORK_DC_BAT_FAIL;

    FAULT_State_NoChange = false;

    return Res;
}




//灯的控制和蜂鸣器的控制
void sysLed_Set ( u8 State )
{
    State = sysLed_Pretreat ( State );

    switch ( SysRunType )
    {
    case SysRun_Norm:
        switch ( State )
        {
        case LED_MODE_WORK_RUN:
            LedDoing ( LED_WORK, 0,	LED_STATE2_FLASH,	1000,	0,	0 );	//单位100ms: 500ms开,500ms关

            if ( FAULT_State_NoChange )					//初始化状态
            {
                LedDoing ( LED_TX,	0,	LED_STATE0_OFF, 	0,		0,	0 );
                LedDoing ( LED_GZ,	0,	LED_STATE0_OFF,		0,		0,	0 );
            }
            else if ( FAULT_State.Flag._3g )			//1.3g故障, 2.3g+电源故障
            {
                LedDoing ( LED_TX,	0,	LED_STATE0_OFF, 	0,		0,	0 );
                LedDoing ( LED_GZ,	0,	LED_STATE1_ON,		0,		0,	0 );
            }
            else if ( FAULT_State.Flag._dc_bat )		//3g正常, 电源故障
            {
                LedDoing ( LED_TX,	0,	LED_STATE1_ON,		0,		0,	0 );
                LedDoing ( LED_GZ,	0,	LED_STATE2_FLASH,	500,	0,	0 );
            }
            else										//3g正常, 电源正常
            {
                LedDoing ( LED_TX,	0,	LED_STATE1_ON,		0,		0,	0 );
                LedDoing ( LED_GZ,	0,	LED_STATE0_OFF, 	0,		0,	0 );
            }

            break;

        case LED_MODE_WORK_3G_OK:
            LedDoing ( LED_TX,	0,	LED_STATE1_ON,		0,		0,	0 );
            LedDoing ( LED_GZ,	0,	LED_STATE0_OFF, 	0,		0,	0 );
            break;

        case LED_MODE_WORK_3G_FAIL:
            LedDoing ( LED_TX,	0,	LED_STATE0_OFF, 	0,		0,	0 );
            LedDoing ( LED_GZ,	0,	LED_STATE1_ON,		0,		0,	0 );
            break;

        case LED_MODE_WORK_DC_BAT_OK:
            LedDoing ( LED_GZ,	0,	LED_STATE0_OFF, 	0,		0,	0 );
            break;

        case LED_MODE_WORK_DC_BAT_FAIL:
            LedDoing ( LED_GZ,	0,	LED_STATE2_FLASH,	500,	0,	0 );
            break;

        case LED_MODE_WORK_3G_TX:
            LedDoing ( LED_TX,	0,	LED_STATE5_FLASH_DELAY, 100, 200, 1 ); //100ms开,开关1次
            break;

        case LED_MODE_WORK_3G_RX:
            LedDoing ( LED_TX,	0,	LED_STATE5_FLASH_DELAY, 100, 400, 1 ); //100ms开,开关2次
            break;

        case LED_MODE_TEST_RUN:
        case LED_MODE_TEST_KGL_IN:
            break;
        }

        break;

    case SysRun_Test:
        switch ( State )
        {
        case LED_MODE_WORK_RUN:
            break;

        case LED_MODE_WORK_3G_OK:
            LedDoing ( LED_TX,	0,	LED_STATE1_ON,		0,		0,	0 );
            break;

        case LED_MODE_WORK_3G_FAIL:
            LedDoing ( LED_TX,	0,	LED_STATE0_OFF, 	0,		0,	0 );
            break;

        case LED_MODE_WORK_DC_BAT_OK:
        case LED_MODE_WORK_DC_BAT_FAIL:
        case LED_MODE_WORK_3G_TX:
        case LED_MODE_WORK_3G_RX:
            break;

        case LED_MODE_TEST_RUN:
            LedDoing ( LED_WORK, 0,	LED_STATE2_FLASH,	200,	0,	0 );
            LedDoing ( LED_GZ,	0,	LED_STATE2_FLASH,	200,	0,	0 );
            LedDoing ( LED_TX,	0,	LED_STATE2_FLASH,	200,	0,	0 );
            break;

        case LED_MODE_TEST_KGL_IN:
            LedDoing ( LED_GZ,	0,	LED_STATE4_NOT, 	0,		0,	0 );
            break;
        }

        break;
    }

    if ( SysRunType == SysRun_Norm )
    {
        if ( FAULT_StateOld.State != FAULT_State.State )
        {
            FAULT_StateOld.State = FAULT_State.State;
            ( FAULT_State.State == 0 ) ? StopBeep() : StartBeep ( WARMING_BEEP );    //判断是否是正常状态，是正常则停，告警则响
        }
    }

}

#endif

/*************** (C) COPYRIGHT Kinve JQ  *******END OF FILE **************/

