#include "BLV_BUS_Dev_C12DimmingFun.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 "Log_api.h"
#include "includes.h"

/*调光驱动头文件*/
#include "BLV_485_Dev_LEDFun.h"


#define C12DIMMING_REPEATSENDTIMEMAX			0x02  	//本设备重发次数上限

uint16_t BLW_C12Dimming_Group_Read(uint32_t devaddr, uint8_t SceneType, uint32_t ReadFlag, uint8_t ReadNum,uint16_t *start);
void BLW_C12Dimming_Group_Ctrl(uint32_t CfgDevAddIn, uint16_t DevInputAddr,uint32_t devaddr, uint32_t CtrlFlag, uint8_t CtrlNum,uint16_t *start);

#define Dev_C12Dimming_RecvData_Len_Max		0x20

/*******************************************************************************
* Function Name  : BLV_BUS_DEV_C12Dimming_Data_Init 
* Description    : C12Dimming设备信息初始化函数
*******************************************************************************/
void BLV_BUS_DEV_C12Dimming_Data_Init(void)
{
	Device_Public_Information_G BUS_Public;
	BUS_C12DIMMING_INFO C12Dimming_Info;

	memset(&BUS_Public,0,sizeof(Device_Public_Information_G));
	memset(&C12Dimming_Info,0,sizeof(BUS_C12DIMMING_INFO));
	
	
	BUS_Public.addr = 0x00;						//设备地址
	BUS_Public.type = DEV_RS485_PWM;			//设备类型
	BUS_Public.port = 0x03;						//设备发送端口
	BUS_Public.baud = 115200;					//设备发送波特率
	BUS_Public.retry_num = C12DIMMING_REPEATSENDTIMEMAX;		//设备数据重发次数
	BUS_Public.wait_time = 0x64;		//设备数据发送等待回复时间 - 100ms
	BUS_Public.polling_cf = (uint32_t)&BLV_BUS_C12Dimming_Cycle_Call;
	BUS_Public.processing_cf = (uint32_t)&BLV_BUS_C12Dimming_Data_Processing;
	
	BUS_Public.DevFunInfo.Dev_Output_Ctrl = BLV_C12Dimming_Control_State;
	BUS_Public.DevFunInfo.Dev_Output_Loop_State_Get = BLV_C12Dimming_Read_State; 
	
	BUS_Public.DevFunInfo.Dev_Output_Group_Ctrl = BLW_C12Dimming_Group_Ctrl; //
	BUS_Public.DevFunInfo.Dev_Output_Loop_Group_State_Get_ptr = BLW_C12Dimming_Group_Read; //	
//	C12Dimming_Info.set_light[C12Dimming_CH1] = 50;
//	C12Dimming_Info.fade_time = 1;
//	C12Dimming_Info.light_Control_Flag |= 1<<C12Dimming_CH1;
	
	C12Dimming_Info.on_off = 0xFFF;   //开关状态置1
	C12Dimming_Info.set_whole_light = 100;   //全局亮度100
	C12Dimming_Info.set_whole_upper_limit = 100;  //全局可调上限100
	C12Dimming_Info.set_whole_lower_limit = 0;  //全局可调下限0
	C12Dimming_Info.whole_Control_Flag |= BUS_C12DIMMING_WHOLE_LIGHT | BUS_C12DIMMING_WHOLE_UPPER_LIMIT | BUS_C12DIMMING_WHOLE_LOWER_LIMIT;
	C12Dimming_Info.DevOffline = C12Dimming_Info.DevOfflineLast = DEV_IS_LINEUNINIT;   //在线状态初始化
	Add_BUS_Device_To_List2(&BUS_Public,(uint8_t *)&C12Dimming_Info,sizeof(BUS_C12DIMMING_INFO));
}

/*******************************************************************************
* Function Name  : BLV_BUS_C12Dimming_Cycle_Call 
* Description    : C12Dimming轮询发送函数函数
* Input          : 
	dev_addr	: 设备信息地址
* Return         :
	0x00：没问题
	0x01：错误 - 设备类型不对
*******************************************************************************/
uint8_t BLV_BUS_C12Dimming_Cycle_Call(uint32_t dev_addr)
{
//	uint16_t temp = 0;
	Device_Public_Information_G BUS_Public;
	BUS_C12DIMMING_INFO C12Dimming_Info;
	
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),dev_addr);				
	SRAM_DMA_Read_Buff((uint8_t *)&C12Dimming_Info,sizeof(BUS_C12DIMMING_INFO),dev_addr+Dev_Privately);

	if((BUS_Public.port == Bus_port) && (BUS485_Info.BUS_Start == Baud_Wait)) {
		//切换波特率过程中,只发送询问数据
		//Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"切换波特率过程中,只发送询问数据");
		
		BUS_C12Dimming_Inquire_Datasend(&BUS_Public, &C12Dimming_Info);
		
		BUS_Public.check = 0x00;
		BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&C12Dimming_Info, sizeof(BUS_C12DIMMING_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),dev_addr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&C12Dimming_Info,sizeof(BUS_C12DIMMING_INFO),dev_addr+Dev_Privately);
		
		return RS485OCCUPYTIME;
	}

	if(C12Dimming_Info.on_off_Control_Flag != 0x00)  //开关
	{
		BUS_C12Dimming_OnOff_Control_Datasend(&BUS_Public, &C12Dimming_Info);
	}
	else if(C12Dimming_Info.light_Control_Flag != 0x00)   //百分比亮度
	{
		BUS_C12Dimming_Light_Control_Datasend(&BUS_Public, &C12Dimming_Info);
	}
	else if(C12Dimming_Info.whole_Control_Flag != 0x00)  //全局控制
	{
		BUS_C12Dimming_Whole_Control_Datasend(&BUS_Public, &C12Dimming_Info);
	}
	else if(C12Dimming_Info.light_updown_Control_Flag != 0x00)  //相对调节
	{
		BUS_C12Dimming_UpDown_Control_Datasend(&BUS_Public, &C12Dimming_Info);
	}
	else 
	{
		BUS_C12Dimming_Inquire_Datasend(&BUS_Public, &C12Dimming_Info);
	}
	
	C12Dimming_Info.inquire_tick = SysTick_1ms;
	
//	temp = BLV_Communication_Baud_Adjust2(&C12Dimming_Info.comm_record, &BUS_Public);
//	if(temp == 0x01)
//	{
//		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"C12Dimming Adjust Baud:%ld!",BUS_Public.baud);
//	}
//	BLV_Communication_Record(&C12Dimming_Info.comm_record,0x01,0x00);
	
	if(C12Dimming_Info.DevSendCnt > C12DIMMING_REPEATSENDTIMEMAX)
	{
		if(C12Dimming_Info.DevOffline != DEV_IS_OFFLINE)
		{
			Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"C12Dimming DEV_IS_OFFLINE");
			LOG_Device_Online_Record(BUS_Public.type,BUS_Public.addr,LogInfo_Device_Offline);	//记录设备离线SRAM_Read_Byte(dev_addr+Dev_Addr)
		}
		C12Dimming_Info.DevOffline = DEV_IS_OFFLINE;		//判为离线
		C12Dimming_Info.DevSendCnt = 0;
		if(C12Dimming_Info.DevOffline != C12Dimming_Info.DevOfflineLast)	//当前在线状态不等于上一次在线状态
		{
			C12Dimming_Info.DevOfflineLast = C12Dimming_Info.DevOffline;	//更新上一次在线状态
			Write_Device_Fault_State(BUS_Public.type,BUS_Public.addr,In_ErrFun_LineState,DEV_IS_OFFLINE);	//保存设备在线状态到SRAM
		}
	}else {
		C12Dimming_Info.DevSendCnt++;   				//发送次数累计
	}
	
	BUS_Public.check = 0x00;
	BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&C12Dimming_Info, sizeof(BUS_C12DIMMING_INFO));
	SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),dev_addr);/*将数据保存*/
	SRAM_DMA_Write_Buff((uint8_t *)&C12Dimming_Info,sizeof(BUS_C12DIMMING_INFO),dev_addr+Dev_Privately);
	
	return RS485OCCUPYTIME;
}

