/**
  ******************************************************************************
  * @file    BLV_485_Dev_A9RELAYFun.c
  * @author  BLW RCU  development Team
  * @version SongHan
  * @date    2022年03月27日
  * @brief  
	*    A9RELAY继电器扩展模块 每个模块有9路
  ******************************************************************************
  */



#include "includes.h"
#include "BLV_485_Dev_A9RELAYFun.h"
#include "string.h"
#include "SPI_SRAM.h"
#include "usart.h"
#include "check_fun.h"
#include "DBG.h"
#include "rw_logging.h"
#include "SRAM_Variable_Address.h"
#include "Log_api.h"
#include "BLV_Dev_Action.h"


/*A9RELAY扩展板驱动代码*/

#define REPEATSENDTIMEMAX         0x03  //重发次数上限

#define   A9EXPANDTYPE           14	    //A9RELAY类型

#define A9RELAYSnMin                 0x08            //最大Sn号
#define A9RELAYSnMax                 0x0F            //最大Sn号

/*本地函数调用开始*/
uint8_t BLW_A9RELAYCycleCtrl(uint32_t dev_addr); 
uint8_t BLW_Rs485_A9RELAY_Check(uint32_t dev_addr,uint32_t data_addr,uint16_t len);
void BLW_Rs485_A9RELAY_Ctrl(Device_Public_Information_G *BUS_Public, NOR_HVOUT_INFO* DevHVoutInfo);
void A9Relay_Cycle(Device_Public_Information_G *BUS_Public, NOR_HVOUT_INFO *DevHVoutInfo);      //2023-07-29

//void Dev_A9RELAY_Ctrl(uint32_t CfgDevAddIn, uint16_t DevInputAddr, uint32_t devaddr,uint16_t loop,uint16_t start);
//uint16_t Dev_A9RELAY_Read(uint32_t devaddr,uint16_t loop);
//uint16_t BLW_A9RELAY_Group_Read(uint32_t devaddr, uint8_t SceneType, uint32_t ReadFlag, uint8_t ReadNum,uint16_t *start);
//void BLW_A9RELAY_Group_Ctrl(uint32_t CfgDevAddIn, uint16_t DevInputAddr,uint32_t devaddr, uint32_t CtrlFlag, uint8_t CtrlNum,uint16_t *start);

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

/*******************************************************************************
* Function Name  : BLW_RS485_A9RELAY_Data_Init 
* Description    : 添加设备到链表 - 轮询设备
* Input          : 
	type	: 添加的设备类型
	addr	: 添加的设备地址
	polling_cf		：设备的轮询数据发送回调函数
	processing_cf	：设备的数据处理回调函数
* Return         : None
*******************************************************************************/
void BLW_RS485_A9RELAY_Data_Init(Device_Public_Information_G *BUS_Public, NOR_HVOUT_INFO *DevHVoutInfo)
{
//	Device_Public_Information_G BUS_Public;
//	NOR_HVOUT_INFO DevHVoutInfo;
//	
//	memset(&BUS_Public,0,sizeof(Device_Public_Information_G));
//	memset(&DevHVoutInfo,0,sizeof(NOR_HVOUT_INFO));
//	
//	BUS_Public.addr = devaddr;		//设备地址
//	BUS_Public.type = DEV_A9RELAYTYPE;		//设备类型
//	BUS_Public.port = Polling_Port;		//设备发送端口
//	BUS_Public.baud = 9600;		//设备发送波特率
//	BUS_Public.retry_num = 0x03;	//设备数据重发次数
//	BUS_Public.wait_time = 0x0064;	//设备数据发送等待回复时间 - 100ms
	BUS_Public->polling_cf = (uint32_t)&BLW_A9RELAYCycleCtrl;
	BUS_Public->processing_cf = (uint32_t)&BLW_Rs485_A9RELAY_Check;
	
//	BUS_Public.DevFunInfo.Dev_Output_Ctrl = Dev_A9RELAY_Ctrl;
//	BUS_Public.DevFunInfo.Dev_Output_Loop_State_Get = Dev_A9RELAY_Read;  
//	BUS_Public.DevFunInfo.Dev_Output_Group_Ctrl = BLW_A9RELAY_Group_Ctrl; //
//	BUS_Public.DevFunInfo.Dev_Output_Loop_Group_State_Get_ptr = BLW_A9RELAY_Group_Read; //
//	
//	/*初始化A9RELAY扩展板初始值*/
//	DevHVoutInfo.HVoutLoopValidNum = A9RELAY_BUFF_Size;
//	
//	Add_POLL_Device_To_List2(&BUS_Public,(uint8_t *)&DevHVoutInfo,sizeof(NOR_HVOUT_INFO));
}



