
/**
  ******************************************************************************
  * @file    BLV_485_Dev_LEDFun.c
  * @author  BLW RCU  development Team
  * @version SongHan
  * @date    2021年12月08日
  * @brief  
	*    PBLED总线调光 每组16个  目前只支持第0组
	* @log  202205261640   SongHan   增加调光回路限制
  ******************************************************************************
  */



#include "BLV_485_Dev_A8PBFun.h"



#include "string.h"
#include "SPI_SRAM.h"
#include "usart.h"
#include "check_fun.h"
#include "DBG.h"
#include "rw_logging.h"
#include "SRAM_Variable_Address.h"

#include "includes.h"


/*A8PB调光驱动代码*/
#define A8PB_OUT_CH_MAX  0x10   //A8PB调光有16路

#define REPEATSENDTIMEMAX         0x04  //重发次数上限
#define A8PBUPLIGHT              100    //最高亮80
#define A8PBDOWNLIGHT            0    //最低亮20

#define A8PBGRADUALTIME         30   //30  即300ms
#define LEDCTRLGROUPHAVELOOP   16//0x08         //每组共有回路数16路  

#define A8PBSnMin               0      //最小Sn号
#define A8PBSnMax               7     //最大Sn号

#define DEVA8PBTYPE               25           //A8PB类型  

/*整体调光宏定义开始*/
#define GLOBAL_STEP_VALUE   0x14   //步进20
#define GLOBAL_MAX_VALUE    0x64   //最大亮度
#define GLOBAL_MIN_VALUE    0x14   //最小亮度

/*整体调光宏定义结束*/

/*本地函数调用开始*/
uint8_t BLW_A8PBCycleCtrl(uint32_t dev_addr);           //发送
uint8_t BLW_Rs485_A8PB_Check(uint32_t dev_addr,uint32_t data_addr,uint16_t len);  //接收
void BLW_Rs485_A8PB_Ctrl(Device_Public_Information_G *BUS_Public, RS485_LED_INFO* Rs485A8PBInfo);      //状态控制
void BLW_Rs485_A8PB_Way_Ctrl(Device_Public_Information_G *BUS_Public, RS485_LED_INFO* Rs485LEDInfo);  //方式控制
void BLW_Rs485_A8PB_Read(Device_Public_Information_G *BUS_Public, RS485_LED_INFO* Rs485LEDInfo);   //读取当前亮度
void BLW_A8PB_GlobalValue_Set(Device_Public_Information_G *BUS_Public, RS485_LED_INFO* Rs485LEDInfo);   //全局亮度设置


/*本地函数调用结束*/

/*******************************************************************************
* Function Name  : BLW_RS485_A8PB_Data_Init 
* Description    : 添加设备到链表 - 轮询设备
* Input          : 
	type	: 添加的设备类型
	addr	: 添加的设备地址
	polling_cf		：设备的轮询数据发送回调函数
	processing_cf	：设备的数据处理回调函数
* Return         : None
*******************************************************************************/
void BLW_RS485_A8PB_Data_Init(Device_Public_Information_G *BUS_Public, RS485_LED_INFO *Rs485LED, uint16_t LoopNum)
{
	BUS_Public->polling_cf = (uint32_t)&BLW_A8PBCycleCtrl;
	BUS_Public->processing_cf = (uint32_t)&BLW_Rs485_A8PB_Check;
	
	if(LoopNum >= A8PB_OUT_CH_MAX)   //输出回路超过范围
	{
		Rs485LED->LEDLoopValidNum = A8PB_OUT_CH_MAX;
	}
	else if(0x00 == LoopNum)
	{
		Rs485LED->LEDLoopValidNum = 0x01;
	}
	else              //未超过范围
	{
		Rs485LED->LEDLoopValidNum = LoopNum;
	}
	
	Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"添加A8PB设备，回路:%d", Rs485LED->LEDLoopValidNum);
	
}



