/*****************************************************************
;Project:  Light
;MCU:       
;Date:     
;File:      
;Function:  
******************************************************************/
#include "cyclingLed_task.h"
#include "config.h"
#include "tick.h"
#include "ws2812.h"
#include "detect_task.h"
/*---------------------------------------------------------------*/
//外部变量调用声明
extern TaskFlagUnion_t tTaskFlag;
/*---------------------------------------------------------------*/
//变量定义 
unsigned short cycSpeed = 0;  //骑行速度
unsigned char cycReSpeedFlag = 0;  //骑行速度刷新标志
/*---------------------------------------------------------------*/
//骑行LED旋转任务
void cyclingLed_Task(void)
{
	 static unsigned char  rotate_cycLed=0;  //当前轮转到的led
   static unsigned short cycDelay=100;   //设置灯的转圈速度
   static unsigned short timer_cyc = 0;   //设置指定的时间节点（相当于定时闹钟）
   static unsigned int   cycColor = 0x0000;
	 static unsigned short  timer_cycIntervalMax = 0;  //骑行数据接收间隔最长时间
		
	  if(tTaskFlag.tFlag.cycFlag == 1)
    { 
        if( Get_TaskInitFlag() == 1) 
        {
          WS_CloseAll();
          rotate_cycLed= 0;
          TimerSet(timer_cycIntervalMax,TIME_CYCBREAK_MAX);  
          TimerSet(timer_cyc,0);					
          Set_TaskInitFlag(0);  
        }
				
        //--每4秒刷新一次骑行数据，没有刷新就将速度置为0
        if(cycReSpeedFlag == 1)
            TimerSet(timer_cycIntervalMax,TIME_CYCBREAK_MAX);  //刷新定时器时间（串口更新数据了就会刷新）
        if(TimerCheck(timer_cycIntervalMax,TIME_CYCBREAK_MAX))
        {
           cycSpeed= 0;
           cycReSpeedFlag = 1;
           TimerSet(timer_cycIntervalMax,TIME_CYCBREAK_MAX);  //刷新定时器时间
        }
        				     
        //骑行速度改变			
        if(cycReSpeedFlag == 1)
        { 
            if( (cycSpeed <= 5) )
            {
                cycColor = 0xff0000;  //绿色
                cycDelay = 125;
            }
            else if( (cycSpeed > 5) && (cycSpeed <= 20) )
            {
                cycColor = 0xff0000;  //绿色
                cycDelay = 125;
            }
            else if( (cycSpeed > 20) && (cycSpeed <= 30) )
            {
                cycColor = 0xffff00;  //黄色出行
                cycDelay = 65;
            }
            else if( (cycSpeed > 30))
            {
                cycColor = 0x00ff00;  //红色
                cycDelay = 35;
            }
            else
            {
                cycColor = 0x000000;
                cycDelay = 20;
            }
            
            cycReSpeedFlag = 0;
        }
    
        //--轮流显示10个灯
        if (TimerCheck(timer_cyc,cycDelay))   //检查闹钟时间是否到达
        {
            LedBuff_CleanAll();//ledBuff全写低
            //---指定ledBuff写入color
            LedBuff_WriteOrder( rotate_cycLed ,cycColor);
            LedBuff_WriteOrder((rotate_cycLed+1)%10 ,cycColor);
            LedBuff_WriteOrder((rotate_cycLed+2)%10 ,cycColor);
            WS_Show();
            //--设置一个时间闹钟
            TimerSet(timer_cyc,cycDelay);
            //--设置下一个要显示的灯
            rotate_cycLed++;  
            if(rotate_cycLed>=LED_NUM)
                rotate_cycLed=0;
        }
    }  
}
