#include "includes.h"


void Dev_ColorTemp_Dis(uint32_t DevAddr);
void BLV_ColorTemp_Control_State(uint32_t CfgDevAddIn, uint16_t DevInputAddr, uint32_t devaddr,uint16_t DevOutputLoop,uint16_t start);
void ColorTemp_Control_MapOutput(ColorTemp_LoopMap *loop_info,uint8_t loop,uint8_t bright,uint8_t color_temp);
uint16_t BLV_ColorTemp_Read_State(uint32_t devaddr,uint16_t loop);
	
void BLV_Nor_Dev_ColorTemp_Init(LOGICFILE_DEVICE_INFO *dev_info,uint8_t *data,uint16_t data_len){
	Device_Public_Information_G BUS_Public;
	NOR_ColorTemp_INFO ColorTemp_Inf;
	
	memset(&BUS_Public,0,sizeof(Device_Public_Information_G));
	memset(&ColorTemp_Inf,0,sizeof(NOR_ColorTemp_INFO));

	BUS_Public.addr = dev_info->addr;		//设备地址
	BUS_Public.type = DEV_Virtual_ColorTemp;	//设备类型
	BUS_Public.port = dev_info->type;		//设备发送端口
	BUS_Public.baud = 0000;			        //设备发送波特率

	BUS_Public.DevFunInfo.Dev_Data_Process = Dev_ColorTemp_Dis;                  //
	BUS_Public.DevFunInfo.Dev_Output_Ctrl = BLV_ColorTemp_Control_State;         //
	BUS_Public.DevFunInfo.Dev_Output_Loop_State_Get = BLV_ColorTemp_Read_State;
	
	/*RCU根据MCU复位状态进行设备初始化 - 
		保存标志位成立的情况下，如果是看门狗复位、软件复位、外部手动复位，将之前保存的状态进行设备初始化
		否则采用默认状态初始化(默认状态可通过RCU_POWER_Deivce_State_Init 宏定义 修改默认初始化状态)
	*/
	if( (DevActionGlobal.sram_save_flag == 0xA8) && ((SRAM_Read_DW(SRAM_LOG_RCU_Reboot_Reason) == 0x01) || (SRAM_Read_DW(SRAM_LOG_RCU_Reboot_Reason) == 0x03) || (SRAM_Read_DW(SRAM_LOG_RCU_Reboot_Reason) == 0x05)) )
	{
		ColorTemp_Inf.global_light = DevActionGlobal.DimGlobalValue;
		ColorTemp_Inf.global_light_last = DevActionGlobal.DimGlobalValue;
	}else{
		ColorTemp_Inf.global_light = 80;
		ColorTemp_Inf.global_light_last = 80;		
	}
	
	Add_Nor_Device_To_List2(&BUS_Public,(uint8_t *)&ColorTemp_Inf,sizeof(NOR_ColorTemp_INFO));
}

/*色温绑定回路*/
void Dev_ColorTemp_LoopBound(uint32_t DevAddr,uint8_t *data,uint16_t len){
	Device_Public_Information_G BUS_Public;
	NOR_ColorTemp_INFO ColorTemp_Inf;
	uint8_t temp_num = 0x00;
	uint8_t temp_idenx = 0x00;
	
	uint32_t tempaddr = 0;
	
	Dbg_Println(DBG_BIT_Debug_STATUS_bit,"%s ",__func__);
	
	if(NULL == DevAddr)		return ;
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),DevAddr);	//拷贝所有共有数据	
	SRAM_DMA_Read_Buff((uint8_t *)&ColorTemp_Inf,sizeof(NOR_ColorTemp_INFO),DevAddr+Dev_Privately); 
	
	if(data[0] == BUS_Public.addr){
		
		if(data[1] <= DNOR_ColorTemp_LoopNumMax){
			temp_num = data[1];
		}else{
			Dbg_Println(DBG_BIT_Debug_STATUS_bit,"%s Loop Max !",__func__);
		}
		
		temp_num = data[1] * 16 + 2;
		if(temp_num <= len){
			/*限制设置输出回路 - 目前最大为 DNOR_ColorTemp_LoopNumMax*/
			if(data[1] <= DNOR_ColorTemp_LoopNumMax){
				temp_num = data[1];
			}else{
				temp_num = DNOR_ColorTemp_LoopNumMax;
			}
			
			for(uint8_t i=0;i<temp_num;i++){
				ColorTemp_Inf.Map_Loop_Info[i].type = data[2 + i*16];
				
				Dbg_Println(DBG_BIT_Debug_STATUS_bit,"Map Loop Info Type:%d",data[2]);
				/*添加色温映射输出回路*/
				for(uint8_t j=0;j<DNOR_ColorTemp_MapLoopNum;j++){
					temp_idenx = 2 + i*16 + j*3 + 1;
					ColorTemp_Inf.Map_Loop_Info[i].Map_Type[j] = data[temp_idenx ];
					ColorTemp_Inf.Map_Loop_Info[i].Map_Addr[j] = data[temp_idenx + 1];
					ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[j] = data[temp_idenx + 2];
					
					if( (ColorTemp_Inf.Map_Loop_Info[i].Map_Type[j] != 0x00) && (ColorTemp_Inf.Map_Loop_Info[i].Map_Type[j] != 0x00) ){
						/*找到映射输出对应设备的地址*/
						ColorTemp_Inf.Map_Loop_Info[i].Map_DevAddr[j] = Find_Device_List_Information(ColorTemp_Inf.Map_Loop_Info[i].Map_Type[j],ColorTemp_Inf.Map_Loop_Info[i].Map_Addr[j]);
						
						if(tempaddr != ColorTemp_Inf.Map_Loop_Info[i].Map_DevAddr[j])
						{
							tempaddr = ColorTemp_Inf.Map_Loop_Info[i].Map_DevAddr[j];
							if(ColorTemp_Inf.MapDev_Inf.MapDevNum < CCT_DEVMAP_LIST_MAX)
							{
								ColorTemp_Inf.MapDev_Inf.MapDevAddr[ColorTemp_Inf.MapDev_Inf.MapDevNum] = tempaddr;
								ColorTemp_Inf.MapDev_Inf.MapDevNum++;
							}
						}
						
					}
					
					Dbg_Println(DBG_BIT_Debug_STATUS_bit,"Map Loop %d:%02x %02x %02x %08x",j,	\
						ColorTemp_Inf.Map_Loop_Info[i].Map_Type[j],		\
						ColorTemp_Inf.Map_Loop_Info[i].Map_Addr[j],		\
						ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[j],	\
						ColorTemp_Inf.Map_Loop_Info[i].Map_DevAddr[j]
					);
				}
			}
		}else{
			Dbg_Println(DBG_BIT_Debug_STATUS_bit,"%s Len Error !",__func__);
		}
	}else{
		Dbg_Println(DBG_BIT_Debug_STATUS_bit,"%s Addr Error !",__func__);
	}
	
	BUS_Public.check = 0x00;
	BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&ColorTemp_Inf, sizeof(NOR_ColorTemp_INFO));
	SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),DevAddr);/*将数据保存*/
	SRAM_DMA_Write_Buff((uint8_t *)&ColorTemp_Inf,sizeof(NOR_ColorTemp_INFO),DevAddr+Dev_Privately);	
}

