/**
  ******************************************************************************
  * @file    BLV_485_Dev_LEDFun.c
  * @author  BLW RCU  development Team
  * @version SongHan
  * @date    2021年12月08日
  * @brief  
	*    485调光总表，所有的调光驱动尽可能放在这张表里面
	* @log  202205121720   SongHan   群读增加记忆调光的判断  CFG_Dev_CtrlWay_Is_Dim_Open
	* @log  202205261640   SongHan   调光回路限制放在外设驱动里面
  ******************************************************************************
  */
#include "BLV_485_Dev_LEDFun.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"

/*调光驱动头文件开始*/
#include "BLV_485_Dev_PWMFun.h"
#include "BLV_485_Dev_A9LDFun.h"
#include "BLV_485_Dev_SliderFun.h"
#include "BLV_485_Dev_A8PBFun.h"
#include "BLV_485_Dev_C12DimFun.h"

/*调光驱动头文件结束*/

/*LED调光驱动代码*/

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

#define LEDGRADUALTIME         30   //30  即300ms
#define LEDSnMax               15     //最大Sn号
#define DEVLEDTYPE             24           //LED类型

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

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

/*函数指针宏定义*/
typedef void (*DevFunP)(Device_Public_Information_G *BUS_Public, RS485_LED_INFO *Rs485LED, uint16_t LoopNum);	//传递共用数据指针 私有数据指针

/*设备驱动宏定义开始*/
#define RS485_DEV_PRO_FUN_01    DevExistJudgge(RS485_LED_PWM_Flag, BLW_RS485_PWM_Data_Init)  //((DevFunP)NULL)        //PWM调光
#define RS485_DEV_PRO_FUN_02    DevExistJudgge(RS485_LED_A9LD_Flag, BLW_RS485_A9LD_Data_Init)  //((DevFunP)NULL)      //A9LD调光
#define RS485_DEV_PRO_FUN_03    DevExistJudgge(RS485_LED_Slider_Flag, BLW_RS485_Slider_Data_Init)//((DevFunP)NULL)           //滑动条调光C7T的地址跟
#define RS485_DEV_PRO_FUN_04    DevExistJudgge(RS485_LED_A8PB_Flag, BLW_RS485_A8PB_Data_Init)   //((DevFunP)NULL)             //A8PB总线调光
#define RS485_DEV_PRO_FUN_05    DevExistJudgge(RS485_LED_C12Dim_Flag, BLW_RS485_C12Dim_Data_Init) //((DevFunP)NULL)            //C12调光
#define RS485_DEV_PRO_FUN_06    DevExistJudgge(RS485_LED_RGB_Flag, BLW_RS485_RGB_Data_Init)  //((DevFunP)NULL)    //宝来威灯带模块

/*设备驱动宏定义结束*/

/*本地函数调用开始*/
//uint8_t BLW_LEDCycleCtrl(uint32_t devaddr); 
//uint8_t BLW_Rs485_LED_Check(uint32_t devaddr,uint32_t data_addr,uint16_t len);
//void BLW_Rs485_LED_4LOOP_Ctrl(uint32_t devaddr, RS485_LED_INFO* Rs485LEDInfo);
void BLW_LED_Group_Ctrl(uint32_t CfgDevAddIn, uint16_t DevInputAddr,uint32_t devaddr, uint32_t CtrlFlag, uint8_t CtrlNum,uint16_t *start);
uint16_t BLW_LED_Group_Read(uint32_t devaddr, uint8_t SceneType,uint32_t ReadFlag, uint8_t ReadNum,uint16_t *start);

//void BLW_RS485_LED_Data_Init(uint8_t devaddr);
//uint8_t BLW_LEDCycleCtrl(uint32_t dev_addr);
//uint8_t BLW_Rs485_LED_Check(uint32_t dev_addr,uint32_t data_addr,uint16_t len);
void BLW_LED_Control_State(uint32_t CfgDevAddIn, uint16_t DevInputAddr, uint32_t devaddr,uint16_t loop,uint16_t start);
uint16_t BLW_LED_Read_State(uint32_t devaddr,uint16_t loop);
//uint8_t LED_State_Change_Get(uint32_t devaddr);
//void BLW_LED_Group_Control_State(uint32_t devaddr,uint16_t state);
uint8_t Get_BLV485_LED_Online_Status(uint32_t devaddr);
//void Pwm_State_Add(uint32_t devaddr);
//void Pwm_State_Dec(uint32_t devaddr);
/*本地函数调用结束*/