/*******************************************************************************
* Function Name  : BLW_LEDCycleCtrl 
* Description    : BLWLED调光轮询兼控制发送函数
*******************************************************************************/
uint8_t BLW_A8PBCycleCtrl(uint32_t dev_addr) 
{
	uint8_t i;     //用于遍历所有回路
	uint8_t Ret = RS485OCCUPYNOTIME;
	Device_Public_Information_G BUS_Public;
	RS485_LED_INFO Rs485LEDInfo;
	uint8_t KeepFlag = 0x00;
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),dev_addr);	//拷贝所有共有数据
	SRAM_DMA_Read_Buff((uint8_t *)&Rs485LEDInfo,sizeof(RS485_LED_INFO),dev_addr+Dev_Privately);
	
	for(i = 0; i < Rs485LEDInfo.LEDLoopValidNum; i++)
	{
		if(Rs485LEDInfo.DevSendBuf_last[i] != Rs485LEDInfo.DevSendBuf[i])
		{
			Rs485LEDInfo.LEDCtrlFlag |= (0x01<<i);
			Rs485LEDInfo.DevSendBuf_last[i] = Rs485LEDInfo.DevSendBuf[i];
//			Rs485LEDInfo.DevReadBuf[i] = 0x01;         //状态变化置一
//			memcpy(Rs485LEDInfo.DevSendBuf_last, Rs485LEDInfo.DevSendBuf, Rs485LEDInfo.LEDLoopValidNum);   //同步这组数据
			Rs485LEDInfo.LEDCtrlCnt = REPEATSENDTIMEMAX;

//			break;
		} 
	}
	
	for(i = 0; i < Rs485LEDInfo.LEDLoopValidNum; i++)
	{
		if(Rs485LEDInfo.DevCtrlWayBuf_last[i] != Rs485LEDInfo.DevCtrlWayBuf[i])  // 0x00
		{
			Rs485LEDInfo.LEDWayCtrlFlag |=( 0x01<<i);   //相应的方式控制标志置一
			Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"调光控制方式置位:%d",Rs485LEDInfo.LEDWayCtrlFlag);  //
			
			Rs485LEDInfo.LEDWayCtrlCnt = REPEATSENDTIMEMAX;
			Rs485LEDInfo.DevCtrlWayBuf_last[i] = Rs485LEDInfo.DevCtrlWayBuf[i];//方式控制，只能一路一路控制
		} 
	}

    if(Rs485LEDInfo.Dim_Global_Value != DevActionGlobal.DimGlobalValue)     //2025-07-15,YYW,全局亮度比较
    {
        Rs485LEDInfo.Dim_Global_Value = DevActionGlobal.DimGlobalValue;
        Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"A8PBFun--全局亮度改变:",Rs485LEDInfo.Dim_Global_Value); 
    }    
	
	if(Rs485LEDInfo.Dim_Global_Value_Last != Rs485LEDInfo.Dim_Global_Value)     //全局亮度
	{
		Rs485LEDInfo.Dim_Global_Value_Last = Rs485LEDInfo.Dim_Global_Value;
		Rs485LEDInfo.Dim_GV_Flag |= (0x01<<7);
	}
	
	
	if((Rs485LEDInfo.LEDUpLightLimitLast != Rs485LEDInfo.LEDUpLightLimit) || (Rs485LEDInfo.LEDDownLightLimitLast != Rs485LEDInfo.LEDDownLightLimit))         //可调上限
	{	
		Rs485LEDInfo.LEDUpLightLimitLast = Rs485LEDInfo.LEDUpLightLimit;
		Rs485LEDInfo.Dim_GV_Flag |= (0x01<<6);
		
		Rs485LEDInfo.LEDDownLightLimitLast = Rs485LEDInfo.LEDDownLightLimit;
		Rs485LEDInfo.Dim_GV_Flag |= (0x01<<5);
	}
	
