#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "control_comm.h"
#include "CC2530_transparent_analyze.h"
#include "hardware_init.h"
#include "Key.h"
#include "BL55072A.h"
#include "SHTC3.h"
#include "display_common.h"
#include "VCNL3040.h"
#include "mfg_factory.h"

u16    wind_open_time    =  330 ;    //3ms

u16   temp_open_time     = 200  ;   //4ms

u8   wind_close_time    = 250  ;   //4ms

u16   temp_close_time   = 250 ;    //5ms

#define   data_number   10

////////////////////////非透传协议四个网络状态/////////////////////////////


#define PERSON_TRAGGER_MIN_RANGE     	20



u8  zero_num = 0;
u8  Level_H = 0;
u8  Level_L = 0;
u8  check_zero_flage = 0;


u16 guDelay_ms = NULL;

							
u8  network_state  = OUT_LINE;

u8  send_data_seq = 0;

u8 Device_status[25]=  { 
						16,			 //len
                        0X00,		 //错误码  				0	
                        0X00,		//开关状态          		1
						0x00,		//童锁状态				2		
                        0X03,		//运行模式 				3
                        0X00,		//设定温度高字节			4
                        0X1A,		//设定温度低字节			5
                        0X00,		//环境温度高字节			6
                        0X1A,		//环境温度低字节			7
                        0X01,		//当前风速				8
						0x01,		//摆风模式				9
                        0X14,	    //定时关机时间  			10
						0x0F,		 //滤网清洗时间高字节  	11
						0XA0,		 //滤网清洗时间低字节  	12
						0x00,		 //屏保时间				13
						0x00,		//校准温度				14
						0x00,		//勿扰模式				15		
	                  }; 

TIMEING_OPTION  _Time_Power_Off = {0,0,0,0};
#define sBacklight_INIT   	{ 1,0,0}
BACKLIGHT_CONTROL sBacklight = sBacklight_INIT; 
ZIGBEE_UART_CONTROL sUartDetect = {1,0,0,0};

//STRAINER_OPTION Strainer_Net_Used_Time = {0,0,0,0};		//产测时间，状态保存

#if 1 // add by dingmz_frc.20201215
u8 g_u8Temp_Buffer[40] = { 0 };
u8 g_u8Temp_Length = 0;
#endif

u32 Strainer_Net_Used_Time = 0;
u16 Board_test_time = 0;

struct device_t device;													 
													 
int temp_average[data_number] = {0};													 												 
//static TP_u16 serial_num[7] = {0};
																																																 																															
extern u8 net_leave_flage; 


extern  Lcd_Status_Info_t  LCDShowTemp_state_t;	

static void delay_300ms_report_start(void);
static void delay_300ms_report_stop(void);
///**********************************************
//	上电默认设备类型0（冷热分管）
//**********************************************/
//void  Tap_Init(void)
//{
//    save_tap_status[0]  = 0;
//    save_tap_status[1]  = 0;
//    save_tap_status[2]  = 0;
//    save_tap_status[3]  = 1;
//    save_tap_status[4]  = 0;
//    save_tap_status[5]  = 0;
//    
//    switch(Oled_Display_Info.system_type)
//    {
//        case HOT_AND_COLD_APART:   //冷热分管
//        {
//            if (Oled_Display_Info.real_temperature >= 280)
//            {
//                Oled_Display_Info.operation_mode = 1;   //冷
//                save_tap_status[0]   = 1;
//            }
//            else if (Oled_Display_Info.real_temperature <=  200)
//            {		
//                Oled_Display_Info.operation_mode = 2;	 //热
//                save_tap_status[1]   = 1;			 
//            }
//            else 
//            {
//                Oled_Display_Info.operation_mode = 3;	 //通风
//            }										 
//            break;							 							 
//        }
//        case ONLY_COLD:    //单冷     //无热阀，无制热模式   冷阀根据温度调节开关
//        {
//            
//            if (Oled_Display_Info.real_temperature >= 280)
//            {
//                Oled_Display_Info.operation_mode = 1;   //冷
//                save_tap_status[0]   = 1;
//            }
//            else 
//            {
//                Oled_Display_Info.operation_mode = 3;	 //通风
//            }	
//            break;							 							 
//        }
//        case ONLY_HOT:  //单热
//        {
//            if (Oled_Display_Info.real_temperature <=  200)
//            {		
//                Oled_Display_Info.operation_mode = 2;	 //热
//                save_tap_status[1]   = 1;			 
//            }
//            else 
//            {
//                Oled_Display_Info.operation_mode = 3;	 //通风
//            }
//            break;							 							 
//        }	
//        case HOT_AND_COLD_SHARED:  //冷热共管
//        {
//            if (Oled_Display_Info.real_temperature >= 280)
//            {
//                Oled_Display_Info.operation_mode = 1;   //冷
//                save_tap_status[0]   = 1;
//            }
//            else if (Oled_Display_Info.real_temperature <=  200)
//            {		
//                Oled_Display_Info.operation_mode = 2;	 //热
//                save_tap_status[0]   = 1;			 
//            }
//            else 
//            {
//                Oled_Display_Info.operation_mode = 3;	 //通风
//            }            
//	
//            break;							 							 
//        }								 
//    }	    
//    
//    
//}
void Display_Status(void);
/***********************************************************************
*   重要任务，不停的轮询
***********************************************************************/	
void main_task(void)
{
	key_scan();
	transparent_msg_deal();
	LcdShowRunningAll();
	uart_msg_deal();						//产测数据解析
	kLedModuleActionDetectCallback();		//zigbee指示灯操作
	Display_Status();
	
	#ifndef DEVICE_TYPE_AIR_CONDITION
	Tap_Switch();
	#endif
}