/*******************************************************************************
* @name  :  LED调光驱动总表
* @para  : 
*   DataLen :数据长度
*   Data：   数据数组
* @attention   : 将调光的所有设备参数传进来，
*
*******************************************************************************/
void BLW_RS485_LED_Data_Init(uint16_t DataLen, uint8_t *Data)
{
	Device_Public_Information_G BUS_Public;
	RS485_LED_INFO Rs485LED;

	memset(&BUS_Public,0,sizeof(Device_Public_Information_G));
	memset(&Rs485LED,0,sizeof(RS485_LED_INFO));
	
	BUS_Public.addr = Data[1];		//设备485地址
	BUS_Public.type = Data[0];		//设备类型
	BUS_Public.baud = Data[3] + (Data[4]<<8) + (Data[5]<<16) + (Data[6]<<24);		    //设备发送波特率9600
	BUS_Public.retry_num = 0x03;	//设备数据重发次数
	BUS_Public.wait_time = 0x0064;	//设备数据发送等待回复时间 - 100ms
	BUS_Public.Protocol = Data[7];	//设备协议
	
    Rs485LED.PWM_Set_Time=30;     
	Rs485LED.RGB_Set_Time=30;
	Rs485LED.A8PB_Set_Time=30;
	Rs485LED.A9LD_Set_Time=30;
	Rs485LED.C12_Set_Time=30;
	
	BUS_Public.DevFunInfo.Dev_Output_Ctrl = BLW_LED_Control_State;
	BUS_Public.DevFunInfo.Dev_Output_Loop_State_Get = BLW_LED_Read_State;   
	
	BUS_Public.DevFunInfo.Dev_Output_Group_Ctrl = BLW_LED_Group_Ctrl; //
	BUS_Public.DevFunInfo.Dev_Output_Loop_Group_State_Get_ptr = BLW_LED_Group_Read; //	
	/*初始化LED调光初始值*/
//	if(Data[10] >= LED_BUFF_Size)   //输出回路超过范围
//	{
//		Rs485LED.LEDLoopValidNum = LED_BUFF_Size;
//	}
//	else              //未超过范围
//	{
//		Rs485LED.LEDLoopValidNum = Data[10];
//	}
	Rs485LED.DevOffline = DEV_IS_LINEUNINIT;
	Rs485LED.DevOfflineLast = DEV_IS_LINEUNINIT;   //在线状态初始化
	switch(Data[7])
	{
		case	 ENUM_RS485_DEV_PRO_01: if(NULL!=RS485_DEV_PRO_FUN_01) RS485_DEV_PRO_FUN_01(&BUS_Public, &Rs485LED, Data[10]);break; //        
		case	 ENUM_RS485_DEV_PRO_02: if(NULL!=RS485_DEV_PRO_FUN_02) RS485_DEV_PRO_FUN_02(&BUS_Public, &Rs485LED, Data[10]);break; //          
		case	 ENUM_RS485_DEV_PRO_03: if(NULL!=RS485_DEV_PRO_FUN_03) RS485_DEV_PRO_FUN_03(&BUS_Public, &Rs485LED, Data[10]);break;                                 //3
		case	 ENUM_RS485_DEV_PRO_04: if(NULL!=RS485_DEV_PRO_FUN_04) RS485_DEV_PRO_FUN_04(&BUS_Public, &Rs485LED, Data[10]);break;
		case	 ENUM_RS485_DEV_PRO_05: if(NULL!=RS485_DEV_PRO_FUN_05) RS485_DEV_PRO_FUN_05(&BUS_Public, &Rs485LED, Data[10]);break;
		case	 ENUM_RS485_DEV_PRO_06: if(NULL!=RS485_DEV_PRO_FUN_06) RS485_DEV_PRO_FUN_06(&BUS_Public, &Rs485LED, Data[10]);break;
		
	}
	
	
	switch(Data[2])
	{
		case Active_Port:   //主动端口
			BUS_Public.port = Active_Port;		//设备发送端口
			Add_ACT_Device_To_List2(&BUS_Public,(uint8_t *)&Rs485LED,sizeof(RS485_LED_INFO));
			Act485_Info.device_num += 1;
			break;  
		case Polling_Port:   //轮询端口
			BUS_Public.port = Polling_Port;		//设备发送端口
			Add_POLL_Device_To_List2(&BUS_Public,(uint8_t *)&Rs485LED,sizeof(RS485_LED_INFO));   //将填充后的数据添加到链表
			Poll485_Info.device_num += 1;	
			break;
		case Bus_port:    //总线端口
			BUS_Public.port = Bus_port;		//设备发送端口		
			Add_BUS_Device_To_List2(&BUS_Public,(uint8_t *)&Rs485LED,sizeof(RS485_LED_INFO));
			BUS485_Info.device_num += 1;
			break;
	}
}