/*******************************************************************************
* Function Name  : BUS_C12Dimming_Light_Control_Datasend 
* Description    : C12Dimming 设定亮度数据打包发送函数
*******************************************************************************/
void BUS_C12Dimming_Light_Control_Datasend(Device_Public_Information_G *BUS_Public, BUS_C12DIMMING_INFO *Dev_Info)
{
	uint8_t send_buff[25];
	memset(send_buff,0,25);
	

	/*通道mask*/
	send_buff[PKT_PARA] = Dev_Info->light_Control_Flag;       
	send_buff[PKT_PARA+1] = Dev_Info->light_Control_Flag>>8;
	send_buff[PKT_PARA+2] = Dev_Info->fade_time;        //渐变时间（单位100ms）    
	
	for(uint8_t i=0, j = C12Dimming_CH_MAX-1; i<C12Dimming_CH_MAX; i++, j--)
	{
		if((Dev_Info->light_Control_Flag & ( 0x01 << i )))
		{
			send_buff[PKT_PARA+3+j] = Dev_Info->set_light[i]; 
			
		}
	}
	
	send_buff[PKT_ADD_FM] = 0x00;			//本机地址
	if(Dev_Info->DevSendCnt == 0x00)
	{
		Dev_Info->DevSendSN++;
		if(Dev_Info->DevSendSN > 0x0F)	Dev_Info->DevSendSN = 0x00;
		send_buff[PKT_TYPE] |= (Dev_Info->DevSendSN & 0x0F);	//设置发送序号
	}else{	
		//本包数据为重发数据
		send_buff[PKT_TYPE] |= (Dev_Info->DevSendSN & 0x0F);
		send_buff[PKT_TYPE] |= BUS_Retry_Flag;
	}
	send_buff[PKT_DevType] = C12DIMMING_TYPE;				//设备类型
	send_buff[PKT_ADD_TO] = 0x01;					//设备地址 - C5固定设备地址为0x01
	send_buff[PKT_LEN] = 0x16;						//本次发送数据长度
	send_buff[PKT_CHKSUM] = 0x00;					//校验
	send_buff[PKT_CMD] = C12_SET_LIGHT_CMD;		//设置亮度命令
	
	Dev_Info->last_light_Control_Flag = Dev_Info->light_Control_Flag;
	
	Dev_Info->Send_Type = C12_SET_LIGHT_CMD;
	
	send_buff[PKT_CHKSUM] = CheckSum(send_buff, send_buff[PKT_LEN]);
//	temp = SRAM_Read_Byte(dev_addr+Dev_port);
	MCU485_SendString(BUS_Public->port, send_buff, send_buff[PKT_LEN]);
	Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"%s",__func__);
	
	/*日志记录*/
	LOG_Device_COMM_Send_Control_Record(BUS_Public->port, BUS_Public->baud,send_buff,send_buff[PKT_LEN]);
}

/*******************************************************************************
* Function Name  : BUS_C12Dimming_OnOff_Control_Datasend 
* Description    : C12Dimming 设定开关数据打包发送函数
*******************************************************************************/
void BUS_C12Dimming_OnOff_Control_Datasend(Device_Public_Information_G *BUS_Public, BUS_C12DIMMING_INFO *Dev_Info)
{
	uint8_t send_buff[15];
	memset(send_buff,0,15);
	

	/*通道mask*/
	send_buff[PKT_PARA] = Dev_Info->on_off_Control_Flag;       
	send_buff[PKT_PARA+1] = Dev_Info->on_off_Control_Flag>>8;
	send_buff[PKT_PARA+2] = Dev_Info->set_onoff;        //开关状态   
	
	send_buff[PKT_ADD_FM] = 0x00;			//本机地址
	if(Dev_Info->DevSendCnt == 0x00)
	{
		Dev_Info->DevSendSN++;
		if(Dev_Info->DevSendSN > 0x0F)	Dev_Info->DevSendSN = 0x00;
		send_buff[PKT_TYPE] |= (Dev_Info->DevSendSN & 0x0F);	//设置发送序号
	}else{	
		//本包数据为重发数据
		send_buff[PKT_TYPE] |= (Dev_Info->DevSendSN & 0x0F);
		send_buff[PKT_TYPE] |= BUS_Retry_Flag;
	}
	send_buff[PKT_DevType] = C12DIMMING_TYPE;				//设备类型
	send_buff[PKT_ADD_TO] = 0x01;					//设备地址 - C5固定设备地址为0x01
	send_buff[PKT_LEN] = 0x0A;						//本次发送数据长度
	send_buff[PKT_CHKSUM] = 0x00;					//校验
	send_buff[PKT_CMD] = C12_SET_ONOFF_CMD;		//设置开关命令
	
	Dev_Info->last_on_off_Control_Flag = Dev_Info->on_off_Control_Flag;
	Dev_Info->Send_Type = C12_SET_ONOFF_CMD;
	
	send_buff[PKT_CHKSUM] = CheckSum(send_buff, send_buff[PKT_LEN]);
//	temp = SRAM_Read_Byte(dev_addr+Dev_port);
	MCU485_SendString(BUS_Public->port, send_buff, send_buff[PKT_LEN]);
	
	/*日志记录*/
	LOG_Device_COMM_Send_Control_Record(BUS_Public->port, BUS_Public->baud,send_buff,send_buff[PKT_LEN]);
	
	Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"%s",__func__);
}

/*******************************************************************************
* Function Name  : BUS_C12Dimming_Whole_Control_Datasend 
* Description    : C12Dimming 设定全局数据打包发送函数
*******************************************************************************/
void BUS_C12Dimming_Whole_Control_Datasend(Device_Public_Information_G *BUS_Public, BUS_C12DIMMING_INFO *Dev_Info)
{
	uint8_t send_buff[15];
	memset(send_buff,0,15);
	

	/*通道mask*/
	send_buff[PKT_PARA] = Dev_Info->whole_Control_Flag;       
	send_buff[PKT_PARA+1] = Dev_Info->set_whole_light;  						//全局亮度
	send_buff[PKT_PARA+2] = Dev_Info->set_whole_upper_limit;        //全局上限
	send_buff[PKT_PARA+3] = Dev_Info->set_whole_lower_limit;        //全局下限
	
	send_buff[PKT_ADD_FM] = 0x00;			//本机地址
	if(Dev_Info->DevSendCnt == 0x00)
	{
		Dev_Info->DevSendSN++;
		if(Dev_Info->DevSendSN > 0x0F)	Dev_Info->DevSendSN = 0x00;
		send_buff[PKT_TYPE] |= (Dev_Info->DevSendSN & 0x0F);	//设置发送序号
	}else{	
		//本包数据为重发数据
		send_buff[PKT_TYPE] |= (Dev_Info->DevSendSN & 0x0F);
		send_buff[PKT_TYPE] |= BUS_Retry_Flag;
	}
	send_buff[PKT_DevType] = C12DIMMING_TYPE;				//设备类型
	send_buff[PKT_ADD_TO] = 0x01;					//设备地址 - C5固定设备地址为0x01
	send_buff[PKT_LEN] = 0x0B;						//本次发送数据长度
	send_buff[PKT_CHKSUM] = 0x00;					//校验
	send_buff[PKT_CMD] = C12_SET_WHOLE_CMD;		//设置全局命令
	
	Dev_Info->last_whole_Control_Flag = Dev_Info->whole_Control_Flag;
	Dev_Info->Send_Type = C12_SET_WHOLE_CMD;
	
	send_buff[PKT_CHKSUM] = CheckSum(send_buff, send_buff[PKT_LEN]);
//	temp = SRAM_Read_Byte(dev_addr+Dev_port);
	MCU485_SendString(BUS_Public->port, send_buff, send_buff[PKT_LEN]);
	
	/*日志记录*/
	LOG_Device_COMM_Send_Control_Record(BUS_Public->port, BUS_Public->baud,send_buff,send_buff[PKT_LEN]);
	Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"%s",__func__);
}

