

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

#include "includes.h"

#define RGB_Light_Belt_REPEATSENDTIMEMAX			0x03  	//本设备重发次数上限

/*******************************************************************************
* Function Name  : BLV_485_Dev_RGB_Light_Belt_Init 
* Description    : RGB灯带设备信息初始化函数
*******************************************************************************/
void BLV_485_Dev_RGB_Light_Belt_Init(uint8_t devaddr)
{
	Device_Public_Information_G BUS_Public;
	RS485_RGB_LIGHT_BELT_INFO Light_belt_info;

	memset(&BUS_Public,0,sizeof(Device_Public_Information_G));
	memset(&Light_belt_info,0,sizeof(RS485_RGB_LIGHT_BELT_INFO));
	
	
	BUS_Public.addr = devaddr;						//设备地址
	BUS_Public.type = DEV_RGB_LIGHT_BELT_Type;			//设备类型
	BUS_Public.port = Polling_Port;						//设备发送端口
	BUS_Public.baud = 9600;					//设备发送波特率
	BUS_Public.retry_num = RGB_Light_Belt_REPEATSENDTIMEMAX;		//设备数据重发次数
	BUS_Public.wait_time = 0x64;		//设备数据发送等待回复时间 - 100ms
	BUS_Public.polling_cf = (uint32_t)&BLV_RGB_Light_Belt_Cycle_Call;
	BUS_Public.processing_cf = (uint32_t)&BLV_RGB_Light_Belt_Data_Processing;
	
//	BUS_Public.DevFunInfo.Dev_Output_Ctrl = BLV_C12Dimming_Control_State;
	BUS_Public.DevFunInfo.Dev_Output_Loop_State_Get = Get_BLV_RGB_Light_Loop_Value; 
	
	Add_POLL_Device_To_List2(&BUS_Public,(uint8_t *)&Light_belt_info,sizeof(RS485_RGB_LIGHT_BELT_INFO));
}

/*******************************************************************************
* Function Name  : BLV_RGB_Light_Belt_CycleDis 
* Description    : RGB灯带485数据发送处理函数，包括重发

* Return         : 
	0x01：有数据需要发送
	0x00：没有数据需要发送
*******************************************************************************/
uint8_t BLV_RGB_Light_Belt_Cycle_Call(uint32_t DevAdd)  
{
	RS485_RGB_LIGHT_BELT_INFO Light_belt_info;       //灯带局部变量

	uint8_t val_check = 0;
	
	/*拷贝设备私有信息*/
	SRAM_DMA_Read_Buff((uint8_t *)&Light_belt_info,sizeof(RS485_RGB_LIGHT_BELT_INFO),DevAdd+Dev_Privately);
	
	if(Light_belt_info.light_Control_Flag != 0x00)  //设置亮度
	{
		BLV_RGB_Light_Belt_Light_Control_Datasend(DevAdd, &Light_belt_info);
	}
	else if(Light_belt_info.loop_dimming_Control_Flag != 0x00)  //循环调光
	{
		BLV_RGB_Light_Belt_loop_dimming_Control_Datasend(DevAdd, &Light_belt_info);
	}
	else if(Light_belt_info.stop_dimming_Control_Flag != 0x00)  //停止动作
	{
		BLV_RGB_Light_Belt_stop_dimming_Control_Datasend(DevAdd, &Light_belt_info);
	}
	else 
	{
		if(Light_belt_info.loop_dimming_flg == 1)    //当前未处于循环调光状态，才查询亮度
		{
			return 0;
		}
		BLV_RGB_Light_Belt_Light_Inquire_Datasend(DevAdd, &Light_belt_info);
	}
	
	if(Light_belt_info.DevSendCnt > RGB_Light_Belt_REPEATSENDTIMEMAX)
	{
		Light_belt_info.DevOffline = DEV_IS_OFFLINE;		//判为离线
	}else {
		Light_belt_info.DevSendCnt++;   				//发送次数累计
	}
	
	SRAM_DMA_Write_Buff((uint8_t *)&Light_belt_info,sizeof(RS485_RGB_LIGHT_BELT_INFO),DevAdd+Dev_Privately);
	/*计算校验值*/
	SRAM_Write_Byte(0x00,DevAdd+Dev_Check);
	val_check = Dev_CheckSum(DevAdd, sizeof(RS485_RGB_LIGHT_BELT_INFO) + Dev_Privately);
	SRAM_Write_Byte(val_check,DevAdd+Dev_Check);		//校验值重新写入
	
	return 0x00;
}