//	if(Rs485LEDInfo.LEDDownLightLimitLast != Rs485LEDInfo.LEDDownLightLimit)    //可调下限
//	{
//		Rs485LEDInfo.LEDDownLightLimitLast = Rs485LEDInfo.LEDDownLightLimit;
//		Rs485LEDInfo.Dim_GV_Flag |= (0x01<<5);
//	}
	

	
	if(0x00 != Rs485LEDInfo.Dim_GV_Flag)                    //2024-1-30
	{
		BLW_A8PB_GlobalValue_Set(&BUS_Public, &Rs485LEDInfo);             //全局亮度设置
		Ret = RS485OCCUPYTIME;
	}
	else if(0x00 != Rs485LEDInfo.LEDCtrlFlag)
	{
		if(0x00 != Rs485LEDInfo.LEDCtrlCnt)
		{
			Rs485LEDInfo.LEDCtrlCnt--; 
		}
		else
		{
			Rs485LEDInfo.LEDCtrlFlag = 0x00;
		}
		if(0x00 != Rs485LEDInfo.LEDCtrlCnt)
		{
			BLW_Rs485_A8PB_Ctrl(&BUS_Public, &Rs485LEDInfo);           //状态控制
			Ret = RS485OCCUPYTIME;   //非0 发送
		}
		else
		{
			KeepFlag = 0x01;  //为0保存
		}
	}
	else if(0x00 != Rs485LEDInfo.LEDWayCtrlFlag)
	{		

		if(0x00 != Rs485LEDInfo.LEDWayCtrlCnt)
		{
			Rs485LEDInfo.LEDWayCtrlCnt--; 
		}
		else
		{
			Rs485LEDInfo.LEDWayCtrlFlag = 0x00;
		}

		if(0x00 != Rs485LEDInfo.LEDWayCtrlFlag)
		{
			BLW_Rs485_A8PB_Way_Ctrl(&BUS_Public, &Rs485LEDInfo);	 //方式控制
			Ret = RS485OCCUPYTIME;  //非0发送  
		}		
		else
		{
			KeepFlag = 0x01;  //为0保存
		}
		
	}	
	else if(Rs485LEDInfo.LEDLightnessReadFlag)//  
	{

		if(0x00 != Rs485LEDInfo.LEDLightnessReadCnt)
		{
			Rs485LEDInfo.LEDLightnessReadCnt--; 
		}
		else
		{
			Rs485LEDInfo.LEDLightnessReadFlag = 0x00;
		}
		BLW_Rs485_A8PB_Read(&BUS_Public, &Rs485LEDInfo);           //读取
		Ret = RS485OCCUPYTIME;
	}
	
	if(RS485OCCUPYTIME == Ret)
	{
		KeepFlag = 0x01;
		if(Rs485LEDInfo.DevSendCnt > REPEATSENDTIMEMAX)
		{
			if(Rs485LEDInfo.DevOffline != DEV_IS_OFFLINE)
			{
				Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"Rs485A9LED DEV_IS_OFFLINE");
				LOG_Device_Online_Record(DEV_LEDTYPE,BUS_Public.addr,LogInfo_Device_Offline);	//记录设备离线
			}
			Rs485LEDInfo.DevOffline = DEV_IS_OFFLINE;   //离线
			if(Rs485LEDInfo.DevOffline != Rs485LEDInfo.DevOfflineLast)	//当前在线状态不等于上一次在线状态
			{
				Rs485LEDInfo.DevOfflineLast = Rs485LEDInfo.DevOffline;	//更新上一次在线状态
				Write_Device_Fault_State(BUS_Public.type,BUS_Public.addr,In_ErrFun_LineState,DEV_IS_OFFLINE);	//保存设备在线状态到SRAM
			}
			if(0x00 != Rs485LEDInfo.Dim_GV_Flag)
			{
				Rs485LEDInfo.Dim_GV_Flag = 0x00;
			}
		}
		else
		{
			Rs485LEDInfo.DevSendCnt++;   //
		}
		
		/*通讯统计*/
		BLV_Communication_Record(&Rs485LEDInfo.comm_record,0x01,0x00);
		Rs485LEDInfo.inquire_tick = SysTick_1ms;
	}
	
	if(0x01 == KeepFlag)
	{
		BUS_Public.check = 0x00;
		BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&Rs485LEDInfo, sizeof(RS485_LED_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),dev_addr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&Rs485LEDInfo,sizeof(RS485_LED_INFO),dev_addr+Dev_Privately);
				
		//Dbg_Print(DBG_BIT_DEVICE_STATUS_bit,"LED crc_val:%02X\r\n",crc_val);
	}

	
	

	
	return Ret;
}

#define CH1_A8PBLWDLIGHT_LOC   15   //通道1所处位置