/*******************************************************************************
* Function Name  : BLW_RS485_LED_For_Logic_Init 
* Description    : 调光初始化信息来自逻辑文件
*******************************************************************************/
void BLW_RS485_LED_For_Logic_Init(LOGICFILE_DEVICE_INFO *dev_info,uint8_t *data,uint16_t data_len)
{
	Device_Public_Information_G BUS_Public;
	RS485_LED_INFO Rs485LED;

	memset(&BUS_Public,0,sizeof(Device_Public_Information_G));
	memset(&Rs485LED,0,sizeof(RS485_LED_INFO));
	
	BUS_Public.addr = dev_info->addr;		//设备485地址
	BUS_Public.type = dev_info->type;		//设备类型
	BUS_Public.baud = dev_info->baud;;		//设备发送波特率9600
	BUS_Public.retry_num = 0x03;	//设备数据重发次数
	BUS_Public.wait_time = 0x0064;	//设备数据发送等待回复时间 - 100ms
	BUS_Public.Protocol = dev_info->version;
	
	Rs485LED.PWM_Set_Time=30;
	Rs485LED.RGB_Set_Time=30;
	Rs485LED.A8PB_Set_Time=30;
	Rs485LED.A9LD_Set_Time=30;
	Rs485LED.C12_Set_Time=30;
	
	
	Rs485LED.Dim_Global_Value = 100;
	DevActionGlobal.DimGlobalValue = Rs485LED.Dim_Global_Value;
	DevActionGlobal.Dim_Upper_limit = 100;
	DevActionGlobal.Dim_Lower_limit = 0;
	
	BUS_Public.DevFunInfo.Dev_Output_Ctrl = BLW_LED_Control_State;
	BUS_Public.DevFunInfo.Dev_Output_Loop_State_Get = BLW_LED_Read_State;   
	
	BUS_Public.DevFunInfo.Dev_Output_Group_Ctrl = BLW_LED_Group_Ctrl; //
	BUS_Public.DevFunInfo.Dev_Output_Loop_Group_State_Get_ptr = BLW_LED_Group_Read; //	
	/*初始化LED调光初始值*/
//	if(dev_info->output_num >= LED_BUFF_Size)   //输出回路超过范围
//	{
//		Rs485LED.LEDLoopValidNum = LED_BUFF_Size;
//	}
//	else if(0x00 == dev_info->output_num)
//	{
//		Rs485LED.LEDLoopValidNum = 0x01;
//	}
//	else              //未超过范围
//	{
//		Rs485LED.LEDLoopValidNum = dev_info->output_num;
//	}
	Rs485LED.DevOfflineLast = DEV_IS_ONLINE;   //在线状态初始化
	Rs485LED.DevOffline = Rs485LED.DevOfflineLast;
	switch(BUS_Public.Protocol)		//协议版本
	{
		case	 ENUM_RS485_DEV_PRO_01: if(NULL!=RS485_DEV_PRO_FUN_01) RS485_DEV_PRO_FUN_01(&BUS_Public, &Rs485LED, dev_info->output_num);break; //        
		case	 ENUM_RS485_DEV_PRO_02: if(NULL!=RS485_DEV_PRO_FUN_02) RS485_DEV_PRO_FUN_02(&BUS_Public, &Rs485LED, dev_info->output_num);break; //          
		case	 ENUM_RS485_DEV_PRO_03: if(NULL!=RS485_DEV_PRO_FUN_03) RS485_DEV_PRO_FUN_03(&BUS_Public, &Rs485LED, dev_info->output_num);break;                                 //3
		case	 ENUM_RS485_DEV_PRO_04: if(NULL!=RS485_DEV_PRO_FUN_04) RS485_DEV_PRO_FUN_04(&BUS_Public, &Rs485LED, dev_info->output_num);break;
		case	 ENUM_RS485_DEV_PRO_05: if(NULL!=RS485_DEV_PRO_FUN_05) RS485_DEV_PRO_FUN_05(&BUS_Public, &Rs485LED, dev_info->output_num);break;
		case	 ENUM_RS485_DEV_PRO_06: if(NULL!=RS485_DEV_PRO_FUN_06) RS485_DEV_PRO_FUN_06(&BUS_Public, &Rs485LED, dev_info->output_num);break;
		
	}
	
	switch(dev_info->port)
	{
		case Active_Port:   //主动端口
			BUS_Public.port = Active_Port;		//设备发送端口
			Add_ACT_Device_To_List2(&BUS_Public,(uint8_t *)&Rs485LED,sizeof(RS485_LED_INFO));
			Act485_Info.device_num += 1;
			break;  
		case Polling_Port:   //轮询端口
			BUS_Public.port = Polling_Port;		//设备发送端口
			Add_POLL_Device_To_List2(&BUS_Public,(uint8_t *)&Rs485LED,sizeof(RS485_LED_INFO));   //将填充后的数据添加到链表
			Poll485_Info.device_num += 1;	
			break;
		case Bus_port:    //总线端口
			BUS_Public.port = Bus_port;		//设备发送端口		
			Add_BUS_Device_To_List2(&BUS_Public,(uint8_t *)&Rs485LED,sizeof(RS485_LED_INFO));
			BUS485_Info.device_num += 1;
			break;
	}
	
}