/*******************************************************************************
* Function Name  : BUS_C12Dimming_UpDown_Control_Datasend 
* Description    : C12Dimming 设定相对调节数据打包发送函数
*******************************************************************************/
void BUS_C12Dimming_UpDown_Control_Datasend(Device_Public_Information_G *BUS_Public, BUS_C12DIMMING_INFO *Dev_Info)
{
	uint8_t send_buff[15];
	memset(send_buff,0,15);
	

	/*通道mask*/
	send_buff[PKT_PARA] = Dev_Info->light_updown_Control_Flag;       
	send_buff[PKT_PARA+1] = Dev_Info->light_updown_Control_Flag>>8;
	
	send_buff[PKT_PARA+2] = Dev_Info->set_light_updown_mode;        //相对调节模式
	send_buff[PKT_PARA+3] = Dev_Info->set_light_updown_dir;        //相对调节方向
	send_buff[PKT_PARA+4] = Dev_Info->set_light_updown_num;        //相对调节步进值
	
	send_buff[PKT_ADD_FM] = 0x00;			//本机地址
	if(Dev_Info->DevSendCnt == 0x00)
	{
		Dev_Info->DevSendSN++;
		if(Dev_Info->DevSendSN > 0x0F)	Dev_Info->DevSendSN = 0x00;
		send_buff[PKT_TYPE] |= (Dev_Info->DevSendSN & 0x0F);	//设置发送序号
	}else{	
		//本包数据为重发数据
		send_buff[PKT_TYPE] |= (Dev_Info->DevSendSN & 0x0F);
		send_buff[PKT_TYPE] |= BUS_Retry_Flag;
	}
	send_buff[PKT_DevType] = C12DIMMING_TYPE;				//设备类型
	send_buff[PKT_ADD_TO] = 0x01;					//设备地址 - C5固定设备地址为0x01
	send_buff[PKT_LEN] = 0x0C;						//本次发送数据长度
	send_buff[PKT_CHKSUM] = 0x00;					//校验
	send_buff[PKT_CMD] = C12_SET_LIGHT_UPDOWN_CMD;		//设置相对调节命令
	
	Dev_Info->last_light_updown_Control_Flag = Dev_Info->light_updown_Control_Flag;
	Dev_Info->Send_Type = C12_SET_LIGHT_UPDOWN_CMD;
	
	send_buff[PKT_CHKSUM] = CheckSum(send_buff, send_buff[PKT_LEN]);
//	temp = SRAM_Read_Byte(dev_addr+Dev_port);
	MCU485_SendString(BUS_Public->port, send_buff, send_buff[PKT_LEN]);
	
	/*日志记录*/
	LOG_Device_COMM_Send_Control_Record(BUS_Public->port, BUS_Public->baud,send_buff,send_buff[PKT_LEN]);	
	Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"%s",__func__);
}

/*******************************************************************************
* Function Name  : BUS_C12Dimming_Inquire_Datasend 
* Description    : C12Dimming Inching数据打包发送函数
*******************************************************************************/
void BUS_C12Dimming_Inquire_Datasend(Device_Public_Information_G *BUS_Public, BUS_C12DIMMING_INFO *Dev_Info)
{
	uint8_t send_buff[8];
	memset(send_buff,0,8);
	
	send_buff[PKT_ADD_FM] = 0x00;			//本机地址
	if(Dev_Info->DevSendCnt == 0x00)
	{
		Dev_Info->DevSendSN++;
		if(Dev_Info->DevSendSN > 0x0F)	Dev_Info->DevSendSN = 0x00;
		send_buff[PKT_TYPE] |= (Dev_Info->DevSendSN & 0x0F);	//设置发送序号
	}else{	
		//本包数据为重发数据
		send_buff[PKT_TYPE] |= (Dev_Info->DevSendSN & 0x0F);
		send_buff[PKT_TYPE] |= BUS_Retry_Flag;
	}
	send_buff[PKT_DevType] = C12DIMMING_TYPE;				//设备类型
	send_buff[PKT_ADD_TO] = 0x01;					//设备地址 - C5固定设备地址为0x01
	send_buff[PKT_LEN] = 0x07;						//本次发送数据长度
	send_buff[PKT_CHKSUM] = 0x00;					//校验
	send_buff[PKT_CMD] = C12_READ_LIGHT_ONOFF_CMD;		//设置轮询命令
	Dev_Info->Send_Type = C12_READ_LIGHT_ONOFF_CMD;
	
	
	send_buff[PKT_CHKSUM] = CheckSum(send_buff, send_buff[PKT_LEN]);
//	temp = SRAM_Read_Byte(dev_addr+Dev_port);
	MCU485_SendString(BUS_Public->port, send_buff, send_buff[PKT_LEN]);
	
//	if(Dev_Info->DevSendCnt > C12DIMMING_REPEATSENDTIMEMAX)
//	{
//		Dev_Info->DevOffline = DEV_IS_OFFLINE;		//判为离线
//		Dev_Info->DevSendCnt = 0;
//	}
//	Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"C12Dimming_Inquire\r\n");
}

