/*****************************************************************
;Project:   Light
;MCU:       
;Date:     
;File:      
;Function:  
******************************************************************/
#include "detect_task.h"
#include "shockLed_task.h"
#include "chargeLed_task.h"
#include "tick.h"
#include "adc.h"
#include "TaskRun.h"
#include "config.h"
/*---------------------------------------------------------------*/
//变量定义
TaskSwitch_t tTaskSwitch;
static TaskSwitch_t Last_tTaskSwitch = TaskSwitch_None;  //初始化为没有任务执行
static unsigned char TaskInitFlag = 1;  //任务初始化标志位
/*---------------------------------------------------------------*/
//外部变量调用声明
extern TaskFlagUnion_t  tTaskFlag;
extern unsigned char VoltageStableFlag;  //电池电压稳定标志
/*---------------------------------------------------------------*/
//检测车子的状态 1: 充电状态  2：震动显示电量状态
static void DetectConnect_Task(void)
{
    //--如果在充电
    if(tTaskFlag.tFlag.chargeFlag == 1)
    {
        tTaskSwitch= TaskSwitch_Charge;
    }
    else //--如果没充电
    {
        tTaskSwitch = TaskSwitch_shock;
    } 
}
/*---------------------------------------------------------------*/
//任务切换
void Task_Switch(void)
{
	static unsigned short timer_Task = 0;
  static unsigned char Task_state = 0;
	
	switch(Task_state) 
	{
		case 0:
			
			ADC_Sample();              //1: 计算电池电量百分比 2: 检测是否在充电 
		  DetectConnect_Task(); 
			TimerSet(timer_Task, 20);  //设置20ms定时，每20ms侦测一次
		  if(VoltageStableFlag==1)
			{
				Task_state = 1;
			}
			break;
			
		case 1: 
			  if(TimerCheck(timer_Task,20)) 
        {
					if(tTaskSwitch != Last_tTaskSwitch ) //判断前后两次任务是否一致
					{
						 tTaskFlag.TaskFlagValue &= 0x01;  //清空标志位
						
						 if(tTaskSwitch == TaskSwitch_shock)
						 {
							TaskRunProcess = (*shockLed_Task);
							tTaskFlag.tFlag.shockFlag = 1; 
							VoltageStableFlag = 0;
						 }
						 else
						 {
               if(tTaskSwitch == TaskSwitch_Charge)
               {
                 TaskRunProcess = (*chargeLed_Task);
                 tTaskFlag.tFlag.chargeFlag = 1; 
               }
						 }					 
						 Last_tTaskSwitch = tTaskSwitch;
						 Set_TaskInitFlag(1);  //说明有任务切换			 
					}
					
					Task_state = 0;
        }
        break;
					
		default: 
			Task_state = 0; 
		  break;
	}
	
}
/*---------------------------------------------------------------*/
//--获取任务初始化状态
unsigned char Get_TaskInitFlag(void)
{
  return TaskInitFlag;
}
/*---------------------------------------------------------------*/
//--设置任务初始化状态
void Set_TaskInitFlag(unsigned char Flag)
{
  TaskInitFlag = Flag;
}
/*---------------------------------------------------------------*/
