/**************************** (C) COPYRIGHT 2015 Fortiortech shenzhen *****************************
* File Name          : IRScan.c
* Author             : Billy Long Fortiortech  Market Dept
* Version            : V1.0
* Date               : 01/07/2015
* Description        : This file contains main function used for Motor Control.
***************************************************************************************************
* All Rights Reserved
**************************************************************************************************/ 


/* Includes -------------------------------------------------------------------------------------*/
#include <FU68xx_2.h>
#include <Myproject.h>


/* Private typedef ------------------------------------------------------------------------------*/
/* Private define -------------------------------------------------------------------------------*/
/* Private macro --------------------------------------------------------------------------------*/
/* Private variables ----------------------------------------------------------------------------*/
IRScan_TypeDef xdata IRScan;
IRControl_TypeDef xdata IRControl;
IRControl_AutoPowerDef AutoPowerState;
uint16 PowerUpTimeCount=0;				//上电一分钟计时
uint8	NatureWinFlag = 0;
uint32  dimmer_cnt = 0;
int8  dimmer_level = 30;
uint8 natural_mode = 0;
uint32 dimmer_interval = 0;
const uint8 code NatureWinCode[9]={1,3,7,5,2,8,6,4,9};

#define BYTE0(dwTemp)       (*((char *)(&dwTemp)))
#define BYTE1(dwTemp)       (*((char *)(&dwTemp) + 1))
#define BYTE2(dwTemp)       (*((char *)(&dwTemp) + 2))
#define BYTE3(dwTemp)       (*((char *)(&dwTemp) + 3))

/* Private function prototypes ------------------------------------------------------------------*/
/* Private functions ----------------------------------------------------------------------------*/

/*-------------------------------------------------------------------------------------------------
	Function Name :	void IRInit(void)
	Description   :	
	Input         :	无
  Output				:	无
-------------------------------------------------------------------------------------------------*/
void IRInit(void)
{
	IRScan.BitValue = 0;
	IRScan.BitCnt = 0;
	IRScan.Bit0Cnt = 0;
	IRScan.ByteValue = 0;
	IRScan.UserCode = 0;
	IRScan.DataCode = 0;
	IRScan.IRReceiveFlag = 0;
	IRScan.CurrentUserCode =0;
	IRScan.OldDataCodeMixTimes = 0;
	IRScan.CurrentDataCode = 0;
	IRScan.OverFlowStatus=0;

	IRScan.TempDataCode[0]=0;
	IRScan.TempDataCode[1]=0;	
	IRScan.TempDataCode[2]=0;
	IRScan.TempDataCode[3]=0;		
	IRScan.DataCodeNum = 0;
	
	IRScan.DataCodeLengthCover = 1;
	IRScan.UserCodeLengthCover = 1;
	IRScan.DataCodeLengthCover = (IRScan.DataCodeLengthCover<<(DataCodeLength))-1;
	IRScan.UserCodeLengthCover = (IRScan.UserCodeLengthCover<<(UserCodeLength))-1;
	
	IRControl.FlagONOFF = 0;
	IRControl.FlagFR = 0;
	IRControl.FlagSpeed = 0;
	IRControl.TargetSpeed = 0;
	IRControl.ONOFFStatus =0;
	IRControl.FlagNatureWind =0;
	IRControl.FlagLED =0;
	IRControl.FlagAutoPower =0;
	IRControl.FlagUserCodeSave = 0;
	IRControl.FlagUserCodeRead = 1;				//上电读出一次UserCode
	IRControl.LEDONOFFStatus = 0;
	
	mcFRState.OldTargetSpeed = 1;
	mcFRState.FR = 0;
	mcFRState.FlagFR = 0;
	mcFRState.FRStatus = 0;
	
	AutoPowerState.FlagAutoPower=0;
	AutoPowerState.Timer10sec=0;
	AutoPowerState.ShutDowntime=2;
	AutoPowerState.CurrentTime=0;
	
	#if (Motor_Speed_Control_Mode == SPEED_LOOP_CONTROL)
	{
		IRControl.SpeedLevel[0] = MOTOR_SPEED_LEVEL_0;
		IRControl.SpeedLevel[1] = MOTOR_SPEED_LEVEL_1;
		IRControl.SpeedLevel[2] = MOTOR_SPEED_LEVEL_2;
		IRControl.SpeedLevel[3] = MOTOR_SPEED_LEVEL_3;
		IRControl.SpeedLevel[4] = MOTOR_SPEED_LEVEL_4;
		IRControl.SpeedLevel[5] = MOTOR_SPEED_LEVEL_5;
		IRControl.SpeedLevel[6] = MOTOR_SPEED_LEVEL_6;
        IRControl.SpeedLevel[7] = MOTOR_SPEED_LEVEL_7;
		IRControl.SpeedLevel[8] = MOTOR_SPEED_LEVEL_8;
		IRControl.SpeedLevel[9] = MOTOR_SPEED_LEVEL_9;
	}
	#elif (Motor_Speed_Control_Mode == POWER_LOOP_CONTROL)
	{ 
		IRControl.SpeedLevel[0] = MOTOR_POWER_LEVEL_0;
		IRControl.SpeedLevel[1] = MOTOR_POWER_LEVEL_1;
		IRControl.SpeedLevel[2] = MOTOR_POWER_LEVEL_2;
		IRControl.SpeedLevel[3] = MOTOR_POWER_LEVEL_3;
		IRControl.SpeedLevel[4] = MOTOR_POWER_LEVEL_4;
		IRControl.SpeedLevel[5] = MOTOR_POWER_LEVEL_5;
		IRControl.SpeedLevel[6] = MOTOR_POWER_LEVEL_6;
	}
	#endif
}