/*******************************************************************************
* Function Name  : BLW_A9RELAYCycleCtrl 
* Description    : BLWA9RELAY扩展板轮询兼控制发送函数
*******************************************************************************/
uint8_t BLW_A9RELAYCycleCtrl(uint32_t dev_addr) 
{
//	uint16_t temp = 0;
//	uint8_t crc_val = 0;
	uint8_t i;     //用于遍历所有回路
	uint8_t Ret = RS485OCCUPYNOTIME;
	Device_Public_Information_G BUS_Public;	
	NOR_HVOUT_INFO DevHVoutInfo;

	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),dev_addr);				
	SRAM_DMA_Read_Buff((uint8_t *)&DevHVoutInfo,sizeof(NOR_HVOUT_INFO),dev_addr+Dev_Privately);
	
	for(i = 0; i < DevHVoutInfo.HVoutLoopValidNum; i++)
	{
		if(DevHVoutInfo.DevHVoutStateLast[i] != DevHVoutInfo.DevHVoutState[i])
		{
			DevHVoutInfo.DevChangeFlag = 0x01;
			DevHVoutInfo.HVoutCtrlCnt = REPEATSENDTIMEMAX;
			DevHVoutInfo.DevHVoutStateLast[i] = DevHVoutInfo.DevHVoutState[i];
			DevHVoutInfo.A9Relay_ReadFlag = 0x01;
		}
	}
	
	if(0x01 == DevHVoutInfo.DevChangeFlag)
	{
		if(0x00 != DevHVoutInfo.HVoutCtrlCnt)
		{
			DevHVoutInfo.HVoutCtrlCnt--; 
		}
		else
		{
			DevHVoutInfo.DevChangeFlag = 0x00;
		}
		BLW_Rs485_A9RELAY_Ctrl(&BUS_Public, &DevHVoutInfo);           //9路控制，状态控制
		Ret = RS485OCCUPYTIME;
		
		/*通讯记录*/
		BLV_Communication_Record(&DevHVoutInfo.comm_record,0x01,0x00);
	}
	else if((0x00 == DevHVoutInfo.DevChangeFlag)&&(DevHVoutInfo.A9Relay_ReadFlag==0x01))
	{
		if(0x00 != DevHVoutInfo.HVoutCtrlCnt)
		{
			DevHVoutInfo.HVoutCtrlCnt--; 
		}
		else
		{
			DevHVoutInfo.A9Relay_ReadFlag = 0x00;
		}
		A9Relay_Cycle(&BUS_Public, &DevHVoutInfo);
		Ret = RS485OCCUPYTIME;
	}
	
	if(RS485OCCUPYTIME == Ret)
	{
		if(DevHVoutInfo.DevSendCnt > REPEATSENDTIMEMAX)
		{
			if(DevHVoutInfo.DevOffline != DEV_IS_OFFLINE)
			{
				//Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"DevHVoutInfo DEV_IS_OFFLINE");
				LOG_Device_Online_Record(BUS_Public.type,BUS_Public.addr,LogInfo_Device_Offline);	//记录设备离线
			}
			DevHVoutInfo.DevOffline = DEV_IS_OFFLINE;   //离线
			if(DevHVoutInfo.DevOffline != DevHVoutInfo.DevOfflineLast)	//当前在线状态不等于上一次在线状态
			{
				DevHVoutInfo.DevOfflineLast = DevHVoutInfo.DevOffline;	//更新上一次在线状态
				Write_Device_Fault_State(BUS_Public.type,BUS_Public.addr,In_ErrFun_LineState,DEV_IS_OFFLINE);	//保存设备在线状态到SRAM
			}
		}
		else
		{
			DevHVoutInfo.DevSendCnt++;   //
		}
		
		BUS_Public.check = 0x00;
		BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&DevHVoutInfo, sizeof(NOR_HVOUT_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),dev_addr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&DevHVoutInfo,sizeof(NOR_HVOUT_INFO),dev_addr+Dev_Privately);
			
		//Dbg_Print(DBG_BIT_DEVICE_STATUS_bit,"A9RELAY crc_val:%02X\r\n",crc_val);
	}

	
	return Ret;
}