/*******************************************************************************
* Function Name  : BLV_RGB_Light_Belt_Light_Control_Datasend 
* Description    : RGB灯带 设定亮度数据打包发送函数
*******************************************************************************/
void BLV_RGB_Light_Belt_Light_Control_Datasend(uint32_t dev_addr, RS485_RGB_LIGHT_BELT_INFO *Dev_Info)
{
	uint8_t temp = 0;
	uint8_t send_buff[20];
	memset(send_buff,0,20);

	memcpy(&send_buff[11], Dev_Info->light, 4);
	
	for(uint8_t i = 0; i < RGB_LIGHT_BELT_CH_MAX; i++)
	{
		if((Dev_Info->light_Control_Flag & (1<<i)))
		{
			send_buff[11+i] = Dev_Info->set_light[i];
		}
	}
	
	Dev_Info->last_light_Control_Flag = Dev_Info->light_Control_Flag;
	
	send_buff[0] = 0xA6;
	send_buff[1] = RGB_LIGHT_BELT_SET_LIGHT_CMD;
	send_buff[3] = 11;
	send_buff[4] = 0;   //tx
	send_buff[5] = SRAM_Read_Byte(dev_addr+Dev_Addr);;   //rx
	send_buff[6] = RGB_LIGHT_BELT_CONTROL_MODE_SET;   //设置亮度
	send_buff[7] = Dev_Info->fade_time;         //渐变时间
	send_buff[8] = Dev_Info->fade_time>>8;
	send_buff[9] = 0;
	send_buff[10] = 4;     //控制4个回路
	send_buff[2] = CheckSum_Overlook_Check(send_buff, 15, 2);
	
	temp = SRAM_Read_Byte(dev_addr+Dev_port);
	MCU485_SendString(temp, send_buff, 15);
}

/*******************************************************************************
* Function Name  : BLV_RGB_Light_Belt_loop_dimming_Control_Datasend 
* Description    : RGB灯带 循环调光数据打包发送函数
*******************************************************************************/
void BLV_RGB_Light_Belt_loop_dimming_Control_Datasend(uint32_t dev_addr, RS485_RGB_LIGHT_BELT_INFO *Dev_Info)
{
	uint8_t temp = 0;
	uint8_t send_buff[20];
	memset(send_buff,0,20);

//	send_buff[7] = SRAM_Read_Byte(dev_addr+Dev_Addr);
//	send_buff[8] = (Dev_Info->loop_dimming_Control_Flag & 0x0F);
	
	send_buff[7] = Dev_Info->loop_dimming_control;
	send_buff[8] = 0x00;
	
	Dev_Info->last_loop_dimming_Control_Flag = Dev_Info->loop_dimming_Control_Flag;
	
	
	send_buff[0] = 0xA6;
	send_buff[1] = RGB_LIGHT_BELT_SET_ACT_CMD;
	send_buff[3] = 10;
	send_buff[4] = 0;   //tx
	send_buff[5] = SRAM_Read_Byte(dev_addr+Dev_Addr);   //rx
	send_buff[6] = Dev_Info->SN;   //设置亮度
	
	send_buff[9] = RGB_LIGHT_BELT_LOOP_DIMMING_CMD;    //动作命令
	send_buff[10] = RGB_LIGHT_BELT_CONTROL_MODE_LOOP;     //循环调光
	send_buff[11] = Dev_Info->loop_fade_time;         //渐变时间
	send_buff[12] = Dev_Info->loop_fade_time>>8;
	send_buff[13] = Dev_Info->loop_light;          //设定循环调光亮度增量
	send_buff[2] = CheckSum_Overlook_Check(send_buff, 14, 2);
	
	temp = SRAM_Read_Byte(dev_addr+Dev_port);
	MCU485_SendString(temp, send_buff, 14);
}

