/**
  ******************************************************************************
  * @file    BlV_Bus_Dev_PB20_Strip_Fun.c
  * @author  BLW RCU  development Team
  * @version V1.0
  * @date    2023年10月19日
  * @brief  
  *          PB20_Strip驱动
  ******************************************************************************
  */


#include "includes.h"

#if RS485_PB20_LS_Flag

/*本地函数定义开始*/
void BLV_PB20_Strip_Control_State(uint32_t CfgDevAddIn, uint16_t DevInputAddr, uint32_t devaddr,uint16_t loop,uint16_t start);
uint16_t BLV_PB20_Strip_Read_State(uint32_t devaddr,uint16_t loop);
uint8_t BLV_PB20_Strip_Rccv_Check(uint32_t dev_addr,uint32_t data_addr,uint16_t len);
void BLV_PB20_Strip_Dis(uint32_t dev_addr);

/*本地函数定义结束*/


void PB20_Strip_AddToPB(uint32_t devaddr, Device_Public_Information_G *BUS_Public)
{
	Device_Public_Information_G BUS_Public_PB;
	RS485_PB20_INFO Rs485PB20;
	
	if(devaddr != NULL)
	{
		SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public_PB,sizeof(Device_Public_Information_G),devaddr);				
		SRAM_DMA_Read_Buff((uint8_t *)&Rs485PB20,sizeof(RS485_PB20_INFO),devaddr+Dev_Privately);
		
		if(Rs485PB20.StripDevNum < PB20_STRIPLIST_MAX)
		{
			Rs485PB20.Strip_List[Rs485PB20.StripDevNum].Type = BUS_Public->type;
			Rs485PB20.Strip_List[Rs485PB20.StripDevNum].Addr = BUS_Public->addr;
			Rs485PB20.Strip_List[Rs485PB20.StripDevNum].Port = BUS_Public->port;
			
			Rs485PB20.StripDevNum++;
		}
        
		BUS_Public_PB.check = 0x00;
		BUS_Public_PB.check = DoubleData_CheckSum((uint8_t *)&BUS_Public_PB, sizeof(Device_Public_Information_G), (uint8_t *)&Rs485PB20, sizeof(RS485_PB20_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public_PB, sizeof(Device_Public_Information_G),devaddr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&Rs485PB20,sizeof(RS485_PB20_INFO),devaddr+Dev_Privately);
	}
}



/*******************************************************************************
* @name  :  BLV_RS485_PB20_Strip_Data_Init
* @para  : 
*   DataLen :数据长度
*   Data:    数据数组
* @attention :代码配置
*******************************************************************************/
void BLV_RS485_PB20_Strip_Data_Init(uint8_t devaddr)
{
	Device_Public_Information_G BUS_Public;
	RS485_PB20_Strip_INFO Rs485PB20Strip;
	
	memset(&BUS_Public,0,sizeof(Device_Public_Information_G));
	memset(&Rs485PB20Strip,0,sizeof(RS485_PB20_Strip_INFO));
	
	BUS_Public.addr = devaddr;		                                            //设备485地址
	BUS_Public.type = Dev_Rs485_PB20_LS;		                                //设备类型
	BUS_Public.baud = 0x00;//Data[3] + (Data[4]<<8) + (Data[5]<<16) + (Data[6]<<24);   //设备发送波特率9600
//	BUS_Public.retry_num = 0x03;	                                            //设备数据重发次数
//	BUS_Public.wait_time = 0x0064;	                                            //设备数据发送等待回复时间 - 100ms
	BUS_Public.Protocol = 0x01;	                                            //设备协议
	
	BUS_Public.DevFunInfo.Dev_Data_Process = BLV_PB20_Strip_Dis;             
	BUS_Public.DevFunInfo.Dev_Output_Ctrl = BLV_PB20_Strip_Control_State;
	BUS_Public.DevFunInfo.Dev_Output_Loop_State_Get = BLV_PB20_Strip_Read_State;

	Rs485PB20Strip.PB20DevType = 0x30;//Data[15];
	Rs485PB20Strip.PB20DevAddr = 0x01;//Data[16];
	Rs485PB20Strip.PB20DevPort = 0x03;//Data[17];
	Rs485PB20Strip.PB20Addr = Find_AllDevice_List_Information2(Rs485PB20Strip.PB20DevPort,Rs485PB20Strip.PB20DevType,Rs485PB20Strip.PB20DevAddr);
	
	Rs485PB20Strip.LoopValidNum = 0x04;
	Rs485PB20Strip.DevOffline = Rs485PB20Strip.DevOfflineLast = DEV_IS_LINEUNINIT;   //在线状态初始化
	Rs485PB20Strip.Strip_DimTime = 10;
	
    //默认 回路开关状态为 - 开
    for(uint8_t i = 0; i < Rs485PB20Strip.LoopValidNum; i++)
    {
        Rs485PB20Strip.LightBuf[i] = 0x80;
        Rs485PB20Strip.LightBuf_Last[i] = 0x80;
    }
        
//	switch(Data[2])
//	{
//		case 0x00:
			Add_Nor_Device_To_List2(&BUS_Public,(uint8_t *)&Rs485PB20Strip,sizeof(RS485_PB20_Strip_INFO));
//			break;
//		default:
//			break;
//	}
	
	PB20_Strip_AddToPB(Rs485PB20Strip.PB20Addr,&BUS_Public);

}