/**
	* @name 				BLW8路调光亮度得到
	* @para
	*   data 				外设485发过来的数据包，接收指定路数回路亮度
	* @return 	    无
  * @attention    如果不确定调光亮度，就去同步。
  *                           CMD  全局 C8  C7   C6   C5   C4   C3   C2  C1  VH  VL   AH  AL  mask1  mask2
  *          80 04 25 00 0F 1D 50   64  00  00   64   50   54   0F   00  04  80  00   00  00  00     00    //mask1是开关状态   mask是故障状态
  *
	*/
//void BLW_Rs485_A8PB_Lightness_Get(Device_Public_Information_G *BUS_Public, uint8_t *data, RS485_LED_INFO *Rs485LED)
//{
//	Rs485LED->PBLEDTotVol = (data[16]<<8)+data[17];    //保存总电压
//	if(0x00 != data[19])         //电流不为0
//	{
//		Rs485LED->PBLEDValidCur = (data[18]<<8)+data[19];   //保存有效值
//		if(Rs485LED->PBLEDTotCur >= 0xff*10)               //超出量程
//		{
//			Rs485LED->PBLEDUpCur = 0xff;                    //置满
//		}
//		else
//		{
//			Rs485LED->PBLEDUpCur = Rs485LED->PBLEDTotCur/10;  //一个字节的电流上报，
//		}
//	}
//	
//	
//	Rs485LED->PBLEDTotCur = (data[18]<<8)+data[19];    //保存总电流

//}



#define RECDATALENMAX   24   //接收数据最大长度

#define IsMyData     0x00
#define IsNotMyData  0x01
/*******************************************************************************
* Function Name  : BLW_Rs485_LED_Check 
* Description    : BLWLED调光接收数据处理函数
* Input          : 
	dev_addr	: 设备信息地址
	data_addr	: 数据地址
	len			：数据长度
* Return         : 
	0x00：处理成功
	0x01：处理失败
*******************************************************************************/
uint8_t BLW_Rs485_A8PB_Check(uint32_t dev_addr,uint32_t data_addr,uint16_t len)
{
	//Dbg_Print(DBG_BIT_DEVICE_STATUS_bit,"BLW_Rs485_LED_Check\r\n");
	uint8_t rev = IsNotMyData;
//	uint16_t temp;  //保存命令类型和数据长度
	
	Device_Public_Information_G BUS_Public;
	RS485_LED_INFO Rs485LED;
	uint8_t data[RECDATALENMAX];
	
//	uint8_t LED_addr = SRAM_Read_Byte(dev_addr+Dev_Addr);  //485地址
//	uint8_t crc_val = SRAM_Read_Word(data_addr+5);        //保存校验
	if(len > RECDATALENMAX)
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"接收数据长度超出范围!!");
		return rev;     //长度
	}
	
	SRAM_DMA_Read_Buff(data,len,data_addr);   //拷贝482数据
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),dev_addr);				
	
	if((data[0] != BUS_Public.addr) || (DEVA8PBTYPE != (data[2]))
		||(len != (data[4]))
	   )	   //地址不对
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"A8PB设备地址不对或者类型不对或者长度不对!!");
		return rev;				//校验文件头
	}

	
	if(data[5] == CheckSum_Overlook_Check(data, len, 5))   //校验通过
	{
		SRAM_DMA_Read_Buff((uint8_t *)&Rs485LED,sizeof(RS485_LED_INFO),dev_addr+Dev_Privately);
	
		switch(data[6])  
		{
			case 0x5A:      //一次控制16组调光控制回复
			case 0x5B:			//方式控制回复
			case 0x50:			//读取回复
			case 0x35:      //全局亮度设置回复
				rev = IsMyData;
			
				if(Rs485LED.DevOffline != DEV_IS_ONLINE)
				{
					Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"Rs485LED A8PB DEV_IS_ONLINE");
					LOG_Device_Online_Record(DEV_LEDTYPE,BUS_Public.addr,LogInfo_Device_Online);	//记录设备在线
				}
				Rs485LED.DevSendCnt = 0x00;
				Rs485LED.DevOffline = DEV_IS_ONLINE;    //设备在线
				if(Rs485LED.DevOffline != Rs485LED.DevOfflineLast)	//当前在线状态不等于上一次在线状态
				{
					Rs485LED.DevOfflineLast = Rs485LED.DevOffline;	//更新上一次在线状态
					Write_Device_Fault_State(BUS_Public.type,BUS_Public.addr,In_ErrFun_LineState,DEV_IS_ONLINE);	//保存设备在线状态到SRAM
				}
				BLV_Communication_Record(&Rs485LED.comm_record,0x02,0x01);		//记录通讯成功
				break;			
		}
		switch(data[6])         //
		{
			case 0x5A:            //一次控制16组调光控制回复
				Rs485LED.LEDCtrlFlag = 0x00;   
				/*日志记录*/
				LOG_Device_COMM_Control_Reply_Record3(dev_addr+Dev_port,dev_addr+Dev_baud,data_addr,len);
				break;
			case 0x5B:       //方式控制回复0x54
				Rs485LED.LEDWayCtrlFlag = 0x00;  
				/*日志记录*/
				LOG_Device_COMM_Control_Reply_Record3(dev_addr+Dev_port,dev_addr+Dev_baud,data_addr,len);			
				break;
			case 0x50:         //调光亮度得到
//				BLW_Rs485_PBLEDA8_Lightness_Get(&BUS_Public, data, &Rs485LED);  //调光亮度和总电流总电压得到函数
				Rs485LED.LEDLightnessReadFlag = 0x00;	
				break;
			case 0x35:
				Rs485LED.Dim_GV_Flag = 0x00;
				break;
		}		

		
		BUS_Public.check = 0x00;
		BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&Rs485LED, sizeof(RS485_LED_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),dev_addr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&Rs485LED,sizeof(RS485_LED_INFO),dev_addr+Dev_Privately);
			
	}else {
//		Dbg_Print(DBG_BIT_DEVICE_STATUS_bit,"SRAM_CRC16 Fail ,crc_val:%02X SRAM_CRC16:%02X!!\r\n",data[5],SRAM_CRC16(data_addr,len - 2));
		
	}
	
	return rev;
}