/*******************************************************************************
* Function Name  : BLV_RGB_Light_Belt_stop_dimming_Control_Datasend 
* Description    : RGB灯带 停止循环调光数据打包发送函数
*******************************************************************************/
void BLV_RGB_Light_Belt_stop_dimming_Control_Datasend(uint32_t dev_addr, RS485_RGB_LIGHT_BELT_INFO *Dev_Info)
{
	uint8_t temp = 0;
	uint8_t send_buff[10];
	memset(send_buff,0,10);
	
	Dev_Info->last_stop_dimming_Control_Flag = Dev_Info->stop_dimming_Control_Flag;
	
	send_buff[0] = 0xA6;
	send_buff[1] = RGB_LIGHT_BELT_STOP_DIMMING_CMD;
	send_buff[3] = 2;
	send_buff[4] = 0;   //tx
	send_buff[5] = SRAM_Read_Byte(dev_addr+Dev_Addr);   //rx
	
	send_buff[2] = CheckSum_Overlook_Check(send_buff, 6, 2);
	
	temp = SRAM_Read_Byte(dev_addr+Dev_port);
	MCU485_SendString(temp, send_buff, 6);
}

/*******************************************************************************
* Function Name  : BLV_RGB_Light_Belt_Light_Inquire_Datasend 
* Description    : RGB灯带 Inching数据打包发送函数
*******************************************************************************/
void BLV_RGB_Light_Belt_Light_Inquire_Datasend(uint32_t dev_addr, RS485_RGB_LIGHT_BELT_INFO *Dev_Info)
{
	uint8_t temp = 0;

	uint8_t send_buff[10];
	memset(send_buff,0,10);
	
	send_buff[0] = 0xA6;
	send_buff[1] = RGB_LIGHT_BELT_READ_LIGHT_CMD;
	send_buff[3] = 4;
	send_buff[4] = 0;   //tx
	send_buff[5] = SRAM_Read_Byte(dev_addr+Dev_Addr);;   //rx
	send_buff[6] = 0;   				//索引
	send_buff[7] = 4;         //查询数量
	send_buff[2] = CheckSum_Overlook_Check(send_buff, 8, 2);
	
	temp = SRAM_Read_Byte(dev_addr+Dev_port);
	MCU485_SendString(temp, send_buff, 8);
}