/*-------------------------------------------------------------------------------------------------
	Function Name :	void IRValue(void)
	Description   :	红外接收头解码
	Input         :	无
  Output				:	无
-------------------------------------------------------------------------------------------------*/

//void IRValue(void)
//{
//		uint8 tempof1=0;
//	  uint8 tempof2=0;
//	
//		IRScan.Bit0Cnt = TIM3_ARR;
//		if(IRScan.BitCnt==0)
//		{			
//			if((IRScan.Bit0Cnt<=TempBit1stLeadMax)&&(IRScan.Bit0Cnt>=TempBit1stLeadMin))
//			{
//				IRScan.BitCnt++;
//			}
//		}
//		else
//		{
//			if((IRScan.Bit0Cnt >= TempBit0CntMin)&&(IRScan.Bit0Cnt <= TempBit0CntMax))
//			{
//				IRScan.BitValue = 1;
//			}
//			else
//			{
//				IRScan.BitValue = 0;
//			}			
//			
//			if(IRScan.BitCnt<=16)
//			{
//				IRScan.ByteValue += (IRScan.BitValue << (16-IRScan.BitCnt));
//				IRScan.BitCnt ++;		
//				if(IRScan.BitCnt == 16)
//				{
//					IRScan.UserCode = IRScan.ByteValue;
//					IRScan.ByteValue = 0;
//				}
//			}
//			else
//			{
//				IRScan.ByteValue += (IRScan.BitValue << (16-(IRScan.BitCnt-16)));
//				IRScan.BitCnt ++;		
//				
//				if(IRScan.BitCnt == 32)
//				{
//					IRScan.DataCode = IRScan.ByteValue;
//					IRScan.ByteValue = 0;			
//					IRScan.BitCnt = 0;		
//					
//					tempof2 = (uint8)IRScan.DataCode;
//				  tempof1 = IRScan.DataCode>>8;
//					//tempof1 = 0xff -tempof1-1;
//					tempof1 = 0xff -tempof1;
//			
//					if((tempof1 == tempof2)&&(IRScan.UserCode==UserCodeDef))
//					{
//						IRScan.IRReceiveFlag = 1;
//					}
//				}
//			}		
//		
//		}
//}