void A9Relay_Cycle(Device_Public_Information_G *BUS_Public, NOR_HVOUT_INFO *DevHVoutInfo)
{
	uint8_t buff[7] = {0};
	
	buff[0] = 0x00;
	
	buff[1] = BLV_SN_Get(&DevHVoutInfo->RELAYSn, A9RELAYSnMin, A9RELAYSnMax);

	buff[2] = A9EXPANDTYPE;
	
	buff[3] = BUS_Public->addr;
	
	buff[4] = 0x07;
	
	buff[6] = 0x24;   //cmd

	buff[5] = CheckSum_Overlook_Check(buff, 0x07, 5);
	
	MCU485_SendString(BUS_Public->port,buff,0x07);
	
}

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

#define IsMyData     0x00  
#define IsNotMyData  0x01
/*******************************************************************************
* Function Name  : BLW_Rs485_A9RELAY_Check 
* Description    : BLWA9RELAY扩展板接收数据处理函数
* Input          : 
	dev_addr	: 设备信息地址
	data_addr	: 数据地址
	len			：数据长度
* Return         : 
	0x00：处理成功
	0x01：处理失败
*******************************************************************************/
uint8_t BLW_Rs485_A9RELAY_Check(uint32_t dev_addr,uint32_t data_addr,uint16_t len)
{
	uint8_t rev = IsNotMyData;
//	uint8_t temp;  //保存命令类型和数据长度
	Device_Public_Information_G BUS_Public;	
	NOR_HVOUT_INFO DevHVoutInfo;
	uint8_t data[RECDATALENMAX];
	
	uint32_t state = 0x00;
	uint8_t temp_val = 0;
	
//	uint8_t A9RELAY_addr = SRAM_Read_Byte(dev_addr+Dev_Addr); //拿到当前设备的地址
//	uint8_t crc_val = SRAM_Read_Word(data_addr+5);        //保存校验
	
	if(len > RECDATALENMAX)
	{
//		Dbg_Print(DBG_BIT_DEVICE_STATUS_bit,"A9IO接收数据长度超出范围!!\r\n");
		return rev;     //长度
	}
	
	SRAM_DMA_Read_Buff(data,len,data_addr);   //拷贝482数据
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),dev_addr);				
	
	if((data[0] != BUS_Public.addr) || ((0x07 != len)&&(0x0A != len)))	   //地址不对  长度不对
	{
//		Dbg_Print(DBG_BIT_DEVICE_STATUS_bit,"设备地址不对!!\r\n");
		return rev;				//校验文件头
	}

	if(data[5] == CheckSum_Overlook_Check(data, len, 5))   //校验通过
	{
		SRAM_DMA_Read_Buff((uint8_t *)&DevHVoutInfo,sizeof(NOR_HVOUT_INFO),dev_addr+Dev_Privately);
//		temp = SRAM_Read_Byte(data_addr + 6);
		
		BLV_Communication_Record(&DevHVoutInfo.comm_record,0x02,0x01);		//记录通讯成功
		
		switch(data[6])  
		{
			case 0x33:     //按键状态
				break;
			case 0x30:
				rev = IsMyData;
				if(DevHVoutInfo.DevOffline != DEV_IS_ONLINE)
				{
					Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"DevHVoutInfo LogInfo_Device_Online...........");
					LOG_Device_Online_Record(BUS_Public.type,BUS_Public.addr,LogInfo_Device_Online);	//记录设备在线
				}
				DevHVoutInfo.DevSendCnt = 0x00;
				DevHVoutInfo.DevOffline = DEV_IS_ONLINE;    //设备在线
				if(DevHVoutInfo.DevOffline != DevHVoutInfo.DevOfflineLast)	//当前在线状态不等于上一次在线状态
				{
					DevHVoutInfo.DevOfflineLast = DevHVoutInfo.DevOffline;	//更新上一次在线状态
					Write_Device_Fault_State(BUS_Public.type,BUS_Public.addr,In_ErrFun_LineState,DEV_IS_ONLINE);	//保存设备在线状态到SRAM
				}
				DevHVoutInfo.DevChangeFlag = 0x00;
				
				/*日志记录*/
				LOG_Device_COMM_Control_Reply_Record3(dev_addr+Dev_port,dev_addr+Dev_baud,data_addr,len);
				break;
			case 0x34:
				//2023-07-29
				DevHVoutInfo.A9Relay_ReadFlag = 0x00;
				DevHVoutInfo.DevSendCnt = 0x00;
			
				state |= data[9]<<16;
				state |= data[8]<<8;
				state |= data[7];
			
				for(uint8_t i = 0; i < DevHVoutInfo.HVoutLoopValidNum; i++)
				{
					temp_val = ( (state >> i ) & 0x01 );
					if( DevHVoutInfo.DevHVoutSaveState[i] != temp_val)
					{		
						DevHVoutInfo.DevHVoutSaveState[i] = temp_val;					
						if( temp_val == 0x01 )
						{
							Udp_Addtion_Roomstate(Dev_Host_HVout,BUS_Public.addr,i+1,0x01);
						}
						else if( temp_val == 0x00 )
						{
							Udp_Addtion_Roomstate(Dev_Host_HVout,BUS_Public.addr,i+1,0x02);
						}
					}
				}
				break;			
		}
		BUS_Public.check = 0x00;
		BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&DevHVoutInfo, sizeof(NOR_HVOUT_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),dev_addr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&DevHVoutInfo,sizeof(NOR_HVOUT_INFO),dev_addr+Dev_Privately);
					
	}

	return rev;
}



