/*****************************************************************
;Project:  Light
;MCU:       
;Date:     
;File:      
;Function:  
******************************************************************/
#include "LowPow_Task.h"
#include "tick.h"
#include "config.h"
#include "ws2812.h"
#include "delay.h"
#include "key.h"
#include "gpio.h"
#include "wdt.h"
/*---------------------------------------------------------------*/
//外部变量调用声明
extern TaskFlagUnion_t tTaskFlag;
extern unsigned char BetteryConnectFlag;
extern uint8_t Adv_State;
/*---------------------------------------------------------------*/
//变量定义
unsigned int SleepCount = 0;
/*---------------------------------------------------------------*/
//函数声明
static void PowerDown(void);
/*---------------------------------------------------------------*/
//低功耗任务
void LowPow_Task(void)
{
  if(tTaskFlag.TaskFlagValue==0)
	{
		if((SleepCount >= SLEEPTIME)&&(Adv_State == 0))
		{
			 __disable_irq();
			 LedBuff_WriteNum(10,0x00ffff);  //粉色
       WS_Show_Disable_irq();
			 delay_ms(1000);
			 LedBuff_WriteNum(10,0x000000);
       WS_Show_Disable_irq();
       WS_Power(DISABLE); 			
			 __disable_irq();
			
			 wdt_clear();   //休眠前喂一次狗
			 wdt_disable(); //关闭看门狗
			
			 PowerDown();
			
			 SystemSleep();
			 WS_Power(ENABLE);
			 delay_ms(10);
			 __enable_irq();
			 wdt_enable(128*5); //换醒后使能看门狗
		}
	}
  else //有任务执行
	{
		SleepCount = 0;
	}
		
}

/*---------------------------------------------------------------*/
//浅度休眠换相配置
static void PowerDown(void)
{
	struct gap_wakeup_config pw_cfg;
		
	pw_cfg.wakeup_type = SLEEP_WAKEUP;
	pw_cfg.wdt_wakeup_en = (bool)false;
	pw_cfg.rtc_wakeup_en = (bool)false;
	pw_cfg.timer_wakeup_en = (bool)false;
	pw_cfg.gpi_wakeup_en = (bool)true;
	pw_cfg.gpi_wakeup_cfg =  U32BIT(SHOCK_PIN);	//中断唤醒pin
	WakeupConfig(&pw_cfg);
}
/*---------------------------------------------------------------*/