/*-------------------------------------------------------------------------------------------------
	Function Name :	void SetSpeed(uint8 SpeedLevel)
	Description   :	速度设置
	Input         :	SpeedLevel：速度挡位
	Output		  :	无
-------------------------------------------------------------------------------------------------*/
void SetSpeed(uint8 SpeedLevel)
{
	IRControl.TargetSpeed = SpeedLevel;
	if(!mcSpeedRamp.FlagONOFF)
	{
//		IRControl.ONOFFStatus =1;
//		IRControl.FlagONOFF = 1;
		mcSpeedRamp.FlagONOFF = 1;
	}
	IRControl.FlagSpeed = 1;
	mcFRState.OldTargetSpeed = IRControl.TargetSpeed;
	//SetBuzzer(1,1);
}

/*-------------------------------------------------------------------------------------------------
	Function Name :	void SetAutoPower(uint16 Time)
	Description   :	定时设置
	Input         :	Time：定时时长
	Output		  :	无
-------------------------------------------------------------------------------------------------*/
void SetAutoPower(uint16 Time)
{
	IRControl.FlagAutoPower = 1;
	AutoPowerState.ShutDowntime = Time;
	//SetBuzzer(1,1);
}

/*-------------------------------------------------------------------------------------------------
	Function Name :	void RFValue(void)
	Description   :	RF接收头解码
	Input         :	无
  Output				:	无
-------------------------------------------------------------------------------------------------*/
void RFValue(void)
{
	static uint8 IRLastBitValue = 0;
    static uint8 UserSavedStatus = 0;
    
    static uint8 first_receive_falg = 0;
    static uint8 RF_cnt = 0;
    
    uint8 i = 0;
    
    IRScan.Bit0Cnt = TIM3__ARR-TIM3__DR; //获取低电平时长 （PWM周期 - 高电平时长）上升沿到上升沿
    if(IRScan.BitCnt==0)
    {			
        if((IRScan.Bit0Cnt <= TempLeadCodeMax)&&(IRScan.Bit0Cnt >= TempLeadCodeMin))//引导码
        {
            IRScan.BitCnt++;
        }
    }
    if(IRScan.BitCnt==1)
    {
        IRScan.Bit0Cnt = TIM3__DR;
        if((IRScan.Bit0Cnt >= TempBitValue1Min)&&(IRScan.Bit0Cnt <= TempBitValue1Max))
        {
          IRLastBitValue = 1;			//最后一位数据为1
        }
        else if((IRScan.Bit0Cnt >= TempBitValue0Min)&&(IRScan.Bit0Cnt <= TempBitValue0Max))
        {
          IRLastBitValue = 0;				//最后一位数据为0
        }	       
    }
	
	if(IRScan.BitCnt>=1)
	{
		IRScan.Bit0Cnt = TIM3__DR;
		if((IRScan.Bit0Cnt >= TempBitValue1Min)&&(IRScan.Bit0Cnt <= TempBitValue1Max))
		{
			//当前数据为1
			IRScan.BitValue = 1;
		}
		else if((IRScan.Bit0Cnt >= TempBitValue0Min)&&(IRScan.Bit0Cnt <= TempBitValue0Max))
		{
			//当前数据为0
			IRScan.BitValue = 0;
		}			
			
        if(IRScan.BitCnt<=AllCodeLength+2) //获取所有数据
        {
            IRScan.ByteValue += (IRScan.BitValue << (AllCodeLength-IRScan.BitCnt+2));
            
            IRScan.BitCnt ++;
            if(IRScan.BitCnt == AllCodeLength+2) //接收完成
            {
                IRScan.ByteValue += IRLastBitValue;
                IRScan.UserCode = (((IRScan.ByteValue << 6) & 0xFFFF0000) >> 16);
                IRScan.DataCode = ((IRScan.ByteValue << 6) & 0x0000FFC0);
                IRScan.ByteValue = 0;
//                uart_buf[0] = (IRScan.UserCode&0xFF00)>>8;
//                uart_buf[1] = (IRScan.UserCode&0x00FF);
//                uart_buf[2] = (IRScan.DataCode&0xFF00)>>8;
//                uart_buf[3] = (IRScan.DataCode&0x00FF);
//                for(i=0;i<4;i++)
//                {
//                    UT_DR = uart_buf[i];
//                    while(!TI);					//等待发送完成
//                    TI = 0;							//发送完成中断标志位清零	
//                }
                if(IRScan.UserCode!=IRScan.CurrentUserCode)//地址码不一样，对码
                {
                    if(!UserSavedStatus)
                    {
                        //地址码与当前地址不一致时执行读出地址码
                        IRControl.FlagUserCodeRead=1; 
                        IRScan.OverFlowStatus =1;
                    }
                }
                else //地址码一样 数据有效 包括地址码为0
                {
                    if(IRScan.CurrentDataCode!=IRScan.DataCode)
                    {
                        IRScan.CurrentDataCode=IRScan.DataCode;
                        IRScan.ValidDataCnt = 1;
                        IRScan.DiffFlag = 1;
                        //IRScan.IRReceiveFlag = 1;	
                    }
                    else
                    {
                        if(((IRDATAMASK&IRScan.DataCode) == IRDIMEERADD) || ((IRDATAMASK&IRScan.DataCode) == IRDIMEERSUB))//调光一直响应
                        {
                            if(dimmer_interval > 1000)
                            {
                                dimmer_interval = 0;
                                IRScan.IRReceiveFlag = 1;
                            }
                        }
                        else
                        {
                            if(IRScan.DiffFlag == 1)
                            {
                                IRScan.ValidDataCnt++;
                                if(IRScan.ValidDataCnt >= 2)
                                {
                                    IRScan.IRReceiveFlag = 1; 
                                    IRScan.ValidDataCnt =0;
                                    IRScan.DiffFlag = 0;
                                }
                            }
                        }
                    }
                }
                
                if((IRScan.DataCode == IRLEARN)&&(IRScan.OverFlowStatus==1))//接收到遥控学习指令
                {
                    IRScan.OldDataCodeMixTimes++;
                    if(IRScan.OldDataCodeMixTimes > 1)
                    {
                        IRScan.OverFlowStatus = 0;
                        if((!UserSavedStatus)&&(Time.PowerUpTimeCount<30000))//上电30秒内
                        {
                            SetBuzzer(5,3);
                            IRScan.CurrentUserCode = IRScan.UserCode;
                            IRControl.FlagUserCodeSave=1;	
                            BZScan.FlagBZ = 1;
                            BZScan.BZMultitimes=1;
                            UserSavedStatus = 1; 
                        }
                        IRScan.OldDataCodeMixTimes =0;            
                    }                    
                }
                else
                {
                    IRScan.OldDataCodeMixTimes =0;           
                }
                IRScan.BitCnt = 0;
            }
        } 
    }
}