/*******************************************************************************
* Function Name  : BLW_A9RELAY_Control_State 
* Description    : BLWA9RELAY扩展板状态控制函数
* Input          : 
	devaddr	: 设备信息地址
  loop    ：回路
	start   ：状态
* Return  : 无
* attention : temp1非0才会进行保存
*******************************************************************************/
//void BLW_A9RELAY_Control_State(uint32_t devaddr,uint8_t loop,uint8_t start)
//{
////	uint16_t temp = 0;
//	uint8_t temp1 = 0;
//	Device_Public_Information_G BUS_Public;	
//	NOR_HVOUT_INFO DevHVoutInfo;
//	
//	if(devaddr == 0x00)	return;
//	

//	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),devaddr);				
//	SRAM_DMA_Read_Buff((uint8_t *)&DevHVoutInfo,sizeof(NOR_HVOUT_INFO),devaddr+Dev_Privately);
//	
//	if(loop < DevHVoutInfo.HVoutLoopValidNum)  //有效回路
//	{
//		if(DevHVoutInfo.DevSendBuf[loop] != start)  //不同
//		{
//			DevHVoutInfo.DevSendBuf[loop] = start; 
//			Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"DevSendBuf loop:%d,start:%d\r\n",loop,start);
//			temp1++;
//		}
//	}
//	
//	if(temp1 != 0x00)
//	{
//		BUS_Public.check = 0x00;
//		BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&DevHVoutInfo, sizeof(NOR_HVOUT_INFO));
//		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),devaddr);/*将数据保存*/
//		SRAM_DMA_Write_Buff((uint8_t *)&DevHVoutInfo,sizeof(NOR_HVOUT_INFO),devaddr+Dev_Privately);
//				
//	}
//}