/*******************************************************************************
* Function Name  : BLV_RGB_Light_Belt_Data_Processing 
* Description    : 灯带数据处理函数
* Input          : 
	dev_addr	: 设备信息地址
	data_addr	: 数据地址
	len			：数据长度
* Return         : 
	0x00：处理成功
	0x01：处理失败
*******************************************************************************/
uint8_t BLV_RGB_Light_Belt_Data_Processing(uint32_t dev_addr,uint32_t data_addr,uint16_t len)
{
	uint8_t crc_val = 0;
	uint8_t temp = 0,temp1 = 0;
	uint8_t buff[4] = {0};
	
	RS485_RGB_LIGHT_BELT_INFO Light_belt_info;       //灯带局部变量
	SRAM_DMA_Read_Buff((uint8_t *)&Light_belt_info,sizeof(RS485_RGB_LIGHT_BELT_INFO),dev_addr+Dev_Privately);
	
	if(SRAM_Read_Byte(data_addr) != 0x6A)    //头不对
	{
		Dbg_Print(DBG_BIT_DEVICE_STATUS_bit,"RGB_Light_Belt Head Fail! \r\n");
		return 0x01;
	}
	if(Dev_CheckSum(data_addr,len) != 0x00) 
	{
		Dbg_Print(DBG_BIT_DEVICE_STATUS_bit,"RGB_Light_Belt Check Fail! \r\n");
		return 0x01;	//和校验
	}
	temp = SRAM_Read_Byte(data_addr + 4);
	if(temp != SRAM_Read_Byte(dev_addr+ Dev_Addr))	
	{
		Dbg_Print(DBG_BIT_DEVICE_STATUS_bit,"RGB_Light_Belt PKT_ADD_FM Fail! \r\n");
		return 0x01;						//发送地址不对
	}
	
	temp = SRAM_Read_Byte(data_addr + 5);
	if(temp != 0x00)	
	{
		Dbg_Print(DBG_BIT_DEVICE_STATUS_bit,"RGB_Light_Belt PKT_ADD_TO Fail! \r\n");
		return 0x01;						//接收地址不对
	}
	temp = SRAM_Read_Byte(data_addr + 3);
	if(temp != (len-4))		
	{
		Dbg_Print(DBG_BIT_DEVICE_STATUS_bit,"RGB_Light_Belt PKT_LEN Fail! \r\n");
		return 0x01;						//长度不对
	}	
	
	temp = SRAM_Read_Byte(data_addr + 1);
	switch(temp)
	{
		
		case RGB_LIGHT_BELT_READ_LIGHT_REPLY:         //读亮度回复
			if(SRAM_Read_Byte(data_addr + 6) == 0xE0)   //无错误
			{
				SRAM_DMA_Read_Buff(buff, 4, data_addr+ 7);    
				if(memcmp(buff, Light_belt_info.light, 4) != 0)   //亮度改变
				{
					memcpy(Light_belt_info.light, buff, 4);     //拷贝亮度
					temp1++;
				}
			}
			else Dbg_Print(DBG_BIT_DEVICE_STATUS_bit,"RGB_Light_Belt Fail：%02X\r\n", SRAM_Read_Byte(data_addr + 6));
			
			break;
		case RGB_LIGHT_BELT_SET_LIGHT_REPLY:            //设置亮度回复
			
			if(SRAM_Read_Byte(data_addr + 6) == 0xE0)   //无错误
			{
				Light_belt_info.loop_dimming_flg = 0;    //循环调光标记清零
				Light_belt_info.light_Control_Flag &= ~(Light_belt_info.last_light_Control_Flag);
			}
			else Dbg_Print(DBG_BIT_DEVICE_STATUS_bit,"RGB_Light_Belt Fail：%02X\r\n", SRAM_Read_Byte(data_addr + 6));
			
			break;
		case RGB_LIGHT_BELT_SET_ACT_REPLY:     //循环调光动作命令回复
			if(SRAM_Read_Byte(data_addr + 6) == 0xE0)   //无错误
			{
				Light_belt_info.loop_dimming_flg = 1;    //标记循环调光
				Light_belt_info.loop_dimming_Control_Flag &= ~(Light_belt_info.last_loop_dimming_Control_Flag);
				temp1++;
			}
			else Dbg_Print(DBG_BIT_DEVICE_STATUS_bit,"RGB_Light_Belt Fail：%02X\r\n", SRAM_Read_Byte(data_addr + 6));
			break;
		case RGB_LIGHT_BELT_STOP_DIMMING_REPLY:     //停止动作命令回复
			if(SRAM_Read_Byte(data_addr + 6) == 0xE0)   //无错误
			{
				Light_belt_info.loop_dimming_flg = 0;    //循环调光标记清零
				Light_belt_info.stop_dimming_Control_Flag &= ~(Light_belt_info.last_stop_dimming_Control_Flag);
				temp1++;
			}
			else Dbg_Print(DBG_BIT_DEVICE_STATUS_bit,"RGB_Light_Belt Fail：%02X\r\n", SRAM_Read_Byte(data_addr + 6));
			
			break;
		default: return 0x01;
	}
	
	Light_belt_info.DevSendCnt = 0x00;
	Light_belt_info.DevOffline = DEV_IS_ONLINE;    //设备在线
	
	if(temp1 != 0x00)	/*灯带变化 ，需要保存日志*/
	{
		LOG_Save_Device_Change(data_addr, len);
	}
	
	/*数据解析完毕,保存SRAM中*/
	SRAM_DMA_Write_Buff((uint8_t *)&Light_belt_info,sizeof(RS485_RGB_LIGHT_BELT_INFO),dev_addr+Dev_Privately);
	/*计算校验值*/
	SRAM_Write_Byte(0x00,dev_addr+Dev_Check);
	temp = sizeof(RS485_RGB_LIGHT_BELT_INFO) + Dev_Privately;
	crc_val = Dev_CheckSum(dev_addr,temp);
	SRAM_Write_Byte(crc_val,dev_addr+Dev_Check);		//校验值重新写入
	
	return 0x00;
}