/*-------------------------------------------------------------------------------------------------
	Function Name :	void IRScanControl(void)
	Description   :	遥控信号处理
	Input         :	无
	Output		  :	无
-------------------------------------------------------------------------------------------------*/
uint16 Temp_Dir = 0; 
uint16 Temp_Level = 0;
uint16 Temp_Data = 0;

//void IRScanControl(void)
//{
//	if(IRScan.IRReceiveFlag)
//	{
//		switch(IRScan.DataCode)
//		{
//			case IRONOFF:
//			{
//				if(!IRControl.FlagUserCodeSave)
//				{
////					if(IRControl.ONOFFStatus)
//					if(mcSpeedRamp.FlagONOFF == 1)
//					{
////						IRControl.ONOFFStatus = 0;
//						IRControl.TargetSpeed = 0;
//						mcSpeedRamp.FlagONOFF = 0;
//						
//						//在反转过程中关机时退出反转状态
//						mcFRState.FlagFR = 0;		
//						mcFRState.FRStatus = 0;
//						
//					}
//					else
//					{
//						//关机状态执行开机
////						IRControl.ONOFFStatus =1;
////						IRControl.FlagONOFF = 1;
//						
//						mcSpeedRamp.FlagONOFF = 1;
//						if(mcFRState.OldTargetSpeed == 0)
//						{
//							mcFRState.OldTargetSpeed = 1;
//						}
//						IRControl.TargetSpeed = mcFRState.OldTargetSpeed;
//					}
//					SetBuzzer(1,1);
//					NatureWinFlag =0;		
//					IRControl.FlagSpeed = 1;
//				}
//				break;
//			}
////			case IRFR:	
////			{
////				IRControl.FlagFR = 1;
////				SetBuzzer(1,1);
////				break;
////			}
//			case IRLED:	
//			{
//				IRControl.FlagLED=1;
//				SetBuzzer(1,1);	
//				break;
//			}
//			case IRSpeed1:	
//			{
//				SetSpeed(1);
//				NatureWinFlag =0;
//				break;
//			}
//			case IRSpeed2:	
//			{
//				SetSpeed(2);
//				NatureWinFlag =0;
//				break;
//			}
//			case IRSpeed3:		 
//			{
//				SetSpeed(3);
//				NatureWinFlag =0;
//				break;
//			}
//			case IRSpeed4:		 
//			{
//				SetSpeed(4);
//				NatureWinFlag =0;
//				break;
//			}
//			case IRSpeed5:		 
//			{
//				SetSpeed(5);
//				NatureWinFlag =0;
//				break;
//			}		
//			case IRSpeed6:		 
//			{
//				SetSpeed(6);
//				NatureWinFlag =0;
//				break;
//			}			 
////			case IRAUTOPOWER1H:	
////			{
////				SetAutoPower(360);		//1H
////				break;
////			}				
////			case IRAUTOPOWER3H:	
////			{
////				SetAutoPower(720);		//2H
////				break;
////			}				 
////			case IRAUTOPOWER6H:	
////			{
////				SetAutoPower(1440);		//4H
////				break;
////			}
////			case IRNatureWind:
////			{
////				 if(!IRControl.ONOFFStatus)
////				 {
////					IRControl.ONOFFStatus =1;
////					IRControl.FlagONOFF = 1;		
////				 }
////				 if(!NatureWinFlag)
////				 {
////					NatureWinFlag=1;
////					IRControl.FlagSpeed = 1;
////					IRControl.TargetSpeed = 1;
////				 }
////				 SetBuzzer(1,1);
////				break;
////			}
//			default:
//			break;		 
//		}
//		IRScan.IRReceiveFlag = 0;
//	}	
//}