/*******************************************************************************
* Function Name  : BLW_A9RELAY_Read_State 
* Description    : BLWA9RELAY扩展板状态读取函数
* Input          : 
	devaddr	: 设备信息地址
  loop    ：回路
* Return  : 无
*******************************************************************************/
//uint8_t BLW_A9RELAY_Read_State(uint32_t devaddr,uint8_t loop)
//{
//	if(devaddr == 0x00)
//		return 0x00;
//	
//	NOR_HVOUT_INFO DevHVoutInfo;
//	
//	SRAM_DMA_Read_Buff((uint8_t *)&DevHVoutInfo,sizeof(NOR_HVOUT_INFO),devaddr+Dev_Privately);	
//	
//	if(loop >= DevHVoutInfo.HVoutLoopValidNum)
//	{
//		return 0x00;    //超出范围 直接返回0
//	}
//	
//	return DevHVoutInfo.DevSendBuf[loop];
//	
//}

/*获取A9RELAY扩展板在线状态*/
//uint8_t Get_BLV485_A9RELAY_Online_Status(uint32_t devaddr)
//{
//	NOR_HVOUT_INFO DevHVoutInfo;
//	
//	SRAM_DMA_Read_Buff((uint8_t *)&DevHVoutInfo,sizeof(NOR_HVOUT_INFO),devaddr+Dev_Privately);
//	
//	if(DevHVoutInfo.DevOffline == DEV_IS_ONLINE)
//	{
//		return 0x01;
//	}
//	return 0x02;
//}


#define A9EXPANDCTRLLEN    12    //扩展板控制报文
/**
	* @name BLW 9路 集中扩展板控制函数
	* @para
	*   dev_addr     设备起始地址   
	* @return         无
  * @attention      发送循环里调用，变量已经被判断
	*/
void BLW_Rs485_A9RELAY_Ctrl(Device_Public_Information_G *BUS_Public, NOR_HVOUT_INFO* DevHVoutInfo)
{
//	uint8_t port = SRAM_Read_Byte(dev_addr+Dev_port);      //通讯端口
//	uint8_t DevAddr = SRAM_Read_Byte(dev_addr+Dev_Addr);   //设备地址
	                               //己方地址 发送类型  设备类型      收方地址    LEN          校验和   CMD  4个灯
	uint8_t SendBuf[A9EXPANDCTRLLEN] = {0x00,   0x00,   A9EXPANDTYPE,   0x00,  A9EXPANDCTRLLEN,  0x18,  0x20, 0x00, 0x00,  0x00,  0x00, 0x00};   //
	uint8_t i;	

	SendBuf[1] |= BLV_SN_Get(&DevHVoutInfo->RELAYSn, A9RELAYSnMin, A9RELAYSnMax);
												 
	SendBuf[3] = BUS_Public->addr; // DevAddr;   
//	memcpy(&SendBuf[6], &RS485RelayExpandInfo.DevRelayBuf[SwiAddr*2], 2); //扩展板， 下标从5开始
	for(i = 0; i < DevHVoutInfo->HVoutLoopValidNum; i++)
	{
		switch(DevHVoutInfo->DevHVoutState[i])
		{
			case 0x01:
				SendBuf[7+i/4] |= 0x02<<((i%4)*2);       //开
				break;
			case 0x00:
				SendBuf[7+i/4] |= 0x01<<((i%4)*2);      //关
				break;
		}
	}
	SendBuf[5] = CheckSum_Overlook_Check(SendBuf, A9EXPANDCTRLLEN, 5);	
		
	MCU485_SendString(BUS_Public->port,SendBuf,A9EXPANDCTRLLEN);
	
	/*日志记录*/
	LOG_Device_COMM_Send_Control_Record(BUS_Public->port, BUS_Public->baud, SendBuf,A9EXPANDCTRLLEN);	
}