static void Set_Error_Code(u8 error_code)
{
	if(Oled_Display_Info.screen_page == 1){
		Oled_Display_Info.Air_Device.error = error_code;		//传感器故障
		if( Get_Display_Status())
		{
			Display_Up_Contend(AIR_CONDITION);
		} 
	}
	else if(Oled_Display_Info.screen_page == 2){
		Oled_Display_Info.FLOOR_Device.error = error_code;
		if(Get_Display_Status())
		{
			Display_Up_Contend(FLOOR_HEATING);
		} 
	}
	else if(Oled_Display_Info.screen_page == 3){
		Oled_Display_Info.FAN_Device.error = error_code;		
		if( Get_Display_Status())
		{
			Display_Up_Contend(FAN);
		} 
	}
}
/***********************************************************************
*   函 数 名: record_dev_used_time  温度初始化
***********************************************************************/
void temp_init(void)
{
u8 i = 0;	
    s16 tmep_data;
    int temp_new_data = 0;
	SHTC3_GetTemp(&tmep_data);
	if( (tmep_data >1000)||(tmep_data <- 100))               //温度传感器错误
    {
		Set_Error_Code(1);
    }
    else
    {
        temp_new_data = tmep_data;

        for (i = 0;i < data_number;i ++)
        {
            temp_average[i] = temp_new_data;
        }
        
        temp_new_data = (temp_new_data / 5) * 5; 
		
		if(Oled_Display_Info.screen_page == 1){		
			temp_new_data  = temp_new_data + Oled_Display_Info.Air_Device.correct_temperature;
		}
		else if(Oled_Display_Info.screen_page == 2){
			temp_new_data  = temp_new_data + Oled_Display_Info.FLOOR_Device.correct_temperature;
		}
		else if(Oled_Display_Info.screen_page == 3){
			temp_new_data  = temp_new_data + Oled_Display_Info.FAN_Device.correct_temperature;
		}
        
        if (temp_new_data <= -100)
        {
            temp_new_data = -95;       
        }
        else if (temp_new_data >= 1000)
        {        
            temp_new_data = 995;      
        }    
		if(Oled_Display_Info.screen_page == 1){		
			Oled_Display_Info.Air_Device.real_temperature = temp_new_data;
		}
		else if(Oled_Display_Info.screen_page == 2){
			Oled_Display_Info.FLOOR_Device.real_temperature = temp_new_data;
		}
		else if(Oled_Display_Info.screen_page == 3){
			Oled_Display_Info.FAN_Device.real_temperature = temp_new_data;
		}
	}		
}
///***********************************************************************
//*   函 数 名: record_dev_used_time  温度读取
//***********************************************************************/
void  shtc3_sensor_measure_loop(void)
{  
    u8 i = 0;	
    s16 tmep_data;
	static u16  abnormal_times = 0;
    int temp_new_data = 0;
	SHTC3_GetTemp(&tmep_data);
	if( (tmep_data >1000)||(tmep_data <- 100))               //温度传感器错误
    {
		if(abnormal_times < 0xfe)
			abnormal_times++;
		else
			abnormal_times = 3;
		if(abnormal_times >=3 )
			Set_Error_Code(1);									//温度传感器错误
		
    }
    else
    {
		if((Oled_Display_Info.screen_page == 1)&&(Oled_Display_Info.Air_Device.error == 1))		//传感器故障
		{
			Oled_Display_Info.Air_Device.error = 0;
			if( Get_Display_Status())
			{
				LCDShowTemp_state_t.LCD_place = DELAY_OFF_TIME_DISPLAY;	
				LCDShowTemp_state_t.LCD_display_count = 0;				
				LCDShowTemp_state_t.LCD_state = OFF;
				LcdShowCTR(LCDShowTemp_state_t);
			}
			state_change_report(OPCODE_GENERAL_REPORT_STATUS,1);
		}
		
        temp_new_data = tmep_data;

        for (i = 0;i < data_number - 1;i ++)
        {
            temp_average[i] = temp_average[i + 1];
        }
        temp_average[data_number - 1] = temp_new_data;
        temp_new_data = 0;

        for (i = 0;i < data_number;i ++)
        {
            temp_new_data  += temp_average[i];
        }
        
        temp_new_data = (( temp_new_data / data_number ) / 5) * 5; 
		
		if(Oled_Display_Info.screen_page == 1){		
			temp_new_data  = temp_new_data + Oled_Display_Info.Air_Device.correct_temperature;
		}
		else if(Oled_Display_Info.screen_page == 2){
			temp_new_data  = temp_new_data + Oled_Display_Info.FLOOR_Device.correct_temperature;
		}
		else if(Oled_Display_Info.screen_page == 3){
			temp_new_data  = temp_new_data + Oled_Display_Info.FAN_Device.correct_temperature;
		}
        
        if (temp_new_data <= -100)
        {
            temp_new_data = -95;       
        }
        else if (temp_new_data >= 1000)
        {        
            temp_new_data = 995;      
        }   
//		printf("measure temp data = %d\r\n",temp_new_data);		
        if ( (ABS(Oled_Display_Info.Air_Device.real_temperature, temp_new_data)>10) 
			 &&(Oled_Display_Info.screen_page == 1) )	{
				 
            Oled_Display_Info.Air_Device.real_temperature = temp_new_data;
            state_change_report(OPCODE_GENERAL_REPORT_STATUS,1);// 上报状态					
        }	
		else if ( (ABS(Oled_Display_Info.FLOOR_Device.real_temperature, temp_new_data)>10) 
			 &&(Oled_Display_Info.screen_page == 2) )	{
				 
			 Oled_Display_Info.FLOOR_Device.real_temperature = temp_new_data;
            state_change_report(OPCODE_GENERAL_REPORT_STATUS,2);// 上报状态	
		}	
		else if ( (ABS(Oled_Display_Info.FAN_Device.real_temperature, temp_new_data)>10) 
			 &&(Oled_Display_Info.screen_page == 3) )	{
				 
			 Oled_Display_Info.FAN_Device.real_temperature = temp_new_data;
            state_change_report(OPCODE_GENERAL_REPORT_STATUS,3);// 上报状态	
		}			 
    }
}
static u8 Save_Data(u8 device_type,u8 index,u32 *write_data)
{
	if(device_type == AIR_CONDITION)
	{
		u8 temp = 0;
		if(index == 0)
		{
			*write_data = 0;
			*write_data = (*write_data | Oled_Display_Info.aging_test_flag) << 8;	 	//保存老化标志位
			
			*write_data = (*write_data | Oled_Display_Info.binding_device) << 8; 		//保存绑定信息

            if (Oled_Display_Info.Air_Device.correct_temperature < 0)           	  	//保存温度校准值
            {
                temp = 256 + Oled_Display_Info.Air_Device.correct_temperature;
                *write_data = (*write_data | temp) ;
            }
            else
            {
                *write_data = (*write_data | Oled_Display_Info.Air_Device.correct_temperature);	
            }
			*write_data = *write_data | SAVE_FLAGE;
			
		}
		else if(index == 1)
		{
			*write_data = 0;
			*write_data = (*write_data | Oled_Display_Info.Air_Device.screen_save_time) << 8;	//保存屏保时间
			
			temp = (u8)(Oled_Display_Info.Air_Device.filter_wash_time >>8 );
			*write_data = (*write_data | temp) << 8;											//保存滤网时间高字节
			
			temp = (u8)(Oled_Display_Info.Air_Device.filter_wash_time&0xff );
			*write_data = (*write_data | temp) ;												//保存滤网时间低字节
			
			*write_data = *write_data | SAVE_FLAGE;
		
		}
		else if(index == 2)
		{
			*write_data = 0;
			*write_data = (*write_data | Oled_Display_Info.Air_Device.lock_flage) <<8;		//保存童锁	
			
			*write_data = (*write_data | Oled_Display_Info.Air_Device.no_disturb_mode)<<8;	//保存勿扰模式
			
			*write_data = (*write_data | Oled_Display_Info.Air_Device.operation_mode);		//风机盘管模式
			
			*write_data = *write_data | SAVE_FLAGE;
			
		}
		else if(index == 3)
		{
			*write_data = 0;
			*write_data = (*write_data | Oled_Display_Info.is_dispaly_cur_temp);		//保存是否显示温度	
			
			*write_data = *write_data | SAVE_FLAGE;
			return 1;
		}
	}
	else if(device_type == FLOOR_HEATING)
	{
		u8 temp = 0;
		if(index == 0)
		{
			*write_data = 0;
			*write_data = (*write_data | Oled_Display_Info.aging_test_flag) << 8;	 		//保存老化标志位
			
			*write_data = (*write_data | Oled_Display_Info.binding_device) << 8; 		//保存绑定信息

            if (Oled_Display_Info.FLOOR_Device.correct_temperature < 0)           	  					//保存温度校准值
            {
                temp = 256 + Oled_Display_Info.FLOOR_Device.correct_temperature;
                *write_data = (*write_data | temp);
            }
            else
            {
                *write_data = (*write_data | Oled_Display_Info.FLOOR_Device.correct_temperature) ;	
            }
			
			*write_data = *write_data | SAVE_FLAGE;
		}
		else if(index == 1)
		{
			*write_data = 0;
			*write_data = (*write_data | Oled_Display_Info.FLOOR_Device.lock_flage)<<8;		//保存童锁	
			
			*write_data = (*write_data | Oled_Display_Info.FLOOR_Device.screen_save_time) << 8;	//保存屏保时间
			
			*write_data = (*write_data | Oled_Display_Info.FLOOR_Device.no_disturb_mode);	//保存勿扰模式
			
			*write_data = *write_data | SAVE_FLAGE;
		}
		else if(index == 2)
		{
			*write_data = 0;
			*write_data = (*write_data | Oled_Display_Info.is_dispaly_cur_temp);		//保存是否显示温度	
			
			*write_data = *write_data | SAVE_FLAGE;
			return 1;
		}
	
	}
	else if(device_type == FAN)
	{
		u8 temp = 0;
		if(index == 0)
		{
			*write_data = 0;
			*write_data = (*write_data | Oled_Display_Info.aging_test_flag) << 8;	 		//保存老化标志位
			
			*write_data = (*write_data | Oled_Display_Info.binding_device) << 8; 		//保存绑定信息

            if (Oled_Display_Info.FAN_Device.correct_temperature < 0)           	  	//保存温度校准值
            {
                temp = 256 + Oled_Display_Info.FAN_Device.correct_temperature;
                *write_data = (*write_data | temp);
            }
            else
            {
                *write_data = (*write_data | Oled_Display_Info.FAN_Device.correct_temperature);	
            }
			*write_data = *write_data | SAVE_FLAGE;
			
		}
		else if(index == 1)
		{
			*write_data = 0;
			*write_data = (*write_data | Oled_Display_Info.FAN_Device.screen_save_time) << 8;	//保存屏保时间
			
			temp = (u8)(Oled_Display_Info.FAN_Device.filter_wash_time >>8 );
			*write_data = (*write_data | temp) << 8;											//保存滤网时间高字节
			
			temp = (u8)(Oled_Display_Info.FAN_Device.filter_wash_time&0xff );
			*write_data = (*write_data | temp);											//保存滤网时间低字节
			
			*write_data = *write_data | SAVE_FLAGE;
		}
		else if(index == 2)
		{
			*write_data = 0;
			*write_data = (*write_data | Oled_Display_Info.FAN_Device.lock_flage<<8);			//保存童锁	
			
			*write_data = (*write_data | Oled_Display_Info.FAN_Device.no_disturb_mode)<<8;		//保存勿扰模式
			
			*write_data = (*write_data | Oled_Display_Info.is_dispaly_cur_temp);		//保存是否显示温度	
			
			*write_data = *write_data | SAVE_FLAGE;
			return 1;
		}
	
	}
	return 0;
}
static void Load_Data(u8 device_type,u8 index,u32 load_data)
{
	if(device_type == AIR_CONDITION)
	{
		u8 temp = 0;
		if(index == 0)
		{
			temp = load_data&0xff;
			if ((temp &0X80) == 0X80)   //负数
			{
				Oled_Display_Info.Air_Device.correct_temperature = temp - 256;
			}
			else if ((temp &0X80) ==0X00) //正数
			{
				Oled_Display_Info.Air_Device.correct_temperature = temp ;
			} 
			Oled_Display_Info.binding_device = (load_data>>8)&0xff;
											
			Oled_Display_Info.aging_test_flag     = (load_data>>16)&0xff;
			
		}
		else if(index == 1)
		{
			Oled_Display_Info.Air_Device.filter_wash_time = (u16)load_data;
			
			Oled_Display_Info.Air_Device.screen_save_time = (load_data>>16)&0xff;		
		}
		else if(index == 2)
		{
			Oled_Display_Info.Air_Device.operation_mode = load_data&0xff;
			
			Oled_Display_Info.Air_Device.no_disturb_mode = (load_data>>8)&0xff;
			
			Oled_Display_Info.Air_Device.lock_flage = (load_data>>16)&0xff;
			
		}
		else if(index == 3)
		{
			Oled_Display_Info.is_dispaly_cur_temp = load_data&0xff;
		}
	}
	else if(device_type == FLOOR_HEATING)
	{
		u8 temp = 0;
		if(index == 0)
		{
			temp = load_data&0xff;
			if ((temp &0X80) == 0X80)   //负数
			{
				Oled_Display_Info.FLOOR_Device.correct_temperature = temp - 256;
			}
			else if ((temp &0X80) ==0X00) //正数
			{
				Oled_Display_Info.FLOOR_Device.correct_temperature = temp ;
			} 
			Oled_Display_Info.binding_device = (load_data>>8)&0xff;
											
			Oled_Display_Info.aging_test_flag     = (load_data>>16)&0xff;
		}
		else if(index == 1)
		{
			Oled_Display_Info.FLOOR_Device.no_disturb_mode 	= load_data&0xff;
			
			Oled_Display_Info.FLOOR_Device.screen_save_time 	= (load_data>>8)&0xff;	

			Oled_Display_Info.FLOOR_Device.lock_flage 		= (load_data>>16)&0xff;
		}
		else if(index == 2)
		{
			Oled_Display_Info.is_dispaly_cur_temp = load_data&0xff;
		}
	}
	else if(device_type == FAN)
	{
		u8 temp = 0;
		if(index == 0)
		{
			temp = load_data&0xff;
			if ((temp &0X80) == 0X80)   //负数
			{
				Oled_Display_Info.FAN_Device.correct_temperature = temp - 256;
			}
			else if ((temp &0X80) ==0X00) //正数
			{
				Oled_Display_Info.FAN_Device.correct_temperature = temp ;
			} 
			Oled_Display_Info.binding_device = (load_data>>8)&0xff;
											
			Oled_Display_Info.aging_test_flag     = (load_data>>16)&0xff;
		}
		else if(index == 1)
		{
			Oled_Display_Info.FAN_Device.filter_wash_time = (u16)load_data;
			
			Oled_Display_Info.FAN_Device.screen_save_time = (load_data>>16)&0xff;		
		}
		else if(index == 2)
		{
			Oled_Display_Info.is_dispaly_cur_temp = load_data&0xff;
			
			Oled_Display_Info.FAN_Device.no_disturb_mode = (load_data>>8)&0xff;
			
			Oled_Display_Info.FAN_Device.lock_flage = (load_data>>16)&0xff;
		}
	}
}

///***********************************************************************
//*   函 数 名: save_data
//*   功能说明: 保存温度校准值
//*   形     参：无
//*   返 回 值: 无
//***********************************************************************/
 void save_data(void)
{ 
    u8  i = 0;
    u32 save_flage = 0;
	u32 flash_addr;
    SYS_UnlockReg();

	flash_addr = Flash_Base_Addr;     //0x00007000
	FMC_Erase(flash_addr);				     //清除	
	
    for ( i = 0;i < 128 ; i++ )
    {		
        save_flage  = FMC_Read(flash_addr);

        if (save_flage == NO_FMC_WRITE)                           //未写数据
        {					
            save_flage = 0;								
            if(!Save_Data(Oled_Display_Info.device_type,i,&save_flage))
				FMC_Write(flash_addr,save_flage);
			else
			{
				FMC_Write(flash_addr,save_flage);				//写入完成
				break;
			}  
        }
        else                                       //已经被写过
        {
            if ( flash_addr == SAVE_INDEX_ADDR )  //全部被写满
            {
               break;
			}
                      							
        }
		flash_addr = flash_addr + OFFSET_ADDR;    

    }
    SYS_LockReg();   
}


