
/**
  ******************************************************************************
  * @file    BLV_485_Dev_C12DimFun.c
  * @author  BLW RCU  development Team
  * @version SongHan
  * @date    2022年03月27日
  * @brief  
	*    C12Dim调光扩展，C12主机自带的12路调光
  ******************************************************************************
  */



#include "BLV_485_Dev_C12DimFun.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 "Log_api.h"
#include "BLV_Dev_Action.h"


/*C12Dim调光驱动代码*/
#define C12DIM_OUT_CH_MAX  0x0C   //C12Dim调光有12路

#define REPEATSENDTIMEMAX         0x03  //重发次数上限
#define C12DimUPLIGHT              100    //最高亮80
#define C12DimDOWNLIGHT            0    //最低亮20

#define C12DimGRADUALTIME         30   //30  即300ms

#define C12DimSnMin               0      //最小Sn号
#define C12DimSnMax               15     //最大Sn号

#define C12Rs485AddrDefault       0x01   //默认为1地址  与外设通讯仍然用默认地址

#define DEVC12DimTYPE               03           //C12Dim类型  

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

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

/*本地函数调用开始*/
uint8_t BLW_C12DimCycleCtrl(uint32_t dev_addr);           //发送
uint8_t BLW_Rs485_C12Dim_Check(uint32_t dev_addr,uint32_t data_addr,uint16_t len);  //接收
void BLW_Rs485_C12Dim_Ctrl(Device_Public_Information_G *BUS_Public, RS485_LED_INFO* Rs485C12DimInfo);      //状态控制
void BLW_Rs485_C12Dim_Way_Ctrl(Device_Public_Information_G *BUS_Public, RS485_LED_INFO* Rs485LEDInfo);  //方式控制
void BLW_Rs485_C12Dim_Read(Device_Public_Information_G *BUS_Public, RS485_LED_INFO* Rs485LEDInfo);   //读取当前亮度
void BLW_C12_GlobalValue_Set(Device_Public_Information_G *BUS_Public, RS485_LED_INFO* Rs485LEDInfo);   //全局亮度设置


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

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

	Rs485LED->LEDCtrlFlag = 0x01;

//	Add_POLL_Device_To_List2(BUS_Public,(uint8_t *)Rs485LED,sizeof(RS485_LED_INFO));
}



/*******************************************************************************
* Function Name  : BLW_LEDCycleCtrl 
* Description    : BLWLED调光轮询兼控制发送函数
*******************************************************************************/
uint8_t BLW_C12DimCycleCtrl(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;
			Rs485LEDInfo.LEDCtrlCnt = REPEATSENDTIMEMAX;
			Rs485LEDInfo.DevReadBuf[i] = 0x01;         //状态变化置一
			memcpy(Rs485LEDInfo.DevSendBuf_last, Rs485LEDInfo.DevSendBuf, Rs485LEDInfo.LEDLoopValidNum);   //同步这组数据

			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 有效回路：%d\r\n",Rs485LEDInfo.LEDWayCtrlFlag,Rs485LEDInfo.LEDLoopValidNum);  //
			
			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,"C12DimFun--全局亮度改变:",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_C12_GlobalValue_Set(&BUS_Public, &Rs485LEDInfo);              //全局亮度设置
		Ret = RS485OCCUPYTIME;
	}
	else if(0x01 == Rs485LEDInfo.LEDCtrlFlag)
	{
		if(0x00 != Rs485LEDInfo.LEDCtrlCnt)
		{
			Rs485LEDInfo.LEDCtrlCnt--; 
		}
		else
		{
			Rs485LEDInfo.LEDCtrlFlag = 0x00;
		}
		BLW_Rs485_C12Dim_Ctrl(&BUS_Public, &Rs485LEDInfo);           //状态控制
		Ret = RS485OCCUPYTIME;
	}
	else if(0x00 != Rs485LEDInfo.LEDWayCtrlFlag)
	{		

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

		if(0x00 != Rs485LEDInfo.LEDWayCtrlFlag)
		{
			BLW_Rs485_C12Dim_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_C12Dim_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,"Rs485 C12 LED 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++;   //
		}
		Rs485LEDInfo.inquire_tick = SysTick_1ms;
		
		/*通讯统计*/
		BLV_Communication_Record(&Rs485LEDInfo.comm_record,0x01,0x00);
	}
	
	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 RECDATALENMAX   0x20   //接收数据最大长度

#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_C12Dim_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_Print(DBG_BIT_DEVICE_STATUS_bit,"接收数据长度超出范围!!\r\n");
		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] != C12Rs485AddrDefault) || (DEVC12DimTYPE != (data[2]))   //
		||(len != (data[4]))
	   )	   //地址不对
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"C12Dim 格式不对");
		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);