/**@name  扩展板状态变化得
	*@para  无
  *@ret   为1 有变化   为0  无变化
	*/
//uint8_t A9RELAY_State_Change_Get(uint32_t devaddr)
//{
//	uint8_t i;
//	uint8_t Ret = 0x00;
//	uint8_t temp1 = 0;
//	Device_Public_Information_G BUS_Public;	
//	NOR_HVOUT_INFO DevHVoutInfo;

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

	



/*******************************************************************************
* Function Name  : BLW_A9RELAY_Control_State 
* Description    : BLWA9RELAY扩展板状态控制函数
* Input          : 
	devaddr	: 设备信息地址
  loop    ：回路
	start   ：状态
* Return  : 无
* attention : temp1非0才会进行保存
*******************************************************************************/
//void Dev_A9RELAY_Ctrl(uint32_t CfgDevAddIn, uint16_t DevInputAddr, uint32_t devaddr,uint16_t loop,uint16_t start)
//{
//	uint8_t temp1 = 0;
//	Device_Public_Information_G BUS_Public;	
//	NOR_HVOUT_INFO DevHVoutInfo;
//	
//	if(devaddr == 0x00)	return;
//	

//	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),devaddr);				
//	SRAM_DMA_Read_Buff((uint8_t *)&DevHVoutInfo,sizeof(NOR_HVOUT_INFO),devaddr+Dev_Privately);
//	
//	if(loop < DevHVoutInfo.HVoutLoopValidNum)  //有效回路
//	{
//		if(DevHVoutInfo.DevSendBuf[loop] != start)  //不同
//		{
//			DevHVoutInfo.DevSendBuf[loop] = start; 
//			Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"A9IO DevSendBuf loop:%d,start:%d\r\n",loop,start);
//			temp1++;
//		}
//	}
//	
//	if(temp1 != 0x00)
//	{
//		BUS_Public.check = 0x00;
//		BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&DevHVoutInfo, sizeof(NOR_HVOUT_INFO));
//		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),devaddr);/*将数据保存*/
//		SRAM_DMA_Write_Buff((uint8_t *)&DevHVoutInfo,sizeof(NOR_HVOUT_INFO),devaddr+Dev_Privately);
//			
//	}
//}


/*******************************************************************************
* Function Name  : BLW_A9RELAY_Read_State 
* Description    : BLWA9RELAY扩展板状态读取函数
* Input          : 
	devaddr	: 设备信息地址
  loop    ：回路
* Return  : 无
*******************************************************************************/
//uint16_t Dev_A9RELAY_Read(uint32_t devaddr,uint16_t loop)
//{
//	if(devaddr == 0x00)
//		return 0x00;
//	
//	NOR_HVOUT_INFO DevHVoutInfo;
//	
//	SRAM_DMA_Read_Buff((uint8_t *)&DevHVoutInfo,sizeof(NOR_HVOUT_INFO),devaddr+Dev_Privately);	
//	
//	if(loop >= DevHVoutInfo.HVoutLoopValidNum)
//	{
//		return 0x00;    //超出范围 直接返回0
//	}
//	
//	return DevHVoutInfo.DevSendBuf[loop];
//	
//}



/*******************************************************************************
* Function Name  : BLW_A9RELAY_Group_Ctrl 
* Description    : BLW扩展板群控
* Input          : 
	devaddr	: 设备信息地址
  CtrlFlag：控制标志  不能超过32路
	CtrlNum : 控制的设备个数   以最后一个为准
	start   ：状态    0x01开  0x02关
* Return  : 无
* attention : temp1非0才会进行保存
*******************************************************************************/
//void BLW_A9RELAY_Group_Ctrl(uint32_t CfgDevAddIn, uint16_t DevInputAddr,uint32_t devaddr, uint32_t CtrlFlag, uint8_t CtrlNum,uint16_t *start)
//{
//	uint8_t temp1 = 0;
////	uint1_t state;    //0关  1开
//	uint8_t i;
//	Device_Public_Information_G BUS_Public;   //共用	
//	NOR_HVOUT_INFO DevA9RELAYInfo;       //扩展板局部变量