///***********************************************************************
//*   函 数 名: recover_current_state
//*   功能说明: 读取掉电前保存的数值
//*   形     参：无
//*   返 回 值: 无
//***********************************************************************/
void recover_current_state(void)
{   
    u8 i = 0;
    u32  save_flage = 0;
	u32  flash_addr = 0;
    SYS_UnlockReg();

    flash_addr = Flash_Base_Addr;    //从高地址查起    0x000071F8     //504	

    for ( i = 0;i < 128 ; i++ )
    {   			  
        save_flage  = FMC_Read(flash_addr);
        if (save_flage != NO_FMC_WRITE)    //
        {				
            if ((save_flage  & 0xFF000000) == SAVE_FLAGE	)	
            {
               Load_Data(Oled_Display_Info.device_type,i,save_flage);
			   //printf("##recover_current_state,index = %d,read data = 0x%4x\r\n",i,save_flage);
            }
			else
				break;
			flash_addr = flash_addr + OFFSET_ADDR;				
        }
        else 
			break;					
    }					

    SYS_LockReg();
}
void ReDefine_AIR_Condition_Syetem_Mode(void)
{
	s16  temp ; 
	SHTC3_GetTemp(&temp);
	if(temp > 280)
		Oled_Display_Info.Air_Device.system_type = SYSTEM_MODE_COOLING;
	else if(temp <200)
		Oled_Display_Info.Air_Device.system_type = SYSTEM_MODE_HEATING;
	else
		Oled_Display_Info.Air_Device.system_type = SYSTEM_MODE_AIR;
}
/***********************************************************************
*   函 数 名: record_dev_used_time
*   功能说明: 实时记录老化使用时间
*   形     参：无
*   返 回 值: 无
***********************************************************************/
static void sensor_measure_loop(void)
{
	static u16 CurrentValue 	= 0;
	static u16 LastValue 		= 0 ;
	static u8 board_test_flag   = 0;
	static u8 report_times		= 0;
	extern u8 First_Exit_Factory_Mode	;
	CurrentValue = VCNL3040_Get_Current_Data();
	if( (CurrentValue  > LastValue)&&( (CurrentValue - LastValue) >= PERSON_TRAGGER_MIN_RANGE ))
	{
		board_test_flag = Oled_Display_Info.recv_lcd_off &0x80;
		report_times = Oled_Display_Info.recv_lcd_off&0x0f;
		Reload_Save_Time();
		if((First_Exit_Factory_Mode == 0)&&(board_test_flag == 0))
			Detect_Human_Dispaly();
		if(report_times >0)
		{
			delay_300ms_report_start();					//延时上报接近感应任务开启
			if(board_test_flag)
				BL55072A_DisplayALL(); 
			else
				Oled_Display_Info.recv_lcd_off--;
		}
	}
	LastValue = CurrentValue;
}

/***********************************************************************
*   函 数 名: record_dev_used_time
*   功能说明: 实时记录老化使用时间
*   形     参：无
*   返 回 值: 无
***********************************************************************/
static void record_dev_used_time(void)
{
}
/***********************************************************************
*   函 数 名: spake_control_loop
*   功能说明: 蜂鸣器控制
*   形     参：无
*   返 回 值: 无
***********************************************************************/
#define   Spake_Continue_Time_ms		100
#define   Spake_Interval_Time_ms		300
static    u8 		Spake_Flag = 0;
static    u8 		LED_FLASH_Flag = 0;
static void spake_control_loop(void)
{
}
static void Spake_Control(void)
{
	static u16 i = 0;
	if(Spake_Flag)
	{	
		i++;
		if(Oled_Display_Info.net_work_flage == IN_LINE)
		{
			if(i == 1)
			{
				PWM_ConfigOutputChannel(PWM, 0, 1000, 70);
				//printf("##111spake_control_loop i = %d,ON\n",i);
			}
			if(i == Spake_Continue_Time_ms)
			{
				PWM_ConfigOutputChannel(PWM, 0, 1000, 0);
				i = 0;
				Spake_Flag = 0;
			}
		}
		else{
			if((i == 1)||(i == (Spake_Continue_Time_ms+Spake_Interval_Time_ms) ))
			{
				PWM_ConfigOutputChannel(PWM, 0, 1000, 70);
			}
			if((i == (2*Spake_Continue_Time_ms+Spake_Interval_Time_ms) ) ||( i == Spake_Continue_Time_ms) )
			{
				PWM_ConfigOutputChannel(PWM, 0, 1000, 0);
				if(i == 2*Spake_Continue_Time_ms+Spake_Interval_Time_ms)
				{
					i = 0;
					Spake_Flag = 0;
				}
				
			}
		}
	}
}
static void LED_Flash_Control(void)
{
	static u16 i = 0;
	if(LED_FLASH_Flag)
	{	
		i++;
		if((i == 1)||(i == 400)||(i == 800))
		{
			LED3 = 0;
		}
		if((i == 200)||(i == 600)||(i == 1000))
		{
			LED3 = 1;
			if(i == 1000)
			{
				i = 0;
				LED_FLASH_Flag = 0;
			}
		}
	}
}
void ChildLockLedFlash(void)
{
	LED_FLASH_Flag = 1;
}
extern void delay_confirm_system_mode(void);
void delay_confirm_system_control(u8 start)
{
	if(start)
	{
		task_comps[4].timer       = task_comps[4].interval_time;				//reload timer
		task_comps[4].task_switch = SWITCH_ON;
	}
	else
		task_comps[4].task_switch = SWITCH_OFF;

}
extern  void delay_confirm_fan_speed(void);
void delay_confirm_fan_speed_control(u8 start)
{
	if(start)
	{
		task_comps[5].timer       = task_comps[5].interval_time;				//reload timer
		task_comps[5].task_switch = SWITCH_ON;
	}
	else
		task_comps[5].task_switch = SWITCH_OFF;

}
/**************************************************************************************************/
/*上电延时回调处理
**************************************************************************************************/
void Setup_Task_Start(void)
{
	task_comps[6].task_switch = SWITCH_ON;
}
static void Setup_Task_Stop(void)
{
	task_comps[6].task_switch = SWITCH_OFF;
}
void setup_handler(void)
{
	u8 res = 0;
	static u8 count = 0;
	if(count == 0)
	{
		count = 1;
		
		//空调设备判断当前温度，初始化当前模式
		if(Oled_Display_Info.device_type == AIR_CONDITION)
		{
			ReDefine_AIR_Condition_Syetem_Mode();
		}
		
		//未老化完成，则进入老化阶段
		if(Oled_Display_Info.aging_test_flag == 0)
			aging_test_start();
		
		temp_init();
		
		//上电显示
		Detect_Human_Dispaly();
	}
	else if(count == 1){
		count = 2;
		//上电请求设备版本消息以及网络状态
		Query_Device_Info(res);	
	}
	else 
	{
		u8 all_ff[8] ={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff} ;
		if(memcmp(Oled_Display_Info.zigbee_mac,all_ff,8) != 0)					//连续读取命令
		{
			Setup_Task_Stop();
			count = 0;
			return;
		}
		else if(count >4)
		{
			Setup_Task_Stop();
			count = 0;
			return;
		}
		Query_Device_Info(res);	
		count++;	
	}
}
/**************************************************************************************************/
/**************************************************************************************************/
/*操作温度校准延时回调处理
**************************************************************************************************/
void delay_operate_calibrate_temper_Task_Start(void)
{
	task_comps[7].timer       = task_comps[7].interval_time;
	task_comps[7].task_switch = SWITCH_ON;
}
void delay_operate_calibrate_temper_Task_Stop(void)
{
	task_comps[7].task_switch = SWITCH_OFF;
}
void delay_operate_calibrate_temper(void)
{
	delay_operate_calibrate_temper_Task_Stop();
	if(sKey_Operation[Oled_Display_Info.screen_page-1].operate_calibrate_temper)
	{
		sKey_Operation[Oled_Display_Info.screen_page-1].operate_calibrate_temper = 0;
		PowerOff();
	}
}
/**************************************************************************************************/
/*操作温度校准延时回调处理
**************************************************************************************************/
void delay_operate_delay_off_Task_Start(void)
{
	task_comps[17].timer       = task_comps[17].interval_time;
	task_comps[17].task_switch = SWITCH_ON;
}
void delay_operate_delay_off_Task_Stop(void)
{
	task_comps[17].task_switch = SWITCH_OFF;
}
void delay_operate_delay_off(void)
{
	delay_operate_delay_off_Task_Stop();
	if(sKey_Operation[Oled_Display_Info.screen_page-1].operate_delay_off_status)
	{
		sKey_Operation[Oled_Display_Info.screen_page-1].operate_delay_off_status = 0;
		//超时，关闭
		Delay_OFF_Rel_Display(0,0,0,0,0,0);
		Oled_Display_whole();
	}
}
/**************************************************************************************************/
/*操作温度校准确定后，延时2s关机处理
**************************************************************************************************/
void operate_calibrate_temper_delay_2s_off_Task_Start(void)
{
	task_comps[8].task_switch = SWITCH_ON;
}
void operate_calibrate_temper_delay_2s_off_Task_Stop(void)
{
	task_comps[8].task_switch = SWITCH_OFF;
}
u8  is_operate_calibrate_temper_delay_2s_off_Task_start(void)
{
	return (task_comps[8].task_switch == SWITCH_ON);
}
void operate_calibrate_temper_delay_2s_off(void)
{
	u8 i = 0;
	operate_calibrate_temper_delay_2s_off_Task_Stop();
	for(i =0;i<3;i++)						
	{
		if(sKey_Operation[i].operate_calibrate_temper)
			sKey_Operation[i].operate_calibrate_temper = 0;
	}
	PowerOff();
}
/**************************************************************************************************/
/*中宏协议数据重传
**************************************************************************************************/
static void task_retransmit(void)
{

}
/**************************************************************************************************/
/*中宏协议轮询空调状态
**************************************************************************************************/
static void task_poll_check(void)
{

}
/**************************************************************************************************/
/*喂狗  循环执行类任务，间隔5s
**************************************************************************************************/
static void task_feed_dog(void)
{
    SYS_UnlockReg();					//lmm debug  20201130
    
    WDT_RESET_COUNTER();
    
    SYS_LockReg();
}
/**************************************************************************************************/
/*老化测试相关函数，1s一次
**************************************************************************************************/
static void aging_test(void)
{
   static u16  Board_test_time  	= 0;
   static u8   aging_step  			= 0; 
   static u8   light_or_dark_flang  = 0;
   extern u8 First_Exit_Factory_Mode;
   Board_test_time ++;
    /*原函数只执行一次所以此处只让它执行一次 by wmj*/   
   if (Board_test_time == FAST_AGING_STEP1_TIME)               //15min后进入老化状态
   {    
		if(Oled_Display_Info.net_work_flage == OUT_LINE )
		{
			BL55072A_Clear();
			BL55072A_DisplayALL();  
			//task_comps[0].task_switch = SWITCH_OFF; // 标志清0	     
			task_comps[2].task_switch = SWITCH_OFF; // 标志清0	         
			task_comps[3].task_switch = SWITCH_OFF; // 标志清0
			task_comps[4].task_switch = SWITCH_OFF; // 标志清0             
			aging_step = 1;
			light_or_dark_flang = 0;
			
		}
		Oled_Display_Info.aging_test_flag = 1;	
		aging_test_start();
   }
   else if(Board_test_time < FAST_AGING_STEP1_TIME)
   {
		aging_test_start();
   }
   if((Board_test_time%10 ==0)&&(aging_step==1) )//前15分钟过去后执行此函数
   {
		switch(light_or_dark_flang)
		{
			case 0 :
			{
				LCD_CONTROL(1);
				BL55072A_DisplayALL(); 
				light_or_dark_flang = 1;
				if(!kAllLedIsBlinking())
					Control_LED_ON_OFF(0xff);
				#if	DEVICE_TYPE_FLOOR_HEATING
				Relay_Action(0x01,0x01);
				#elif DEVICE_TYPE_FAN
				Relay_Action(0x03,0x03);
				#endif
				break;
			}
			case 1:
			{
				LCD_CONTROL(0);
				BL55072A_Clear();
				light_or_dark_flang = 0;
				if(!kAllLedIsBlinking())
					Control_LED_ON_OFF(0);
				#if	DEVICE_TYPE_FLOOR_HEATING
				Relay_Action(0x01,0x00);
				#elif DEVICE_TYPE_FAN
				Relay_Action(0x03,0x00);
				#endif
				break;
			}
			default :
				break;
		}
		aging_test_start();	
   	}
    if(Board_test_time>= FAST_AGING_STEP2_TIME)	 	//8小时
    {	
		aging_step = 2;
		First_Exit_Factory_Mode = 1;
		Oled_Display_Info.aging_test_flag = 2;		//
		save_data();								//老化完成，写入标志位

		LCD_CONTROL(1);
		BL55072A_DisplayALL();  
		#if	DEVICE_TYPE_FLOOR_HEATING
		Relay_Action(0x01,0x01);
		#elif DEVICE_TYPE_FAN
		Relay_Action(0x03,0x03);
		#endif
    }
}
void aging_test_start(void)
{
	task_comps[11].task_switch = SWITCH_ON;    

}
void aging_test_stop(void)
{
	task_comps[11].task_switch = SWITCH_OFF;    

}
void delay5s_leave_net(void)
{

}
/******************************延时上报接近感应触发*************************************/
static void delay_300ms_report_start(void)
{
	task_comps[14].task_switch = SWITCH_ON;    

}
static void delay_300ms_report_stop(void)
{
	task_comps[14].task_switch = SWITCH_OFF;    

}
static void delay_300ms_report_task(void)
{
	delay_300ms_report_stop();
	proximity_triagger_report();					//单板产测上报   
	if(Oled_Display_Info.net_work_flage == IN_LINE)		
		proximity_triagger_report_zigbee();     	//整机产测上报

	//printf("start report proximity_triagger event\r\n");
}
/******************************5s退老化，延时复位*************************************/
void task_exit_aging_test_start(void)
{
	task_comps[13].task_switch = SWITCH_ON;    

}
static void task_exit_aging_test_stop(void)
{
	task_comps[13].task_switch = SWITCH_OFF;    

}
//强退老化后，主动复位
static void task_exit_aging_test(void)
{
	task_exit_aging_test_stop();
	SYS_UnlockReg();
	SYS_ResetChip();
}