//		temp = SRAM_Read_Byte(data_addr + 6);
		
		//Dbg_Print_Buff(DBG_BIT_DEVICE_STATUS_bit,"C12Dimming Recv:",data,len);
		
		switch(data[6])  
		{
			case 0x30:			//读取回复
			case 0x31:      	//控制回复
			case 0x32:			//方式回复
			case 0x33:      	//全局亮度设置回复
				rev = IsMyData;
			
				if(Rs485LED.DevOffline != DEV_IS_ONLINE)
				{
					Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"Rs485LED 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 0x31:
				Rs485LED.LEDCtrlFlag = 0x00;
				/*日志记录*/
				LOG_Device_COMM_Control_Reply_Record(BUS_Public.port,BUS_Public.baud,data,len);
				break;
			case 0x32:
				Rs485LED.LEDWayCtrlFlag = 0x00;
				/*日志记录*/
				LOG_Device_COMM_Control_Reply_Record(BUS_Public.port,BUS_Public.baud,data,len);
				break;
			case 0x30:
				/*检测到状态改变，才保存*/
				Rs485LED.LEDLightnessReadFlag = 0x00;	
				//Dbg_Print_Buff(DBG_BIT_DEVICE_STATUS_bit,"C12Dimming Recv:",data,len);
				if( Rs485LED.init_flag == 0x00)
				{
					/*读取状态 - 复位后的状态*/
					
					for(uint8_t i = 0; i < Rs485LED.LEDLoopValidNum; i++)
					{
						Rs485LED.DevSendBuf[i] = data[8 + i] & 0x7F;
						Rs485LED.DevSendBuf[i] = 0x00;				//2025-11-05 临时解决
						Rs485LED.DevSendBuf_last[i] = Rs485LED.DevSendBuf[i];
						Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"C12Dimming CH%d Init State:%d",i,Rs485LED.DevSendBuf[i]);
					}
					
					Rs485LED.init_flag = 1;
				}
				
				break;
			case 0x33:
				Rs485LED.Dim_GV_Flag = 0x00;
			    Dbg_Print_Buff(DBG_BIT_DEVICE_STATUS_bit,"C12 Global Set Ack:",data,len);
				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 C12DimCTRLSENDLEN    22     //C12调光控制发送函数
/**
	* @name BLW 12路 集中调光控制函数
	* @para
	*   dev_addr     设备起始地址   
	* @return         无
  * @attention      发送循环里调用，变量已经被判断
	*/
void BLW_Rs485_C12Dim_Ctrl(Device_Public_Information_G *BUS_Public, RS485_LED_INFO* Rs485LEDInfo)
{
	uint8_t i;
	                                     //己方地址 发送类型 设备类型      收方地址     LEN              校验和   CMD  开4通道 渐变0.1s 保存   CH4    CH3    CH2    CH1 
	uint8_t SendBuf[C12DimCTRLSENDLEN];// = {0x00,   0x00,  DEVC12DimTYPE,    0x01,   C12DimCTRLSENDLEN,  0x18,  0x21,  0x0f,   0x64,   0x00,  0x50,  0x50,  0x50,  0x50};
		
	SendBuf[0] = 0x00;   //本机地址
	SendBuf[1] = 0x01;   //清0
	SendBuf[1] |= BLV_SN_Get(&Rs485LEDInfo->LEDSn, 0, C12DimSnMax);
	SendBuf[2] = DEVC12DimTYPE;   //设备类型
	
	SendBuf[3] = C12Rs485AddrDefault; //BUS_Public->addr;   //默认1地址
	SendBuf[4] = C12DimCTRLSENDLEN;       //长度
	SendBuf[6] = 0x21;    //设置亮度
	SendBuf[7] = 0xff; //开低8通道
	SendBuf[8] = 0x0f; //开高4通道
//	SendBuf[9] = C12DimGRADUALTIME;       //调光渐变时间
	SendBuf[9] = Rs485LEDInfo->C12_Set_Time;  //调光渐变时间
	Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"C12调光渐变时间:%d \r\n",Rs485LEDInfo->C12_Set_Time);
	for(i = 0; i < Rs485LEDInfo->LEDLoopValidNum; i++)
	{
		SendBuf[21-i] = Rs485LEDInfo->DevSendBuf[i] & 0x7F;
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"C12Dimming CH%d:%d \r\n",i,Rs485LEDInfo->DevSendBuf[i]);
	}
//	memcpy(&SendBuf[14], &Rs485LEDInfo->DevSendBuf[0], Rs485LEDInfo->LEDLoopValidNum);  //指示灯控制
	SendBuf[5] = CheckSum_Overlook_Check(SendBuf, C12DimCTRLSENDLEN, 5);	
		
	MCU485_SendString(BUS_Public->port,SendBuf,C12DimCTRLSENDLEN);

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