/*******************************************************************************
* Function Name  : BLV_RGB_Light_Belt_Control_Light 
* Description    : 灯带 控制亮度
* Input          : 
	dev_addr	:设备地址
Control_Flag		:控制回路 bit0~3: ch1~ch4
	chx		: 回路亮度
	fadeTime：渐变
*******************************************************************************/
void BLW_RGB_Control_State(uint32_t CfgDevAddIn, uint16_t DevInputAddr,uint32_t devaddr, uint16_t loop,uint16_t start)
{
	uint16_t temp = 0;
	uint8_t crc_val = 0,temp1 = 0;
	uint8_t CtrlMode;   //控制模式
	uint8_t CtrlContect;
	
	if(devaddr == 0x00)	return;
	
	RS485_RGB_LIGHT_BELT_INFO Light_belt_info;       //灯带局部变量

	SRAM_DMA_Read_Buff((uint8_t *)&Light_belt_info,sizeof(RS485_RGB_LIGHT_BELT_INFO),devaddr+Dev_Privately);
	
	if(loop >= RGB_LIGHT_BELT_CH_MAX)    //有效回路
	{
		return ;
	}
	CtrlMode = start&0x00ff;    //控制模式
	CtrlContect = start>>8;     //控制内容
	
	switch(CtrlMode)
	{
		case CFG_Dev_CtrlWay_Is_Dim_Stop:
		case CFG_Dev_CtrlWay_Is_Dim_Up:
		case CFG_Dev_CtrlWay_Is_Dim_Down:
			Light_belt_info.PWMLightnessReadFlag = 0x01;    //唯有停止，向上向下，才会发送读取命令
			Light_belt_info.PWMLightnessReadCnt = REPEATSENDTIMEMAX;
			Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"调光读取置位，触发模式:%d\r\n", CtrlMode);  //
			temp1++;
			break;
	}
	switch(CtrlMode)
	{
		case CFG_Dev_CtrlWay_Is_Dim_CycleUp:   //方式控制
		case CFG_Dev_CtrlWay_Is_Dim_CycleDown:
		case CFG_Dev_CtrlWay_Is_Dim_Stop:
			Light_belt_info.DevCtrlWayBuf[loop] = CtrlMode;  // - CFG_Dev_CtrlWay_Is_PWM_START + 1
			Light_belt_info.DevCtrlWayContect[loop] = CtrlContect;  //保存控制内容
			temp1++;
			break;
		case CFG_Dev_CtrlWay_Is_Open:         //状态控制
		case CFG_Dev_CtrlWay_Is_Close:
			Light_belt_info.DevSendBuf[loop] = CtrlContect; //亮度赋值
			temp1++;
			break;
		case CFG_Dev_CtrlWay_Is_Dim_Open:   //记忆开
			if(0x00 != Light_belt_info.PWMLightRelease[loop])  //如果存在保存的值
			{
				CtrlContect = Light_belt_info.PWMLightRelease[loop];   //改   开
			}
			else if(0x00 != CtrlContect)   //如果不存在
			{
				CtrlContect = CtrlContect;   //以配置为准
			}
			else 
			{
				CtrlContect = 0x64;      //默认最亮
			}
			Light_belt_info.DevSendBuf[loop] = CtrlContect; //亮度赋值
			temp1++;
			break;
		case CFG_Dev_CtrlWay_Is_Dim_Up:         //向上调光
			if(Light_belt_info.DevSendBuf[loop] + CtrlContect <= 100)
			{
				Light_belt_info.DevSendBuf[loop] += CtrlContect;
			}
			else
			{
				Light_belt_info.DevSendBuf[loop] = 100;     //如果超过100即是最亮
			}
			temp1++;
			break;
		case CFG_Dev_CtrlWay_Is_Dim_Down:       //向下调光
			if(Light_belt_info.DevSendBuf[loop]  >= CtrlContect)
			{
				Light_belt_info.DevSendBuf[loop] -= CtrlContect;
			}
			else
			{
				Light_belt_info.DevSendBuf[loop] = 0;     //如果减不够，即是0
			}
			temp1++;
			break;
//		case CFG_Dev_CtrlWay_Is_Dim_UP_Limit:   //调光上限
//			Light_belt_info.PWMUpLightLimit = CtrlContect; //亮度赋值
//			break;
//		case CFG_Dev_CtrlWay_Is_Dim_DOWN_Limit: //调光下限
//			Light_belt_info.PWMDownLightLimit = CtrlContect; //亮度赋值
//			break;
	}	
	