void Spake_Start(void)
{
	//task_comps[3].task_switch = SWITCH_ON;
	Spake_Flag   = 1;
}

void Ararm_Init(void)
{	
	//if ( Oled_Display_Info.alarm_flag ==0 )  //新设备，未经过产测
	{
	    task_comps[11].task_switch = SWITCH_ON;     			
	}

}


 /* 任务添加请往后，严禁中间插入，否则出错 */   
struct task_components_t task_comps[] =             
{   
    {0, SWITCH_ON,  ALWAYS_RUN, SET_FLAG_0,  0,0,               main_task               		},       //主任务，无延时轮询
    {1, SWITCH_OFF,  RUN_FLAG_0, SET_FLAG_0,  Sec_5,Sec_5, 	    record_dev_used_time 			},         //使用时间记录
	{2, SWITCH_ON, RUN_FLAG_0, SET_FLAG_0,  Ms_200,Ms_200, 		sensor_measure_loop 			},         //传感器数据采集
	{3, SWITCH_OFF, RUN_FLAG_0, SET_FLAG_0,  Ms_100,Ms_100, 	spake_control_loop 				},         //蜂鸣器控制			(未使用)
	{4, SWITCH_OFF, RUN_FLAG_0, SET_FLAG_0,  Sec_3,Sec_3, 		delay_confirm_system_mode 		},         //空调模式延时确认
	{5, SWITCH_OFF, RUN_FLAG_0, SET_FLAG_0,  Sec_10,Sec_10, 	delay_confirm_fan_speed 		},         //风速延时确认
	{6, SWITCH_OFF, RUN_FLAG_0, SET_FLAG_0,  Ms_500,Ms_500, 	setup_handler 					},         //上电延时启动
	{7, SWITCH_OFF, RUN_FLAG_0, SET_FLAG_0,  Sec_15,Sec_15, 	delay_operate_calibrate_temper 	},         //15s操作倒计时
	{8, SWITCH_OFF, RUN_FLAG_0, SET_FLAG_0,  Sec_2,Sec_2, 		operate_calibrate_temper_delay_2s_off 	}, //确认校准温度后延时2s关机
	{9, SWITCH_OFF, RUN_FLAG_0, SET_FLAG_0,  Sec_1,Sec_1,       task_retransmit           		},         //重传
	{10,SWITCH_ON,  RUN_FLAG_0, SET_FLAG_0,  Sec_15,Sec_15,     task_poll_check           		} ,        //查询空调状态15s
	{11,SWITCH_OFF,  RUN_FLAG_1, SET_FLAG_0, Sec_1,Sec_1,		aging_test             			},         //老化测试
	{12,SWITCH_ON,  RUN_FLAG_1, SET_FLAG_0, Sec_3,Sec_3,		task_feed_dog             		},         //喂狗
	{13,SWITCH_OFF,  RUN_FLAG_0, SET_FLAG_0, Sec_3,Sec_3,		task_exit_aging_test        	},         //退老化5s快闪
	{14,SWITCH_OFF,  RUN_FLAG_0, SET_FLAG_0, Ms_300,Ms_300,		delay_300ms_report_task         },         //延时300ms上报接近感应触发事件
	{15,SWITCH_ON, RUN_FLAG_0, SET_FLAG_0,  Sec_5,Sec_5, 	    shtc3_sensor_measure_loop 		},         //温湿度传感器数据采集
	#if DEVICE_TYPE_FLOOR_HEATING
	{16,SWITCH_OFF, RUN_FLAG_0, SET_FLAG_0,   Sec_3,Sec_3,     	Tap_Status_Scan         		},   	   //阀门实时动态开关  
	#elif DEVICE_TYPE_FAN
	{16,SWITCH_OFF, RUN_FLAG_0, SET_FLAG_0,   Min_1,Min_1,     	Tap_Status_Scan         		},   	   //阀门实时动态开关
	#else
	{16,SWITCH_OFF, RUN_FLAG_0, SET_FLAG_0,   Sec_3,Sec_3,     	Tap_Status_Scan         		},   	   //阀门实时动态开关
	#endif
	{17, SWITCH_OFF, RUN_FLAG_0, SET_FLAG_0,  Sec_15,Sec_15, 	delay_operate_delay_off 		},         //15s操作延时关机
};

u8 task_num = sizeof(task_comps)/sizeof(task_comps[0]);	