/*******************************************************************************
* Function Name  : BLV_BUS_C5IO_Data_Processing 
* Description    : C5IO数据处理函数
* Input          : 
	dev_addr	: 设备信息地址
	data_addr	: 数据地址
	len			：数据长度
* Return         : 
	0x00：处理成功
	0x01：处理失败
*******************************************************************************/
uint8_t BLV_BUS_C12Dimming_Data_Processing(uint32_t dev_addr,uint32_t data_addr,uint16_t len)
{
	uint8_t temp = 0,temp1 = 0;
	
	Device_Public_Information_G BUS_Public;
	BUS_C12DIMMING_INFO C12Dimming_Info;
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),dev_addr);				
	SRAM_DMA_Read_Buff((uint8_t *)&C12Dimming_Info,sizeof(BUS_C12DIMMING_INFO),dev_addr+Dev_Privately);
	
	if(Dev_CheckSum(data_addr,len) != 0x00) 
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"C12Dimming Check Fail!");
		return 0x01;	//和校验
	}
	
	uint16_t deal_len = len;
	if(deal_len > Dev_C12Dimming_RecvData_Len_Max)	
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"Dev_C12Dimming Check_Len Fail!");
		return 0x01;
	}
	uint8_t deal_buff[deal_len+2];
	memset(deal_buff,0,sizeof(deal_buff));
	SRAM_DMA_Read_Buff(deal_buff,deal_len,data_addr);
	
	if(deal_buff[PKT_ADD_FM] != 0x01)	
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"C12Dimming PKT_ADD_FM Fail!");
		return 0x01;						//发送地址不对
	}

	if((deal_buff[PKT_TYPE] & 0x0F) != C12Dimming_Info.DevSendSN)	
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"C12Dimming PKT_TYPE Fail!");
		return 0x01;			//序号不对
	}

	if(deal_buff[PKT_DevType] != C12DIMMING_TYPE)	
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"C12Dimming PKT_DevType Fail!");
		return 0x01;					//设备类型不对
	}

	if(deal_buff[PKT_ADD_TO] != 0x00)	
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"C12Dimming PKT_ADD_TO Fail!");
		return 0x01;						//接收地址不对
	}

	if(deal_buff[PKT_LEN] != len)		
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"C12Dimming PKT_LEN Fail!");
		return 0x01;						//长度不对
	}	
	
	
	//收到回复确定，清空回复标志

	switch(deal_buff[PKT_CMD])
	{
		
		case C12_READ_LIGHT_ONOFF_REPLY_CMD:
			if(deal_buff[PKT_PARA] != C12Dimming_Info.whole_light)  //全局亮度改变
			{
				temp1++;
				C12Dimming_Info.whole_light = deal_buff[PKT_PARA];    //更新全局亮度
			}
			for(uint8_t i=0,j=C12Dimming_CH_MAX-1; i<C12Dimming_CH_MAX; i++, j--)
			{
				
				temp = deal_buff[PKT_PARA + 1 + i];
				if(temp & (1<<7))    //开状态
				{
					if(C12Dimming_Info.on_off & (1<<j))  //开状态
					{
						//未变化
					}
					else
					{
						temp1++;
						C12Dimming_Info.on_off |= 1<<j;
					}
				}
				else
				{
					if(C12Dimming_Info.on_off & (1<<j))  //开状态
					{
						temp1++;
						C12Dimming_Info.on_off &= ~(1<<j); 
					}
				}
				if(C12Dimming_Info.light[j] != (temp & 0x7F))   //回路亮度改变
				{
					temp1++;
					C12Dimming_Info.light[j] = temp & 0x7F;   //更新回路亮度
				}
			}
			
			if(temp1 != 0x00 && C12Dimming_Info.light_updown_mode == BUS_C12DIMMING_UPDOWN_MODE_STOP)	/*C12Dimming变化 ，需要保存日志*/
			{
				Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"C12Dimming State Change");
				LOG_Device_COMM_ASK_TO_Reply_Record(BUS_Public.port, BUS_Public.baud,(SysTick_1ms - C12Dimming_Info.inquire_tick),deal_buff,deal_len);
			}
			break;
		case C12_SET_LIGHT_REPLY_CMD:
			Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"C12_SET_LIGHT_REPLY_CMD");
			C12Dimming_Info.light_Control_Flag &= ~(C12Dimming_Info.last_light_Control_Flag);
			C12Dimming_Info.light_updown_mode = BUS_C12DIMMING_UPDOWN_MODE_STOP;   //设定亮度会打断调光
			
			LOG_Device_COMM_Control_Reply_Record(BUS_Public.port, BUS_Public.baud,deal_buff,deal_len);
			break;
		case C12_SET_ONOFF_REPLY_CMD:
			Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"C12_SET_ONOFF_REPLY_CMD");
			C12Dimming_Info.light_updown_mode = BUS_C12DIMMING_UPDOWN_MODE_STOP;   //设定开关会打断调光
			C12Dimming_Info.on_off_Control_Flag &= ~(C12Dimming_Info.last_on_off_Control_Flag);
		
			LOG_Device_COMM_Control_Reply_Record(BUS_Public.port, BUS_Public.baud,deal_buff,deal_len);
			break;
		case C12_SET_WHOLE_REPLY_CMD:
			Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"C12_SET_WHOLE_REPLY_CMD");
			C12Dimming_Info.whole_Control_Flag &= ~(C12Dimming_Info.last_whole_Control_Flag);
			C12Dimming_Info.whole_lower_limit = C12Dimming_Info.set_whole_lower_limit;
			C12Dimming_Info.whole_upper_limit = C12Dimming_Info.set_whole_upper_limit;
		
			LOG_Device_COMM_Control_Reply_Record(BUS_Public.port, BUS_Public.baud,deal_buff,deal_len);
			break;
		case C12_SET_LIGHT_REPLY_UPDOWN_CMD:
			Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"C12_SET_LIGHT_REPLY_UPDOWN_CMD");
			C12Dimming_Info.light_updown_Control_Flag &= ~(C12Dimming_Info.last_light_updown_Control_Flag);
			C12Dimming_Info.light_updown_mode = C12Dimming_Info.set_light_updown_mode;
			C12Dimming_Info.light_updown_dir = C12Dimming_Info.set_light_updown_dir;
			C12Dimming_Info.light_updown_num = C12Dimming_Info.set_light_updown_num;
		
			LOG_Device_COMM_Control_Reply_Record(BUS_Public.port, BUS_Public.baud,deal_buff,deal_len);
			break;
		default: return 0x01;
	}
	
	BLV_Communication_Record(&C12Dimming_Info.comm_record,0x02,0x01);		//记录通讯成功
	
	if(C12Dimming_Info.DevOffline == DEV_IS_OFFLINE)
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"C12Dimming LogInfo_Device_Online...........");
		LOG_Device_Online_Record(BUS_Public.type,BUS_Public.addr,LogInfo_Device_Online);	//记录设备离线
	}
	C12Dimming_Info.DevSendCnt = 0x00;
	C12Dimming_Info.DevOffline = DEV_IS_ONLINE;    //设备在线
	if(C12Dimming_Info.DevOffline != C12Dimming_Info.DevOfflineLast)	//当前在线状态不等于上一次在线状态
	{
		C12Dimming_Info.DevOfflineLast = C12Dimming_Info.DevOffline;	//更新上一次在线状态
		Write_Device_Fault_State(BUS_Public.type,BUS_Public.addr,In_ErrFun_LineState,DEV_IS_ONLINE);	//保存设备在线状态到SRAM
	}
	BUS_Public.check = 0x00;
	BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&C12Dimming_Info, sizeof(BUS_C12DIMMING_INFO));
	SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),dev_addr);/*将数据保存*/
	SRAM_DMA_Write_Buff((uint8_t *)&C12Dimming_Info,sizeof(BUS_C12DIMMING_INFO),dev_addr+Dev_Privately);
	
	return 0x00;
}

/*******************************************************************************
* Function Name  : BUS_C12Dimming_Control_CHx_OnOff 
* Description    : C12Dimming 控制单路开关状态
* Input          : 
	dev_addr	:设备地址
	loop		:控制开关回路
	start		:控制开关状态 0x01:开 0x02:关 0x03:取反 
*******************************************************************************/
void BUS_C12Dimming_Control_CHx_OnOff(uint32_t dev_addr,BUS_C12DIMMING_NUM_E loop,uint8_t start)
{
	uint8_t temp1 = loop;
	
	Device_Public_Information_G BUS_Public;
	BUS_C12DIMMING_INFO C12Dimming_Info;
	
	if(dev_addr == 0x00) return;
	
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),dev_addr);				
	SRAM_DMA_Read_Buff((uint8_t *)&C12Dimming_Info,sizeof(BUS_C12DIMMING_INFO),dev_addr+Dev_Privately);
	
	if(temp1 < C12Dimming_CH_MAX)
	{
		if(start == 0x01)
		{
			C12Dimming_Info.set_onoff = BUS_C12DIMMING_CHx_ON;
			C12Dimming_Info.on_off_Control_Flag |= 0x0001<<temp1;
			
			Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"BUS_C12DIMMING_Control_OnOff %d ON",temp1);
		}else if(start == 0x02)
		{
			C12Dimming_Info.set_onoff = BUS_C12DIMMING_CHx_OFF;
			C12Dimming_Info.on_off_Control_Flag |= 0x0001<<temp1;
			
			Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"BUS_C12DIMMING_Control_OnOff %d OFF",temp1);
		}else if(start == 0x03)
		{
			if((C12Dimming_Info.on_off & (1<<temp1)))  //当前是开状态
			{
				C12Dimming_Info.set_onoff = BUS_C12DIMMING_CHx_OFF;
				C12Dimming_Info.on_off_Control_Flag |= 0x0001<<temp1;
			}
			else
			{
				C12Dimming_Info.set_onoff = BUS_C12DIMMING_CHx_ON;
				C12Dimming_Info.on_off_Control_Flag |= 0x0001<<temp1;
			}
			
			Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"BUS_C12DIMMING_Control_OnOff %d TOGGLE",temp1);
		}
	}

	BUS_Public.check = 0x00;
	BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&C12Dimming_Info, sizeof(BUS_C12DIMMING_INFO));
	SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),dev_addr);/*将数据保存*/
	SRAM_DMA_Write_Buff((uint8_t *)&C12Dimming_Info,sizeof(BUS_C12DIMMING_INFO),dev_addr+Dev_Privately);
	
}