/*******************************************************************************
* @name  :  PB20Strip设备初始化
* @para  : 
*   dev_info :逻辑文件指针
*   data     :数据指针
*   data_len :数据长度
* @attention :逻辑配置
*******************************************************************************/
void BLV_RS485_PB20_Strip_For_Logic_Init(LOGICFILE_DEVICE_INFO *dev_info,uint8_t *data,uint16_t data_len)
{
	Device_Public_Information_G BUS_Public;
	RS485_PB20_Strip_INFO Rs485PB20Strip;
	
	memset(&BUS_Public,0,sizeof(Device_Public_Information_G));
	memset(&Rs485PB20Strip,0,sizeof(RS485_PB20_Strip_INFO));
	
	BUS_Public.addr = dev_info->addr;	      //设备485地址
	BUS_Public.type = dev_info->type;		  //设备类型
	BUS_Public.baud = dev_info->baud;		  //设备发送波特率9600
//	BUS_Public.retry_num = 0x03;			  //设备数据重发次数
//	BUS_Public.wait_time = 0x0064;			  //设备数据发送等待回复时间 - 100ms
	BUS_Public.Protocol = dev_info->version;  //协议号
	
	BUS_Public.DevFunInfo.Dev_Data_Process = BLV_PB20_Strip_Dis;             
	BUS_Public.DevFunInfo.Dev_Output_Ctrl = BLV_PB20_Strip_Control_State;
	BUS_Public.DevFunInfo.Dev_Output_Loop_State_Get = BLV_PB20_Strip_Read_State;
	
	Rs485PB20Strip.PB20DevType = dev_info->parent_type;
	Rs485PB20Strip.PB20DevAddr = dev_info->parent_addr;
	Rs485PB20Strip.PB20DevPort = dev_info->parent_port;
	Rs485PB20Strip.PB20Addr = Find_AllDevice_List_Information2(Rs485PB20Strip.PB20DevPort,Rs485PB20Strip.PB20DevType,Rs485PB20Strip.PB20DevAddr);
	
	Rs485PB20Strip.LoopValidNum = dev_info->output_num;
	Rs485PB20Strip.DevOffline = Rs485PB20Strip.DevOfflineLast = DEV_IS_LINEUNINIT;   //在线状态初始化
	Rs485PB20Strip.Strip_DimTime = 10;
	
	//默认 回路开关状态为 - 开
    for(uint8_t i = 0; i < Rs485PB20Strip.LoopValidNum; i++)
    {
        Rs485PB20Strip.LightBuf[i] = 0x80;
        Rs485PB20Strip.LightBuf_Last[i] = 0x80;
    }
	
	switch(dev_info->port)
	{
		case 0x00:
			Add_Nor_Device_To_List2(&BUS_Public,(uint8_t *)&Rs485PB20Strip,sizeof(RS485_PB20_Strip_INFO));
			break;
		default:
			break;
	}
	
	PB20_Strip_AddToPB(Rs485PB20Strip.PB20Addr,&BUS_Public);

}