void UART1_IRQHandler(void)   //串口接收中断
{
    uint8_t ch = 0xFF;
    if (UART_GET_INT_FLAG(UART1, UART_INTEN_RDAIEN_Msk))
    {
        UART_ClearIntFlag(UART1, UART_INTEN_RDAIEN_Msk); //清除标志位
        ch = UART_READ(UART1);  
        transparent_msg_recive(ch);
    }
}
void UART0_IRQHandler(void)   //串口接收中断
{
    uint8_t ch = 0xFF;

    if (UART_GET_INT_FLAG(UART0, UART_INTEN_RDAIEN_Msk))
    {
        UART_ClearIntFlag(UART0, UART_INTEN_RDAIEN_Msk); //清除标志位
        
        ch = UART_READ(UART0);  
		
//		//单板测试相关数据解析
		if(!uart_msg_recive(ch))
			return;
    }
}
extern KEY_OPERATE  sKey_Operation[3];
extern void Delay_Power_Off_Control(u8 control_type,u16 flashing_time_ms,u16 flash_interval_time_ms,u8 show_num);
void Reset_DelayOFF_timer(u8 device_type)
{
	sKey_Operation[device_type-1].operate_delay_off_ms = 0;
	sKey_Operation[device_type-1].operate_delay_off_s  = 0;
	sKey_Operation[device_type-1].operate_delay_off_min  = 0;
}
/***********************************************************************
定时关机计时
***********************************************************************/
void Power_Off_Scan (void)
{
	static u8 i = 0;
	for(i = 0;i<3;i++)
	{	
		if((i == 0)&&(Oled_Display_Info.Air_Device.power_off_flage == 0))
		{
			Reset_DelayOFF_timer(AIR_CONDITION);
			continue;
		}
		if((i == 1)&&(Oled_Display_Info.FLOOR_Device.power_off_flage == 0))
		{
			Reset_DelayOFF_timer(FLOOR_HEATING);
			continue;
		}
		if((i == 2)&&(Oled_Display_Info.FAN_Device.power_off_flage == 0))
		{
			Reset_DelayOFF_timer(FAN);
			continue;
		}
		sKey_Operation[i].operate_delay_off_ms ++;	
		if (sKey_Operation[i].operate_delay_off_ms >= 1000)
		{
			sKey_Operation[i].operate_delay_off_ms = 0;
			sKey_Operation[i].operate_delay_off_s ++;
			if (sKey_Operation[i].operate_delay_off_s >= 60)
			{
				sKey_Operation[i].operate_delay_off_s = 0;
				sKey_Operation[i].operate_delay_off_min ++;

				if (sKey_Operation[i].operate_delay_off_min >= 30)
				{
					sKey_Operation[i].operate_delay_off_min = 0;
					if (i == 0){   //定时关机
						if(Oled_Display_Info.Air_Device.power_off_time >=5)
							Oled_Display_Info.Air_Device.power_off_time -= 5;				
					}
					else if(i == 1){
						if(Oled_Display_Info.FLOOR_Device.power_off_time >=5)
							Oled_Display_Info.FLOOR_Device.power_off_time -= 5;	
					}
					else if(i == 2){
						if(Oled_Display_Info.FAN_Device.power_off_time >=5)
							Oled_Display_Info.FAN_Device.power_off_time -= 5;	
					}
					sKey_Operation[i].operate_delay_off_display_flag = 1;
				}			 
			}
		}
	}
	
}
void Reset_Fiter_Time(u8 device_type)
{
	sKey_Operation[device_type-1].filter_calc.operate_delay_off_ms = 0;
	sKey_Operation[device_type-1].filter_calc.operate_delay_off_s  = 0;
	sKey_Operation[device_type-1].filter_calc.operate_delay_off_min  = 0;
	sKey_Operation[device_type-1].filter_calc.operate_delay_off_h  = 0;
}
/***********************************************************************
滤网开启计时
***********************************************************************/
void Filter_Start_Scan (void)						
{
	static u8 i = 0;
	for(i = 0;i<3;i=i+2)
	{	
		if((i == 0)&&(Oled_Display_Info.Air_Device.on_off == 0))
		{
			Reset_Fiter_Time(AIR_CONDITION);
			continue;
		}
		if((i == 2)&&(Oled_Display_Info.FAN_Device.on_off==0))
		{
			Reset_Fiter_Time(FAN);
			continue;
		}
		sKey_Operation[i].filter_calc.operate_delay_off_ms ++;	
		if (sKey_Operation[i].filter_calc.operate_delay_off_ms >= 1000)
		{
			sKey_Operation[i].filter_calc.operate_delay_off_ms = 0;
			sKey_Operation[i].filter_calc.operate_delay_off_s ++;
			if (sKey_Operation[i].filter_calc.operate_delay_off_s >= 60)//60
			{
				sKey_Operation[i].filter_calc.operate_delay_off_s = 0;
				sKey_Operation[i].filter_calc.operate_delay_off_min ++;

				if (sKey_Operation[i].filter_calc.operate_delay_off_min >= 60)
				{
					sKey_Operation[i].filter_calc.operate_delay_off_min = 0;
					sKey_Operation[i].filter_calc.operate_delay_off_h++;
					if ((i == 0)&&(Oled_Display_Info.Air_Device.filter_wash_time>=1)){   //定时关机
						Oled_Display_Info.Air_Device.filter_wash_time -= 1;	
						save_data();						
					}
					else if((i == 2)&&(Oled_Display_Info.FAN_Device.filter_wash_time >=1)){
						Oled_Display_Info.FAN_Device.filter_wash_time -= 1;		
						save_data();
					}
					if ( (Oled_Display_Info.Air_Device.filter_wash_time <= 0 )&&(i == 0)	)   //Test
					{
						Reset_Fiter_Time(AIR_CONDITION);
						if(Oled_Display_Info.Air_Device.error !=3)
						{
							Oled_Display_Info.Air_Device.error = 3;				//滤网清洗时间
							sKey_Operation[i].filter_calc.operate_delay_off_display_flag = 1;
						}
					}
					if((Oled_Display_Info.FAN_Device.filter_wash_time <= 0)&&(i == 2)	)
					{
						Reset_Fiter_Time(FAN);
						if(Oled_Display_Info.FAN_Device.error !=3)
						{
							Oled_Display_Info.FAN_Device.error = 3;				//滤网清洗时间
							sKey_Operation[i].filter_calc.operate_delay_off_display_flag = 1;	
						}							
					}
				}			 
			}
		}
	}
	
}
u16 Get_Current_Backlight_Time(void)
{
	if(Oled_Display_Info.screen_page == AIR_CONDITION)
		return (30*(Oled_Display_Info.Air_Device.screen_save_time+1));
	else if(Oled_Display_Info.screen_page == FLOOR_HEATING)
		return (30*(Oled_Display_Info.FLOOR_Device.screen_save_time+1));
	else if(Oled_Display_Info.screen_page == FAN)
		return (30*(Oled_Display_Info.FAN_Device.screen_save_time+1) );
	return 0;
}
/***********************************************************************
背光倒计时
***********************************************************************/	
void Backlight_Handle (void)
{
    if (sBacklight.blacklight_flag)
    {
		static u16 backlight_temp = 0;
		backlight_temp = Get_Current_Backlight_Time();
        sBacklight.time_blacklight_ms ++;
        if (sBacklight.time_blacklight_ms >= 1000) 
        {
            sBacklight.time_blacklight_ms = 0;
            sBacklight.time_blacklight_s ++;	
            if (sBacklight.time_blacklight_s >= backlight_temp)				//30
            {
                sBacklight.time_blacklight_s = 0;
                sBacklight.blacklight_flag = 0;
				sBacklight.blacklight_display_flag = 1;
            } 
        }
    }
}
extern Local_Config_Operation  local_config_operate;
void Display_Status(void)
{
	u8 i = 0;
	//息屏显示
	if(sBacklight.blacklight_display_flag)	
	{
		sBacklight.blacklight_display_flag = 0;
		EnterScreenSaveMode();					//熄灭进入屏保模式
	}
	//串口故障显示
	if(sUartDetect.zigbee_uart_display_flag)
	{
		sUartDetect.zigbee_uart_display_flag = 0;
		Set_Error_Code(2);		//ZIGBEE串口故障
	}
	//配置模式超时显示
	if(local_config_operate.config_timeout_display)
	{
		local_config_operate.config_timeout_display = 0;
		Control_OnOff_handler(Oled_Display_Info.device_type,0);     //显示关		
	}	
	for(i = 0;i<3;i++)
	{
		//延时关机显示
		if(sKey_Operation[i].operate_delay_off_display_flag)
		{
			u8  dispaly_data ;
			sKey_Operation[i].operate_delay_off_display_flag = 0;
			
			if(Oled_Display_Info.screen_page==1)
				dispaly_data = Oled_Display_Info.Air_Device.power_off_time;
			else if(Oled_Display_Info.screen_page==2)
				dispaly_data = Oled_Display_Info.FLOOR_Device.power_off_time;
			else
				dispaly_data = Oled_Display_Info.FAN_Device.power_off_time;
			
			if((Oled_Display_Info.screen_page == (i+1))&&(Get_Display_Status()))
			{
				if(IsPowerOffFlashing())
				{
					Delay_Power_Off_Control(FLASHING,15000,500,dispaly_data);	
					sKey_Operation[i].record_adjust_delay_off = dispaly_data;
				}
				else
					Display_poweroff_time(dispaly_data);
			}
			if ( (Oled_Display_Info.Air_Device.power_off_time <= 0 )&&(i == 0)	)
			{
				if(Oled_Display_Info.screen_page == AIR_CONDITION)
					PowerOff();
				
				Oled_Display_Info.Air_Device.on_off = 0;
				#if ( (DEV_TYPE == 0) &&(DEVICE_TYPE_AIR_CONDITION ==1))
				Control_AIR_CONDITION_By_RS485(AIR_CMD_ONOFF,Oled_Display_Info.Air_Device.on_off);		//控制开关
				#endif
				Oled_Display_Info.Air_Device.power_off_flage = 0;
			}
			else if((Oled_Display_Info.FLOOR_Device.power_off_time <= 0)&&(i == 1)	)
			{
				if(Oled_Display_Info.screen_page == FLOOR_HEATING)
					PowerOff();
				#if FLOOR_HEATING
				Control_OnOff_handler(FLOOR_HEATING,0);
				#endif
				Oled_Display_Info.FLOOR_Device.on_off = 0;
				Oled_Display_Info.FLOOR_Device.power_off_flage = 0;
			}
			else if((Oled_Display_Info.FAN_Device.power_off_time <= 0)&&(i == 2)	)
			{
				if(Oled_Display_Info.screen_page == FAN)
					PowerOff();
				#if DEVICE_TYPE_FAN
				Control_OnOff_handler(FAN,0);
				#endif
				Oled_Display_Info.FAN_Device.on_off = 0;
				Oled_Display_Info.FAN_Device.power_off_flage = 0;
			}
			state_change_report(OPCODE_GENERAL_REPORT_STATUS,i+1);// 上报状态
		}
		//滤网时间显示
		if(sKey_Operation[i].filter_calc.operate_delay_off_display_flag)
		{
			sKey_Operation[i].filter_calc.operate_delay_off_display_flag = 0;
			if(i == 0)
			{
				if(Get_Display_Status())
				{
					Display_Up_Contend(AIR_CONDITION);
				}
				state_change_report(OPCODE_GENERAL_REPORT_STATUS,1);
			}
			else if(i == 2)
			{
				if(Get_Display_Status())
				{
					Display_Up_Contend(FAN);
				}
				state_change_report(OPCODE_GENERAL_REPORT_STATUS,3);
			
			}
		}
	}
}
/***********************************************************************
串口故障检测，1分钟未收到应答，显示串口故障
***********************************************************************/
void Set_Uart_Detect_Flag(void)
{
	sUartDetect.zigbee_uart_flag = 1;
}
void Reset_Uart_Detect_Flag(void)
{
	sUartDetect.zigbee_uart_flag = 0;
}
void Zigbee_Uart_Error_Handle (void)
{
    if (sUartDetect.zigbee_uart_flag)
    {
        sUartDetect.zigbee_uart_ms ++;
        if (sUartDetect.zigbee_uart_ms >= 1000) 
        {
            sUartDetect.zigbee_uart_ms = 0;
            sUartDetect.zigbee_uart_s ++;	
            if (sUartDetect.zigbee_uart_s >= 105)				
            {
                sUartDetect.zigbee_uart_s = 0;
                sUartDetect.zigbee_uart_flag = 0;
				sUartDetect.zigbee_uart_display_flag = 1;
            } 
        }
    }
	else{
		sUartDetect.zigbee_uart_s  = 0;
		sUartDetect.zigbee_uart_ms = 0;
	}
}
/*
1:代表当前未处于屏保模式且状态为开，0表示处于关机或者进入屏保模式，
获取显示的状态
*/
u8 Get_Display_Status(void)
{
	if(Oled_Display_Info.screen_page == 1)
	{
		if((sBacklight.blacklight_flag)&&(Oled_Display_Info.Air_Device.on_off))
			return 1;
	}
	else if(Oled_Display_Info.screen_page == 2)
	{
		if((sBacklight.blacklight_flag)&&(Oled_Display_Info.FLOOR_Device.on_off))
			return 1;
	}
	else if(Oled_Display_Info.screen_page == 3)
	{
		if((sBacklight.blacklight_flag)&&(Oled_Display_Info.FAN_Device.on_off))
			return 1;
	}
	return 0;
}
u8 IsLcdEnterScreenSave(void)
{
	return (sBacklight.blacklight_flag == 0);
}
///**********************************************************************
//定时器0中断  中断频率1ms
//**********************************************************************/
void TMR0_IRQHandler(void)
{ 
    static u8 i = 0;
    TIMER_ClearIntFlag(TIMER0);
    for(i = 0; i < task_num; i++)                                       // 逐个任务时间处理
    {
        if(task_comps[i].task_switch == SWITCH_ON)                      // 任务开关
        {
            if (task_comps[i].timer)                                    // 时间不为0
            {
                task_comps[i].timer--;                                  // 减去一个节拍

                if (task_comps[i].timer == 0)                           // 时间减完了
                {
                    task_comps[i].timer = task_comps[i].interval_time; // 恢复计时器值，重新下一次
                    task_comps[i].run_flag = RUN_FLAG_1;               // 任务可以运行
                }
            }
        }
    }
	
	Oled_Display_Info.local_timer ++;
	
    if(GetkeyScanTime()) SetkeyScanTime( GetkeyScanTime() - 1 );


    if(GetLCDShowTime()) SetLCDShowTime( GetLCDShowTime() - 1 );
	
	if(GetNetScanTime()) SetNetScanTime( GetNetScanTime() - 1 );				//zigbee 图标闪烁控制
	
    transparent_time_manage(1);

    if( guDelay_ms ) guDelay_ms--;


    Power_Off_Scan();     				//定时关机计时

    Backlight_Handle ();  				//背光倒计时
	
	Filter_Start_Scan();  				//滤网清洗计时
	
	Spake_Control();					//蜂鸣器控制
	
	LED_Flash_Control(); 				//童锁按键(led3)闪烁
	
	Setup_Mode_Handle(); 				//配置模式10s超时
	
	Zigbee_Uart_Error_Handle();  		//zigbee串口故障检测
}  
/*********************************************************************/
void  zero_check_init(void)
{
#if DEVICE_TYPE_FLOOR_HEATING

  wind_open_time  =  330; 
  wind_close_time  = 250;  

#elif  DEVICE_TYPE_FAN
  
  wind_open_time  =  166; //6ms
  wind_close_time  = 166; //6ms
  
  temp_open_time  =  330; 
  temp_close_time  = 500; 
  #endif
}
/*********************************************************************
定时器1中断  继电器过零点
*********************************************************************/
void TMR1_IRQHandler(void)    //过零点
{
    TIMER_ClearIntFlag(TIMER1); 
	#if ( (DEV_TYPE == 1) &&(DEVICE_TYPE_AIR_CONDITION ==1)) ||(DEVICE_TYPE_FLOOR_HEATING) ||(DEVICE_TYPE_FAN)
    switch(Tap_Control.set_tap)
    {
        case RELAY1:    //新风高风阀       //地暖继电器             
        {
            if ( Tap_Control.save_tap_status[5] == 0)
            {
                if (Tap_Control.tap_to_do[Tap_Control.set_tap] == 1)
                {	
                    K1 = 0;
                }
                else if (Tap_Control.tap_to_do[Tap_Control.set_tap] == 2)
                {
                    K1 = 1;
                }	
            }							
            break;	
        }
		#if  DEVICE_TYPE_FAN
        case RELAY2:      //新风中风阀   
        {
            if ( Tap_Control.save_tap_status[5] == 0)
            {
                if (Tap_Control.tap_to_do[Tap_Control.set_tap] == 1)
                {	
                    K2 = 0;
                }
                else if (Tap_Control.tap_to_do[Tap_Control.set_tap] == 2)
                {
                    K2 = 1;
                }	
            }	
            break;	
        }	
        case RELAY3:    // 新风低风阀   
        {
            if ( Tap_Control.save_tap_status[5] == 0)
            {
                if (Tap_Control.tap_to_do[Tap_Control.set_tap] == 1)
                {	
                    K3 = 0;
                }
                else if (Tap_Control.tap_to_do[Tap_Control.set_tap] == 2)
                {
                    K3 = 1;
                }	
            }	
            break;	
        }	
        case RELAY4:      //新风旁通阀   
        {
            if ( Tap_Control.save_tap_status[5] == 0)
            {
                if (Tap_Control.tap_to_do[Tap_Control.set_tap] == 1)
                {	
                    K4 = 0;
                }
                else if (Tap_Control.tap_to_do[Tap_Control.set_tap] == 2)
                {
                    K4 = 1;
                }	
            }	
            break;	
        }	
		#endif
        default :
        break;        
    }
	
	Tap_Control.tap_to_do[Tap_Control.set_tap] = 0;
	#if DEVICE_TYPE_FAN
	Tap_Control.set_tap ++;
	if(Tap_Control.set_tap == 4)
    {
        Tap_Control.set_tap = 0;
    }	
	#endif
	
    Tap_Control.tap_end_flage	= 0;
    Timer1_Close();	
	#endif	
}
#define Delay1us   3//3*0.34