//		if(Light_belt_info.DevSendBuf[loop] != start)  //不同
//		{
//			Light_belt_info.DevSendBuf[loop] = start; 
//			Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"DevSendBuf loop:%d,start:%d\r\n",loop,start);
//			temp1++;
//		}
	
	
	if(temp1 != 0x00)
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"DevSendBuf loop:%d,start:%d\r\n",loop,start);
		
		/*将数据保存进入SRAM中*/
		SRAM_DMA_Write_Buff((uint8_t *)&Light_belt_info,sizeof(RS485_RGB_LIGHT_BELT_INFO),devaddr+Dev_Privately);
		
		/*计算校验值*/
		SRAM_Write_Byte(0x00,devaddr+Dev_Check);
		temp = sizeof(RS485_RGB_LIGHT_BELT_INFO) + Dev_Privately;
		crc_val = Dev_CheckSum(devaddr,temp);
		SRAM_Write_Byte(crc_val,devaddr+Dev_Check);		//校验值重新写入
	}	
}

/*******************************************************************************
* Function Name  : BLV_RGB_Light_Belt_Control_Light 
* Description    : 灯带 控制亮度
* Input          : 
	dev_addr	:设备地址
Control_Flag		:控制回路 bit0~3: ch1~ch4
	chx		: 回路亮度
	fadeTime：渐变
*******************************************************************************/
void BLV_RGB_Light_Belt_Control_Light(uint32_t dev_addr, uint8_t Control_Flag, uint8_t ch1, uint8_t ch2, uint8_t ch3, uint8_t ch4, uint16_t fadeTime)
{
	uint8_t crc_val = 0;
	uint16_t temp = 0;
	RS485_RGB_LIGHT_BELT_INFO Light_belt_info;       //灯带局部变量
	SRAM_DMA_Read_Buff((uint8_t *)&Light_belt_info,sizeof(RS485_RGB_LIGHT_BELT_INFO),dev_addr+Dev_Privately);
	
	for(uint8_t i=0; i< RGB_LIGHT_BELT_CH_MAX; i++)
	{
		if(Control_Flag & (1<<i))
		{
			Light_belt_info.light_Control_Flag |= 1<<i;
			switch(i)
			{
				case 0:
					Light_belt_info.set_light[i] = ch1;
				break;
				case 1:
					Light_belt_info.set_light[i] = ch2;
				break;
				case 2:
					Light_belt_info.set_light[i] = ch3;
				break;
				case 3:
					Light_belt_info.set_light[i] = ch4;
				break;
				default: break;
			}
			
		}
	}
	Light_belt_info.fade_time = fadeTime;  
	
	SRAM_DMA_Write_Buff((uint8_t *)&Light_belt_info,sizeof(RS485_RGB_LIGHT_BELT_INFO),dev_addr+Dev_Privately);
	/*计算校验值*/
	SRAM_Write_Byte(0x00,dev_addr+Dev_Check);
	temp = sizeof(RS485_RGB_LIGHT_BELT_INFO) + Dev_Privately;
	crc_val = Dev_CheckSum(dev_addr,temp);
	SRAM_Write_Byte(crc_val,dev_addr+Dev_Check);		//校验值重新写入
	
}