/*******************************************************************************
* Function Name  : BLV_PB20_Strip_Control_State 
* Description    : PB20Strip控制函数
* Input          : 
	devaddr	: 设备信息地址
	loop    : 回路
	start   : 状态
* Return  : 无
* attention : temp1非0才会进行保存
*******************************************************************************/
void BLV_PB20_Strip_Control_State(uint32_t CfgDevAddIn, uint16_t DevInputAddr, uint32_t devaddr,uint16_t loop,uint16_t start)
{
	Device_Public_Information_G BUS_Public;
	RS485_PB20_Strip_INFO Rs485PB20Strip;
	
	uint8_t CtrlMode;     //控制模式
	uint8_t CtrlContect;  //控制内容
	
	uint8_t temp1 = 0x00;
	
	if(devaddr == 0x00)	return;
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),devaddr);				
	SRAM_DMA_Read_Buff((uint8_t *)&Rs485PB20Strip,sizeof(RS485_PB20_Strip_INFO),devaddr+Dev_Privately);
	
	if(loop >= Rs485PB20Strip.LoopValidNum)    //有效回路
	{
		return ;
	}
	
	CtrlMode = start&0x00ff;    //控制模式
	CtrlContect = start>>8;     //控制内容
	
	Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"PB20Strip %d Loop:%d Ctrl mode:%d state:%d Light:%d",BUS_Public.addr,loop,CtrlMode,CtrlContect,Rs485PB20Strip.LightBuf[loop] & 0x7f);
	
	switch(CtrlMode)
	{
		case PB20_Strip_LIGHT_OPEN:             //开关控制 + 亮度调节
            Rs485PB20Strip.LightBuf[loop] |= (0x01 << 7);
            Rs485PB20Strip.LightBuf[loop] &= 0x80;
            Rs485PB20Strip.LightBuf[loop] |= (CtrlContect & 0x7f);
		

			temp1++;
            break;
		case PB20_Strip_LIGHT_CLOSE:            //开关控制 + 亮度调节
            Rs485PB20Strip.LightBuf[loop] &= ~(0x01 << 7);
            Rs485PB20Strip.LightBuf[loop] &= 0x80;
            Rs485PB20Strip.LightBuf[loop] |= (CtrlContect & 0x7f);


			temp1++;
			break;
        case PB20_Strip_OnlySwitch:
            if(CtrlContect == 0x01){
				
                Rs485PB20Strip.LightBuf[loop] |= (0x01 << 7);
                
            }else if(CtrlContect == 0x02){
              
                Rs485PB20Strip.LightBuf[loop] &= ~(0x01 << 7);
            }

			temp1++;
            break;
       case PB20_Strip_OnlyBright:
            
			Rs485PB20Strip.LightBuf[loop] &= 0x80;
            Rs485PB20Strip.LightBuf[loop] |= (CtrlContect & 0x7f);

			temp1++;
            break;
//		case PB20_Strip_Dim_Up:                 //向上调光 - 百分比控制
//			if((Rs485PB20Strip.LightBuf[loop] & 0x7f) + (CtrlContect & 0x7f) <= 100)
//			{
//				Rs485PB20Strip.LightBuf[loop] += (CtrlContect & 0x7f);
//			}
//			else
//			{
//				Rs485PB20Strip.LightBuf[loop] &= 0x80;
//				Rs485PB20Strip.LightBuf[loop] |= 0x64;
//			}

//			temp1++;
//			break;
//		case PB20_Strip_Dim_Down:               //向下调光 - 百分比控制
//			if((Rs485PB20Strip.LightBuf[loop] & 0x7f) > (CtrlContect & 0x7f))
//			{
//				Rs485PB20Strip.LightBuf[loop] -= (CtrlContect & 0x7f);
//			}
//			else
//			{
//				Rs485PB20Strip.LightBuf[loop] &= 0x80;
//			}

//			temp1++;
//			break;
//		case PB20_Strip_Dim_Up_Limit:          //唯有亮的灯 向上调光 至上限  -  百分比控制
//			if((Rs485PB20Strip.LightBuf[loop] & 0x7f) > 0)
//			{
//				if((Rs485PB20Strip.LightBuf[loop] & 0x7f) + (CtrlContect & 0x7f) <= 100)
//				{
//					Rs485PB20Strip.LightBuf[loop] += (CtrlContect & 0x7f);
//				}
//				else
//				{
//					Rs485PB20Strip.LightBuf[loop] &= 0x80;
//					Rs485PB20Strip.LightBuf[loop] |= 0x64;
//				}

//			}
//			
//			temp1++;
//			break;
//		case PB20_Strip_Dim_Down_Limit:        //唯有亮的灯 向下调光 至下限  -  百分比控制
//			if((Rs485PB20Strip.LightBuf[loop] & 0x7f) > 0)
//			{
//				if((Rs485PB20Strip.LightBuf[loop] & 0x7f) > (CtrlContect & 0x7f))
//				{
//					Rs485PB20Strip.LightBuf[loop] -= (CtrlContect & 0x7f);
//				}
//				else
//				{
//					Rs485PB20Strip.LightBuf[loop] &= 0x80;
//				}

//			}
//			temp1++;
//			break;
		case PB20_Strip_Ctrl_UpperLimit:        //向上调光至上限
		case PB20_Strip_Ctrl_LowerLimit:        //向下调光至下限
		case PB20_Strip_Cycle_Dim_Up:           //向上循环   
		case PB20_Strip_Cycle_Dim_Down:         //向下循环
		case PB20_Strip_Cycle_Stop:             //停止调光
		case PB20_Strip_Dim_Up:                 //向上调光 - 相对调节
		case PB20_Strip_Dim_Down:               //向下调光 - 相对调节
		case PB20_Strip_Dim_Up_Limit:           //唯有亮的灯 向上调光 至上限 相对调节
		case PB20_Strip_Dim_Down_Limit:         //唯有亮的灯 向下调光 至下限 相对调节
		case PB20_Strip_Dim_Cir:                //相对调节 - 反向
		case PB20_Strip_Dim_OnCir:              //相对调节 - 反向  -  唯有亮的灯
			Rs485PB20Strip.CtrlMode[loop] = CtrlMode;
			Rs485PB20Strip.CtrlTime[loop] = CtrlContect;
		
			Rs485PB20Strip.AG_Flag |= ( 0x01 << loop);
		
			temp1++;
			break;
		case PB20_Strip_Cycle_Dim_Up_Limit:     //唯有亮的灯 向上循环 至上限
		case PB20_Strip_Cycle_Dim_Down_Limit:   //唯有亮的灯 向下循环 至下限
			if((Rs485PB20Strip.LightBuf[loop] & 0x7f) > 0)
			{
				Rs485PB20Strip.CtrlMode[loop] = CtrlMode;
				Rs485PB20Strip.CtrlTime[loop] = CtrlContect;
				
				Rs485PB20Strip.AG_Flag |= ( 0x01 << loop);
				
				if(Rs485PB20Strip.CtrlMode[loop] == PB20_Strip_Cycle_Dim_Down_Limit)
				{
					Rs485PB20Strip.LightBuf[loop] &= 0x80; 
					Rs485PB20Strip.LightBuf_Last[loop] &= 0x80; 
				}
				temp1++;
			}
			break;
		case PB20_Strip_DimTime_Set:            //渐变时间设置
			Rs485PB20Strip.Strip_DimTime = CtrlContect;
			temp1++;
			break;
		default:
			break;
	}
	
	
	if(temp1 != 0x00)
	{
		BUS_Public.check = 0x00;
		BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&Rs485PB20Strip, sizeof(RS485_PB20_Strip_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),devaddr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&Rs485PB20Strip,sizeof(RS485_PB20_Strip_INFO),devaddr+Dev_Privately);
	}
}