void delay_us( u32 time)
{

    u32 j = 0;
    long i=0;

    for(i=0;i<time;i++)
    {
        for(j=0; j<Delay1us; j++);
    }

}
// 
///**********************************************************************
//    过零信号检测中断	P25 
//***********************************************************************/
void GPIO234_IRQHandler()
{   
	#if ( (DEV_TYPE == 1) &&(DEVICE_TYPE_AIR_CONDITION ==1)) ||(DEVICE_TYPE_FLOOR_HEATING) ||(DEVICE_TYPE_FAN)
    if (( P2->INTSRC & BIT5) > 0)       //过零信号检测中断	P25
    {
        P2->INTSRC = BIT5;              /* 清中断标志 */
		
        if (Tap_Control.tap_to_do[Tap_Control.set_tap] == 1)
        {	
            if (Tap_Control.set_tap < 3)
            {
                Timer1_Open(wind_close_time);
            }
            else
            {
                Timer1_Open(temp_close_time);
            }            
        }
        else if (Tap_Control.tap_to_do[Tap_Control.set_tap] == 2)
        {
            if (Tap_Control.set_tap < 3)
            {
                Timer1_Open(wind_open_time);
            }
            else
            {
                Timer1_Open(temp_open_time);
            }
            
        }			
        GPIO_DisableEINT0(P2, 5);
    }			
	#endif
}
/***********************************************************************
*   函 数 名: Update_Dev_Info
*   功能说明: 更新系统状态信息
*   形     参：无
*   返 回 值:  无
***********************************************************************/
void Update_Dev_Info(u8 channel_id)
{
    s16 temp_num = 0;
	u16 temp = 0;
	u8 i = 0;
	if(channel_id == 1)
	{
		Device_status[i++] = 19;													//长度
		
		Device_status[i++] = Oled_Display_Info.Air_Device.error;					//错误码
		Device_status[i++] = Oled_Display_Info.Air_Device.on_off;      				//开关状态
		Device_status[i++] = Oled_Display_Info.Air_Device.lock_flage; 				//童锁
		Device_status[i++] = Oled_Display_Info.Air_Device.system_type; 				//运行模式

		temp_num = Oled_Display_Info.Air_Device.set_temperature;               		//获得设置温度	
		Device_status[i++] = (temp_num & 0XFF);                       				//设定温度低字节		
		Device_status[i++] = ((temp_num >> 8) &0XFF);                 				//设定温度高字节
		

		temp_num = Oled_Display_Info.Air_Device.real_temperature;
		Device_status[i++] = (temp_num & 0XFF);                       				//环境温度低字节
		Device_status[i++] = ((temp_num >> 8) &0XFF);                 				//环境温度高字节

		Device_status[i++] = Oled_Display_Info.Air_Device.wind_speed;            	//当前风速
		Device_status[i++] = Oled_Display_Info.Air_Device.air_wind_direct;       	//摆风模式
		Device_status[i++] = Oled_Display_Info.Air_Device.power_off_time;           //延时关机
		
		temp = Oled_Display_Info.Air_Device.filter_wash_time;						//获取滤网清洗时间
		Device_status[i++] =(temp & 0XFF);         									//滤网清洗时间高字节
		Device_status[i++] = ((temp >> 8) &0XFF);        							//滤网清洗时间高字节
		
		temp = Oled_Display_Info.Air_Device.screen_save_time;						//屏保时间
		Device_status[i++] =(temp & 0XFF);         									//屏保时间高字节
		Device_status[i++] = ((temp >> 8) &0XFF);        							//屏保时间高字节
		
		Device_status[i++] =Oled_Display_Info.Air_Device.correct_temperature;		//校准温度
		Device_status[i++] =Oled_Display_Info.Air_Device.no_disturb_mode;			//勿扰模式
		Device_status[i++] =Oled_Display_Info.Air_Device.air_product_id;			//空调品牌
		Device_status[i++] =Oled_Display_Info.Air_Device.air_type;					//空调类型
	}
	else if(channel_id == 2)
	{
		i= 0;
		
		Device_status[0] = 0;
		
		Device_status[1] = Oled_Display_Info.FLOOR_Device.error;						//错误码

		Device_status[2] = Oled_Display_Info.FLOOR_Device.on_off;      				//开关状态
		Device_status[3] = Oled_Display_Info.FLOOR_Device.lock_flage; 				//童锁
		Device_status[4] = Oled_Display_Info.FLOOR_Device.system_type; 				//运行模式

		temp_num = Oled_Display_Info.FLOOR_Device.set_temperature;               		//获得设置温度
		Device_status[5] = (temp_num & 0XFF);                       				//设定温度低字节
		Device_status[6] = ((temp_num >> 8) &0XFF);                 				//设定温度高字节
		

		temp_num = Oled_Display_Info.FLOOR_Device.real_temperature;
		Device_status[7] = (temp_num & 0XFF);                       				//环境温度低字节
		Device_status[8] = ((temp_num >> 8) &0XFF);                 				//环境温度高字节
		
		Device_status[9] = Oled_Display_Info.FLOOR_Device.power_off_time;           //延时关机
		
		temp = Oled_Display_Info.FLOOR_Device.screen_save_time;						//屏保时间
		Device_status[10] =(temp & 0XFF);         									//屏保时间高字节
		Device_status[11] = ((temp >> 8) &0XFF);        							//屏保时间高字节
		
		Device_status[12] =Oled_Display_Info.FLOOR_Device.correct_temperature;		//校准温度
		Device_status[13] =Oled_Display_Info.FLOOR_Device.no_disturb_mode;			//勿扰模式
		
		Device_status[0] = 13;	
	}
	else if(channel_id == 3)
	{
		i = 0;
		Device_status[0] = 0;														//长度
		
		Device_status[1] = Oled_Display_Info.FAN_Device.error;					//错误码

		Device_status[2] = Oled_Display_Info.FAN_Device.on_off;      				//开关状态
		Device_status[3] = Oled_Display_Info.FAN_Device.lock_flage; 				//童锁
		Device_status[4] = Oled_Display_Info.FAN_Device.system_type; 				//运行模式

		temp_num = Oled_Display_Info.FAN_Device.real_temperature;
		Device_status[5] = (temp_num & 0XFF);                       				//环境温度低字节
		Device_status[6] = ((temp_num >> 8) &0XFF);                 				//环境温度高字节
		

		Device_status[7] = Oled_Display_Info.FAN_Device.wind_speed;            	//当前风速
		Device_status[8] = Oled_Display_Info.FAN_Device.power_off_time;           //延时关机
		
		temp = Oled_Display_Info.FAN_Device.filter_wash_time;						//获取滤网清洗时间
		Device_status[9] =(temp & 0XFF);         									//滤网清洗时间高字节
		Device_status[10] = ((temp >> 8) &0XFF);        							//滤网清洗时间高字节
		
		
		temp = Oled_Display_Info.FAN_Device.screen_save_time;						//屏保时间
		Device_status[11] =(temp & 0XFF);         									//屏保时间高字节
		Device_status[12] = ((temp >> 8) &0XFF);        							//屏保时间高字节
		
		Device_status[13] =Oled_Display_Info.FAN_Device.correct_temperature;		//校准温度
		Device_status[14] =Oled_Display_Info.FAN_Device.no_disturb_mode;			//勿扰模式
	
		Device_status[0] = 14;														//长度
	}
   
}
/////***********************************************************************
////*   函 数 名: transparent_data_analyze
////*   功能说明: 透传数据解析
////*   形     参：recv_data 串口接收到的信息
////*   返 回 值: 无
////***********************************************************************/
void transparent_data_analyze(PROTOCOL_DEAL recv_data)
{	
//   // u16 set_temp_num = 0;

    //static u8 correct_tem_num = 0;
    static u8 arg_data[50] = {0};
	u8 device_type = recv_data.Channel_ID;

    //s16 temp_num = 0;
//	printf("#########transparent_data_analyze,opcode = 0x%2x,=%d,payload :\r\n",recv_data.Opcode,recv_data.Channel_ID);
//	Print_Buff((u8 *)recv_data.ARG,(u8)recv_data.ARG_len);
    
    switch(recv_data.Opcode)
    {
		/********************************************Common CMD Handler*************************************************************/
		case _OPCODE_QUERY_NET_STATE:             	    				//网络状态查询
        {	
			
			if( (kNetIndicatorIsBlinking() ) &&(((recv_data.ARG[1] == IN_LINE)&&(Oled_Display_Info.net_work_flage ==OUT_LINE ) )||
												((recv_data.ARG[1] == OUT_LINE)&&(Oled_Display_Info.net_work_flage ==IN_LINE ) )))                              //是否在闪烁
			{
				memset(arg_data,0,sizeof(arg_data));
				network_state_machine(recv_data.ARG[1]);
			}
			Reset_Uart_Detect_Flag();
			if((Oled_Display_Info.screen_page == 1)&&(Oled_Display_Info.Air_Device.error == 2))		//通讯故障
			{
				Oled_Display_Info.Air_Device.error = 0;
				if( Get_Display_Status())
				{
					LCDShowTemp_state_t.LCD_place = DELAY_OFF_TIME_DISPLAY;	
					LCDShowTemp_state_t.LCD_display_count = 0;				
					LCDShowTemp_state_t.LCD_state = OFF;
					LcdShowCTR(LCDShowTemp_state_t);
				}
				state_change_report(OPCODE_GENERAL_REPORT_STATUS,1);
			}
			break;
		}
		case OPCODE_GENERAL_BING_DEVICE:    							//绑定设备
        {
			u8 bind_type;
			u8 bind_ep;
			u8 temp = 0;
			u8 temp1 = 0;
            memset(arg_data,0,sizeof(arg_data));
			bind_type 	= recv_data.ARG[0];
			bind_ep 	= recv_data.ARG[1];
			temp  = Oled_Display_Info.binding_device&0x03;
			temp1 = ((Oled_Display_Info.binding_device&0x0c)>>2);
			if(bind_type  == 1)										//绑定
			{
				if((Oled_Display_Info.binding_device&0xf0) == 0)	//绑定第一个设备
				{
					Oled_Display_Info.binding_device  = 0x10;        //绑定设备个数为1
					Oled_Display_Info.binding_device |= bind_ep;
				}
				else if((Oled_Display_Info.binding_device&0xf0) == 0x10) //绑定第二个设备
				{
					if(temp != bind_ep)
					{
						temp = Oled_Display_Info.binding_device&0x03;
						Oled_Display_Info.binding_device  = 0x20 | temp;        //绑定设备个数为2
						Oled_Display_Info.binding_device |= (bind_ep<<2);
					}
				}
				arg_data[0] = 0;
			}
			else{													//解绑
				if((Oled_Display_Info.binding_device&0xf0) == 0x10)	//绑定第一个设备
				{
					if(temp == bind_ep)
						Oled_Display_Info.binding_device  = 0;
					else if(temp1 == bind_ep)
						Oled_Display_Info.binding_device  = 0;
				}
				else if((Oled_Display_Info.binding_device&0xf0) == 0x20) //绑定第二个设备
				{
					if(temp == bind_ep)
					{
						temp = Oled_Display_Info.binding_device&0x0c;
						Oled_Display_Info.binding_device  = (0x10 | temp);        //绑定设备个数为2
					}
					else if(temp1 == bind_ep)
					{
						temp = Oled_Display_Info.binding_device&0x03;
						Oled_Display_Info.binding_device  = (0x10 | temp);        //绑定设备个数为2
					}
				}
				if(Oled_Display_Info.screen_page == bind_ep)
				{
					Oled_Display_Info.screen_page = Oled_Display_Info.device_type;
					//显示所有的界面
					display_current_page();
				}
				arg_data[0] = 0;
			}
			save_data();
			arg_data[1] 	= recv_data.ARG[0];
			arg_data[2]		= recv_data.ARG[1];
            ack_dev_info(recv_data,arg_data,3);	
            break;
        }
		case OPCODE_GENERAL_MCU_DATA_QUERY:	//zigbee读取mcu信息
        {
            memset(arg_data,0,sizeof(arg_data));
			arg_data[0] = 0;
            arg_data[1] = Oled_Display_Info.mcu_Version;
			arg_data[2] = 0x10;
		#if DEVICE_TYPE_FLOOR_HEATING
			arg_data[3] = 2;										//设备类型	 地暖
			arg_data[4] = 0;										//设备子类型  电地暖
			device_type = 2;
			recv_data.Channel_ID = 2;
		#elif DEVICE_TYPE_FAN
			arg_data[3] = 3;										//设备类型    新风
			arg_data[4] = 0;										//设备子类型  无
			device_type = 3;
			recv_data.Channel_ID = 3;
		#elif DEVICE_TYPE_AIR_CONDITION
			arg_data[3] = 1;										//设备类型    空调
			arg_data[4] = 0;										//设备子类型  风机盘管
			device_type = 1;
			recv_data.Channel_ID = 1;
		#endif
			Update_Dev_Info(device_type);
			//printf("device_type =%d arg_data[3] = 0x%x\r\n",device_type ,arg_data[3]);
			memmove(&arg_data[5],&Device_status[1],Device_status[0]);
            ack_dev_info(recv_data,arg_data,Device_status[0]+5);	
            break;				
        }
		case _OPCODE_PUSH_NETWORK_STATE:	//网络状态推送
        {
			 memset(arg_data,0,sizeof(arg_data));
			network_state_machine(recv_data.ARG[0]);
			//清除本地绑定信息
			if(recv_data.ARG[0] == 0)
			{
				if(Oled_Display_Info.binding_device != 0)
				{
					Oled_Display_Info.binding_device = 0;				//清除绑定信息
					
					Oled_Display_Info.screen_page = Oled_Display_Info.device_type;
					//显示所有的界面
					display_current_page();
				}
				
				kNetIndicatorOptTrigger(0, 200, 200, (5*1000)/(200+200), 1, 0 );
				if(Oled_Display_Info.device_type == 1)
					Oled_Display_Info.Air_Device.no_disturb_mode = 0;
				else if(Oled_Display_Info.device_type == 2)
					Oled_Display_Info.FLOOR_Device.no_disturb_mode = 0;
				else if(Oled_Display_Info.device_type == 3)
					Oled_Display_Info.FAN_Device.no_disturb_mode = 0;
				save_data();
			}
			
			arg_data[0] = 0;
            arg_data[1] = recv_data.ARG[0];
            ack_dev_info(recv_data,arg_data,2);	
            break;				
        }
		case OPCODE_GENERAL_LCD_OFF:    					  //息屏控制指令
        {
			if(recv_data.ARG[0] == 0)
			{
				BL55072A_Clear();									
				LCD_CONTROL(0);		
				Operate_Led_Button(Oled_Display_Info.device_type,0);
				Oled_Display_Info.recv_lcd_off = 5;
			}				
            memset(arg_data,0,sizeof(arg_data));
            arg_data[0] = 0;
            arg_data[1] = recv_data.ARG[0];
            ack_dev_info(recv_data,arg_data,2);	
            break;
        }
		case _OPCODE_SET_NET_STATE:
		{
			u8 network_stat = recv_data.ARG[1];
			//清除本地绑定信息
			if(network_stat == 0)
			{
				Oled_Display_Info.binding_device = 0;				//清除绑定信息
				
				kNetIndicatorOptTrigger(0, 200, 200, (5*1000)/(200+200), 1, 0 );
				if(Oled_Display_Info.device_type == 1)
					Oled_Display_Info.Air_Device.no_disturb_mode = 0;
				else if(Oled_Display_Info.device_type == 2)
					Oled_Display_Info.FLOOR_Device.no_disturb_mode = 0;
				else if(Oled_Display_Info.device_type == 3)
					Oled_Display_Info.FAN_Device.no_disturb_mode = 0;
				save_data();
			}
			else if(network_stat == 1)								//收到开始入网应答
			{
				kNetIndicatorOptTrigger(0, 800, 800, (61*1000)/(800+800), 1, 0 );
			}
			
			break;
		}
		case OPCODE_GENERAL_STATUS_QUERY:             	   			   	 //查询设备状态,待测试
        {			
			Oled_Display_Info.zigbee_Version = recv_data.ARG[1];
			network_state_machine(recv_data.ARG[2]);
			memcpy(Oled_Display_Info.zigbee_mac,&recv_data.ARG[3],sizeof(Oled_Display_Info.zigbee_mac));
			//printf("data = %x,%x\r\n",recv_data.ARG[1],recv_data.ARG[2]);
            break;
        }
		case OPCODE_GENERAL_OTA_CONTROL:             	   			   	 //待测试
        {	
			u8 Ota_status;
			Ota_status = recv_data.ARG[0];
			 memset(arg_data,0,sizeof(arg_data));
			switch(Ota_status)
			{
				case OTA_STATUS_START:
				{
					kNetIndicatorOptTrigger(0, 400, 2800, 0xffffffff, 0, 1 );
				}
					break;
				case OTA_STATUS_SUCCESS:
				{
					kNetIndicatorOptTrigger(0, 0, 0, 0, 1, 1 );
					break;
				}
				case OTA_STATUS_FAILURE:
				{
					kNetIndicatorOptTrigger(0, 200, 200, 5*1000/(200+200), 0, 1 );
				}
					break;
			}
			arg_data[0] = 0;
            arg_data[1] = Ota_status;
            ack_dev_info(recv_data,arg_data,2);	
            break;
        }
		/**************************************Private CMD Handler*******************************************************************/
        case OPCODE_GENERAL_SWITCH:             						//执行开关动作
        {  
			Set_CMD_Handler(device_type,OPCODE_GENERAL_SWITCH,recv_data.ARG,recv_data.ARG_len);
            memset(arg_data,0,sizeof(arg_data));
            arg_data[0] = recv_data.ARG[0];
            arg_data[1] = recv_data.ARG[1];
            ack_dev_info(recv_data,arg_data,2);	
            break;
        }
	
        case OPCODE_GENERAL_OPERATION_MODE:    			    			//设定运行模式
        {
			Set_CMD_Handler(device_type,OPCODE_GENERAL_OPERATION_MODE,recv_data.ARG,recv_data.ARG_len);
            memset(arg_data,0,sizeof(arg_data));
            arg_data[0] = recv_data.ARG[0];
            arg_data[1] = recv_data.ARG[1];
            ack_dev_info(recv_data,arg_data,2);	
            break;
        }
		 case OPCODE_GENERAL_SET_TEMPERATURE:    			    		//设定目标温度
        {
			Set_CMD_Handler(device_type,OPCODE_GENERAL_SET_TEMPERATURE,recv_data.ARG,recv_data.ARG_len);
            memset(arg_data,0,sizeof(arg_data));
            arg_data[0] = recv_data.ARG[0];
            arg_data[1] = recv_data.ARG[1];
			arg_data[2] = recv_data.ARG[2];
            ack_dev_info(recv_data,arg_data,3);	
            break;
        }
		 case OPCODE_GENERAL_SET_WIND_SPEED:    			    //设定风速
        {
			Set_CMD_Handler(device_type,OPCODE_GENERAL_SET_WIND_SPEED,recv_data.ARG,recv_data.ARG_len);
            memset(arg_data,0,sizeof(arg_data));
            arg_data[0] = recv_data.ARG[0];
            arg_data[1] = recv_data.ARG[1];
            ack_dev_info(recv_data,arg_data,2);	
            break;
        }
		 case OPCODE_GENERAL_ERROR_CODE:    			       //故障推送
        {
			Set_CMD_Handler(device_type,OPCODE_GENERAL_ERROR_CODE,recv_data.ARG,recv_data.ARG_len);
            memset(arg_data,0,sizeof(arg_data));
            arg_data[0] = recv_data.ARG[0];
            arg_data[1] = recv_data.ARG[1];
            ack_dev_info(recv_data,arg_data,2);	
            break;
        }
		 case OPCODE_GENERAL_SET_CHILD_LOCK:    			    //设定童锁
        {
			Set_CMD_Handler(device_type,OPCODE_GENERAL_SET_CHILD_LOCK,recv_data.ARG,recv_data.ARG_len);
            memset(arg_data,0,sizeof(arg_data));
            arg_data[0] = recv_data.ARG[0];
            arg_data[1] = recv_data.ARG[1];
            ack_dev_info(recv_data,arg_data,2);	
            break;
        }
		case OPCODE_GENERAL_SET_DELAY_OFF_TIME:    			    //延时关机
        {
			Set_CMD_Handler(device_type,OPCODE_GENERAL_SET_DELAY_OFF_TIME,recv_data.ARG,recv_data.ARG_len);
            memset(arg_data,0,sizeof(arg_data));
            arg_data[0] = recv_data.ARG[0];
            arg_data[1] = recv_data.ARG[1];
            ack_dev_info(recv_data,arg_data,2);	
            break;
        }
		 case OPCODE_GENERAL_SET_SWING_MODE:    			    //设定摆风模式
        {
			Set_CMD_Handler(device_type,OPCODE_GENERAL_SET_DELAY_OFF_TIME,recv_data.ARG,recv_data.ARG_len);
            memset(arg_data,0,sizeof(arg_data));
            arg_data[0] = recv_data.ARG[0];
            arg_data[1] = recv_data.ARG[1];
            ack_dev_info(recv_data,arg_data,2);	
            break;
        }
		case OPCODE_GENERAL_SET_SCREEN_SAVE_TIME:    			//设定屏保时间
        {
			Set_CMD_Handler(device_type,OPCODE_GENERAL_SET_SCREEN_SAVE_TIME,recv_data.ARG,recv_data.ARG_len);
            memset(arg_data,0,sizeof(arg_data));
            arg_data[0] = recv_data.ARG[0];
            arg_data[1] = recv_data.ARG[1];
            ack_dev_info(recv_data,arg_data,2);	
            break;
        }
		case OPCODE_GENERAL_SET_FILTER_CLEANING_TIME:    	   //设定滤网清洗时间
        {
			Set_CMD_Handler(device_type,OPCODE_GENERAL_SET_FILTER_CLEANING_TIME,recv_data.ARG,recv_data.ARG_len);
            memset(arg_data,0,sizeof(arg_data));
            arg_data[0] = recv_data.ARG[0];
            arg_data[1] = recv_data.ARG[1];
			arg_data[2] = recv_data.ARG[2];
            ack_dev_info(recv_data,arg_data,3);	
            break;
        }
		case OPCODE_GENERAL_CURRENT_REAL_TEMPER:    			//同步当时温度
        {
			Set_CMD_Handler(device_type,OPCODE_GENERAL_CURRENT_REAL_TEMPER,recv_data.ARG,recv_data.ARG_len);
            memset(arg_data,0,sizeof(arg_data));
            arg_data[0] = recv_data.ARG[0];
            arg_data[1] = recv_data.ARG[1];
			arg_data[2] = recv_data.ARG[2];
            ack_dev_info(recv_data,arg_data,3);
            break;
        }
		case OPCODE_GENERAL_SET_TEMPER_CALIBRATION:    			//设定校准温度
        {
			Set_CMD_Handler(device_type,OPCODE_GENERAL_SET_TEMPER_CALIBRATION,recv_data.ARG,recv_data.ARG_len);
            memset(arg_data,0,sizeof(arg_data));
            arg_data[0] = recv_data.ARG[0];
            arg_data[1] = recv_data.ARG[1];
            ack_dev_info(recv_data,arg_data,2);	
            break;
        }
		case OPCODE_GENERAL_SET_NO_DISTURB_MODE:    			//设定勿扰模式
        {
			Set_CMD_Handler(device_type,OPCODE_GENERAL_SET_NO_DISTURB_MODE,recv_data.ARG,recv_data.ARG_len);
            memset(arg_data,0,sizeof(arg_data));
            arg_data[0] = recv_data.ARG[0];
            arg_data[1] = recv_data.ARG[1];
            ack_dev_info(recv_data,arg_data,2);	
            break;
        }	
	}
}