/*******************************************************************************
* Function Name  : BUS_C12Dimming_Control_OnOff 
* Description    : C12Dimming 控制多个回路开关状态
* Input          : 
	dev_addr	:设备地址
	on_off_Control_Flag		:控制开关回路
	start		:控制开关状态 0x01:开 0x02:关
*******************************************************************************/
void BUS_C12Dimming_Control_OnOff(uint32_t dev_addr, uint16_t Control_Flag, uint8_t start)
{
	Device_Public_Information_G BUS_Public;
	BUS_C12DIMMING_INFO C12Dimming_Info;
	
	if(dev_addr == 0x00) return;
	
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),dev_addr);				
	SRAM_DMA_Read_Buff((uint8_t *)&C12Dimming_Info,sizeof(BUS_C12DIMMING_INFO),dev_addr+Dev_Privately);
	
	if(start == 0x01)
	{
		C12Dimming_Info.set_onoff = BUS_C12DIMMING_CHx_ON;
		C12Dimming_Info.on_off_Control_Flag = Control_Flag;
		
		Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"BUS_C12DIMMING_Control_OnOff ALL ON");
	}else if(start == 0x02)
	{
		C12Dimming_Info.set_onoff = BUS_C12DIMMING_CHx_OFF;
		C12Dimming_Info.on_off_Control_Flag = Control_Flag;
		
		Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"BUS_C12DIMMING_Control_OnOff ALL OFF");
	}
	BUS_Public.check = 0x00;
	BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&C12Dimming_Info, sizeof(BUS_C12DIMMING_INFO));
	SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),dev_addr);/*将数据保存*/
	SRAM_DMA_Write_Buff((uint8_t *)&C12Dimming_Info,sizeof(BUS_C12DIMMING_INFO),dev_addr+Dev_Privately);
	
	
}

/*******************************************************************************
* Function Name  : BUS_C12Dimming_Control_CHx_Light 
* Description    : C12Dimming 控制单路亮度
* Input          : 
	dev_addr	:设备地址
	loop		:控制开关回路
	light		:亮度值
	fadeTime：渐变时间
*******************************************************************************/
void BUS_C12Dimming_Control_CHx_Light(uint32_t dev_addr, BUS_C12DIMMING_NUM_E loop, uint8_t light, uint8_t fadeTime)
{
	uint8_t temp1 = loop;
	Device_Public_Information_G BUS_Public;
	BUS_C12DIMMING_INFO C12Dimming_Info;
	
	if(dev_addr == 0x00) return;
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),dev_addr);				
	SRAM_DMA_Read_Buff((uint8_t *)&C12Dimming_Info,sizeof(BUS_C12DIMMING_INFO),dev_addr+Dev_Privately);
	
	if(temp1 < C12Dimming_CH_MAX)
	{
		C12Dimming_Info.set_light[temp1] = light;   
		C12Dimming_Info.fade_time = fadeTime;
		C12Dimming_Info.light_Control_Flag |= 1<<temp1;
	}
	BUS_Public.check = 0x00;
	BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&C12Dimming_Info, sizeof(BUS_C12DIMMING_INFO));
	SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),dev_addr);/*将数据保存*/
	SRAM_DMA_Write_Buff((uint8_t *)&C12Dimming_Info,sizeof(BUS_C12DIMMING_INFO),dev_addr+Dev_Privately);
		
}

/*******************************************************************************
* Function Name  : BUS_C12Dimming_Control_Light 
* Description    : C12Dimming 控制多个回路亮度
* Input          : 
	dev_addr	:设备地址
	on_off_Control_Flag		:控制开关回路
	light		:亮度
	fadeTime: 渐变时间
	onoff：是否操作开关位 0：不处理 1：打开相应应开关位 2：关闭相应开关位 
*******************************************************************************/
void BUS_C12Dimming_Control_Light(uint32_t dev_addr, uint16_t Control_Flag, uint8_t light, uint8_t fadeTime, uint8_t onoff)
{
	Device_Public_Information_G BUS_Public;
	BUS_C12DIMMING_INFO C12Dimming_Info;
	
	if(dev_addr == 0x00) return;
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),dev_addr);				
	SRAM_DMA_Read_Buff((uint8_t *)&C12Dimming_Info,sizeof(BUS_C12DIMMING_INFO),dev_addr+Dev_Privately);
	
	Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"%s",__func__);
	
	for(uint8_t i=0; i< C12Dimming_CH_MAX; i++)
	{
		if(Control_Flag & (1<<i))
		{
			C12Dimming_Info.light_Control_Flag |= 1<< i;
			C12Dimming_Info.set_light[i] = light;
			if(onoff)
			{
				if(onoff == 1 && (C12Dimming_Info.on_off & (1<<i)) == 0)  //打开对应开关位
				{
					C12Dimming_Info.set_onoff = BUS_C12DIMMING_CHx_ON;
					C12Dimming_Info.on_off_Control_Flag |= 1<<i;
				}
				else if(onoff == 2 && (C12Dimming_Info.on_off & (1<<i)) != 0)  //关闭对应开关位
				{
					C12Dimming_Info.set_onoff = BUS_C12DIMMING_CHx_OFF;
					C12Dimming_Info.on_off_Control_Flag |= 1<<i;
				}
			}
		}
		
	}
	C12Dimming_Info.fade_time = fadeTime;
	
	
	BUS_Public.check = 0x00;
	BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&C12Dimming_Info, sizeof(BUS_C12DIMMING_INFO));
	SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),dev_addr);/*将数据保存*/
	SRAM_DMA_Write_Buff((uint8_t *)&C12Dimming_Info,sizeof(BUS_C12DIMMING_INFO),dev_addr+Dev_Privately);
	
	
}