#define C12DimWAYCTRLLEN   12
/*向上循环调光*/
/**
	* @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_C12Dim_Way_Ctrl(Device_Public_Information_G *BUS_Public, RS485_LED_INFO* Rs485LEDInfo)
{
	
	uint8_t i;
	                                     //己方地址  发送类型 设备类型，    收方地址        LEN           校验和   CMD  开4通道 模式  方向  步进
	uint8_t   SendBuf[C12DimWAYCTRLLEN];// = {0x00,   0x00,  DEVC12DimTYPE,     0x01,   C12DimWAYCTRLLEN,  0xBD,  0x24,  0x00,  0x01, 0x02, 0x01};
	

	SendBuf[0] = 0x00;                                                   //已方地址
	SendBuf[1] = BLV_SN_Get(&Rs485LEDInfo->LEDSn, 0, C12DimSnMax);   //帧
	SendBuf[2] = DEVC12DimTYPE;       //类型
	SendBuf[3] = 0x01;
	SendBuf[4] = C12DimWAYCTRLLEN;    //长度
	SendBuf[6] = 0x22;                 //命令，方式调光命令
	SendBuf[7] = Rs485LEDInfo->LEDWayCtrlFlag&0xFF;
	SendBuf[8] = (Rs485LEDInfo->LEDWayCtrlFlag>>8)&0xFF;

	for(i = 0; i < Rs485LEDInfo->LEDLoopValidNum; i++)
	{
		if((Rs485LEDInfo->LEDWayCtrlFlag>>i)&0x01)
		{
			switch(Rs485LEDInfo->DevCtrlWayBuf_last[i])
			{
				case CFG_Dev_CtrlWay_Is_Dim_CycleUp:    //向上循环
					SendBuf[9] =  0x01;									//填入控制方式
					SendBuf[10] = 0x01;      
					break;
				case CFG_Dev_CtrlWay_Is_Dim_CycleDown:  //向下循环
					SendBuf[9] =  0x01;  
					SendBuf[10] = 0x00;
					break;
				case CFG_Dev_CtrlWay_Is_Dim_Up:        //向上调光
					SendBuf[9] =  0x00;
					SendBuf[10] = 0x01;     		
					break;
				case CFG_Dev_CtrlWay_Is_Dim_Down:	   //向下调光
					SendBuf[9] =  0x00;
					SendBuf[10] = 0x00;     		
					break;
				case CFG_Dev_CtrlWay_Is_Dim_Stop:    //停止调光
					SendBuf[9] =  0x02;
					SendBuf[10] = 0x00;                 //无意义
					break;
			}
			break;   //只调用一次
		}
	}
	
	SendBuf[11] = Rs485LEDInfo->DevCtrlWayContect[i];   //填入控制
	
	SendBuf[5] = CheckSum_Overlook_Check(SendBuf, C12DimWAYCTRLLEN, 5);	
		
	MCU485_SendString(BUS_Public->port,SendBuf,C12DimWAYCTRLLEN);
	
	/*日志记录*/
	LOG_Device_COMM_Send_Control_Record(BUS_Public->port,BUS_Public->baud,SendBuf,C12DimCTRLSENDLEN);
}

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

	SendBuf[0] = 0x00;                                                   //已方地址	
	SendBuf[1] = 0x00;
	SendBuf[1] = BLV_SN_Get(&Rs485LEDInfo->LEDSn, 0, C12DimSnMax);   //帧
  SendBuf[2] = DEVC12DimTYPE;       //类型
	SendBuf[3] = C12Rs485AddrDefault;  //BUS_Public->addr;
	SendBuf[4] = C12DimREADLEN;    //长度
	SendBuf[6] = 0x20;
	
	SendBuf[5] = CheckSum_Overlook_Check(SendBuf, C12DimREADLEN, 5);	
		
	MCU485_SendString(BUS_Public->port,SendBuf,C12DimREADLEN);	
}

//全局亮度设置
void BLW_C12_GlobalValue_Set(Device_Public_Information_G *BUS_Public, RS485_LED_INFO* Rs485LEDInfo)
{
	uint8_t lens = 0x0B;
	uint8_t SendBuf[lens];
	memset(SendBuf,0,sizeof(SendBuf));
	
	SendBuf[0] = 0x00;                                               //已方地址	
	SendBuf[1] = BLV_SN_Get(&Rs485LEDInfo->LEDSn, 0, C12DimSnMax);   //帧
	SendBuf[2] = DEVC12DimTYPE;                                      //类型
	SendBuf[3] = C12Rs485AddrDefault;  
	SendBuf[4] = lens;                                               //长度
	SendBuf[6] = 0x23;                                               //Cmd
	
	SendBuf[7] = Rs485LEDInfo->Dim_GV_Flag;
	SendBuf[8] = Rs485LEDInfo->Dim_Global_Value;                     //全局亮度
	SendBuf[9] = Rs485LEDInfo->LEDUpLightLimit;                      //可调上限
	SendBuf[10] = Rs485LEDInfo->LEDDownLightLimit;                   //可调下限
	
	SendBuf[5] = CheckSum_Overlook_Check(SendBuf, lens, 5);

	Dbg_Print_Buff(DBG_BIT_DEVICE_STATUS_bit,"BLW_C12_GlobalValue_Set Buff",SendBuf,lens);
		
	MCU485_SendString(BUS_Public->port,SendBuf,lens);	
    
    /*日志记录*/
	LOG_Device_COMM_Send_Control_Record(BUS_Public->port,BUS_Public->baud,SendBuf,lens);

}
