/*
 ******************************************************************************
 *
 * @file     UserApp.c
 * @brief    UserApp module
 *                 
 *
 * @version  1.0
 * @date     2023/03/27   15:00:40
 * @author   Alex Xu
 *
 * Copyright (c) 2013-2099,Tkplusemi Technology Co.,Ltd.
 * All Rights Reserved
 *
 * History:
 * Revision          Date           Author            Desc
 * 1.0.0            2023/03/27      Alex          build this file
 ******************************************************************************/
 /*_____ I N C L U D E S ____________________________________________________*/

#include "userapp.h"
#include "gpio.h"
#include "adc.h"
#include "hall.h"
#include "key.h"
#include "sleep.h"
#include "system.h"
#include "bat.h"
#include "sys_tim.h"
#include "charger_module.h"
#include "discharge_module.h"
#include "led.h"
#include "vox_module.h"

/******************************************************************************\
                          Macro definitions
\******************************************************************************/

/******************************************************************************\
                             Variables definitions
\******************************************************************************/

uint8_t LED_On_Timer = 0;							//led亮时间计时
uint8_t LED1_On_Timer = 0;							//led1亮时间计时

uint8_t Earphone_Chg_LED_On_Timer = 0;    			//耳机出入仓led亮时间计时

bit LED_On_Flag = 0;

/******************************************************************************\
                             Functions definitions
\******************************************************************************/

/*****************************************************************************
* Function		: void LED_Display_App(void)
* Description	: 5ms调用周期					
* Input 		: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
/*
充电：
1、充电中，无其它行为打断时，开关盒状态下都显示充电常亮灯效（1Hz闪烁）；
2、充满，常亮；可被其它灯效打断；

非充电：
1、首次使用开盖灯效：①高电：亮4s后灭；②低电：1Hz闪4s后灭；
2、非首次使用开盖灯效：①耳机在盒：亮4s后灭；②耳机不在盒：？？？？
						③充电中，开盖闪一下，如何闪？亮多久后灭？
3、关盖灯效，高电：亮4s后灭；低电：1Hz闪4s后灭；
4、关盒状态下无灯效显示；

*/
#ifdef LED_DISPLAY	