/*******************************************************************************
* Function Name  : BUS_C12Dimming_Control_Light_Currency 
* Description    : C12Dimming 可以同时设置多个回路不同亮度
* Input          : 
	dev_addr	:设备地址
	on_off_Control_Flag		:控制开关回路
	light		:12路亮度数组的地址
	fadeTime: 渐变时间
	onoff：是否操作开关位 0：不处理 1：打开相应应开关位 2：关闭相应开关位 
*******************************************************************************/
void BUS_C12Dimming_Control_Light_Currency(uint32_t dev_addr, uint16_t Control_Flag, uint8_t* light, uint8_t fadeTime, uint8_t onoff)
{
	Device_Public_Information_G BUS_Public;
	BUS_C12DIMMING_INFO C12Dimming_Info;
	
	if(dev_addr == 0x00) return;
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),dev_addr);				
	SRAM_DMA_Read_Buff((uint8_t *)&C12Dimming_Info,sizeof(BUS_C12DIMMING_INFO),dev_addr+Dev_Privately);
	
	for(uint8_t i=0; i< C12Dimming_CH_MAX; i++)
	{
		if(Control_Flag & (1<<i))
		{
			C12Dimming_Info.light_Control_Flag |= 1<< i;
			C12Dimming_Info.set_light[i] = light[i];   
			if(onoff)
			{
				if(onoff == 1 && (C12Dimming_Info.on_off & (1<<i)) == 0)  //打开对应开关位
				{
					C12Dimming_Info.set_onoff = BUS_C12DIMMING_CHx_ON;
					C12Dimming_Info.on_off_Control_Flag |= 1<<i;
				}
				else if(onoff == 2 && (C12Dimming_Info.on_off & (1<<i)) != 0)  //关闭对应开关位
				{
					C12Dimming_Info.set_onoff = BUS_C12DIMMING_CHx_OFF;
					C12Dimming_Info.on_off_Control_Flag |= 1<<i;
				}
			}
		}
		
	}
	C12Dimming_Info.fade_time = fadeTime;
	
	BUS_Public.check = 0x00;
	BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&C12Dimming_Info, sizeof(BUS_C12DIMMING_INFO));
	SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),dev_addr);/*将数据保存*/
	SRAM_DMA_Write_Buff((uint8_t *)&C12Dimming_Info,sizeof(BUS_C12DIMMING_INFO),dev_addr+Dev_Privately);
	
	
}

/*******************************************************************************
* Function Name  : BUS_C12Dimming_Control_Whole 
* Description    : C12Dimming 控制全局参数
* Input          : 
	dev_addr	:设备地址
	mask		:控制回路
	light		:亮度值
	upper_limit：上限
	lower_limit：下限
*******************************************************************************/
void BUS_C12Dimming_Control_Whole(uint32_t dev_addr, uint8_t mask, uint8_t light, uint8_t upper_limit, uint8_t lower_limit)
{
	Device_Public_Information_G BUS_Public;
	BUS_C12DIMMING_INFO C12Dimming_Info;
	
	if(dev_addr == 0x00) return;
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),dev_addr);				
	SRAM_DMA_Read_Buff((uint8_t *)&C12Dimming_Info,sizeof(BUS_C12DIMMING_INFO),dev_addr+Dev_Privately);
	
	if(mask & BUS_C12DIMMING_WHOLE_LIGHT)   //设置全局亮度
	{
		C12Dimming_Info.set_whole_light = light;   
		C12Dimming_Info.light_Control_Flag |= BUS_C12DIMMING_WHOLE_LIGHT;
	}
	if(mask & BUS_C12DIMMING_WHOLE_UPPER_LIMIT)   //设置全局可调上限
	{
		C12Dimming_Info.set_whole_upper_limit = upper_limit;
		C12Dimming_Info.whole_upper_limit = upper_limit;
		C12Dimming_Info.light_Control_Flag |= BUS_C12DIMMING_WHOLE_UPPER_LIMIT;
	}
	if(mask & BUS_C12DIMMING_WHOLE_LOWER_LIMIT)   //设置全局可调下限
	{
		C12Dimming_Info.set_whole_lower_limit = lower_limit;
		C12Dimming_Info.whole_lower_limit = lower_limit;
		C12Dimming_Info.light_Control_Flag |= BUS_C12DIMMING_WHOLE_LOWER_LIMIT;
	}
	BUS_Public.check = 0x00;
	BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&C12Dimming_Info, sizeof(BUS_C12DIMMING_INFO));
	SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),dev_addr);/*将数据保存*/
	SRAM_DMA_Write_Buff((uint8_t *)&C12Dimming_Info,sizeof(BUS_C12DIMMING_INFO),dev_addr+Dev_Privately);
	
	
}

/*******************************************************************************
* Function Name  : BUS_C12Dimming_Control_CHx_UpDown 
* Description    : C12Dimming 控制单路递增递减调节
* Input          : 
	dev_addr	:设备地址
	loop		:控制开关回路
	mode		:模式
	dir			：方向
	num     : 步进值
*******************************************************************************/
void BUS_C12Dimming_Control_CHx_UpDown(uint32_t dev_addr, BUS_C12DIMMING_NUM_E loop, uint8_t mode, uint8_t dir, uint8_t num)
{
	uint8_t temp1 = loop;
	Device_Public_Information_G BUS_Public;
	BUS_C12DIMMING_INFO C12Dimming_Info;
	
	if(dev_addr == 0x00) return;
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),dev_addr);				
	SRAM_DMA_Read_Buff((uint8_t *)&C12Dimming_Info,sizeof(BUS_C12DIMMING_INFO),dev_addr+Dev_Privately);
	
	if(temp1 < C12Dimming_CH_MAX)
	{
		C12Dimming_Info.set_light_updown_mode = mode;   
		C12Dimming_Info.set_light_updown_dir = dir;
		C12Dimming_Info.set_light_updown_num = num;
		C12Dimming_Info.light_updown_Control_Flag |= 1<<temp1;
	}
	
	BUS_Public.check = 0x00;
	BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&C12Dimming_Info, sizeof(BUS_C12DIMMING_INFO));
	SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),dev_addr);/*将数据保存*/
	SRAM_DMA_Write_Buff((uint8_t *)&C12Dimming_Info,sizeof(BUS_C12DIMMING_INFO),dev_addr+Dev_Privately);
	
	
}

/*******************************************************************************
* Function Name  : BUS_C12Dimming_Control_UpDown 
* Description    : C12Dimming 控制多路递增递减调节
* Input          : 
	dev_addr	:设备地址
	loop		:控制开关回路
	mode		:模式
	dir			：方向
	num     : 步进值
*******************************************************************************/
void BUS_C12Dimming_Control_UpDown(uint32_t dev_addr, uint16_t Control_Flag, uint8_t mode, uint8_t dir, uint8_t num)
{

	Device_Public_Information_G BUS_Public;
	BUS_C12DIMMING_INFO C12Dimming_Info;
	
	if(dev_addr == 0x00) return;
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),dev_addr);				
	SRAM_DMA_Read_Buff((uint8_t *)&C12Dimming_Info,sizeof(BUS_C12DIMMING_INFO),dev_addr+Dev_Privately);
	
	for(uint8_t i=0; i< C12Dimming_CH_MAX; i++)
	{
		if(Control_Flag & (1<<i))
		{
			C12Dimming_Info.set_light_updown_mode = mode;   
			C12Dimming_Info.set_light_updown_dir = dir;
			C12Dimming_Info.set_light_updown_num = num;
			C12Dimming_Info.light_updown_Control_Flag |= 1<<i;
		}
	}
	
	BUS_Public.check = 0x00;
	BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&C12Dimming_Info, sizeof(BUS_C12DIMMING_INFO));
	SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),dev_addr);/*将数据保存*/
	SRAM_DMA_Write_Buff((uint8_t *)&C12Dimming_Info,sizeof(BUS_C12DIMMING_INFO),dev_addr+Dev_Privately);
	
}


//#if Logic_Scheduing_EN

#include "BLV_Dev_Action.h"