/*******************************************************************************
* Function Name  : BLW_LED_Control_State 
* Description    : BLWLED调光状态控制函数
* Input          : 
	devaddr	: 设备信息地址
  loop    ：回路
	start   ：状态
* Return  : 无
* attention : temp1非0才会进行保存
*******************************************************************************/
void BLW_LED_Control_State(uint32_t CfgDevAddIn, uint16_t DevInputAddr, uint32_t devaddr,uint16_t loop,uint16_t start)
{
	uint8_t temp1 = 0;
	uint8_t CtrlMode;   //控制模式
	uint8_t CtrlContect;
	Device_Public_Information_G BUS_Public;
	RS485_LED_INFO Rs485LED;
	
	if(devaddr == 0x00)	return;
	

	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),devaddr);				
	SRAM_DMA_Read_Buff((uint8_t *)&Rs485LED,sizeof(RS485_LED_INFO),devaddr+Dev_Privately);
	
	if(loop >= Rs485LED.LEDLoopValidNum)    //有效回路
	{
		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:
			Rs485LED.LEDLightnessReadFlag = 0x01;    //唯有停止，向上向下，才会发送读取命令
			Rs485LED.LEDLightnessReadCnt = REPEATSENDTIMEMAX;
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"调光读取置位，触发模式:%d 读取标志:%d", CtrlMode, Rs485LED.LEDLightnessReadFlag);  //
			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:
			Rs485LED.DevCtrlWayBuf[loop] = CtrlMode;  // - CFG_Dev_CtrlWay_Is_LED_START + 1
			Rs485LED.DevCtrlWayContect[loop] = CtrlContect;  //保存控制内容
			temp1++;
			break;
		case CFG_Dev_CtrlWay_Is_Open:         //状态控制
		case CFG_Dev_CtrlWay_Is_Close:
            
			Rs485LED.DevSendBuf[loop] = CtrlContect; //亮度赋值
			temp1++;
			break;
		case CFG_Dev_CtrlWay_Is_Dim_Open:   //记忆开
			if(0x00 != Rs485LED.LEDLightRelease[loop])  //如果存在保存的值
			{
				CtrlContect = Rs485LED.LEDLightRelease[loop];   //改   开
			}
			else if(0x00 != CtrlContect)   //如果不存在
			{
				CtrlContect = CtrlContect;   //以配置为准
			}
			else 
			{
				CtrlContect = 0x64;      //默认最亮
			}
			Rs485LED.DevSendBuf[loop] = CtrlContect; //亮度赋值
			temp1++;
			break;
		case CFG_Dev_CtrlWay_Is_Dim_Up:         //向上调光
			if(Rs485LED.DevSendBuf[loop] + CtrlContect <= 100)
			{
				Rs485LED.DevSendBuf[loop] += CtrlContect;
			}
			else
			{
				Rs485LED.DevSendBuf[loop] = 100;     //如果超过100即是最亮
			}
			temp1++;
			break;
		case CFG_Dev_CtrlWay_Is_Dim_Down:       //向下调光
			if(Rs485LED.DevSendBuf[loop]  >= CtrlContect)
			{
				Rs485LED.DevSendBuf[loop] -= CtrlContect;
			}
			else
			{
				Rs485LED.DevSendBuf[loop] = 0;     //如果减不够，即是0
			}
			temp1++;
			break;
		case CFG_Dev_CtrlWay_Is_Dim_Up_Limit:         //向上调光
			if(0x00 != Rs485LED.DevSendBuf[loop])
			{
				if(Rs485LED.DevSendBuf[loop] + CtrlContect <= 100)
				{
					Rs485LED.DevSendBuf[loop] += CtrlContect;
				}
				else
				{
					Rs485LED.DevSendBuf[loop] = 100;     //如果超过100即是最亮
				}
				temp1++;
			}
			break;
		case CFG_Dev_CtrlWay_Is_Dim_Down_Limit:       //向下调光
			if(Rs485LED.DevSendBuf[loop]  > CtrlContect)
			{
				Rs485LED.DevSendBuf[loop] -= CtrlContect;
				temp1++;				
			}

			break;				
//		case CFG_Dev_CtrlWay_Is_Dim_UP_Limit:   //调光上限
//			Rs485LED.LEDUpLightLimit = CtrlContect; //亮度赋值
//			break;
//		case CFG_Dev_CtrlWay_Is_Dim_DOWN_Limit: //调光下限
//			Rs485LED.LEDDownLightLimit = CtrlContect; //亮度赋值
//			break;
	}	
	
