/*****************************************************************
;Project:  Light
;MCU:       
;Date:     
;File:      
;Function:  
******************************************************************/
#include "chargeLed_task.h"
#include "config.h"
#include "tick.h"
#include "ws2812.h"
#include "adc.h"
#include "detect_task.h"
/*---------------------------------------------------------------*/
//外部变量调用声明 
extern AD_Value_t AD_Value;
extern TaskFlagUnion_t tTaskFlag;
/*---------------------------------------------------------------*/
//充电显示LED任务
void chargeLed_Task(void)
{
	static unsigned char  charge_state=0;
	static unsigned short timer_charge = 0;   //设置指定的时间节点（相当于定时闹钟）
	static unsigned short delay_charge = 1;   //设置闹钟延时，ms
	static unsigned int   chargeColor = 0x000000;
	static unsigned char  remainBatteryLed=0;  //用adc采样转换成相应数量的亮灯
  static unsigned char  rotate_chargeLed=0;  //当前轮转到的led
		
	if(tTaskFlag.tFlag.chargeFlag == 1)
	{
		 if(Get_TaskInitFlag() == 1)
		 {
				WS_CloseAll();
				Set_TaskInitFlag(0);//--初始化一次就OK  
			  delay_charge = 600-(AD_Value.PowerScale*4);
				charge_state = 0;
        timer_charge = 0;
			  remainBatteryLed = AD_Value.PowerScale/10;
			  rotate_chargeLed = remainBatteryLed; 
				chargeColor = CHARGE_SAFECOLOR;	
		 }
		 
		 switch(charge_state)
		 {
			 case 0:
				      if(TimerCheck(timer_charge,delay_charge)) 
							{
                LedBuff_CleanAll();
                LedBuff_WriteNum(remainBatteryLed,chargeColor);    //给当前电量的ledBuff写入color
                LedBuff_WriteOrder(rotate_chargeLed ,chargeColor); //指定ledBuff写入color
                WS_Show();           //点灯
                rotate_chargeLed++;  //设置下一个要显示的灯					
								if(rotate_chargeLed >= 10) //转一圈后调整一次
								{
											chargeColor = CHARGE_SAFECOLOR; //充电颜色就显示绿色
											//--得到以10个灯等比显示当前电量的灯数
	                    //数据处理：将每10个划分一个等级，0~9->0,10~19->1,90~99->9,100->10
                      //remainBatteryLed = powerScale/10; 
											//数据处理：将每10个划分一个等级，0~9->0,10~19->1,90~99->9,97~100->10
											if(AD_Value.PowerScale >= 100)
													remainBatteryLed = 10;
											else
													remainBatteryLed = AD_Value.PowerScale/10;
											
											if(remainBatteryLed==0) //每转一圈调整一次轮转开始的灯序号
												 rotate_chargeLed = 0; 
											else
												rotate_chargeLed = remainBatteryLed-1; //轮转开始的灯序号
											
											delay_charge = 600-(AD_Value.PowerScale*4);//设置延时,随电压变化而变化
								}
							 charge_state= 1;
							}
				      break;
							
			 case 1:
				       TimerSet(timer_charge,delay_charge); //设置一个时间闹钟
               charge_state= 0;
				       break;
			 
			 default: charge_state = 0;break;
		 }
	}
}