#define A8PBCTRLSENDLEN   29   //控制长度
#define A8PB1LOC      28   //
/**
	* @name BLW 4路 集中调光控制函数
	* @para
	*   dev_addr     设备起始地址   
	* @return         无
  * @attention      发送循环里调用，变量已经被判断
	*/
void BLW_Rs485_A8PB_Ctrl(Device_Public_Information_G *BUS_Public, RS485_LED_INFO* Rs485LEDInfo)
{
	uint8_t i;
	                                   //己方地址 发送类型  设备类型       收方地址 LEN          校验和   CMD  重发  组号   开8通道 低8通道  渐变0.1s 保存 CH16  CH15  CH14  CH13  CH12  CH11  CH10  CH9   CH8    CH7    CH6    CH5    CH4    CH3    CH2    CH1 
	uint8_t SendBuf[A8PBCTRLSENDLEN];// = {0x00,   0x00,   DEVA8PBTYPE,   0x80,  A8PBCTRLSENDLEN,  0x18,  0x4A, 0x01, 0x00,   0x00,  0x00,    0x64,   0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  0x00,  0x00,  0x00,  0x50,  0x50,  0x50,  0x50};   //
	
	SendBuf[0] = 0x00;    //
	SendBuf[1] = 0x00;    //清0
	SendBuf[1] |= BLV_SN_Get(&Rs485LEDInfo->LEDSn, A8PBSnMin, A8PBSnMax);
  SendBuf[2] = DEVA8PBTYPE;       //类型
	SendBuf[3] = BUS_Public->addr;   //
	SendBuf[4] = A8PBCTRLSENDLEN;    //长度
	SendBuf[6] = 0x4A;                 //命令，状态调光命令
	SendBuf[7] = 0x01;                //重发
	SendBuf[8] = 0x00;                //组号
	SendBuf[9] = Rs485LEDInfo->LEDCtrlFlag/0x100;                //控制标志高字节
	SendBuf[10] = Rs485LEDInfo->LEDCtrlFlag%0x100;               //控制标志低字节
//	SendBuf[11] = 0x64;               //渐变0.1S
	SendBuf[11]=Rs485LEDInfo->A8PB_Set_Time;
	Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"A8PB调光渐变时间:%d ",Rs485LEDInfo->A8PB_Set_Time);
	SendBuf[12] = 0xF0;               //不保存
	
	for(i = 0; i < Rs485LEDInfo->LEDLoopValidNum; i++)
	{
		SendBuf[A8PB1LOC-i] = Rs485LEDInfo->DevSendBuf[i];
	}	