//		if(Rs485LED.DevSendBuf[loop] != start)  //不同
//		{
//			Rs485LED.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",loop,start);
	
		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),devaddr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&Rs485LED,sizeof(RS485_LED_INFO),devaddr+Dev_Privately);
		
	}
}
/*******************************************************************************
* Function Name  : BLW_LED_Read_State 
* Description    : BLWLED调光状态读取函数
* Input          : 
	devaddr	: 设备信息地址
  loop    ：回路
* Return  : 无
*******************************************************************************/
uint16_t BLW_LED_Read_State(uint32_t devaddr,uint16_t loop)
{
	if(devaddr == 0x00)
		return 0x00;
	
	RS485_LED_INFO Rs485LED;
	
	SRAM_DMA_Read_Buff((uint8_t *)&Rs485LED,sizeof(RS485_LED_INFO),devaddr+Dev_Privately);	
	
	if(loop >= Rs485LED.LEDLoopValidNum)
	{
		return 0x00;    //超出范围 直接返回0
	}
	

	return Rs485LED.DevSendBuf[loop];
		
}

/*获取LED调光在线状态*/
uint8_t Get_BLV485_LED_Online_Status(uint32_t devaddr)
{
	RS485_LED_INFO Rs485LED;
	
	SRAM_DMA_Read_Buff((uint8_t *)&Rs485LED,sizeof(RS485_LED_INFO),devaddr+Dev_Privately);
	
	if(Rs485LED.DevOffline == DEV_IS_ONLINE)
	{
		return 0x01;
	}
	return 0x02;
}


//#define LEDCTRLSENDLEN    18
///**
//	* @name BLW 4路 集中调光控制函数
//	* @para
//	*   dev_addr     设备起始地址   
//	* @return         无
//  * @attention      发送循环里调用，变量已经被判断
//	*/
//void BLW_Rs485_LED_4LOOP_Ctrl(uint32_t dev_addr, RS485_LED_INFO* Rs485LEDInfo)
//{
////  RS485_LED_INFOP Rs485LEDInfo;       //调光局部变量
////	uint8_t i;
//	uint8_t port = SRAM_Read_Byte(dev_addr+Dev_port);      //通讯端口
//	uint8_t DevAddr = SRAM_Read_Byte(dev_addr+Dev_Addr);   //设备地址
//	                  //己方地址 发送类型 设备类型      收方地址 LEN             校验和   CMD  开4通道 渐变0.1s 保存   CH8    CH7    CH6    CH5    CH4    CH3    CH2    CH1 
//	uint8_t SendBuf[18] = {0x00,   0x00,  DEVLEDTYPE,    0x01,   LEDCTRLSENDLEN,  0x18,  0x21,  0x0f,   0x64,   0x00,  0x00,  0x00,  0x00,  0x00,  0x50,  0x50,  0x50,  0x50};
//			
//	
//	SendBuf[1] |= BLV_SN_Get(&Rs485LEDInfo->LEDSn, 0, LEDSnMax);

//		
//	SendBuf[3] = DevAddr;   //
//	SendBuf[8] = LEDGRADUALTIME;       //调光渐变时间
////	for(i = 0; i < Rs485LEDInfo->LEDLoopValidNum; i++)
////	{
////		SendBuf[16-i] = Rs485LEDInfo->DevSendBuf[i];
////	}
//	memcpy(&SendBuf[14], &Rs485LEDInfo->DevSendBuf[0], Rs485LEDInfo->LEDLoopValidNum);  //指示灯控制
//	SendBuf[5] = CheckSum_Overlook_Check(SendBuf, LEDCTRLSENDLEN, 5);	
//		
//	MCU485_SendString(port,SendBuf,LEDCTRLSENDLEN);

////	RS485_Port_SendFun(CfgDevAdd, SendBuf, LEDCTRLSENDLEN, BLWLEDCTRLTIMEOUT);  		
//	
//}

/**@name  调光状态变化得
	*@para  无
  *@ret   为1 有变化   为0  无变化
	*/
//uint8_t LED_State_Change_Get(uint32_t devaddr)
//{
//	uint8_t i;
//	uint8_t Ret = 0x00;
//	uint8_t temp1 = 0;
//	Device_Public_Information_G BUS_Public;
//	RS485_LED_INFO Rs485LEDInfo;