/////***********************************************************************
////*   函 数 名: not_transparent_data_analyze
////*   功能说明: 非透传数据解析
////*   形    参：recv_data 串口接收到的信息
////*   返 回 值: 无
////***********************************************************************/
void not_transparent_data_analyze(PROTOCOL_DEAL recv_data)
{
	//static u8 arg_data[15] = {0};
	//printf("#########not_transparent_data_analyze,opcode = 0x%2x,=%d,payload :\r\n",recv_data.Opcode,recv_data.Channel_ID);
	//Print_Buff((u8 *)recv_data.ARG,(u8)recv_data.ARG_len);
    switch(recv_data.Opcode)
    {
		case _OPCODE_SET_NET_STATE:
		{
			u8 network_stat = recv_data.ARG[1];
			//清除本地绑定信息
			if(network_stat == 0)
			{
				Oled_Display_Info.binding_device = 0;				//清除绑定信息
				
				kNetIndicatorOptTrigger(0, 200, 200, (5*1000)/(200+200), 1, 0 );
				if(Oled_Display_Info.device_type == 1)
					Oled_Display_Info.Air_Device.no_disturb_mode = 0;
				else if(Oled_Display_Info.device_type == 2)
					Oled_Display_Info.FLOOR_Device.no_disturb_mode = 0;
				else if(Oled_Display_Info.device_type == 3)
					Oled_Display_Info.FAN_Device.no_disturb_mode = 0;
				save_data();
			}
			break;
		}
		case OPCODE_GENERAL_STATUS_QUERY:             	   			   	 //查询设备状态
        {			
			Oled_Display_Info.zigbee_Version = recv_data.ARG[1];
			network_state_machine(recv_data.ARG[2]);
			//printf("data = %x,%x\r\n",recv_data.ARG[1],recv_data.ARG[2]);
            break;
        }
        
        default:
        {
            break;
        }
    }
}
/***********************************************************************
*   函 数 名: key_state_change_report
*   功能说明: 触发状态上报
*   形    参：opcode 、channel 、state
*   返 回 值: 无
***********************************************************************/ 
void state_change_report(TP_u16 opcode, TP_u8 channel)
{
    PROTOCOL_SEND send_data;

    TP_u16 send_data_SEQ;

    Update_Dev_Info(channel);	  //更新系统状态信息

	if(send_data_seq < 0xfe)
		send_data_seq ++;
	else
		send_data_seq = 0;
    send_data_SEQ = send_data_seq;

    send_data.ARG           = &Device_status[1];
    send_data.ARG_len       = Device_status[0];
    send_data.Channel_ID    = channel;
    send_data.ControlField  = 0x10;
    send_data.Opcode        = opcode;    
    transparent_protocol_send(send_data, send_data_SEQ);
}