/*******************************************************************************
* Function Name  : BLV_PB20_Strip_Group_Control_State 
* Description    : PB20Strip群制函数
* Input          : 
	devaddr	: 设备信息地址
	loop    : 回路
	start   : 状态
* Return  : 无
* attention : temp1非0才会进行保存
*******************************************************************************/
void BLV_PB20_Strip_Group_Control_State(uint32_t CfgDevAddIn, uint16_t DevInputAddr,uint32_t devaddr, uint32_t CtrlFlag, uint8_t CtrlNum,uint16_t *start)
{
	Device_Public_Information_G BUS_Public;
	RS485_PB20_Strip_INFO Rs485PB20Strip;
	
	uint8_t CtrlMode;     //控制模式
	uint8_t CtrlContect;  //控制内容
	
	uint8_t temp1 = 0x00;
	
	uint8_t i = 0x00;
	
	if(devaddr == 0x00)	return;
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),devaddr);				
	SRAM_DMA_Read_Buff((uint8_t *)&Rs485PB20Strip,sizeof(RS485_PB20_Strip_INFO),devaddr+Dev_Privately);
	
	if(CtrlNum >= Rs485PB20Strip.LoopValidNum)    //有效回路
	{
		CtrlNum = Rs485PB20Strip.LoopValidNum;
	}
	
	for(i = 0; i < CtrlNum; i++)
	{
		if(CtrlFlag&(0x0001<<i))   //非0为控制
		{
			CtrlMode = start[i] & 0x00ff;    //控制模式
			CtrlContect = start[i] >> 8;     //控制内容
	
			
			switch(CtrlMode)
			{
				case PB20_Strip_LIGHT_OPEN:             //开关控制 + 亮度调节
					Rs485PB20Strip.LightBuf[i] |= (0x01 << 7);
					Rs485PB20Strip.LightBuf[i] &= 0x80;
					Rs485PB20Strip.LightBuf[i] |= (CtrlContect & 0x7f);
				
                    Rs485PB20Strip.LS_Flag |= ( 0x01 << i);     //2025-07-08,YYW,即使保存的亮度相同，也会控制
                    
					temp1++;
					break;
				case PB20_Strip_LIGHT_CLOSE:            //开关控制 + 亮度调节
					Rs485PB20Strip.LightBuf[i] &= ~(0x01 << 7);
                
					Rs485PB20Strip.LightBuf[i] &= 0x80;
					Rs485PB20Strip.LightBuf[i] |= (CtrlContect & 0x7f);
                       
                    Rs485PB20Strip.LS_Flag |= ( 0x01 << i);     //2025-07-08,YYW,即使保存的亮度相同，也会控制
					temp1++;
					break;
				case PB20_Strip_OnlySwitch:
					if(CtrlContect == 0x01){
						
						Rs485PB20Strip.LightBuf[i] |= (0x01 << 7);
						
					}else if(CtrlContect == 0x02){
					  
						Rs485PB20Strip.LightBuf[i] &= ~(0x01 << 7);
					}
                    Rs485PB20Strip.SW_Flag |= ( 0x01 << i);
					temp1++;
					break;
			   case PB20_Strip_OnlyBright:
					
					Rs485PB20Strip.LightBuf[i] &= 0x80;
					Rs485PB20Strip.LightBuf[i] |= (CtrlContect & 0x7f);
                    Rs485PB20Strip.LS_Flag |= ( 0x01 << i);
					temp1++;
					break;
		//		case PB20_Strip_Dim_Up:                 //向上调光 - 百分比控制
		//			if((Rs485PB20Strip.LightBuf[i] & 0x7f) + (CtrlContect & 0x7f) <= 100)
		//			{
		//				Rs485PB20Strip.LightBuf[i] += (CtrlContect & 0x7f);
		//			}
		//			else
		//			{
		//				Rs485PB20Strip.LightBuf[i] &= 0x80;
		//				Rs485PB20Strip.LightBuf[i] |= 0x64;
		//			}

		//			temp1++;
		//			break;
		//		case PB20_Strip_Dim_Down:               //向下调光 - 百分比控制
		//			if((Rs485PB20Strip.LightBuf[i] & 0x7f) > (CtrlContect & 0x7f))
		//			{
		//				Rs485PB20Strip.LightBuf[i] -= (CtrlContect & 0x7f);
		//			}
		//			else
		//			{
		//				Rs485PB20Strip.LightBuf[i] &= 0x80;
		//			}

		//			temp1++;
		//			break;
		//		case PB20_Strip_Dim_Up_Limit:          //唯有亮的灯 向上调光 至上限  -  百分比控制
		//			if((Rs485PB20Strip.LightBuf[i] & 0x7f) > 0)
		//			{
		//				if((Rs485PB20Strip.LightBuf[i] & 0x7f) + (CtrlContect & 0x7f) <= 100)
		//				{
		//					Rs485PB20Strip.LightBuf[i] += (CtrlContect & 0x7f);
		//				}
		//				else
		//				{
		//					Rs485PB20Strip.LightBuf[i] &= 0x80;
		//					Rs485PB20Strip.LightBuf[i] |= 0x64;
		//				}

		//			}
		//			
		//			temp1++;
		//			break;
		//		case PB20_Strip_Dim_Down_Limit:        //唯有亮的灯 向下调光 至下限  -  百分比控制
		//			if((Rs485PB20Strip.LightBuf[i] & 0x7f) > 0)
		//			{
		//				if((Rs485PB20Strip.LightBuf[i] & 0x7f) > (CtrlContect & 0x7f))
		//				{
		//					Rs485PB20Strip.LightBuf[i] -= (CtrlContect & 0x7f);
		//				}
		//				else
		//				{
		//					Rs485PB20Strip.LightBuf[i] &= 0x80;
		//				}

		//			}
		//			temp1++;
		//			break;
				case PB20_Strip_Ctrl_UpperLimit:        //向上调光至上限
				case PB20_Strip_Ctrl_LowerLimit:        //向下调光至下限
				case PB20_Strip_Cycle_Dim_Up:           //向上循环   
				case PB20_Strip_Cycle_Dim_Down:         //向下循环
				case PB20_Strip_Cycle_Stop:             //停止调光
					Rs485PB20Strip.CtrlMode[i] = CtrlMode;              //2025-07-09,YYW:
					Rs485PB20Strip.CtrlTime[i] = CtrlContect;
				
					Rs485PB20Strip.AG_Flag |= ( 0x01 << i);
                   
				
					temp1++;
					break;                    
				case PB20_Strip_Dim_Up:                 //向上调光 - 相对调节
				case PB20_Strip_Dim_Down:               //向下调光 - 相对调节
				case PB20_Strip_Dim_Up_Limit:           //唯有亮的灯 向上调光 至上限 相对调节
				case PB20_Strip_Dim_Down_Limit:         //唯有亮的灯 向下调光 至下限 相对调节
				case PB20_Strip_Dim_Cir:                //相对调节 - 反向
				case PB20_Strip_Dim_OnCir:              //相对调节 - 反向  -  唯有亮的灯
					Rs485PB20Strip.CtrlMode[i] = CtrlMode;
					Rs485PB20Strip.CtrlTime[i] = CtrlContect;
				
					Rs485PB20Strip.AG_Flag |= ( 0x01 << i);
                    Rs485PB20Strip.LSIC_Flag |= ( 0x01 << i);      //2025-07-09,YYW,相对调节标志位
                
				
					temp1++;
					break;
				case PB20_Strip_Cycle_Dim_Up_Limit:     //唯有亮的灯 向上循环 至上限
				case PB20_Strip_Cycle_Dim_Down_Limit:   //唯有亮的灯 向下循环 至下限
					if((Rs485PB20Strip.LightBuf[i] & 0x7f) > 0)
					{
						Rs485PB20Strip.CtrlMode[i] = CtrlMode;
						Rs485PB20Strip.CtrlTime[i] = CtrlContect;
						
						Rs485PB20Strip.AG_Flag |= ( 0x01 << i);
						
						if(Rs485PB20Strip.CtrlMode[i] == PB20_Strip_Cycle_Dim_Down_Limit)
						{
							Rs485PB20Strip.LightBuf[i] &= 0x80; 
							Rs485PB20Strip.LightBuf_Last[i] &= 0x80; 
						}
						temp1++;
					}
					break;
				case PB20_Strip_DimTime_Set:            //渐变时间设置
					Rs485PB20Strip.Strip_DimTime = CtrlContect;
					temp1++;
					break;
				default:
					break;
			}
			
			Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"PB20Strip %d Loop:%d Ctrl mode:%d state:%d Light:%d",BUS_Public.addr,i,CtrlMode,CtrlContect,Rs485PB20Strip.LightBuf[i] & 0x7f);
			
		}
	}
		
	
	
	
	
	if(temp1 != 0x00)
	{
		BUS_Public.check = 0x00;
		BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&Rs485PB20Strip, sizeof(RS485_PB20_Strip_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),devaddr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&Rs485PB20Strip,sizeof(RS485_PB20_Strip_INFO),devaddr+Dev_Privately);
	}
}