void IRScanControl(void)
{
    uint16 temp_datacode = 0;
	if(IRScan.IRReceiveFlag)
	{
        SetBuzzer(1,1); 
        //temp_datacode = (IRDIRMASK&IRScan.DataCode);
        //if(Temp_Dir != temp_datacode)
        {
            //Temp_Dir = temp_datacode;
            switch(IRDIRMASK&IRScan.DataCode)//风向
            {
                case IRFRCW:
                    if(mcFRState.FR==CCW)
                        IRControl.FlagFR = 1;
                    break;
                case IRFRCCW:
                    if(mcFRState.FR==CW)
                        IRControl.FlagFR = 1;
                    break;
            }
        }
        
        temp_datacode = (IRLEVELMASK&IRScan.DataCode);
        if(Temp_Level != temp_datacode)
        {
            Temp_Level = temp_datacode;
            switch(IRScan.DataCode&IRLEVELMASK) //转速
            {
                case IRALLOFF://关机
                    if(!IRControl.FlagUserCodeSave)
                    {
                        if(mcSpeedRamp.FlagONOFF == 1)
                        {
    //						IRControl.ONOFFStatus = 0;
                            IRControl.TargetSpeed = 0;
                            mcSpeedRamp.FlagONOFF = 0;
                            
                            //在反转过程中关机时退出反转状态
                            mcFRState.FlagFR = 0;		
                            mcFRState.FRStatus = 0;
                            
                        }
                        NatureWinFlag =0;		
                        IRControl.FlagSpeed = 1;
                    }
                    break;
                case IRSpeed1: //风速1
                    SetSpeed(1);
                    NatureWinFlag =0;
                    break;
                case IRSpeed2://风速2
                    SetSpeed(2);
                    NatureWinFlag =0;
                    break;
                case IRSpeed3://风速3
                    SetSpeed(3);
                    NatureWinFlag =0;
                    break;
                case IRSpeed4://风速4
                    SetSpeed(4);
                    NatureWinFlag =0;
                    break;
                case IRSpeed5://风速5
                    SetSpeed(5);
                    NatureWinFlag =0;
                    break;
                case IRSpeed6://风速6
                    SetSpeed(6);
                    NatureWinFlag =0;
                    break;
                case IRSpeed7://风速7
                    SetSpeed(7);
                    NatureWinFlag =0;
                    break;
                case IRSpeed8://风速8
                    SetSpeed(8);
                    NatureWinFlag =0;
                    break;
                case IRSpeed9:
                    SetSpeed(9);//风速9
                    NatureWinFlag =0;
                    break;
                case IRNatureWind3://自然风3
                    natural_mode = 3;
                    if(!IRControl.ONOFFStatus)
                    {
                        IRControl.ONOFFStatus =1;
                        IRControl.FlagONOFF = 1;		
                    }
                    if(!NatureWinFlag)
                    {
                        NatureWinFlag=1;
                        IRControl.FlagSpeed = 1;
                        IRControl.TargetSpeed = 1;
                    }
                    break;
                case IRNatureWind6://自然风6
                    natural_mode = 6;
                    if(!IRControl.ONOFFStatus)
                    {
                        IRControl.ONOFFStatus =1;
                        IRControl.FlagONOFF = 1;		
                    }
                    if(!NatureWinFlag)
                    {
                        NatureWinFlag=1;
                        IRControl.FlagSpeed = 1;
                        IRControl.TargetSpeed = 1;
                    }
                    break;
                case IRNatureWind9://自然风9  
                    natural_mode = 9;
                    if(!IRControl.ONOFFStatus)
                    {
                        IRControl.ONOFFStatus =1;
                        IRControl.FlagONOFF = 1;		
                    }
                    if(!NatureWinFlag)
                    {
                        NatureWinFlag=1;
                        IRControl.FlagSpeed = 1;
                        IRControl.TargetSpeed = 1;
                    }
                    break;
                default:
                    break;
            }
        }
       
        //temp_datacode = (IRDATAMASK&IRScan.DataCode);
        //if(Temp_Data != temp_datacode)
        {
            //Temp_Data = temp_datacode;
            switch(IRDATAMASK&IRScan.DataCode)
            {
                case IRAUTOPOWER0H:
                    SetAutoPower(0);		//1H定时
                    break;
                case IRAUTOPOWER1H:
                    SetAutoPower(720);		//1H定时
                    break;
                case IRAUTOPOWER2H:
                    SetAutoPower(1440);		//2H定时
                    break;
                case IRAUTOPOWER4H:
                    SetAutoPower(2880);		//4H定时
                    break;
                case IRLEDDIM:              //调光开关
                    IRControl.FlagLED = 1;
                    break;
                case IRLED:                 //普通开关
                    IRControl.FlagLED = 1;
                    dimmer_level = 30;
                    break;
                case IRDIMEERADD:           //调光+
					if(dimmer_level == 1)
						dimmer_level = 40;

					if(dimmer_level == 30)
                        dimmer_level = 30;
					else
						dimmer_level--;
                    break;
                case IRDIMEERSUB:           //调光-
					if(dimmer_level == 39)
						dimmer_level = 0;
                    
					if(dimmer_level == 25)
						dimmer_level = 25;
					else
					    dimmer_level++;
                    break;
                default:
                    break;
            }
        }
        IRScan.DataCode = 0;
        IRScan.IRReceiveFlag = 0;
    }	
}
/*-------------------------------------------------------------------------------------------------
	Function Name : void AutoPowerControl(void)
	Description   :	自动控制开关机
	Input         :	无
  Output				:	无
-------------------------------------------------------------------------------------------------*/
void AutoPowerControl(void)
{
		if(IRControl.FlagAutoPower)
		{
//			if(AutoPowerState.FlagAutoPower)
//			{
//				AutoPowerState.FlagAutoPower=0;
//			}
//			else
//			{
//				AutoPowerState.FlagAutoPower=1;
//			}
            if(AutoPowerState.ShutDowntime > 0)
            {
                AutoPowerState.FlagAutoPower=1;
            }
            else
            {
                AutoPowerState.FlagAutoPower=0;
            }
			
			AutoPowerState.Timer10sec=0;			//每次按遥控重新计时
			AutoPowerState.CurrentTime=0;
			
			IRControl.FlagAutoPower=0;			
		}
	 //自动关机
	 if(AutoPowerState.FlagAutoPower&&IRControl.ONOFFStatus)
		{
			AutoPowerState.Timer10sec++;
			//if(AutoPowerState.Timer10sec>10000)
			if(AutoPowerState.Timer10sec>100)
			{
				AutoPowerState.Timer10sec=0;
				AutoPowerState.CurrentTime++;
				if(AutoPowerState.CurrentTime>=AutoPowerState.ShutDowntime)
				{
//						if(IRControl.ONOFFStatus)
//						{
							IRControl.ONOFFStatus = 0;
							IRControl.TargetSpeed = 0;
//						}
//						else
//						{
//							IRControl.ONOFFStatus =1;
//							PIControlTime = SPEED_LOOP_TIME;
//							IRControl.TargetSpeed = MOTOR_POWER_LEVEL_1;
//						}
					 IRControl.FlagONOFF = 1;			
					 SetBuzzer(1,1);
					 IRControl.FlagSpeed = 1;
					 AutoPowerState.CurrentTime=0;
					 AutoPowerState.FlagAutoPower = 0;
				}
			}
		}
		else
		{
			AutoPowerState.FlagAutoPower=0;
			AutoPowerState.Timer10sec=0;
			AutoPowerState.ShutDowntime=0;
			AutoPowerState.CurrentTime=0;
		}
}