//	if(devaddr == 0x00)	return 0;
//	
//	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),devaddr);				
//	SRAM_DMA_Read_Buff((uint8_t *)&Rs485LEDInfo,sizeof(RS485_LED_INFO),devaddr+Dev_Privately);
//	
//	for(i = 0; i < Rs485LEDInfo.LEDLoopValidNum; i ++)
//	{
//		if(Rs485LEDInfo.DevSendBufNext[i] != Rs485LEDInfo.DevSendBuf[i])   //状态变化
//		{
//			Ret = 0x01;     //有变化	
//			temp1++;
//			Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"LED调光状态变化 DevSendBuf loop:%d,start:%d\r\n", i, Rs485LEDInfo.DevSendBuf[i]);  //调光状态变化打印
//			
//			memcpy(Rs485LEDInfo.DevSendBufNext, Rs485LEDInfo.DevSendBuf, Rs485LEDInfo.LEDLoopValidNum); //同步状态
//			break;
//		}
//	}
//	
//	if(temp1 != 0x00)
//	{
//		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),devaddr);/*将数据保存*/
//		SRAM_DMA_Write_Buff((uint8_t *)&Rs485LEDInfo,sizeof(RS485_LED_INFO),devaddr+Dev_Privately);
//		
//	}
//	
//	return Ret;        
//}




/*******************************************************************************
* Function Name  : BLW_LED_Group_Ctrl 
* Description    : BLW继电器群控
* Input          : 
	devaddr	: 设备信息地址
  CtrlFlag：控制标志  不能超过32路
	CtrlNum : 控制的设备个数   以最后一个为准
	start   ：状态    0x01开  0x02关
* Return  : 无
* attention : temp1非0才会进行保存
*******************************************************************************/
void BLW_LED_Group_Ctrl(uint32_t CfgDevAddIn, uint16_t DevInputAddr,uint32_t devaddr, uint32_t CtrlFlag, uint8_t CtrlNum,uint16_t *start)
{
//	uint8_t crc_val = 0;//,temp1 = 0;
//	uint1_t state;    //0关  1开
	uint8_t i;
	uint8_t CtrlMode = 0,CtrlContect = 0;
	Device_Public_Information_G BUS_Public;
	RS485_LED_INFO Rs485LEDInfo;

	if(devaddr == 0x00)	return;

//	Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"LED调光状态群控控制开始，当前回路标志：%04X 回路上限%d ", CtrlFlag, CtrlNum);

	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),devaddr);				
	SRAM_DMA_Read_Buff((uint8_t *)&Rs485LEDInfo,sizeof(RS485_LED_INFO),devaddr+Dev_Privately);
	
	if(CtrlNum >= Rs485LEDInfo.LEDLoopValidNum)
	{
		CtrlNum = Rs485LEDInfo.LEDLoopValidNum;
	}