//	if(devaddr == 0x00)	return;
//	
//	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),devaddr);	//拷贝所有共有数据	
//	SRAM_DMA_Read_Buff((uint8_t *)&DevA9RELAYInfo,sizeof(NOR_HVOUT_INFO),devaddr+Dev_Privately);
//	
//	if(CtrlNum >= DevA9RELAYInfo.HVoutLoopValidNum)
//	{
//		CtrlNum = DevA9RELAYInfo.HVoutLoopValidNum;
//	}
//	for(i = 0; i < CtrlNum; i++)
//	{
//		if(CtrlFlag&(0x0001<<i))   //非0为控制
//		{
//			switch(start[i])
//			{
//				case 0x01:   //控制开
//				case 0x02://控制关
//					DevA9RELAYInfo.DevSendBuf[i] = start[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 *)&DevA9RELAYInfo, sizeof(NOR_HVOUT_INFO));
//		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),devaddr);/*将数据保存*/
//		SRAM_DMA_Write_Buff((uint8_t *)&DevA9RELAYInfo,sizeof(NOR_HVOUT_INFO),devaddr+Dev_Privately);
//					
//	}	
//}

/*******************************************************************************
* Function Name  : BLW_A9RELAY_Group_Ctrl 
* Description    : BLW扩展板群控
* Input          : 
	devaddr	: 设备信息地址
  CtrlFlag：控制标志  不能超过32路
	CtrlNum : 控制的设备个数   以最后一个为准
	start   ：状态    0x01开  0x02关
* Return  : 所有状态满足 返回开 有一个不满足 返回关
* attention : temp1非0才会进行保存
*******************************************************************************/
//uint16_t BLW_A9RELAY_Group_Read(uint32_t devaddr, uint8_t SceneType, uint32_t ReadFlag, uint8_t ReadNum,uint16_t *start)
//{
//	uint8_t i;
//	uint8_t Ret = 0x00;
//	NOR_HVOUT_INFO DevA9RELAYInfo;       //扩展板局部变量
//	
//	if(devaddr == 0x00)	return 0x00;
//	
//	
////	Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"扩展板状态群控读取开始，回路标志:%08X ", ReadFlag);
//	SRAM_DMA_Read_Buff((uint8_t *)&DevA9RELAYInfo,sizeof(NOR_HVOUT_INFO),devaddr+Dev_Privately);
//	
//	if(ReadNum >= DevA9RELAYInfo.HVoutLoopValidNum)
//	{
//		ReadNum = DevA9RELAYInfo.HVoutLoopValidNum;
//	}
//	switch(SceneType)
//	{
//		case 0x01:
//			for(i = 0; i < ReadNum; i++)
//			{
//				if(ReadFlag&(0x0001<<i))   //非0为读取判断
//				{
//					switch(start[i])
//					{
//						case 0x01:   //状态开
//						case 0x02:   //状态关
//							if(DevA9RELAYInfo.DevSendBuf[i] != start[i])
//							{
//								Ret = DEV_STATE_CLOSE;
//							}
//		//					Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"扩展板状态群控读取开始，当前回路配置:%04X ", start[i]);
//		
//							break; 

//					}
//				}
//			}			
//			break;
//		case 0x02:
//			for(i = 0; i < ReadNum; i++)
//			{
//				if(ReadFlag&(0x0001<<i))   //非0为读取判断
//				{
//					if(0x01 == start[i])//状态开
//					{
//		//					Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"扩展板状态群控读取开始，当前回路配置:%04X ", start[i]);
//						if(DevA9RELAYInfo.DevSendBuf[i] == 0x01)//为1
//						{
//							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;
//}