/*******************************************************************************
* Function Name  : BLV_C12Dimming_Control_State 
* Description    : C12Dimming 控制状态
* Input          : 
	CfgDevAddIn		:设备地址
	DevInputAddr	:控制开关回路
	devaddr			:
	loop			:
	start			:控制开关状态 0x01:开 0x02:关
*******************************************************************************/
void BLV_C12Dimming_Control_State(uint32_t CfgDevAddIn, uint16_t DevInputAddr, uint32_t devaddr,uint16_t loop,uint16_t start)
{
	Device_Public_Information_G BUS_Public;
	BUS_C12DIMMING_INFO C12Dimming_Info;
	
	if(devaddr == 0x00) return;
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),devaddr);				
	SRAM_DMA_Read_Buff((uint8_t *)&C12Dimming_Info,sizeof(BUS_C12DIMMING_INFO),devaddr+Dev_Privately);
	
	uint8_t CtrlMode = 0,CtrlContect = 0;
	
	if(loop >= C12Dimming_CH_MAX) return ;		//控制回路不在范围内
	 
	CtrlMode = start&0x00ff;    //控制模式
	CtrlContect = start>>8;     //控制内容
	
	switch(CtrlMode)
	{
		case CFG_Dev_CtrlWay_Is_Open:
		case CFG_Dev_CtrlWay_Is_Close:
			C12Dimming_Info.set_light[loop] = CtrlContect;   
			C12Dimming_Info.fade_time = DEV_Dimming_Time;
			C12Dimming_Info.light_Control_Flag |= 1<<loop;
			break;
		case CFG_Dev_CtrlWay_Is_Dim_Up:         //向上调光
			C12Dimming_Info.light_Control_Flag |= 1<<loop;
			if(C12Dimming_Info.set_light[loop] + CtrlContect <= 100)
			{
				C12Dimming_Info.set_light[loop] += CtrlContect;
			}
			else
			{
				C12Dimming_Info.set_light[loop] = 100;     //如果超过100即是最亮
			}
			break;
		case CFG_Dev_CtrlWay_Is_Dim_Down:       //向下调光
			C12Dimming_Info.light_Control_Flag |= 1<<loop;
			if(C12Dimming_Info.set_light[loop]  >= CtrlContect)
			{
				C12Dimming_Info.set_light[loop] -= CtrlContect;
			}
			else
			{
				C12Dimming_Info.set_light[loop] = 0;     //如果减不够，即是0
			}
			break;
		case CFG_Dev_CtrlWay_Is_Dim_Up_Limit:         //向上调光至上限
			if(0x00 != C12Dimming_Info.set_light[loop])
			{
				C12Dimming_Info.light_Control_Flag |= 1<<loop;
				if(C12Dimming_Info.set_light[loop] + CtrlContect <= 100)
				{
					C12Dimming_Info.set_light[loop] += CtrlContect;
				}
				else
				{
					C12Dimming_Info.set_light[loop] = 100;     //如果超过100即是最亮
				}	
			}			
			break;
		case CFG_Dev_CtrlWay_Is_Dim_Down_Limit:         //向下调光至下限
			C12Dimming_Info.light_Control_Flag |= 1<<loop;
			if(C12Dimming_Info.set_light[loop]  > CtrlContect)
			{
				C12Dimming_Info.set_light[loop] -= CtrlContect;
			}		
			break;
	}
	
	BUS_Public.check = 0x00;
	BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&C12Dimming_Info, sizeof(BUS_C12DIMMING_INFO));
	SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),devaddr);/*将数据保存*/
	SRAM_DMA_Write_Buff((uint8_t *)&C12Dimming_Info,sizeof(BUS_C12DIMMING_INFO),devaddr+Dev_Privately);
		
}

/*******************************************************************************
* Function Name  : BLV_C12Dimming_Read_State 
* Description    : BLV_C12调光亮度获取
* Input          : 
	devaddr	: 设备信息地址
  loop    ：回路
* Return  : 无
*******************************************************************************/
uint16_t BLV_C12Dimming_Read_State(uint32_t devaddr,uint16_t loop)
{
	if(devaddr == 0x00)	return 0x00;
	BUS_C12DIMMING_INFO C12Dimming_Info;
	SRAM_DMA_Read_Buff((uint8_t *)&C12Dimming_Info,sizeof(BUS_C12DIMMING_INFO),devaddr+Dev_Privately);
	
	if(loop >= C12Dimming_CH_MAX) return 0x00;		//控制回路不在范围内
	
	return C12Dimming_Info.light[loop];
}