/*-------------------------------------------------------------------------------------------------
	Function Name : uint8 GetUserCode(void)
	Description   :	LED显示，IO口电平变化实现
	Input         :	无
  Output				:	无
-------------------------------------------------------------------------------------------------*/
void LEDDisplay(void)
{
//	static uint8 LEDDiaplayStatus=0;
	
	if(IRControl.FlagLED)
	{
		if(IRControl.LEDONOFFStatus==0)
		{
			ResetLEDPin;
			IRControl.LEDONOFFStatus=1;
		}
		else 
		{
			SetLEDPin;
			IRControl.LEDONOFFStatus=0;
		}
		IRControl.FlagLED = 0;
	}
	
}

void NatureWind(void)
{
	static uint16 NatureWindCnt=0;
	static uint8  NatureWindStep=0;
	if((!NatureWinFlag)||(IRControl.FlagFR)||(!IRControl.ONOFFStatus)||(mcState!=mcRun))
	{
		NatureWindCnt = 0;
		NatureWindStep = 0;
		return;
	}
	else
	{
		NatureWindCnt++;
	}
	if(NatureWindCnt>8000)
	{
		NatureWindCnt = 0;
		if(++NatureWindStep>=natural_mode)	NatureWindStep=0;
		IRControl.TargetSpeed = NatureWinCode[NatureWindStep];
		IRControl.FlagSpeed = 1;
	}
}

void Dimmer_Control(void)
{
    if(IRScan.IRReceiveFlag == 0)
    {
        dimmer_interval++;
    }
    dimmer_cnt++;
    if(IRControl.LEDONOFFStatus==1)
    {
        if(dimmer_level == 30)
        {
            ResetLEDPin;
        }
        else if(dimmer_level == 25)
        {
            SetLEDPin;
        }
        else
        {
            if(dimmer_cnt == dimmer_level)
            {
                ResetLEDPin;
            }
            else if (dimmer_cnt == dimmer_level+1)
            {
                SetLEDPin;
            }   
            else if(dimmer_cnt == dimmer_level + 39)
            {
                ResetLEDPin;
            }
            else if (dimmer_cnt == dimmer_level+40)
            {
                SetLEDPin;
            } 
        }
    }
}