///***********************************************************************
//*   函 数 名: Send_Device_Data
//*   功能说明: 设备发送命令
//*   形    参：
//*   返 回 值: 无
//***********************************************************************/
void  Send_Device_Data(u8 channel_id,u16 opcode ,u8 *arg,u8 len)
{
	static  u8  send_data_SEQ  = 0;
	
    PROTOCOL_SEND send_data;  
    
    send_data.ARG          = arg;
    send_data.ARG_len      = len;
    send_data.Channel_ID   = channel_id;
    send_data.ControlField = 0x10;
    send_data.Opcode       = opcode;
    
	if(send_data_seq < 0xfe)
		send_data_seq ++;
	else
		send_data_seq = 0;
	
	send_data_SEQ = send_data_seq;
    
    transparent_protocol_send(send_data, send_data_SEQ);
}
/***********************************************************************
*   函 数 名: network_state_machine
*   功能说明: 网络状态机
*   形    参：recv_network_state 串口接收到的网络状态
*   返 回 值: 无
***********************************************************************/
static void network_state_machine(TP_uc8 recv_network_state)
{  
	switch(recv_network_state)
	{
		case OUT_LINE:                        //未入网
		case JOIN_FAILED:                     //未入网
		{    
			Oled_Display_Info.net_work_flage    = OUT_LINE;
			kNetIndicatorOptTrigger(0, 0, 0, 0, 0, 0 );	
			break;       
		} 
		case IN_LINE:                         //已入网
		{
			kNetIndicatorOptTrigger(0, 0, 0, 0, 1, 1 );
			Oled_Display_Info.net_work_flage    = IN_LINE;				
			break;        
		}
		case IN_LINE_NETWORKING:              //已入网开启组网
		{
			Oled_Display_Info.net_work_flage    = IN_LINE_NETWORKING;	
			kNetIndicatorOptTrigger(0, 800, 800, (3*60*1000)/(800+800), 0, 1 );						
			break;       
		}
		default :
		break;
	}
}
u8 Get_Channel_Id(u8 device_type)
{
	u8 channel_id = 0;
	switch(device_type)
	{
		case AIR_CONDITION:
			channel_id = 1;
			break;
		case FLOOR_HEATING:
			channel_id = 2;
			break;
		case FAN:
			channel_id = 3;
			break;
	}
	return channel_id;
}
void Print_Buff(u8 *data,u8 len)
{
	u8 i= 0;
	//printf("\r\n Buffer Data : ");
	while(i<len)
	{
		//printf("%4x",data[i]);
		i++;
	}
	//printf("\n");
}