/*******************************************************************************
* Function Name  : BLW_C12Dimming_Group_Ctrl 
* Description    : BLW继电器群控
* Input          : 
	devaddr	: 设备信息地址
  CtrlFlag：控制标志  不能超过32路
	CtrlNum : 控制的设备个数   以最后一个为准
	start   ：状态    0x01开  0x02关
* Return  : 无
* attention : temp1非0才会进行保存
*******************************************************************************/
void BLW_C12Dimming_Group_Ctrl(uint32_t CfgDevAddIn, uint16_t DevInputAddr,uint32_t devaddr, uint32_t CtrlFlag, uint8_t CtrlNum,uint16_t *start)
{
//	uint1_t state;    //0关  1开
	uint8_t i;
	uint8_t CtrlMode = 0,CtrlContect = 0;
	Device_Public_Information_G BUS_Public;
	BUS_C12DIMMING_INFO C12Dimming_Info;
	Device_Public_Information_G BUS_PublicIn;
	RS485_LED_INFO Rs485LEDIn;
	
	if(devaddr == 0x00)	return;
	
	if(NULL != CfgDevAddIn)   //触发节点非空
	{
		SRAM_DMA_Read_Buff((uint8_t *)&BUS_PublicIn,sizeof(Device_Public_Information_G),CfgDevAddIn);		
		SRAM_DMA_Read_Buff((uint8_t *)&Rs485LEDIn,sizeof(RS485_LED_INFO),CfgDevAddIn+Dev_Privately);
	}
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),devaddr);				
	SRAM_DMA_Read_Buff((uint8_t *)&C12Dimming_Info,sizeof(BUS_C12DIMMING_INFO),devaddr+Dev_Privately);
	
	if(CtrlNum >= C12Dimming_CH_MAX)
	{
		CtrlNum = C12Dimming_CH_MAX;
	}

	
	for(i = 0; i < CtrlNum; i++)
	{
		if(CtrlFlag&(0x0001<<i))   //非0为控制
		{
//			temp1++;
			CtrlMode = start[i]&0x00ff;    //控制模式
			CtrlContect = start[i]>>8;     //控制内容
			Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"C12调光状态群控控制开始，当前回路：%d配置:%04X ", i+1, start[i]);

			switch(CtrlMode)
			{
				case CFG_Dev_CtrlWay_Is_Open:
				case CFG_Dev_CtrlWay_Is_Close:
					if((NULL != CfgDevAddIn) && (DEV_RS485_PWM == BUS_PublicIn.type))  //调光类型  滑动条协议
					{
						Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"C12调光状态群控存在滑动条，回路:%d亮度：%d ", DevInputAddr, Rs485LEDIn.DevRecBuf[DevInputAddr]);
						
						C12Dimming_Info.set_light[i] = Rs485LEDIn.DevRecBuf[DevInputAddr];   
						C12Dimming_Info.fade_time = DEV_Dimming_Time;
						C12Dimming_Info.light_Control_Flag |= 1<<i;
					}
					else
					{
						C12Dimming_Info.set_light[i] = CtrlContect;   
						C12Dimming_Info.fade_time = DEV_Dimming_Time;
						C12Dimming_Info.light_Control_Flag |= 1<<i;
					}
					break;
				case CFG_Dev_CtrlWay_Is_Dim_Up:         //向上调光
					C12Dimming_Info.light_Control_Flag |= 1<<i;
					if(C12Dimming_Info.set_light[i] + CtrlContect <= 100)
					{
						C12Dimming_Info.set_light[i] += CtrlContect;
					}
					else
					{
						C12Dimming_Info.set_light[i] = 100;     //如果超过100即是最亮
					}
					break;
				case CFG_Dev_CtrlWay_Is_Dim_Down:       //向下调光
					C12Dimming_Info.light_Control_Flag |= 1<<i;
					if(C12Dimming_Info.set_light[i]  >= CtrlContect)
					{
						C12Dimming_Info.set_light[i] -= CtrlContect;
					}
					else
					{
						C12Dimming_Info.set_light[i] = 0;     //如果减不够，即是0
					}
					break;
			case CFG_Dev_CtrlWay_Is_Dim_Up_Limit:         //亮的灯 向上调光至上限
				if(0x00 != C12Dimming_Info.set_light[i])
				{
					C12Dimming_Info.light_Control_Flag |= 1<<i;
					if(C12Dimming_Info.set_light[i] + CtrlContect <= 100)
					{
						C12Dimming_Info.set_light[i] += CtrlContect;
					}
					else
					{
						C12Dimming_Info.set_light[i] = 100;     //如果超过100即是最亮
					}	
				}			
				break;
			case CFG_Dev_CtrlWay_Is_Dim_Down_Limit:         //亮的灯 向下调光至下限
				if(0x00 != C12Dimming_Info.set_light[i])
				{
					C12Dimming_Info.light_Control_Flag |= 1<<i;
					if(C12Dimming_Info.set_light[i]  > CtrlContect)
					{
						C12Dimming_Info.set_light[i] -= CtrlContect;
					}
				}				
				break;	
			case CFG_Dev_CtrlWay_Is_Dim_Up_Step_Cycle_Limit:    //亮的灯 点按循环调光
				if(0x00 != C12Dimming_Info.set_light[i])
				{
					C12Dimming_Info.light_Control_Flag |= 1<<i;
					switch(C12Dimming_Info.LedUpDown[i])      
					{
						case 0x00:   //向上
							if(C12Dimming_Info.set_light[i] + CtrlContect < 100)
							{
								C12Dimming_Info.set_light[i] += CtrlContect;
							}
							else 
							{
								C12Dimming_Info.LedUpDown[i] = 0x01;
								C12Dimming_Info.set_light[i] = 100;     //如果超过100即是最亮
							}
							break;
						case 0x01:  //向下
							if(C12Dimming_Info.set_light[i] > CtrlContect)
							{
								C12Dimming_Info.set_light[i] -= CtrlContect;
								if(C12Dimming_Info.set_light[i] < CtrlContect)
								{
									C12Dimming_Info.LedUpDown[i] = 0x00;   //减到最低就反向
								}
							}
//	 					  else
//							{
//								C12Dimming_Info.LedUpDown[i] = 0x00;
//								C12Dimming_Info.set_light[i] = 0;     //如果减不够，即是0
//							}
							break;
						default:
							C12Dimming_Info.LedUpDown[i] = 0x00;
							break;
					}
					Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"C12调光状态群控控制开始，当前回路：%d执行亮度:%04X ", i+1, C12Dimming_Info.set_light[i]);
				}
				break;				
			case CFG_Dev_CtrlWay_Is_Dim_CycleUp:                   //向上循环  
				C12Dimming_Info.set_light_updown_mode = 0x01;     //模式  开始调节到顶并取反
				C12Dimming_Info.set_light_updown_dir = 0x01;       //递增
				C12Dimming_Info.set_light_updown_num = CtrlContect;       //步进值
				C12Dimming_Info.light_updown_Control_Flag |= 1<<i;    //控制标志
				break;
			case CFG_Dev_CtrlWay_Is_Dim_CycleDown:                   //向下循环  
				C12Dimming_Info.set_light_updown_mode = 0x01;     //模式  开始调节到顶并取反
				C12Dimming_Info.set_light_updown_dir = 0x00;       //递减
				C12Dimming_Info.set_light_updown_num = CtrlContect;       //步进值
				C12Dimming_Info.light_updown_Control_Flag |= 1<<i;    //控制标志
				break;	
			case CFG_Dev_CtrlWay_Is_Dim_Stop:       //停止调光
				C12Dimming_Info.set_light_updown_mode = 0x02;     //模式  停止调光
				C12Dimming_Info.set_light_updown_dir = 0x00;       //递减
				C12Dimming_Info.set_light_updown_num = CtrlContect;       //步进值
				C12Dimming_Info.light_updown_Control_Flag |= 1<<i;    //控制标志				
				break;
			}
		}
	}
	
	
//	if(temp1 != 0x00)
	{
		BUS_Public.check = 0x00;
		BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&C12Dimming_Info, sizeof(BUS_C12DIMMING_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),devaddr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&C12Dimming_Info,sizeof(BUS_C12DIMMING_INFO),devaddr+Dev_Privately);
				
	}	
}

/*******************************************************************************
* Function Name  : BLW_C12Dimming_Group_Ctrl 
* Description    : BLW继电器群控
* Input          : 
	devaddr	: 设备信息地址
  CtrlFlag：控制标志  不能超过32路
	CtrlNum : 控制的设备个数   以最后一个为准
	start   ：状态    0x01开  0x02关
* Return  : 所有状态满足 返回开 有一个不满足 返回关
* attention : temp1非0才会进行保存
*******************************************************************************/
uint16_t BLW_C12Dimming_Group_Read(uint32_t devaddr, uint8_t SceneType, uint32_t ReadFlag, uint8_t ReadNum,uint16_t *start)
{
	uint8_t i;
	uint8_t Ret = 0x00;
	uint8_t CtrlMode = 0,CtrlContect = 0;
	
	if(devaddr == 0x00)	return 0x00;
	
	BUS_C12DIMMING_INFO C12Dimming_Info;       //继电器局部变量
	
//	Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"继电器状态群控读取开始，回路标志:%08X ", ReadFlag);

	SRAM_DMA_Read_Buff((uint8_t *)&C12Dimming_Info,sizeof(BUS_C12DIMMING_INFO),devaddr+Dev_Privately);
	
	if(ReadNum >= C12Dimming_CH_MAX)
	{
		ReadNum = C12Dimming_CH_MAX;
	}
	switch(SceneType)
	{
		case 0x01:   //普通场景
			for(i = 0; i < ReadNum; i++)
			{
				if(ReadFlag&(0x0001<<i))   //非0为读取判断
				{
					CtrlMode = start[i]&0x00ff;    //控制模式
					CtrlContect = start[i]>>8;     //控制内容
		//			Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"C12调光状态群控读取开始，当前回路：%d配置:%04X ", i+1, start[i]);

					switch(CtrlMode)
					{
						case 0x01:   //状态开
						case 0x02:   //状态关
							if(C12Dimming_Info.set_light[i] != CtrlContect)
							{
								Ret = DEV_STATE_CLOSE;
							}
							break;

					}
				}
			}			
			break;
		case 0x02:  //特殊场景
			for(i = 0; i < ReadNum; i++)
			{
				if(ReadFlag&(0x0001<<i))   //非0为读取判断
				{
					CtrlMode = start[i]&0x00ff;    //控制模式
					CtrlContect = start[i]>>8;     //控制内容
		//			Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"C12调光状态群控读取开始，当前回路：%d配置:%04X ", i+1, start[i]);

					if(0x01 == CtrlMode)//状态开
					{
						if(C12Dimming_Info.set_light[i] == CtrlContect)
						{
							Ret = DEV_STATE_OPEN;   //只要一行满足就跳出
							break;
						}
					}
				}
			}			
			break;
	}

	if(0x00 == Ret)   //
	{
		switch(SceneType)
		{
			case 0x01:
				Ret = DEV_STATE_OPEN;
				break;
			case 0x02:
				Ret = DEV_STATE_CLOSE;
				break;
		}
	}
	return Ret;
	
}

//#endif