/*判断映射回路地址是否为同一个设备*/
uint8_t ColorTemp_MapAddr_Same_Judge(NOR_ColorTemp_INFO *dev_info,uint8_t loop){
	uint8_t temp_loop = 0;
	
	Dbg_Println(DBG_BIT_Debug_STATUS_bit,"Loop%d - Type %d",loop,dev_info->Map_Loop_Info[loop].type);
	
	if(dev_info->Map_Loop_Info[loop].type == 0x01){
		temp_loop = 0x02;
	}else if(dev_info->Map_Loop_Info[loop].type == 0x02){
		temp_loop = 0x03;
	}else if(dev_info->Map_Loop_Info[loop].type == 0x03){
		temp_loop = 0x04;
	}else if(dev_info->Map_Loop_Info[loop].type == 0x04){
		temp_loop = 0x05;
	}else {
		return 0xFF;
	}
	
	for(uint8_t i=1;i<temp_loop;i++){
		if(dev_info->Map_Loop_Info[loop].Map_DevAddr[0] != dev_info->Map_Loop_Info[loop].Map_DevAddr[i]){
			
			Dbg_Println(DBG_BIT_Debug_STATUS_bit,"ColorTemp LoopAddr %d - %08x %08x",i,dev_info->Map_Loop_Info[loop].Map_DevAddr[0],dev_info->Map_Loop_Info[loop].Map_DevAddr[i]);
			return 0x01;
		}
	}
	
	return 0x00;
	
}

//void Dev_ColorTemp_Dis(uint32_t DevAddr){
//	Device_Public_Information_G BUS_Public;
//	NOR_ColorTemp_INFO ColorTemp_Inf;
//	uint8_t ctrl_state = 0;
//	uint16_t ctrl_loop[5];
//	uint16_t ctrl_loop_state[LED_BUFF_Size];
//	uint32_t ctrl_flag = 0;
//	uint8_t ctrl_num = 0,save_flag = 0;
//	
//	uint8_t dev_type = 0x00;
//	
//	if(NULL == DevAddr)
//	{
//		return ;
//	}
//	
//	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),DevAddr);	//拷贝所有共有数据	
//	SRAM_DMA_Read_Buff((uint8_t *)&ColorTemp_Inf,sizeof(NOR_ColorTemp_INFO),DevAddr+Dev_Privately); 
//	
//	
//	for(uint8_t n = 0; n < ColorTemp_Inf.MapDev_Inf.MapDevNum; n++)
//	{
//		ctrl_flag = 0x00;
//		ctrl_num = 0x00;
//		memset(ctrl_loop_state,0,sizeof(ctrl_loop_state));
//		
//		//对比是否需要控制
//		for(uint8_t i = 0;i < DNOR_ColorTemp_LoopNumMax; i++)
//		{
//			memset(ctrl_loop,0,sizeof(ctrl_loop));
//			
//			if(ColorTemp_Inf.MapDev_Inf.MapDevAddr[n] == ColorTemp_Inf.Map_Loop_Info[i].Map_DevAddr[0])
//			{
//				if( (ColorTemp_Inf.loop_bright[i] != ColorTemp_Inf.last_loop_bright[i]) \
//				 || (ColorTemp_Inf.loop_ColorTemp[i] != ColorTemp_Inf.last_loop_ColorTemp[i]) )
//				{
//					
//					if(ColorTemp_Inf.loop_bright[i] != ColorTemp_Inf.last_loop_bright[i])
//					{
//						if(ColorTemp_Inf.loop_bright[i] != 0x00)
//						{
//							Udp_Addtion_Roomstate(DEV_Virtual_ColorTemp,BUS_Public.addr,i+1,((ColorTemp_Inf.loop_bright[i] << 8) | 0x0001));
//						}
//						else
//						{
//							Udp_Addtion_Roomstate(DEV_Virtual_ColorTemp,BUS_Public.addr,i+1,((ColorTemp_Inf.loop_bright[i] << 8) | 0x0002));
//						}
//					}
//					
//					if(ColorTemp_Inf.loop_ColorTemp[i] != ColorTemp_Inf.last_loop_ColorTemp[i])
//					{
//						Udp_Addtion_Roomstate(Dev_Rs485_PB20,BUS_Public.addr,i+1,((ColorTemp_Inf.loop_ColorTemp[i] << 8) | 0x0003));
//					}