/*******************************************************************************
* Function Name  : BLV_PB20_Strip_Read_State 
* Description    : PB20Strip状态读取函数
* Input          : 
	devaddr	: 设备信息地址
	loop    :回路
* Return  : 无
*******************************************************************************/
uint16_t BLV_PB20_Strip_Read_State(uint32_t devaddr,uint16_t loop)
{
	
	if(devaddr == 0x00)
		return 0x00;
	
	RS485_PB20_Strip_INFO Rs485PB20Strip;
	
	SRAM_DMA_Read_Buff((uint8_t *)&Rs485PB20Strip,sizeof(RS485_PB20_Strip_INFO),devaddr+Dev_Privately);	
	
	if(loop >= PB20_Strip_BUFF_SIZE)
	{
		//Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BLV_PB20_Strip_Read_State loop >= PB20_Strip_BUFF_SIZE");
		return 0x00;    //超出范围 直接返回0
	}
	

	return Rs485PB20Strip.LightBuf[loop];
	
}



/*******************************************************************************
* Function Name  : BLV_PB20_Strip_Dis 
* Description    : PB20Strip数据处理函数
* Input          : 
	dev_addr	 : 设备信息地址
* Return         : 
*******************************************************************************/
void BLV_PB20_Strip_Dis(uint32_t dev_addr) 
{
	uint8_t i;     //用于遍历所有回路
	Device_Public_Information_G BUS_Public_PBStrip;
	RS485_PB20_Strip_INFO Rs485PB20Strip;
	
	Device_Public_Information_G BUS_Public_PB;
	RS485_PB20_INFO Rs485PB20;
	
	uint8_t strip_flag = 0x00;
	uint8_t pb20_flag = 0x00;
	
	if(NULL == dev_addr)
	{
		return ;
	}
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public_PBStrip,sizeof(Device_Public_Information_G),dev_addr);	//拷贝所有共有数据
	SRAM_DMA_Read_Buff((uint8_t *)&Rs485PB20Strip,sizeof(RS485_PB20_Strip_INFO),dev_addr+Dev_Privately);
	
	if(Rs485PB20Strip.PB20Addr == NULL)
	{
		return ;
	}
	else
	{
		SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public_PB,sizeof(Device_Public_Information_G),Rs485PB20Strip.PB20Addr);				
		SRAM_DMA_Read_Buff((uint8_t *)&Rs485PB20,sizeof(RS485_PB20_INFO),Rs485PB20Strip.PB20Addr+Dev_Privately);
		
		if(BUS_Public_PB.type != Dev_Rs485_PB20)    {
            //Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"%s - Type Error %d",__func__,BUS_Public_PB.type);
            return ;    //判断设备类型
        }
	}
	
	
	for(i = 0; i < Rs485PB20Strip.LoopValidNum; i++)
	{
		
        if( ((Rs485PB20Strip.LightBuf_Last[i] & 0x7F) != (Rs485PB20Strip.LightBuf[i] & 0x7F))||(Rs485PB20Strip.LS_Flag != 0x00) ){  //2025-07-09，YYW
            
            Rs485PB20Strip.LS_Flag &= ~(0x01 << i);
            
            Rs485PB20Strip.LightBuf_Last[i] &= 0x80;
            Rs485PB20Strip.LightBuf_Last[i] |= (Rs485PB20Strip.LightBuf[i] & 0x7F);
//			Rs485PB20Strip.Light_Flag |= (0x01 << i);
			
			if(BUS_Public_PBStrip.addr < 8)
			{
				Rs485PB20.Strip_LLCtrl_Flag[BUS_Public_PBStrip.addr] |= (0x01 << i);
				Rs485PB20.Strip_A00_07_LLCtrl |= (0x01 << (BUS_Public_PBStrip.addr*4 + i));
			}
			else if((BUS_Public_PBStrip.addr >= 8) && (BUS_Public_PBStrip.addr <= 15))
			{
				Rs485PB20.Strip_LLCtrl_Flag[BUS_Public_PBStrip.addr] |= (0x01 << i);
				Rs485PB20.Strip_A08_15_LLCtrl |= (0x01 << (BUS_Public_PBStrip.addr*4 + i - 32));
			}
			
			Rs485PB20.Strip_LL_CtrlLoopNum++;
			
			strip_flag = 0x01;
		    pb20_flag = 0x01;
			
//			Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BLV_PB20_Strip %d Loop:%d Light_Flag:%d A00_07_LLCtrl:%08X  A08_15_LLCtrl:%08X",\
//			BUS_Public_PBStrip.addr,i,Rs485PB20.Strip_LLCtrl_Flag[BUS_Public_PBStrip.addr],Rs485PB20.Strip_A00_07_LLCtrl,Rs485PB20.Strip_A08_15_LLCtrl);
        }
		
        if( ((Rs485PB20Strip.LightBuf_Last[i] & 0x80) != (Rs485PB20Strip.LightBuf[i] & 0x80))||(Rs485PB20Strip.SW_Flag != 0x00) ){  //2025-07-09，YYW
            
            Rs485PB20Strip.SW_Flag &= ~(0x01 << i);
            
            Rs485PB20Strip.LightBuf_Last[i] &= 0x7F;
            Rs485PB20Strip.LightBuf_Last[i] |= (Rs485PB20Strip.LightBuf[i] & 0x80);
//			Rs485PB20Strip.Switch_Flag |= (0x01 << i);
			
			if(BUS_Public_PBStrip.addr < 8)
			{
				Rs485PB20.Strip_LSCtrl_Flag[BUS_Public_PBStrip.addr] |= (0x01 << i);
				Rs485PB20.Strip_A00_07_LSCtrl |= (0x01 << (BUS_Public_PBStrip.addr*4 + i));
			}
			else if((BUS_Public_PBStrip.addr >= 8) && (BUS_Public_PBStrip.addr <= 15))
			{
				Rs485PB20.Strip_LSCtrl_Flag[BUS_Public_PBStrip.addr] |= (0x01 << i);
				Rs485PB20.Strip_A08_15_LSCtrl |= (0x01 << (BUS_Public_PBStrip.addr*4 + i - 32));
			}
			
			Rs485PB20.Strip_LS_CtrlLoopNum++;
			
			strip_flag = 0x01;
		    pb20_flag = 0x01;

//			Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BLV_PB20_Strip %d Loop:%d Switch_Flag:%d Strip_A00_07_LSCtrl:%08X  Strip_A08_15_LSCtrl:%08X",\
//			BUS_Public_PBStrip.addr,i,Rs485PB20.Strip_LSCtrl_Flag[BUS_Public_PBStrip.addr],Rs485PB20.Strip_A00_07_LSCtrl,Rs485PB20.Strip_A08_15_LSCtrl);
        }
		
//		if(Rs485PB20Strip.CtrlMode_Last[i] != Rs485PB20Strip.CtrlMode[i])
		if(Rs485PB20Strip.AG_Flag != 0x00)
		{
//			Rs485PB20Strip.CtrlMode_Last[i] = Rs485PB20Strip.CtrlMode[i];
			Rs485PB20Strip.AG_Flag &= ~(0x01 << i);
			
			if(BUS_Public_PBStrip.addr < 8)
			{
				Rs485PB20.Strip_AGCtrl_Flag[BUS_Public_PBStrip.addr] |= (0x01 << i);
				Rs485PB20.Strip_A00_07_AGCtrl |= (0x01 << (BUS_Public_PBStrip.addr*4 + i));
			}
			else if((BUS_Public_PBStrip.addr >= 8) && (BUS_Public_PBStrip.addr <= 15))
			{
				Rs485PB20.Strip_AGCtrl_Flag[BUS_Public_PBStrip.addr] |= (0x01 << i);
				Rs485PB20.Strip_A08_15_AGCtrl |= (0x01 << (BUS_Public_PBStrip.addr*4 + i - 32));
			}
			
			Rs485PB20.Strip_AG_CtrlLoopNum++;
            
            Dbg_Println(DBG_BIT_SYS_STATUS_bit,"YYW,LSIC_Flag :%d",Rs485PB20Strip.LSIC_Flag);
			if(Rs485PB20Strip.LSIC_Flag != 0x00)
            {
                Rs485PB20Strip.LSIC_Flag &= ~(0x01 << i);
               
                Rs485PB20.Strip_AG_Flag = 0x01;     //LS相对调节
            }
            
            
			strip_flag = 0x01;
		    pb20_flag = 0x01;

//			Dbg_Println(DBG_BIT_SYS_STATUS_bit,"BLV_PB20_Strip %d Loop:%d Cycle_Flag:%d Strip_A00_07_AGCtrl:%08X  Strip_A08_15_AGCtrl:%08X",\
//			BUS_Public_PBStrip.addr,i,Rs485PB20.Strip_AGCtrl_Flag[BUS_Public_PBStrip.addr],Rs485PB20.Strip_A00_07_AGCtrl,Rs485PB20.Strip_A08_15_AGCtrl);
		}
        
        /*向服务器上报状态*/
        if( Rs485PB20Strip.ReportLight[i] != Rs485PB20Strip.LightBuf[i] ){
            uint16_t report_state = 0;
            
            Rs485PB20Strip.ReportLight[i] = Rs485PB20Strip.LightBuf[i];
            if((Rs485PB20Strip.ReportLight[i] & 0x80) != 0x00){
                report_state = Rs485PB20Strip.ReportLight[i] & 0x7F;
                report_state = (report_state << 8) | 0x0001;
                Udp_Addtion_Roomstate(PB20_Strip_Device_Type,BUS_Public_PBStrip.addr,i+1,report_state);
            }else{
                report_state = Rs485PB20Strip.ReportLight[i] & 0x7F;
                report_state = (report_state << 8) | 0x0002;
                Udp_Addtion_Roomstate(PB20_Strip_Device_Type,BUS_Public_PBStrip.addr,i+1,report_state);
            }
			
			strip_flag = 0x01;
        }
	}
	
	
	if(0x01 == strip_flag)
	{
		BUS_Public_PBStrip.check = 0x00;
		BUS_Public_PBStrip.check = DoubleData_CheckSum((uint8_t *)&BUS_Public_PBStrip, sizeof(Device_Public_Information_G), (uint8_t *)&Rs485PB20Strip, sizeof(RS485_PB20_Strip_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public_PBStrip, sizeof(Device_Public_Information_G),dev_addr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&Rs485PB20Strip,sizeof(RS485_PB20_Strip_INFO),dev_addr+Dev_Privately);
	}
	
	if(0x01 == pb20_flag)
	{
		BUS_Public_PB.check = 0x00;
		BUS_Public_PB.check = DoubleData_CheckSum((uint8_t *)&BUS_Public_PB, sizeof(Device_Public_Information_G), (uint8_t *)&Rs485PB20, sizeof(RS485_PB20_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public_PB, sizeof(Device_Public_Information_G),Rs485PB20Strip.PB20Addr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&Rs485PB20,sizeof(RS485_PB20_INFO),Rs485PB20Strip.PB20Addr+Dev_Privately);
	}
	
}


/*******************************************************************************
* Function Name  : BLV_PB20_Strip_Rccv_Check 
* Description    : PB20Strip接收数据处理函数
* Input          : 
	dev_addr	 : 设备信息地址
	data_addr	 : 数据地址
	len			 : 数据长度
* Return         : 
	0x00：处理成功
	0x01：处理失败
*******************************************************************************/
uint8_t BLV_PB20_Strip_Rccv_Check(uint32_t dev_addr,uint32_t data_addr,uint16_t len)
{

	uint8_t rev = 0x01;

	return rev;
}

#endif