//	Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"LED调光状态群控控制开始:实际设备数：%d", CtrlNum);
	
	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,"%s 回路：%d配置:%04X ",__func__, i+1, start[i]);

			switch(CtrlMode)
			{
				case CFG_Dev_CtrlWay_Is_Dim_Stop:            //唯有停止，才会发送读取命令
//				case CFG_Dev_CtrlWay_Is_Dim_Up:
//				case CFG_Dev_CtrlWay_Is_Dim_Down:
					Rs485LEDInfo.LEDLightnessReadFlag = 0x01;    
					Rs485LEDInfo.LEDLightnessReadCnt = REPEATSENDTIMEMAX;
					Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"模式:%d，读取标志:%d", CtrlMode, Rs485LEDInfo.LEDLightnessReadFlag);  //
					break;
			}
			switch(CtrlMode)
			{
				case CFG_Dev_CtrlWay_Is_Dim_CycleUp:   //方式控制
				case CFG_Dev_CtrlWay_Is_Dim_CycleDown:
				case CFG_Dev_CtrlWay_Is_Dim_Stop:
					Rs485LEDInfo.DevCtrlWayBuf[i] = CtrlMode;  // - CFG_Dev_CtrlWay_Is_LED_START + 1
					Rs485LEDInfo.DevCtrlWayContect[i] = CtrlContect;  //保存控制内容
					break;
				case CFG_Dev_CtrlWay_Is_Open:         //状态控制
				case CFG_Dev_CtrlWay_Is_Close:
					Rs485LEDInfo.DevSendBuf[i] = CtrlContect; //亮度赋值
					break;
				case CFG_Dev_CtrlWay_Is_Dim_Open:   //记忆开
					if(0x00 != Rs485LEDInfo.LEDLightRelease[i])  //如果存在保存的值
					{
						CtrlContect = Rs485LEDInfo.LEDLightRelease[i];   //改   开
					}
					else if(0x00 != CtrlContect)   //如果不存在
					{
						CtrlContect = CtrlContect;   //以配置为准
					}
					else 
					{
						CtrlContect = 0x64;      //默认最亮
					}
					//Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"群控调光记忆开，下标:%d，状态:%d\r\n", i, CtrlContect);  //
					Rs485LEDInfo.DevSendBuf[i] = CtrlContect; //亮度赋值
					break;
				case CFG_Dev_CtrlWay_Is_Dim_Up:         //向上调光
					if(Rs485LEDInfo.DevSendBuf[i] + CtrlContect <= 100)
					{
						Rs485LEDInfo.DevSendBuf[i] += CtrlContect;
					}
					else
					{
						Rs485LEDInfo.DevSendBuf[i] = 100;     //如果超过100即是最亮
					}
					break;
				case CFG_Dev_CtrlWay_Is_Dim_Down:       //向下调光
					if(Rs485LEDInfo.DevSendBuf[i]  >= CtrlContect)
					{
						Rs485LEDInfo.DevSendBuf[i] -= CtrlContect;
					}
					else
					{
						Rs485LEDInfo.DevSendBuf[i] = 0;     //如果减不够，即是0
					}
					break;
				case CFG_Dev_CtrlWay_Is_Dim_Up_Limit:         //唯有亮的灯 向上调光 至上限
					if(0x00 != Rs485LEDInfo.DevSendBuf[i])
					{
						if(Rs485LEDInfo.DevSendBuf[i] + CtrlContect <= 100)
						{
							Rs485LEDInfo.DevSendBuf[i] += CtrlContect;
						}
						else
						{
							Rs485LEDInfo.DevSendBuf[i] = 100;     //如果超过100即是最亮
						}
//						temp1++;
					}
					break;
				case CFG_Dev_CtrlWay_Is_Dim_Down_Limit:       //唯有亮的灯 向下调光 至下限
					if(0x00 != Rs485LEDInfo.DevSendBuf[i])
					{
						if(Rs485LEDInfo.DevSendBuf[i]  > CtrlContect)
						{
							Rs485LEDInfo.DevSendBuf[i] -= CtrlContect;
	//						temp1++;				
						}
						else
						{
							Rs485LEDInfo.DevSendBuf[i] = 0x01;        //2024-11-25
						}
					}
					break;						
				case CFG_Dev_CtrlWay_Is_Dim_Up_Step_Cycle:    //点按循环调光
					switch(Rs485LEDInfo.LedUpDown[i])      
					{
						case 0x00:   //向上
							if(Rs485LEDInfo.DevSendBuf[i] + CtrlContect < 100)
							{
								Rs485LEDInfo.DevSendBuf[i] += CtrlContect;
							}
							else 
							{
								Rs485LEDInfo.LedUpDown[i] = 0x01;
								Rs485LEDInfo.DevSendBuf[i] = 100;     //如果超过100即是最亮
							}
							break;
						case 0x01:  //向下
		//					if(Rs485LEDInfo->DevSendBuf[DevOutputLoop] > CtrlContect)
		//					{
		//						Rs485LEDInfo->DevSendBuf[DevOutputLoop] -= CtrlContect;
		//					}
		//					else
							{
								Rs485LEDInfo.LedUpDown[i] = 0x00;
								Rs485LEDInfo.DevSendBuf[i] = 0;     //如果减不够，即是0
							}
							break;
						default:
							Rs485LEDInfo.LedUpDown[i] = 0x00;
							break;
					}
					break;
		//		case CFG_Dev_CtrlWay_Is_Dim_UP_Limit:   //调光上限
		//			Rs485LEDInfo.LEDUpLightLimit = CtrlContect; //亮度赋值
		//			break;
		//		case CFG_Dev_CtrlWay_Is_Dim_DOWN_Limit: //调光下限
		//			Rs485LEDInfo.LEDDownLightLimit = CtrlContect; //亮度赋值
		//			break;
				case CFG_Dev_CtrlWay_Is_PWM_Set_Time:
					Rs485LEDInfo.PWM_Set_Time=CtrlContect;

					//Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"PWM_Set_Time:%d",Rs485LEDInfo.PWM_Set_Time);
					break;
				 case CFG_Dev_CtrlWay_Is_A9LD_Set_Time:
					Rs485LEDInfo.A9LD_Set_Time=CtrlContect;
					//Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"A9LD_Set_Time:%d",Rs485LEDInfo.A9LD_Set_Time);
					break;
				case CFG_Dev_CtrlWay_Is_A8PB_Set_Time:
					Rs485LEDInfo.A8PB_Set_Time=CtrlContect;
					//Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"A8PB_Set_Time:%d",Rs485LEDInfo.A8PB_Set_Time);
					break;
				case CFG_Dev_CtrlWay_Is_C12_Set_Time:	
					Rs485LEDInfo.C12_Set_Time=CtrlContect;
					//Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"C12_Set_Time:%d - %d",Rs485LEDInfo.C12_Set_Time,i);
					break;
				case CFG_Dev_CtrlWay_Is_RGB_Set_Time:
					Rs485LEDInfo.RGB_Set_Time=CtrlContect;
					//Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"RGB_Set_Time :%d",Rs485LEDInfo.RGB_Set_Time);
					break;	
				case Dim_Global_Value_Cmd:    //2024-1-30

					DevActionGlobal.DimGlobalValue = CtrlContect;      		//2025-08-22 
					//Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"Dim Global Value Set:%d",CtrlContect);
					break;
				case Dim_UpLimit_Value_Cmd:    
//					Rs485LEDInfo.LEDUpLightLimit = CtrlContect;
					DevActionGlobal.Dim_Upper_limit = CtrlContect;      //2024-11-01    设置可调上限由原来的限制外设改为限制旋钮输入的值
					//Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"Dim UpLimit Value Set:%d",CtrlContect);
					break;
				case Dim_DnLimit_Value_Cmd:    
//					Rs485LEDInfo.LEDDownLightLimit = CtrlContect;
				    DevActionGlobal.Dim_Lower_limit = CtrlContect;      //2024-11-01    设置可调下限由原来的限制外设改为限制旋钮输入的值
					//Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"Dim DownLimit Value Set:%d",CtrlContect);
					break;
			}	
		}
	}
	
	