/*******************************************************************************
* Function Name  : BLV_RGB_Light_Belt_Control_Loop_Dimming 
* Description    : 灯带 控制循环调光
* Input          : 
	dev_addr	     :设备地址
  Control_Flag	:控制回路 0~2
	light		      : 亮度增量
	fadeTime      ：渐变时间
*******************************************************************************/
void BLV_RGB_Light_Belt_Control_Loop_Dimming(uint32_t dev_addr, uint8_t Control_CH, uint8_t light, uint16_t fadeTime)
{
	uint8_t crc_val = 0;
	uint16_t temp = 0;
	RS485_RGB_LIGHT_BELT_INFO Light_belt_info;       //灯带局部变量
	SRAM_DMA_Read_Buff((uint8_t *)&Light_belt_info,sizeof(RS485_RGB_LIGHT_BELT_INFO),dev_addr+Dev_Privately);
	
//	for(uint8_t i=0; i< RGB_LIGHT_BELT_CH_MAX; i++)
//	{
//		if(Control_Flag & (1<<i))
//		{
//			Light_belt_info.loop_dimming_Control_Flag |= 1<<i;
//		}
//	}
	
	Light_belt_info.loop_dimming_Control_Flag = 0x01;
	Light_belt_info.loop_dimming_control = Control_CH;
	
	
	Light_belt_info.loop_fade_time = fadeTime;
	Light_belt_info.loop_light = light;
	Light_belt_info.SN++;
	
	SRAM_DMA_Write_Buff((uint8_t *)&Light_belt_info,sizeof(RS485_RGB_LIGHT_BELT_INFO),dev_addr+Dev_Privately);
	/*计算校验值*/
	SRAM_Write_Byte(0x00,dev_addr+Dev_Check);
	temp = sizeof(RS485_RGB_LIGHT_BELT_INFO) + Dev_Privately;
	crc_val = Dev_CheckSum(dev_addr,temp);
	SRAM_Write_Byte(crc_val,dev_addr+Dev_Check);		//校验值重新写入
	
}

/*******************************************************************************
* Function Name  : BLV_RGB_Light_Belt_Control_Stop_Dimming 
* Description    : 灯带 停止循环调光
* Input          : 
	dev_addr	     :设备地址
*******************************************************************************/
void BLV_RGB_Light_Belt_Control_Stop_Dimming(uint32_t dev_addr)
{
	uint8_t crc_val = 0;
	uint16_t temp = 0;
	RS485_RGB_LIGHT_BELT_INFO Light_belt_info;       //灯带局部变量
	SRAM_DMA_Read_Buff((uint8_t *)&Light_belt_info,sizeof(RS485_RGB_LIGHT_BELT_INFO),dev_addr+Dev_Privately);
	
	Light_belt_info.stop_dimming_Control_Flag = 1;
	
	SRAM_DMA_Write_Buff((uint8_t *)&Light_belt_info,sizeof(RS485_RGB_LIGHT_BELT_INFO),dev_addr+Dev_Privately);
	/*计算校验值*/
	SRAM_Write_Byte(0x00,dev_addr+Dev_Check);
	temp = sizeof(RS485_RGB_LIGHT_BELT_INFO) + Dev_Privately;
	crc_val = Dev_CheckSum(dev_addr,temp);
	SRAM_Write_Byte(crc_val,dev_addr+Dev_Check);		//校验值重新写入
	
}

/*******************************************************************************
* Function Name  : Get_BLV_RGB_Light_Loop_Value 
* Description    : 获取RGB灯光回路亮度
* Input          : 
	dev_addr	:设备地址
	ch			：灯光回路
*******************************************************************************/
uint16_t Get_BLV_RGB_Light_Loop_Value(uint32_t dev_addr,uint16_t ch)  
{
	if(dev_addr == 0x00)	return 0;
	RS485_RGB_LIGHT_BELT_INFO Light_belt_info;       //灯带局部变量
	SRAM_DMA_Read_Buff((uint8_t *)&Light_belt_info,sizeof(RS485_RGB_LIGHT_BELT_INFO),dev_addr+Dev_Privately);
	
	if(ch < RGB_LIGHT_BELT_CH_MAX)
	{
		return Light_belt_info.light[ch];
	}else {
		return 0;
	}
}