//					
//					Dbg_Println(DBG_BIT_Debug_STATUS_bit,"ColorTemp Loop%d - Light:%02x  CCT:%02x",i,ColorTemp_Inf.loop_bright[i],ColorTemp_Inf.loop_ColorTemp[i]);
//					ColorTemp_Inf.last_loop_bright[i] = ColorTemp_Inf.loop_bright[i];
//					ColorTemp_Inf.last_loop_ColorTemp[i] = ColorTemp_Inf.loop_ColorTemp[i];
//						
//					/*判断输出回路是否都是同一个地址*/
//					if(ColorTemp_MapAddr_Same_Judge(&ColorTemp_Inf,i) == 0x00){
//						
//						/*目前只针对 C12 进行色温调节 , 而且目前只有双色*/
//						ctrl_state = (ColorTemp_Inf.loop_ColorTemp[i] * ColorTemp_Inf.loop_bright[i]) / 100;			//回路1 输出亮度
//						ctrl_loop[0] = (ctrl_state << 8);    //0x01 + (ctrl_state << 8);
//						
//						ctrl_state = (100 - ColorTemp_Inf.loop_ColorTemp[i]) * ColorTemp_Inf.loop_bright[i] / 100;		//回路2 输出亮度
//						ctrl_loop[1] = (ctrl_state << 8);    //0x01 + (ctrl_state << 8);
//						
//						if(ColorTemp_Inf.Map_Loop_Info[i].Map_Type[0] == ColorTemp_Inf.Map_Loop_Info[i].Map_Type[1])    //2024-10-09   增加对C8调光支持
//						{
//							switch(ColorTemp_Inf.Map_Loop_Info[i].Map_Type[0])
//							{
//								case DEV_RS485_PWM:      //C12等485类型调光
//									ctrl_loop[0] += 0x01;
//									ctrl_loop[1] += 0x01;
//									dev_type = DEV_RS485_PWM;
//									break;
//								#if C8_TYPE
//								case Dev_Rs485_PB20:     //C8的PLC调光
//									ctrl_loop[0] += 0x12;
//									ctrl_loop[1] += 0x12;
//									dev_type = Dev_Rs485_PB20;
//									break;
//								case Dev_Rs485_PB20_LS:
//									ctrl_loop[0] += 0x12;
//									ctrl_loop[1] += 0x12;
//									dev_type = Dev_Rs485_PB20_LS;
//									break;
//								#endif
//							}
//						}
//						
//						if( (ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[0] != 0x00) && (ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[0] < LED_BUFF_Size) ){
//							
//							ctrl_flag |= 0x01 << (ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[0] - 1);
//							ctrl_num = ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[0];
//							
//							ctrl_loop_state[ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[0] - 1] = ctrl_loop[0];
//						}
//						
//						if((ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[1] != 0x00) && (ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[1] < LED_BUFF_Size) ) {
//							ctrl_flag |= 0x01 << (ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[1] - 1);
//							
//							if(ctrl_num <= ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[1] ){
//								ctrl_num = ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[1];	//最大的回路号
//							}
//							
//							ctrl_loop_state[ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[1] - 1] = ctrl_loop[1];
//						}
//						
//						Dbg_Println(DBG_BIT_Debug_STATUS_bit,"Loop%d - 0 %04x",i,ctrl_loop[0]);
//						Dbg_Println(DBG_BIT_Debug_STATUS_bit,"Loop%d - 1 %04x",i,ctrl_loop[1]);
//						Dbg_Println(DBG_BIT_Debug_STATUS_bit,"Map %d %d : %d %08x",\
//						ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[0],ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[1],ctrl_num,ctrl_flag);
//						
//					}
//					
//					save_flag = 0x01;
//				}
//			}
//		}
//		
//		switch(dev_type)
//		{
//			case DEV_RS485_PWM:      //C12等485类型调光
//				BLW_LED_Group_Ctrl(0x00, 0x00,ColorTemp_Inf.MapDev_Inf.MapDevAddr[n],ctrl_flag,ctrl_num,ctrl_loop_state);
//				break;
//			#if C8_TYPE
//			case Dev_Rs485_PB20:     
//				BLV_PB20_Group_Control_State(0x00, 0x00,ColorTemp_Inf.MapDev_Inf.MapDevAddr[n],ctrl_flag,ctrl_num,ctrl_loop_state);
//				break;
//			case Dev_Rs485_PB20_LS:
//				BLV_PB20_Strip_Group_Control_State(0x00, 0x00,ColorTemp_Inf.MapDev_Inf.MapDevAddr[n],ctrl_flag,ctrl_num,ctrl_loop_state);
//				break;
//			#endif
//		}
//		
//	
//	}	
//		
//	if(ColorTemp_Inf.global_light != ColorTemp_Inf.global_light_last)      //全局亮度设置
//	{
//		ColorTemp_Inf.global_light_last = ColorTemp_Inf.global_light;
//		Dbg_Println(DBG_BIT_Debug_STATUS_bit,"ColorTemp Global_light:%d set",ColorTemp_Inf.global_light);
//		
//		for(uint8_t n = 0; n < ColorTemp_Inf.MapDev_Inf.MapDevNum; n++)
//		{
//			ctrl_flag = 0x00;
//			ctrl_num = 0x00;
//			memset(ctrl_loop_state,0,sizeof(ctrl_loop_state));
//			
//			ctrl_flag |= (0x01 << 0);
//			ctrl_num = 1;
//			
//			for(uint8_t i = 0;i < DNOR_ColorTemp_LoopNumMax; i++)
//			{
//				if(ColorTemp_Inf.MapDev_Inf.MapDevAddr[n] == ColorTemp_Inf.Map_Loop_Info[i].Map_DevAddr[0])
//				{
//					switch(ColorTemp_Inf.Map_Loop_Info[i].Map_Type[0])
//					{
//						case DEV_RS485_PWM:      //C12等485类型调光
//							ctrl_loop_state[0] = (0x30 + (ColorTemp_Inf.global_light << 8)); 
//							BLW_LED_Group_Ctrl(0x00, 0x00,ColorTemp_Inf.MapDev_Inf.MapDevAddr[n],ctrl_flag,ctrl_num,ctrl_loop_state);
//							break;
//						#if C8_TYPE
//						case Dev_Rs485_PB20:
//						case Dev_Rs485_PB20_LS:	
//							ctrl_loop_state[0] = (0x0A + (ColorTemp_Inf.global_light << 8));
//							uint32_t tempaddr = Find_AllDevice_List_Information2(Bus_port,Dev_Rs485_PB20,0x01);
//							BLV_PB20_Group_Control_State(0x00, 0x00,tempaddr,ctrl_flag,ctrl_num,ctrl_loop_state);
//							n = ColorTemp_Inf.MapDev_Inf.MapDevNum;      //C8中所有PB20-LS都挂载在一个总线，只控制一次
//							break;
//						#endif
//					}
//					break;
//				}
//			}
//		}
//		save_flag = 0x01;
//	}
//		
//	
//	
//	if(save_flag == 0x01)
//	{
//		//保存色温信息
//		BUS_Public.check = 0x00;
//		BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&ColorTemp_Inf, sizeof(NOR_ColorTemp_INFO));
//		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),DevAddr);/*将数据保存*/
//		SRAM_DMA_Write_Buff((uint8_t *)&ColorTemp_Inf,sizeof(NOR_ColorTemp_INFO),DevAddr+Dev_Privately);	
//	}
//	
//}