//	if(temp1 != 0x00)
	{
		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),devaddr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&Rs485LEDInfo,sizeof(RS485_LED_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_LED_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;
	
	uint8_t tempflag =0x00;
	
	if(devaddr == 0x00)	return 0x00;
	
	Device_Public_Information_G BUS_Public;
	RS485_LED_INFO Rs485LEDInfo;       //继电器局部变量
	
//	Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"调光状态群控读取开始，回路标志:%08X ", ReadFlag);
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),devaddr);	
	SRAM_DMA_Read_Buff((uint8_t *)&Rs485LEDInfo,sizeof(RS485_LED_INFO),devaddr+Dev_Privately);
	
	if(ReadNum >= Rs485LEDInfo.LEDLoopValidNum)
	{
		ReadNum = Rs485LEDInfo.LEDLoopValidNum;
	}
	
	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,"调光状态群控读取开始，当前回路：%d配置:%04X ", i+1, start[i]);

					switch(CtrlMode)
					{
						case 0x01:   //状态开
						case 0x02:   //状态关
							if(Rs485LEDInfo.DevSendBuf[i] != CtrlContect)
							{
								Ret = DEV_STATE_CLOSE;
							}
							break;
						case CFG_Dev_CtrlWay_Is_Dim_Open:   //记忆开  当前状态和记忆亮度不一样就算关
							if((0x00 != Rs485LEDInfo.LEDLightRelease[i]) && (Rs485LEDInfo.LEDLightRelease[i] != Rs485LEDInfo.DevSendBuf[i]))
							{
								Ret = DEV_STATE_CLOSE;
							}
							else if((0x00 == Rs485LEDInfo.LEDLightRelease[i]) && (Rs485LEDInfo.DevSendBuf[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,"调光状态群控读取开始，当前回路：%d配置:%04X ", i+1, start[i]);

					if(0x01 == CtrlMode)  //状态开
					{
						if(Rs485LEDInfo.DevSendBuf[i] == CtrlContect)
						{
							Ret = DEV_STATE_OPEN;
							break;
						}
						
					}
				}
			}						
			break;
	}

	if(0x00 == Ret)   //
	{
		switch(SceneType)
		{
			case 0x01:
		//		Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"调光状态群控读取场景状态为开");
				Ret = DEV_STATE_OPEN;
				break;
			case 0x02:
				Ret = DEV_STATE_CLOSE;
				break;
		}
	}
	
	/*2022-10-19 增加调光设备状态变化上报*/
	for(i = 0; i < ReadNum; i++)
	{
		if(Rs485LEDInfo.DevSaveBuf[i]!=Rs485LEDInfo.DevSendBuf[i])
		{
			Rs485LEDInfo.DevSaveBuf[i] = Rs485LEDInfo.DevSendBuf[i];
			if(Rs485LEDInfo.DevSendBuf[i]==0)
			{
				Udp_Addtion_Roomstate(DEV_RS485_PWM,BUS_Public.addr,i+1,((Rs485LEDInfo.DevSendBuf[i]<<8)|0x0002));
			}
			else
			{
				Udp_Addtion_Roomstate(DEV_RS485_PWM,BUS_Public.addr,i+1,((Rs485LEDInfo.DevSendBuf[i]<<8)|0x0001));
			}
			tempflag++;
		}
	}
	
	if(tempflag!=0)
	{
		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),devaddr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&Rs485LEDInfo,sizeof(RS485_LED_INFO),devaddr+Dev_Privately);
	}
	
	

	return Ret;
	
}