void LED_Display_App(void)
{
	if( Earphone_Chg_Flag )										//耳机出入仓呼吸灯循环3次。
	{
		if( Earphone_Chg_LED_On_Timer < LED_OFF_CNT ) 
		{
#ifdef BREATHING_LIGHT
			BL_LED.Breathing_Light_On[LED_R] = 0;
			BL_LED.Breathing_Light_On[LED_G] = 0;
			BL_LED.BL_Timer[LED_R] = BL_DUTY_MAX;
			BL_LED.BL_Timer[LED_G] = BL_DUTY_MAX;
#endif		
			LEDR_OFF();
			LEDG_OFF();
			
			Earphone_Chg_LED_On_Timer++;
		}
		else
		if( Earphone_Chg_LED_On_Timer < LED_HUXI_ON_CNT ) 				//红色LED亮5s后灭。
		{
			Earphone_Chg_LED_On_Timer++;
			
			if( F_batlevel_low || ( CHIP_STA4 & 0x80 ) )            //充电过程中，耳机出入仓，橙色呼吸三下。
			{
#ifdef BREATHING_LIGHT
				BL_LED.Breathing_Light_On[LED_R] = 1;
				BL_LED.Breathing_Light_On[LED_G] = 0;			
#endif
				LEDG_OFF();
			}
			else
			{
#ifdef BREATHING_LIGHT
				BL_LED.Breathing_Light_On[LED_R] = 0;
				BL_LED.Breathing_Light_On[LED_G] = 1;			
#endif
				LEDR_OFF();
			}
		}
		else
		{
#ifdef BREATHING_LIGHT
			BL_LED.Breathing_Light_On[LED_R] = 0;
			BL_LED.Breathing_Light_On[LED_G] = 0;
			BL_LED.BL_Timer[LED_R] = BL_DUTY_MAX;
			BL_LED.BL_Timer[LED_G] = BL_DUTY_MAX;
#endif
			LEDR_OFF();
			LEDG_OFF();

			Earphone_Chg_Flag = 0;
			CoverEvent_Flg_led = 0;
			Earphone_Chg_LED_On_Timer = 0;
		}
	}
	else
	{		
		if ( CHIP_STA4 & 0x80 )     								//充电中
		{	
			if ( Charger_Done ) 									// 充电完成，LED绿灯常亮
			{
				LEDG_ON();
			}
			else 
			{
				if( LED_On_Timer < LED_OFF_CNT ) 
				{
					LEDR_OFF();
					LEDG_OFF();
					
					LED_On_Timer++;
				}
				else			
				if( LED_On_Timer < LED_CHG_ON_CNT ) 				//红色LED亮20s后灭。
				{
					LED_On_Timer++;
					
					LEDR_ON();
				}
				else
				{
					LEDR_OFF();
				}
			}
			
			CoverEvent_Flg_led = 0;
			
			LED_On_Flag = 1;
		}
		else
		{				
			if( LED_On_Flag )
			{
				LEDR_OFF();
				LEDG_OFF();
				
				LED_On_Timer = 0;
				LED1_On_Timer = 0;
				LED_On_Flag = 0;
			}
			
			if ( CoverEvent_Flg_led )								//开关盖，低电橙灯亮5s，非低电绿灯亮5s。
			{			
				if( F_batlevel_low )				
				{								
					if( LED_On_Timer < LED_OFF_CNT ) 
					{
						LEDR_OFF();
						LEDG_OFF();
						
						LED_On_Timer++;
					}
					else
					if( LED_On_Timer < LED_ON_CNT ) 				//开关盖，盒子低电，红色LED亮5s后灭。
					{
						LED_On_Timer++;
						
						LEDR_ON();
					}
					else
					{	
						LEDR_OFF();
						
						CoverEvent_Flg_led = 0;
					}											
				}
				else
				{	
					LEDR_OFF();
					
					if( LED_On_Timer < LED_OFF_CNT ) 
					{
						LEDG_OFF();
						
						LED_On_Timer++;
					}
					else
					if( LED_On_Timer < LED_ON_CNT ) 				//开关盖，盒子非低电，绿色LED亮3s后灭。
					{
						LED_On_Timer++;
						
						LEDG_ON();
					}
					else
					{	
						LEDG_OFF();
						
						CoverEvent_Flg_led = 0;
					}
				}
			}
			else
			{
				if( Key_Press_ll_irq1 && ( CoverStatus == OPEN ) )							//开盖状态下按键长按5s，橙灯亮5s后，绿灯
				{								
					if( LED_On_Timer < LED_OFF_CNT ) 
					{
						LEDR_OFF();
						LEDG_OFF();
						
						LED_On_Timer++;
					}
					else
					if( LED_On_Timer < LED_ON_CNT ) 				//开关盖，盒子低电，红色LED亮5s后灭。
					{
						LED_On_Timer++;
						
						LEDR_ON();
					}
					else
					{	
						LEDR_OFF();
						if( LED1_On_Timer < LED_PAIR_CNT )
						{
							LED1_On_Timer++;
							if( (LED1_On_Timer 
% LED_PAIR_Interval) == 0 )
							{
								LEDG_ON();
							}
							else
							{
								LEDG_OFF();
							}					
						}
						else
						{
							LEDG_OFF();
							
							LED1_On_Timer = 0;
							Key_Press_ll_irq1 = 0;
						}
					}											
				}

			}
		}	
	}
}