void Dev_ColorTemp_Dis(uint32_t DevAddr){
	Device_Public_Information_G BUS_Public;
	NOR_ColorTemp_INFO ColorTemp_Inf;
	uint8_t ctrl_state = 0;
	uint16_t ctrl_loop[5];
	uint16_t ctrl_loop_state[LED_BUFF_Size];
	uint32_t ctrl_flag = 0;
	uint8_t ctrl_num = 0,save_flag = 0;
	
	uint8_t dev_type = 0x00;
	
	if(NULL == DevAddr)
	{
		return ;
	}
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),DevAddr);	//拷贝所有共有数据	
	SRAM_DMA_Read_Buff((uint8_t *)&ColorTemp_Inf,sizeof(NOR_ColorTemp_INFO),DevAddr+Dev_Privately); 
	
	
	for(uint8_t n = 0; n < ColorTemp_Inf.MapDev_Inf.MapDevNum; n++)
	{
		ctrl_flag = 0x00;
		ctrl_num = 0x00;
		memset(ctrl_loop_state,0,sizeof(ctrl_loop_state));
		
		//对比是否需要控制
		for(uint8_t i = 0;i < DNOR_ColorTemp_LoopNumMax; i++)   //0702，遍历所有回路
		{
			memset(ctrl_loop,0,sizeof(ctrl_loop));
			
			if(ColorTemp_Inf.MapDev_Inf.MapDevAddr[n] == ColorTemp_Inf.Map_Loop_Info[i].Map_DevAddr[0])     //比对回路地址
			{
                //1、2025-07-02,YYW，亮度、开关状态变化
                if( (ColorTemp_Inf.Lt_Flag != 0x00) )//||((ColorTemp_Inf.loop_bright[i] != ColorTemp_Inf.last_loop_bright[i])) )               
				{	
                   ColorTemp_Inf.Lt_Flag &= ~(0x01<<i);
                    
					if(ColorTemp_Inf.loop_bright[i] != ColorTemp_Inf.last_loop_bright[i])           //亮度变化处理
					{
						if((ColorTemp_Inf.loop_bright[i]&0x7f) != 0x00)
						{
							Udp_Addtion_Roomstate(DEV_Virtual_ColorTemp,BUS_Public.addr,i+1,(((ColorTemp_Inf.loop_bright[i]&0x7f) << 8) | 0x0001));
						}
						else
						{
							Udp_Addtion_Roomstate(DEV_Virtual_ColorTemp,BUS_Public.addr,i+1,(((ColorTemp_Inf.loop_bright[i]&0x7f) << 8) | 0x0002));
						}
					}
					
					if(ColorTemp_Inf.loop_ColorTemp[i] != ColorTemp_Inf.last_loop_ColorTemp[i])     //色温变化处理
					{
						Udp_Addtion_Roomstate(Dev_Rs485_PB20,BUS_Public.addr,i+1,((ColorTemp_Inf.loop_ColorTemp[i] << 8) | 0x0003));
					}

					
					Dbg_Println(DBG_BIT_Debug_STATUS_bit,"ColorTemp111 Loop%d - Light:%02x  CCT:%02x",i,ColorTemp_Inf.loop_bright[i],ColorTemp_Inf.loop_ColorTemp[i]);
					ColorTemp_Inf.last_loop_bright[i] = ColorTemp_Inf.loop_bright[i];               //更新记录的上个亮度
					ColorTemp_Inf.last_loop_ColorTemp[i] = ColorTemp_Inf.loop_ColorTemp[i];         //更新记录的上个色温
						
					/*判断输出回路是否都是同一个地址*/
					if(ColorTemp_MapAddr_Same_Judge(&ColorTemp_Inf,i) == 0x00){
						
						/*目前只针对 C12 进行色温调节 , 而且目前只有双色*/
						ctrl_state = (ColorTemp_Inf.loop_ColorTemp[i] * (ColorTemp_Inf.loop_bright[i]&0x7f)) / 100;			//回路1 输出亮度
						ctrl_loop[0] = (ctrl_state << 8);//( (ctrl_state | (ColorTemp_Inf.loop_bright[i]&0x80)) << 8);    //0x01 + (ctrl_state << 8);
						
						ctrl_state = (100 - ColorTemp_Inf.loop_ColorTemp[i]) * (ColorTemp_Inf.loop_bright[i]&0x7f) / 100;		//回路2 输出亮度
						ctrl_loop[1] = (ctrl_state << 8);//( (ctrl_state | (ColorTemp_Inf.loop_bright[i]&0x80)) << 8);    //0x01 + (ctrl_state << 8);
						
						if(ColorTemp_Inf.Map_Loop_Info[i].Map_Type[0] == ColorTemp_Inf.Map_Loop_Info[i].Map_Type[1])    //2024-10-09   增加对C8调光支持
						{
							switch(ColorTemp_Inf.Map_Loop_Info[i].Map_Type[0])
							{
								case DEV_RS485_PWM:      //C12等485类型调光
									ctrl_loop[0] += 0x01;
									ctrl_loop[1] += 0x01;
									dev_type = DEV_RS485_PWM;
									break;
								#if C8_TYPE
								case Dev_Rs485_PB20:     //C8的PLC调光
									ctrl_loop[0] += ColorTemp_Inf.CtrlMode[i];
									ctrl_loop[1] += ColorTemp_Inf.CtrlMode[i];
									dev_type = Dev_Rs485_PB20;
									break;
								case Dev_Rs485_PB20_LS:
									ctrl_loop[0] += ColorTemp_Inf.CtrlMode[i];
									ctrl_loop[1] += ColorTemp_Inf.CtrlMode[i];
									dev_type = Dev_Rs485_PB20_LS;
									break;
								#endif
							}
						}
						
						if( (ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[0] != 0x00) && (ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[0] < LED_BUFF_Size) ){
							
							ctrl_flag |= 0x01 << (ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[0] - 1);
							ctrl_num = ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[0];
							
							ctrl_loop_state[ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[0] - 1] = ctrl_loop[0];
						}
						
						if((ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[1] != 0x00) && (ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[1] < LED_BUFF_Size) ) {
							ctrl_flag |= 0x01 << (ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[1] - 1);
							
							if(ctrl_num <= ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[1] ){
								ctrl_num = ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[1];	//最大的回路号
							}
							
							ctrl_loop_state[ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[1] - 1] = ctrl_loop[1];
						}
						
						Dbg_Println(DBG_BIT_Debug_STATUS_bit,"1Loop%d - 0 %04x",i,ctrl_loop[0]);
						Dbg_Println(DBG_BIT_Debug_STATUS_bit,"1Loop%d - 1 %04x",i,ctrl_loop[1]);
						Dbg_Println(DBG_BIT_Debug_STATUS_bit,"1Map %d %d : %d %08x",\
						ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[0],ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[1],ctrl_num,ctrl_flag);
						
					}
					
					save_flag = 0x01;
				}
                
                //2、2025-07-02,YYW，色温变化调节
                if((ColorTemp_Inf.loop_ColorTemp[i] != ColorTemp_Inf.last_loop_ColorTemp[i]) )                
				{	
					if(ColorTemp_Inf.loop_bright[i] != ColorTemp_Inf.last_loop_bright[i])        
					{
						if((ColorTemp_Inf.loop_bright[i]&0x7f) != 0x00)
						{
							Udp_Addtion_Roomstate(DEV_Virtual_ColorTemp,BUS_Public.addr,i+1,(((ColorTemp_Inf.loop_bright[i]&0x7f) << 8) | 0x0001));
						}
						else
						{
							Udp_Addtion_Roomstate(DEV_Virtual_ColorTemp,BUS_Public.addr,i+1,(((ColorTemp_Inf.loop_bright[i]&0x7f) << 8) | 0x0002));
						}
					}
					
					if(ColorTemp_Inf.loop_ColorTemp[i] != ColorTemp_Inf.last_loop_ColorTemp[i])     //色温变化处理
					{
						Udp_Addtion_Roomstate(Dev_Rs485_PB20,BUS_Public.addr,i+1,((ColorTemp_Inf.loop_ColorTemp[i] << 8) | 0x0003));
					}

					
					Dbg_Println(DBG_BIT_Debug_STATUS_bit,"ColorTemp222 Loop%d - Light:%02x  CCT:%02x",i,ColorTemp_Inf.loop_bright[i],ColorTemp_Inf.loop_ColorTemp[i]);
					ColorTemp_Inf.last_loop_bright[i] = ColorTemp_Inf.loop_bright[i];               //更新记录的上个亮度
					ColorTemp_Inf.last_loop_ColorTemp[i] = ColorTemp_Inf.loop_ColorTemp[i];         //更新记录的上个色温
						
					/*判断输出回路是否都是同一个地址*/
					if(ColorTemp_MapAddr_Same_Judge(&ColorTemp_Inf,i) == 0x00){
						
						/*目前只针对 C12 进行色温调节 , 而且目前只有双色*/
						ctrl_state = (ColorTemp_Inf.loop_ColorTemp[i] * (ColorTemp_Inf.loop_bright[i]&0x7f)) / 100;			//回路1 输出亮度
						ctrl_loop[0] = (ctrl_state << 8);//( (ctrl_state | (ColorTemp_Inf.loop_bright[i]&0x80)) << 8);    //0x01 + (ctrl_state << 8);
						
						ctrl_state = (100 - ColorTemp_Inf.loop_ColorTemp[i]) * (ColorTemp_Inf.loop_bright[i]&0x7f) / 100;		//回路2 输出亮度
						ctrl_loop[1] = (ctrl_state << 8);//( (ctrl_state | (ColorTemp_Inf.loop_bright[i]&0x80)) << 8);    //0x01 + (ctrl_state << 8);
						
						if(ColorTemp_Inf.Map_Loop_Info[i].Map_Type[0] == ColorTemp_Inf.Map_Loop_Info[i].Map_Type[1])    //2024-10-09   增加对C8调光支持
						{
							switch(ColorTemp_Inf.Map_Loop_Info[i].Map_Type[0])
							{
								case DEV_RS485_PWM:      //C12等485类型调光
									ctrl_loop[0] += 0x01;
									ctrl_loop[1] += 0x01;
									dev_type = DEV_RS485_PWM;
									break;
								#if C8_TYPE
								case Dev_Rs485_PB20:     //C8的PLC调光
									ctrl_loop[0] += 0x12;
									ctrl_loop[1] += 0x12;
									dev_type = Dev_Rs485_PB20;
									break;
								case Dev_Rs485_PB20_LS:
									ctrl_loop[0] += 0x12;
									ctrl_loop[1] += 0x12;
									dev_type = Dev_Rs485_PB20_LS;
									break;
								#endif
							}
						}
						
						if( (ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[0] != 0x00) && (ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[0] < LED_BUFF_Size) ){
							
							ctrl_flag |= 0x01 << (ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[0] - 1);
							ctrl_num = ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[0];
							
							ctrl_loop_state[ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[0] - 1] = ctrl_loop[0];
						}
						
						if((ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[1] != 0x00) && (ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[1] < LED_BUFF_Size) ) {
							ctrl_flag |= 0x01 << (ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[1] - 1);
							
							if(ctrl_num <= ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[1] ){
								ctrl_num = ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[1];	//最大的回路号
							}
							
							ctrl_loop_state[ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[1] - 1] = ctrl_loop[1];
						}
						
						Dbg_Println(DBG_BIT_Debug_STATUS_bit,"2Loop%d - 0 %04x",i,ctrl_loop[0]);
						Dbg_Println(DBG_BIT_Debug_STATUS_bit,"2Loop%d - 1 %04x",i,ctrl_loop[1]);
						Dbg_Println(DBG_BIT_Debug_STATUS_bit,"2Map %d %d : %d %08x",\
						ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[0],ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[1],ctrl_num,ctrl_flag);
						
					}
					
					save_flag = 0x01;
				}

                
                //3、2025-07-02,YYW，相对调节处理
                if((ColorTemp_Inf.AG_Flag & (0x01<<i))) 
                {
					ColorTemp_Inf.AG_Flag &= ~(0x01<<i);
                       
					/*判断输出回路是否都是同一个地址*/
					if(ColorTemp_MapAddr_Same_Judge(&ColorTemp_Inf,i) == 0x00){

                        if( ColorTemp_Inf.CtrlMode[i] == ColorTemp_PB20_OnlyBright)     //灯光开关状态
                        {
                            ctrl_state = ColorTemp_Inf.CtrlChange[i];
                            ctrl_loop[0] = (ctrl_state << 8);
                            ctrl_state = ColorTemp_Inf.CtrlChange[i];
                            ctrl_loop[1] = (ctrl_state << 8);
                        }else{                      //灯光相对调节
                            
                            //LS_190x的回路1
                            ctrl_state =  ColorTemp_Inf.loop_ColorTemp[i] * ColorTemp_Inf.CtrlChange[i] / 100; 
                            if((ColorTemp_Inf.loop_ColorTemp[i] != 0x00)&&(ColorTemp_Inf.CtrlChange[i] != 0x00)){
                                if(ctrl_state == 0x00){     //2025-07-02,YYW，防止设置的相对调节数值较小，在乘色温比例时直接被计算为0
                                    ctrl_state = 0x01;
                                }
                            }
                            ctrl_loop[0] = (ctrl_state << 8);    //0x01 + (ctrl_state << 8);
                            
                            //LS_190x的回路2
                            ctrl_state =  (100 - ColorTemp_Inf.loop_ColorTemp[i]) * ColorTemp_Inf.CtrlChange[i] / 100; 
                            if((ColorTemp_Inf.loop_ColorTemp[i] != 100)&&(ColorTemp_Inf.CtrlChange[i] != 0x00)){
                                if(ctrl_state == 0x00){     //2025-07-02,YYW，防止设置的相对调节数值较小，在乘色温比例时直接被计算为0
                                    ctrl_state = 0x01;   
                                }
                            }
                            ctrl_loop[1] = (ctrl_state << 8);    //0x01 + (ctrl_state << 8);                       
                        }

                        Dbg_Println(DBG_BIT_Debug_STATUS_bit,"ColorTemp333:%02x,CtrlTime:%02x,loop0:%04x,loop1:%04x",ColorTemp_Inf.loop_ColorTemp[i],ColorTemp_Inf.CtrlChange[i],ctrl_loop[0],ctrl_loop[1]);
                        
						if(ColorTemp_Inf.Map_Loop_Info[i].Map_Type[0] == ColorTemp_Inf.Map_Loop_Info[i].Map_Type[1])    //2024-10-09   增加对C8调光支持
						{
							switch(ColorTemp_Inf.Map_Loop_Info[i].Map_Type[0])
							{
								case DEV_RS485_PWM:      //C12等485类型调光
                                    switch(ColorTemp_Inf.CtrlMode[i]){
                                        case ColorTemp_PB20_OnlySwitch: //灯开关状态
                                            break;
                                        case ColorTemp_PB20_Dim_Up:
                                            ctrl_loop[0] += CFG_Dev_CtrlWay_Is_Dim_Up;
                                            ctrl_loop[1] += CFG_Dev_CtrlWay_Is_Dim_Up;
                                            dev_type = DEV_RS485_PWM;                                            
                                            break;
                                        case ColorTemp_PB20_Dim_Down:
                                            ctrl_loop[0] += CFG_Dev_CtrlWay_Is_Dim_Down;
                                            ctrl_loop[1] += CFG_Dev_CtrlWay_Is_Dim_Down;
                                            dev_type = DEV_RS485_PWM;                                           
                                            break;
                                        case ColorTemp_PB20_Dim_Up_Limit:
                                            ctrl_loop[0] += CFG_Dev_CtrlWay_Is_Dim_Up_Limit;
                                            ctrl_loop[1] += CFG_Dev_CtrlWay_Is_Dim_Up_Limit;
                                            dev_type = DEV_RS485_PWM;                                           
                                            break;
                                        case ColorTemp_PB20_Dim_Down_Limit:
                                            ctrl_loop[0] += CFG_Dev_CtrlWay_Is_Dim_Down_Limit;
                                            ctrl_loop[1] += CFG_Dev_CtrlWay_Is_Dim_Down_Limit;
                                            dev_type = DEV_RS485_PWM;                                            
                                            break;  
                                        default:
                                            break;
                                    }
									break;
								#if C8_TYPE
								case Dev_Rs485_PB20:     //C8的PLC调光
									ctrl_loop[0] += ColorTemp_Inf.CtrlMode[i];
									ctrl_loop[1] += ColorTemp_Inf.CtrlMode[i];
									dev_type = Dev_Rs485_PB20;
									break;
								case Dev_Rs485_PB20_LS:
									ctrl_loop[0] +=  ColorTemp_Inf.CtrlMode[i];
									ctrl_loop[1] +=  ColorTemp_Inf.CtrlMode[i];
									dev_type = Dev_Rs485_PB20_LS;
									break;
								#endif
							}
						}
						
						if( (ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[0] != 0x00) && (ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[0] < LED_BUFF_Size) ){
							
							ctrl_flag |= 0x01 << (ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[0] - 1);
							ctrl_num = ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[0];
							
							ctrl_loop_state[ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[0] - 1] = ctrl_loop[0];
						}
						
						if((ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[1] != 0x00) && (ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[1] < LED_BUFF_Size) ) {
							ctrl_flag |= 0x01 << (ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[1] - 1);
							
							if(ctrl_num <= ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[1] ){
								ctrl_num = ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[1];	//最大的回路号
							}
							
							ctrl_loop_state[ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[1] - 1] = ctrl_loop[1];
						}
						
						Dbg_Println(DBG_BIT_Debug_STATUS_bit,"3Loop%d - 0 %04x",i,ctrl_loop[0]);
						Dbg_Println(DBG_BIT_Debug_STATUS_bit,"3Loop%d - 1 %04x",i,ctrl_loop[1]);
						Dbg_Println(DBG_BIT_Debug_STATUS_bit,"3Map %d %d : %d %08x",\
						ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[0],ColorTemp_Inf.Map_Loop_Info[i].Map_DevLoop[1],ctrl_num,ctrl_flag);
						
					}
					
					save_flag = 0x01;                    
                }                
                
                
			}
		}
		
		switch(dev_type)
		{
			case DEV_RS485_PWM:      //C12等485类型调光
				BLW_LED_Group_Ctrl(0x00, 0x00,ColorTemp_Inf.MapDev_Inf.MapDevAddr[n],ctrl_flag,ctrl_num,ctrl_loop_state);
				break;
			#if C8_TYPE
			case Dev_Rs485_PB20:     
				BLV_PB20_Group_Control_State(0x00, 0x00,ColorTemp_Inf.MapDev_Inf.MapDevAddr[n],ctrl_flag,ctrl_num,ctrl_loop_state);
				break;
			case Dev_Rs485_PB20_LS:

				BLV_PB20_Strip_Group_Control_State(0x00, 0x00,ColorTemp_Inf.MapDev_Inf.MapDevAddr[n],ctrl_flag,ctrl_num,ctrl_loop_state);
				break;
			#endif
		}
		
	}	
    
    
    
    if(ColorTemp_Inf.global_light != DevActionGlobal.DimGlobalValue)     //2025-07-15,YYW,全局亮度比较
    {
        ColorTemp_Inf.global_light = DevActionGlobal.DimGlobalValue;
        Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"ColorTemp--全局亮度改变：%d",ColorTemp_Inf.global_light); 
    }  

	
	if(ColorTemp_Inf.global_light != ColorTemp_Inf.global_light_last)      //全局亮度设置
	{
		ColorTemp_Inf.global_light_last = ColorTemp_Inf.global_light;
		Dbg_Println(DBG_BIT_Debug_STATUS_bit,"ColorTemp Global_light:%d set",ColorTemp_Inf.global_light);
		
		for(uint8_t n = 0; n < ColorTemp_Inf.MapDev_Inf.MapDevNum; n++)
		{
			ctrl_flag = 0x00;
			ctrl_num = 0x00;
			memset(ctrl_loop_state,0,sizeof(ctrl_loop_state));
			
			ctrl_flag |= (0x01 << 0);
			ctrl_num = 1;
			
			for(uint8_t i = 0;i < DNOR_ColorTemp_LoopNumMax; i++)
			{
				if(ColorTemp_Inf.MapDev_Inf.MapDevAddr[n] == ColorTemp_Inf.Map_Loop_Info[i].Map_DevAddr[0])
				{
					switch(ColorTemp_Inf.Map_Loop_Info[i].Map_Type[0])
					{
						case DEV_RS485_PWM:      //C12等485类型调光
							ctrl_loop_state[0] = (0x30 + (ColorTemp_Inf.global_light << 8)); 
							BLW_LED_Group_Ctrl(0x00, 0x00,ColorTemp_Inf.MapDev_Inf.MapDevAddr[n],ctrl_flag,ctrl_num,ctrl_loop_state);
							break;
						#if C8_TYPE
						case Dev_Rs485_PB20:
						case Dev_Rs485_PB20_LS:	
							ctrl_loop_state[0] = (0x0A + (ColorTemp_Inf.global_light << 8));
							uint32_t tempaddr = Find_AllDevice_List_Information2(Bus_port,Dev_Rs485_PB20,0x01);
							BLV_PB20_Group_Control_State(0x00, 0x00,tempaddr,ctrl_flag,ctrl_num,ctrl_loop_state);
							n = ColorTemp_Inf.MapDev_Inf.MapDevNum;      //C8中所有PB20-LS都挂载在一个总线，只控制一次
							break;
						#endif
					}
					break;
				}
			}
		}
		save_flag = 0x01;
	}
		
	
	
	if(save_flag == 0x01)
	{
		//保存色温信息
		BUS_Public.check = 0x00;
		BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&ColorTemp_Inf, sizeof(NOR_ColorTemp_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),DevAddr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&ColorTemp_Inf,sizeof(NOR_ColorTemp_INFO),DevAddr+Dev_Privately);	
	}
	
}