//	SendBuf[8] = A8PBGRADUALTIME;       //调光渐变时间
//	for(i = 0; i < Rs485LEDInfo->LEDLoopValidNum; i++)
//	{
//		SendBuf[13-i] = Rs485LEDInfo->DevSendBuf[i];
//	}
//	memcpy(&SendBuf[14], &Rs485LEDInfo->DevSendBuf[0], Rs485LEDInfo->LEDLoopValidNum);  //指示灯控制
	SendBuf[5] = CheckSum_Overlook_Check(SendBuf, A8PBCTRLSENDLEN, 5);	
		
	MCU485_SendString(BUS_Public->port,SendBuf,A8PBCTRLSENDLEN);

	/*日志记录*/
	LOG_Device_COMM_Send_Control_Record(BUS_Public->port,BUS_Public->baud,SendBuf,A8PBCTRLSENDLEN);
}


#define A8PBWAYCTRLLEN   14
/*向上循环调光*/
/**
	* @name BLW 4路 向上循环调光
	* @para
	*   CfgDevAdd     设备节点   
  *   PBLEDLoop       回路地址   0~3 
	* @return         无
  * @attention      发送循环里调用，变量已经被判断
  *己方地址 发送类型 设备类型 收方地址  整包长度 校验和 命令字  mask
  *  00        09        03      01        0B       51     22    0F    01    00    64
	*/
void BLW_Rs485_A8PB_Way_Ctrl(Device_Public_Information_G *BUS_Public, RS485_LED_INFO* Rs485LEDInfo)
{
	
	uint8_t i;
	                                     //己方地址  发送类型 设备类型，    收方地址 LEN           校验和   CMD  开4通道 模式  方向  步进
	uint8_t SendBuf[A8PBWAYCTRLLEN];// = {0x00,   0x00,  DEVA8PBTYPE,     0x01,   A8PBWAYCTRLLEN,  0xBD,  0x24,  0x00,  0x01, 0x02, 0x01};
//	uint8_t A8PBLoop;      //     

	SendBuf[0] = 0x00;                                                   //已方地址
	SendBuf[1] = BLV_SN_Get(&Rs485LEDInfo->LEDSn, A8PBSnMin, A8PBSnMax);   //帧
  SendBuf[2] = DEVA8PBTYPE;       //类型
	SendBuf[3] = BUS_Public->addr;
	SendBuf[4] = A8PBWAYCTRLLEN;    //长度
	SendBuf[6] = 0x4B;                 //命令，方式调光命令
	SendBuf[7] = 0x01;   //重发 //Rs485LEDInfo->LEDWayCtrlFlag;   //通道MASK Bit0~Bit3分别对应回路1到回路4
  SendBuf[8] = 0x00;  //组地址
	
	for(i = 0; i < Rs485LEDInfo->LEDLoopValidNum; i++)
	{
//		if(Rs485LEDInfo->LEDGroupNum == i/LEDCTRLGROUPHAVELOOP)   //该组下，所有带有变化标志的灯置mask位
		{
			if((Rs485LEDInfo->LEDWayCtrlFlag>>i)&0x01)
			{					
				switch(Rs485LEDInfo->DevCtrlWayBuf_last[i])
				{
					case CFG_Dev_CtrlWay_Is_Dim_CycleUp:  //向上循环
						SendBuf[9] |= (0x01<<(i%LEDCTRLGROUPHAVELOOP))/0x100;    //高mask位
						SendBuf[10] |= (0x01<<(i%LEDCTRLGROUPHAVELOOP))%0x100;   //低mask位
						SendBuf[11] = 0x01;									//填入控制方式
						SendBuf[12] = 0x00;   
						SendBuf[13] = Rs485LEDInfo->DevCtrlWayContect[i];   //填入控制  
						break;
					case CFG_Dev_CtrlWay_Is_Dim_CycleDown:
						SendBuf[9] |= (0x01<<(i%LEDCTRLGROUPHAVELOOP))/0x100;    //高mask位
						SendBuf[10] |= (0x01<<(i%LEDCTRLGROUPHAVELOOP))%0x100;   //低mask位						
						SendBuf[11] = 0x01;  
						SendBuf[12] = 0x01;
						SendBuf[13] = Rs485LEDInfo->DevCtrlWayContect[i];   //填入控制  					
						break;
					case CFG_Dev_CtrlWay_Is_Dim_Stop:    //停止调光
						SendBuf[9] |= (0x01<<(i%LEDCTRLGROUPHAVELOOP))/0x100;    //高mask位
						SendBuf[10] |= (0x01<<(i%LEDCTRLGROUPHAVELOOP))%0x100;   //低mask位						
						SendBuf[11] = 0x02;
						SendBuf[12] = 0x00;                 //无意义					
						SendBuf[13] = Rs485LEDInfo->DevCtrlWayContect[i];   //填入控制  
						break;
				}
//				break;   //只调用一次
			}
		}
	}
	
	SendBuf[5] = CheckSum_Overlook_Check(SendBuf, A8PBWAYCTRLLEN, 5);	
		
	MCU485_SendString(BUS_Public->port,SendBuf,A8PBWAYCTRLLEN);
	
	/*日志记录*/
	LOG_Device_COMM_Send_Control_Record(BUS_Public->port,BUS_Public->baud,SendBuf,A8PBCTRLSENDLEN);
}