#endif
/*****************************************************************************
* Function		: UserAPP
* Description	: 5ms调用周期 					
* Input			: None
* Output		: None
* Return		: None
* Note			: None
*****************************************************************************/
void UserAPP(void)
{
	if( F_sys_tim_100ms )
	{
		F_sys_tim_100ms = 0;
#if ADC_ENABLE
		Vbat_Adc = Vbat_Value();			//获取Bat电压	
#endif

#if HALL_ENABLE
		Hall_Handler(); 		//开关盖检测	
#endif

#if KEY_ENABLE
		Key_Handler();					//按键检测
#endif

#if DISCHARGE_ENABLE
		DisCharge_Handler();			//放电boost功能
#endif

#if CHARGER_ENABLE	
		Charger_Handler();			//充电功能
#endif	

#if VOX_ENABLE
		Vox_Det_Machine();			//开关盖盒子功能逻辑。
#endif	

#ifdef LED_DISPLAY	
		LED_Display_App();			//LED 灯效
#endif

#if BAT_VALUE
		check_bat_level();			//电量计
#endif	

	}
	
	if( F_sys_tim_1s )
	{
		F_sys_tim_1s = 0;

#if SLEEP_ENABLE	

		/*1、唤醒后工作Ns后进入Sleep Mode。
		  2、有VIN存在的情况下，系不会进入sleep mode。
		*/

        if( Decnt_SleepDelay > 0 )    /*能否在数字中实现此功能？*/
        {            							
            Decnt_SleepDelay--;

			/*倒数进Sleep Mode过程中出现了KEY\HALL\Loadon\IOFF中断，则重新计时。*/
			if( Enter_Sleep_Cnt_Restart_Flag )
			{
				Enter_Sleep_Cnt_Restart_Flag = 0;
				Decnt_SleepDelay = ENTER_STANDBYMODE_CNT;
			}
			
			return;
        }
			
		if( Check_Require_Sleep() )
		{			
			/* 唤醒中断使能设置 */		
			SFRADDR = WKUP_EN3;						//vox loadon wake up enable
			SFRDATA |= 0x3C;
			/* Enter Sleep */
			/* InSleep Handler */		//Disable All IRQ		
#if UART0_ENABLE			
			ES0 = 0;		//UART0 IRQ Disable
#endif
			//EX4 = 0;
			EX2 = 0;
			ET0 = 0;		//timer0 IRQ Disable
#if TIMER1_ENABLE			
			ET1 = 0;		//timer1 IRQ Disable
#endif			

			VOX_CTL0 &= ~0x30;    //VOX 5v Off 
			
#if DISCHARGE_ENABLE
			BST_EN = 0; 	 //Boost Disable   
#endif

#if ADC_ENABLE
			SFRADDR = ADC_CTL0; 
			SFRDATA &= ~0x80; 		//ADC Disable.	

			SFRADDR = ADCCS_CTL0;				//AD0~AD4 GPIO constant 20uA current source disable.
			SFRDATA = 0x00;
#endif	
			SFRADDR = PMU_CTL0;					//芯片硬件需求，固定
			SFRDATA &= ~0x30;

			PCON |= 0x02;						//Enter StandbyMode, Only int0 or int1 can wake up system
			
			/* OutSleep Handler */		//Enable IRQ
			
			SFRADDR = PMU_CTL0;   			//芯片硬件需求，固定
			SFRDATA |= 0x30;	

			SFRADDR = ADCCS_CTL0;				//AD0~AD4 GPIO constant 20uA current source enable.
			SFRDATA = 0x10;			
			
#if UART0_ENABLE			
			ES0 = 1;		//UART0 IRQ 
#endif
			//EX4 = 1;
			EX2 = 1;
			ET0 = 1;		//timer0 IRQ
#if TIMER1_ENABLE			
			ET1 = 1;		//timer1 IRQ
#endif	
			Decnt_SleepDelay = ENTER_STANDBYMODE_CNT;
		}
		else
		{
			Decnt_SleepDelay = ENTER_STANDBYMODE_CNT;
		}
#endif
	}
}