void BLV_ColorTemp_Control_State(uint32_t CfgDevAddIn, uint16_t DevInputAddr, uint32_t devaddr,uint16_t DevOutputLoop,uint16_t start)
{
	Device_Public_Information_G BUS_Public;
	NOR_ColorTemp_INFO ColorTemp_Inf;
	uint8_t ctr_loop = DevOutputLoop & 0xFF;    //控制的回路号
	uint8_t ctr_state = (start >> 8) & 0xFF;    //控制内容参数
	uint8_t ctr_mode = start & 0xFF;            //控制模式

    uint8_t temp1 = 0x00;
	
	if(NULL == devaddr)
	{
		return ;
	}
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),devaddr);	//拷贝所有共有数据	
	SRAM_DMA_Read_Buff((uint8_t *)&ColorTemp_Inf,sizeof(NOR_ColorTemp_INFO),devaddr+Dev_Privately); 
	
	if((DevOutputLoop & 0xFF) >= DNOR_ColorTemp_LoopNumMax)	return ;
	
	switch(ctr_mode){
        
		case 0x01:	//开
			//ColorTemp_Inf.loop_bright[ctr_loop] = ctr_state;
            ColorTemp_Inf.loop_bright[ctr_loop] |= (0x01 << 7);         //灯开关状态
            ColorTemp_Inf.loop_bright[ctr_loop] &= 0x80;
            ColorTemp_Inf.loop_bright[ctr_loop] |= (ctr_state & 0x7f);
            
            ColorTemp_Inf.CtrlMode[ctr_loop] = ctr_mode;
            ColorTemp_Inf.Lt_Flag |= ( 0x01 << ctr_loop);
            temp1++;
			break;
		case 0x02:	//关
			//ColorTemp_Inf.loop_bright[ctr_loop] = 0x00;
            ColorTemp_Inf.loop_bright[ctr_loop] &= ~(0x01 << 7);       //灯开关状态
            ColorTemp_Inf.loop_bright[ctr_loop] &= 0x80;
            ColorTemp_Inf.loop_bright[ctr_loop] |= (ctr_state & 0x7f);     

            ColorTemp_Inf.CtrlMode[ctr_loop] = ctr_mode;
            ColorTemp_Inf.Lt_Flag |= ( 0x01 << ctr_loop);
            temp1++;
			break;       
		case 0x03:	//设定色温
			ColorTemp_Inf.loop_ColorTemp[ctr_loop] = ctr_state;
            temp1++;
			break;
		case 0x30:  //全局亮度
			ColorTemp_Inf.global_light = ctr_state;
            DevActionGlobal.DimGlobalValue = ColorTemp_Inf.global_light; //2025-07-10,YYW,全局亮度
            temp1++;
			break;
		case 0x40:  //全局色温
			for(uint8_t i = 0; i < DNOR_ColorTemp_LoopNumMax; i++)
			{
				ColorTemp_Inf.loop_ColorTemp[i] = ctr_state;
			}     

            DevActionGlobal.CCTValue = ctr_state;   //2025-07-10,YYW,全局色温
            temp1++;
			break;   
        case ColorTemp_PB20_OnlySwitch:         //灯开关状态
            if(ctr_state == 0x01){
                ColorTemp_Inf.CtrlChange[ctr_loop] = ctr_state;
                ColorTemp_Inf.loop_bright[ctr_loop] |= (0x01 << 7);
            }else if(ctr_state == 0x02){
                ColorTemp_Inf.CtrlChange[ctr_loop] = ctr_state;
                ColorTemp_Inf.loop_bright[ctr_loop] &= ~(0x01 << 7);
                
            }
            ColorTemp_Inf.CtrlMode[ctr_loop] = ctr_mode;
           
			ColorTemp_Inf.AG_Flag |= ( 0x01 << ctr_loop);            

            temp1++;
            break;
       case ColorTemp_PB20_OnlyBright:          //只调节亮度，不控制灯光开关状态
            ColorTemp_Inf.loop_bright[ctr_loop] &= 0x80;
            ColorTemp_Inf.loop_bright[ctr_loop] |= (ctr_state & 0x7f);
       
            ColorTemp_Inf.CtrlMode[ctr_loop] = ctr_mode;
            ColorTemp_Inf.Lt_Flag |= ( 0x01 << ctr_loop);
            temp1++;
            break;             
        case ColorTemp_PB20_Dim_Up:                 //向上调光 - 相对调节
            ColorTemp_Inf.CtrlChange[ctr_loop] = ctr_state;
        
            ColorTemp_Inf.CtrlMode[ctr_loop] = ctr_mode;    
            ColorTemp_Inf.AG_Flag |= ( 0x01 << ctr_loop);
        
            if((ColorTemp_Inf.loop_bright[ctr_loop] & 0x7F) + ctr_mode <= 100 ) {
                ColorTemp_Inf.loop_bright[ctr_loop] += ctr_state;
            }
            else{
                ColorTemp_Inf.loop_bright[ctr_loop] &= 0x80;
                ColorTemp_Inf.loop_bright[ctr_loop] |= (0x64 & 0x7f);           //如果超过100即是最亮
            }            
        
            temp1++;            
            break;
        case ColorTemp_PB20_Dim_Down:               //向下调光 - 相对调节
            ColorTemp_Inf.CtrlChange[ctr_loop] = ctr_state;
        
            ColorTemp_Inf.CtrlMode[ctr_loop] = ctr_mode;    
            ColorTemp_Inf.AG_Flag |= ( 0x01 << ctr_loop);
        
            if((ColorTemp_Inf.loop_bright[ctr_loop] & 0x7F) > ctr_mode ) {
                ColorTemp_Inf.loop_bright[ctr_loop] -= ctr_state;
            }
            else{
                ColorTemp_Inf.loop_bright[ctr_loop] &= 0x80;
                ColorTemp_Inf.loop_bright[ctr_loop] |= (0x00 & 0x7f);           //0
            }           
        
            temp1++;            
            break;
        case ColorTemp_PB20_Dim_Up_Limit:           //唯有亮的灯 向上调光 至上限 相对调节
//            if(0x00 != (ColorTemp_Inf.loop_bright[ctr_loop] & 0x7F))
            {
                ColorTemp_Inf.CtrlChange[ctr_loop] = ctr_state;     //改变值
            
                ColorTemp_Inf.CtrlMode[ctr_loop] = ctr_mode;    
                ColorTemp_Inf.AG_Flag |= ( 0x01 << ctr_loop);                
                
                if((ColorTemp_Inf.loop_bright[ctr_loop] & 0x7F) + ctr_state <= 100) {
                    ColorTemp_Inf.loop_bright[ctr_loop] += ctr_state;
                }
                else{
                    ColorTemp_Inf.loop_bright[ctr_loop] &= 0x80;
                    ColorTemp_Inf.loop_bright[ctr_loop] |= (0x64 & 0x7f);           //如果超过100即是最亮
                }        
                temp1++;
            }            
            break;
        case ColorTemp_PB20_Dim_Down_Limit:         //唯有亮的灯 向下调光 至下限 相对调节
//            if(0x00 != (ColorTemp_Inf.loop_bright[ctr_loop] & 0x7F))
            {
                ColorTemp_Inf.CtrlChange[ctr_loop] = ctr_state;
            
                ColorTemp_Inf.CtrlMode[ctr_loop] = ctr_mode;    
                ColorTemp_Inf.AG_Flag |= ( 0x01 << ctr_loop);                      
                
                if((ColorTemp_Inf.loop_bright[ctr_loop] & 0x7F) > ctr_mode ) {
                    ColorTemp_Inf.loop_bright[ctr_loop] -= ctr_state;
                }
                else{
                    ColorTemp_Inf.loop_bright[ctr_loop] &= 0x80;
                    ColorTemp_Inf.loop_bright[ctr_loop] |= (0x00 & 0x7f);           //0
                }        
                temp1++;
            }        
            break;
        case 0x50:      //恢复所有回路色温为全局色温，2025-07-10，YYW
			for(uint8_t i = 0; i < DNOR_ColorTemp_LoopNumMax; i++)
			{
				ColorTemp_Inf.loop_ColorTemp[i] = DevActionGlobal.CCTValue;  
			} 
            temp1++;
            break;
		default:
			break;        
	}
	
	Dbg_Println(DBG_BIT_Debug_STATUS_bit,"Loop%d - Mode:%d Light:%d CCT:%d",ctr_loop,ctr_mode,ColorTemp_Inf.loop_bright[ctr_loop],ColorTemp_Inf.loop_ColorTemp[ctr_loop]);
	
	if(temp1!=0x00)
    {
        BUS_Public.check = 0x00;
        BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&ColorTemp_Inf, sizeof(NOR_ColorTemp_INFO));
        SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),devaddr);/*将数据保存*/
        SRAM_DMA_Write_Buff((uint8_t *)&ColorTemp_Inf,sizeof(NOR_ColorTemp_INFO),devaddr+Dev_Privately);
    }
	
}

uint16_t BLV_ColorTemp_Read_State(uint32_t devaddr,uint16_t loop)
{
	uint8_t read_loop = loop & 0xFF;
	if(devaddr == 0x00)	return 0x00;
	
	Device_Public_Information_G BUS_Public;
	NOR_ColorTemp_INFO ColorTemp_Inf;
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),devaddr);	//拷贝所有共有数据	
	SRAM_DMA_Read_Buff((uint8_t *)&ColorTemp_Inf,sizeof(NOR_ColorTemp_INFO),devaddr+Dev_Privately); 
	
	if(read_loop >= DNOR_ColorTemp_LoopNumMax)	return 0x00;
	
	return ColorTemp_Inf.loop_bright[read_loop];
		
}

/*根据*/
void ColorTemp_Control_MapOutput(ColorTemp_LoopMap *loop_info,uint8_t loop,uint8_t bright,uint8_t color_temp){

	switch(loop_info->type){
		case DNOR_ColorTemp_MapType_1:
			
			break;
		case DNOR_ColorTemp_MapType_2:
			
			break;
		case DNOR_ColorTemp_MapType_3:
			
			break;
		case DNOR_ColorTemp_MapType_4:
			
			break;
	}
}