#define  A8PBREADLEN     8
/**
	* @name BLW 4路 集中调光亮度读取函数
	* @para
	*   CfgDevAdd     设备节点   
	* @return         无
  * @attention      发送循环里调用，变量已经被判断
	*/
void BLW_Rs485_A8PB_Read(Device_Public_Information_G *BUS_Public, RS485_LED_INFO* Rs485LEDInfo)
{
									         	      //己方地址 发送类型 设备类型      收方地址 LEN         校验和   CMD   组合
	uint8_t SendBuf[A8PBREADLEN]; // = {0x00,   0x00,  DEVA8PBTYPE,    0x80,   A8PBREADLEN,  0xD8,   0x40,0x00 };

	SendBuf[0] = 0x00;                                                   //已方地址
	SendBuf[1] = BLV_SN_Get(&Rs485LEDInfo->LEDSn, A8PBSnMin, A8PBSnMax);   //帧
  SendBuf[2] = DEVA8PBTYPE;       //类型
	SendBuf[3] = BUS_Public->addr;
	SendBuf[4] = A8PBREADLEN;    //长度
	SendBuf[6] = 0x40;
	SendBuf[7] = 0x00;      //默认0组
	
	SendBuf[5] = CheckSum_Overlook_Check(SendBuf, A8PBREADLEN, 5);	
		
	MCU485_SendString(BUS_Public->port,SendBuf,A8PBREADLEN);	
}
//全局亮度设置
void BLW_A8PB_GlobalValue_Set(Device_Public_Information_G *BUS_Public, RS485_LED_INFO* Rs485LEDInfo)
{
	uint8_t lens = 0x0D;
	uint8_t SendBuf[lens];
	memset(SendBuf,0,sizeof(SendBuf));
	
	SendBuf[0] = 0x00;                                               //已方地址	
	SendBuf[1] = BLV_SN_Get(&Rs485LEDInfo->LEDSn, 0, A8PBSnMax);     //帧
	SendBuf[2] = DEVA8PBTYPE;                                        //类型
	SendBuf[3] = BUS_Public->addr;                                   //Addr
	SendBuf[4] = lens;                                               //长度
	SendBuf[6] = 0x25;                                               //Cmd
	
	SendBuf[7] = 0x00;                                               //重发次数
	SendBuf[8] = Rs485LEDInfo->Dim_GV_Flag;
	SendBuf[9] = Rs485LEDInfo->Dim_Global_Value;                     //全局亮度
	SendBuf[10] = Rs485LEDInfo->LEDUpLightLimit;                      //可调上限
	SendBuf[11] = Rs485LEDInfo->LEDDownLightLimit;                   //可调下限
	SendBuf[12] = 0xF1;                                              //掉电保存
	
	SendBuf[5] = CheckSum_Overlook_Check(SendBuf, lens, 5);	
		
	MCU485_SendString(BUS_Public->port,SendBuf,lens);	

}
