#include "BLV_485_Dev_AirRevealFun.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"

#include "BLV_485_Dev_AirDetectFun.h"

/*AirReveal空气质量显示驱动代码*/

#define REPEATSENDTIMEMAX         0x03  //重发次数上限
#define AirRevealCtrlSendMax  0x03        //超过次数不回复判为离线
#define AirRevealCtrlTIMEOUT     100         //控制超时时间
#define IRSENDCYCLETIME        1000         //轮询间隔时间
#define AirRevealValidMax      0x07         //7路输入检测

#define AirRevealReadLen      0x05          //读取报文长度
#define AirRevealAckLen       0x11          //回复报文长度

/*本地函数调用开始*/
uint8_t BLW_AirRevealCycleCtrl(uint32_t dev_addr); 
uint8_t BLW_Rs485_AirReveal_Check(uint32_t dev_addr,uint32_t data_addr,uint16_t len);
//void BLW_Rs485_AirReveal_4LOOP_Ctrl(uint32_t devaddr, RS485_AirReveal_INFO* Rs485AirRevealInfo);
void BLW_AirReveal_Cycle(Device_Public_Information_G *BUS_Public, RS485_AirReveal_INFOP Rs485AirRevealInfo);

//void Dev_AirReveal_Ctrl(uint32_t CfgDevAddIn, uint16_t DevInputAddr, uint32_t devaddr,uint16_t loop,uint16_t start);
//uint16_t Dev_AirReveal_Read(uint32_t devaddr,uint16_t loop);

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

/*******************************************************************************
* Function Name  : BLW_RS485_AirReveal_Data_Init 
* Description    : 添加设备到链表 - 轮询设备
* Input          : 
	type	: 添加的设备类型
	addr	: 添加的设备地址
	polling_cf		：设备的轮询数据发送回调函数
	processing_cf	：设备的数据处理回调函数
* Return         : None
*******************************************************************************/
void BLW_RS485_AirReveal_Data_Init(uint8_t devaddr)
{
	Device_Public_Information_G BUS_Public;
	RS485_AirReveal_INFO Rs485AirReveal;
	
	memset(&BUS_Public,0,sizeof(Device_Public_Information_G));
	memset(&Rs485AirReveal,0,sizeof(RS485_AirReveal_INFO));
	
	BUS_Public.addr = devaddr;		//设备地址
	BUS_Public.type = DEV_AirRevealTYPE;		//设备类型
	BUS_Public.port = Active_Port;		//设备发送端口
	BUS_Public.baud = 9600;		//设备发送波特率
	BUS_Public.retry_num = 0x03;	//设备数据重发次数
	BUS_Public.wait_time = 0x00C8;	//设备数据发送等待回复时间 - 200ms
//	BUS_Public.polling_cf = (uint32_t)&BLW_AirRevealCycleCtrl;
	BUS_Public.processing_cf = (uint32_t)&BLW_Rs485_AirReveal_Check;
	
	Rs485AirReveal.DevOffline = Rs485AirReveal.DevOfflineLast = DEV_IS_LINEUNINIT;   //在线状态初始化
//	BUS_Public.DevFunInfo.Dev_Output_Ctrl = Dev_AirReveal_Ctrl;
//	BUS_Public.DevFunInfo.Dev_Output_Loop_State_Get = Dev_AirReveal_Read;   
	/*初始化AirReveal空气质量显示初始值*/
	Rs485AirReveal.CfgDevAddAirDetect = Find_AllDevice_List_Information(DEV_AirDetectTYPE, 0x01);
	
	Add_ACT_Device_To_List2(&BUS_Public,(uint8_t *)&Rs485AirReveal,sizeof(RS485_AirReveal_INFO));
}



/*******************************************************************************
* Function Name  : BLW_RS485_AirReveal_For_Logic_Init      2022-08-30
* Description    : 空气质量检测初始化信息来自逻辑文件
*******************************************************************************/
void BLW_RS485_AirReveal_For_Logic_Init(LOGICFILE_DEVICE_INFO *dev_info,uint8_t *data,uint16_t data_len)
{
	Device_Public_Information_G BUS_Public;
	RS485_AirReveal_INFO Rs485AirReveal;
	
	memset(&BUS_Public,0,sizeof(Device_Public_Information_G));
	memset(&Rs485AirReveal,0,sizeof(RS485_AirReveal_INFO));
	
	BUS_Public.addr = dev_info->addr;			//设备地址
	BUS_Public.type = dev_info->type;		//设备类型
	BUS_Public.port = Active_Port;				//设备发送端口
	BUS_Public.baud = dev_info->baud;						//设备发送波特率
	BUS_Public.retry_num = 0x03;				//设备数据重发次数
	BUS_Public.wait_time = 0x00C8;				//设备数据发送等待回复时间

	BUS_Public.processing_cf = (uint32_t)&BLW_Rs485_AirReveal_Check;
	BUS_Public.Protocol = 0x01;  //dev_info->version
	
	Rs485AirReveal.DevOffline = Rs485AirReveal.DevOfflineLast = DEV_IS_LINEUNINIT;   //在线状态初始化
		/*初始化AirReveal空气质量显示初始值*/
	Rs485AirReveal.CfgDevAddAirDetect = Find_AllDevice_List_Information(DEV_AirDetectTYPE, 0x01);
	
	Add_ACT_Device_To_List2(&BUS_Public,(uint8_t *)&Rs485AirReveal,sizeof(RS485_AirReveal_INFO));
}



/*******************************************************************************
* Function Name  : BLW_AirRevealCycleCtrl 
* Description    : BLWAirReveal空气质量显示轮询兼控制发送函数
*******************************************************************************/
uint8_t BLW_AirRevealCycleCtrl(uint32_t dev_addr) 
{
	uint16_t temp = 0;
	uint8_t crc_val = 0;
//	uint8_t i;     //用于遍历所有回路
	uint8_t Ret = RS485OCCUPYNOTIME;    //默认不占用时间
	
	RS485_AirReveal_INFO Rs485AirRevealInfo;
	Device_Public_Information_G BUS_Public;

	SRAM_DMA_Read_Buff((uint8_t *)&Rs485AirRevealInfo,sizeof(RS485_AirReveal_INFO),dev_addr+Dev_Privately);
	SRAM_Read_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), dev_addr);  
	
	if(0x01 == Rs485AirRevealInfo.AirRevealSynFlag)
	{
		if(0x00 != Rs485AirRevealInfo.AirRevealSynCnt)
		{
			Rs485AirRevealInfo.AirRevealSynCnt--; 
		}
		else
		{
			Rs485AirRevealInfo.AirRevealSynFlag = 0x00;
		}
//		BLW_AirReveal_Ack(&BUS_Public, &Rs485AirRevealInfo);  //空气参数同步
		Ret = RS485OCCUPYTIME;                           //控制就被置一
	}	

//	BLW_AirReveal_Cycle(&BUS_Public, &Rs485AirRevealInfo);
	
//	if(RS485OCCUPYTIME == Ret)
	{
		/*通讯统计*/
		BLV_Communication_Record(&Rs485AirRevealInfo.comm_record,0x01,0x00);
		Rs485AirRevealInfo.inquire_tick = SysTick_1ms;
		
		if(Rs485AirRevealInfo.DevSendCnt > REPEATSENDTIMEMAX)
		{
			if(Rs485AirRevealInfo.DevOffline != DEV_IS_OFFLINE)
			{
				Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"%s DEV_OFFLINE",__func__);
				LOG_Device_Online_Record(DEV_AirRevealTYPE,SRAM_Read_Byte(dev_addr+Dev_Addr),LogInfo_Device_Offline);	//记录设备离线
			}
			Rs485AirRevealInfo.DevOffline = DEV_IS_OFFLINE;   //离线
			if(Rs485AirRevealInfo.DevOffline != Rs485AirRevealInfo.DevOfflineLast)	//当前在线状态不等于上一次在线状态
			{
				Rs485AirRevealInfo.DevOfflineLast = Rs485AirRevealInfo.DevOffline;	//更新上一次在线状态
				Write_Device_Fault_State(BUS_Public.type,BUS_Public.addr,In_ErrFun_LineState,DEV_IS_OFFLINE);	//保存设备在线状态到SRAM
			}
		}
		else
		{
			Rs485AirRevealInfo.DevSendCnt++;   //
		}
		
		/*将数据保存进入SRAM中*/
		SRAM_DMA_Write_Buff((uint8_t *)&Rs485AirRevealInfo,sizeof(RS485_AirReveal_INFO),dev_addr+Dev_Privately);
		
		/*计算校验值*/
		SRAM_Write_Byte(0x00,dev_addr+Dev_Check);
		temp = sizeof(RS485_AirReveal_INFO) + Dev_Privately;
		crc_val = Dev_CheckSum(dev_addr,temp);
		SRAM_Write_Byte(crc_val,dev_addr+Dev_Check);		//校验值重新写入
		//Dbg_Print(DBG_BIT_DEVICE_STATUS_bit,"AirReveal crc_val:%02X\r\n",crc_val);
	}

	
	

	
	return Ret;
}

/*传感器参数回复函数*/
//#define BLWAIRDETEASKLEN   17
void BLW_AirReveal_Ack(Device_Public_Information_G *BUS_Public, RS485_AirReveal_INFOP Rs485AirRevealInfo)
{
	uint8_t SendData[AirRevealAckLen]; 
	RS485_AirDetect_INFO Rs485AirDetect;
	uint32_t dev_addr;
	
	if(NULL == Rs485AirRevealInfo->CfgDevAddAirDetect)
	{
		Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"无可用的空气质量检测");		
		return ;
	}
	dev_addr = Rs485AirRevealInfo->CfgDevAddAirDetect;
	
	SRAM_DMA_Read_Buff((uint8_t *)&Rs485AirDetect,sizeof(RS485_AirDetect_INFO),dev_addr+Dev_Privately);
	
	SendData[0] = BUS_Public->addr;  //地址
	SendData[1] = 0x66;                //读
  
	
	memcpy(&SendData[2],(uint8_t *)Rs485AirDetect.DevReadBuf, Rs485AirDetect.AirDetectInputValidNum*2);         //拷贝空气参数				
	
//	NetCRC16(SendData,8);
	
	SendData[AirRevealAckLen-1] = SOR_CRC(SendData,AirRevealAckLen-1);
	
	MCU485_SendString(BUS_Public->port,SendData,AirRevealAckLen);

}
/**
	*@name 空气质量显示接收数据处理函数
  *@para  
  *   BUS_Public     公用变量
  *   data           数据包
  *   Rs485AirReveal 私有变量
  *@ret 0表示无变化  1表示有变化
  */
//uint8_t AirRevealDataProc(Device_Public_Information_G* BUS_Public, uint8_t *data, RS485_AirReveal_INFO *Rs485AirReveal)
//{
//	uint8_t Ret = 0x00;
//	uint8_t i;
//	

//	return Ret;
//}

#define RECDATALENMAX AirRevealAckLen
#define IsMyData     0x00  
#define IsNotMyData  0x01
/*******************************************************************************
* Function Name  : BLW_Rs485_AirReveal_Check 
* Description    : BLWAirReveal空气质量显示接收数据处理函数
* Input          : 
	dev_addr	: 设备信息地址
	data_addr	: 数据地址
	len			：数据长度
* Return         : 
	0x00：处理成功
	0x01：处理失败
*******************************************************************************/
uint8_t BLW_Rs485_AirReveal_Check(uint32_t dev_addr,uint32_t data_addr,uint16_t len)
{
	//Dbg_Print(DBG_BIT_DEVICE_STATUS_bit,"BLW_Rs485_AirReveal_Check\r\n");
	uint8_t rev = IsNotMyData;
	uint8_t temp;  //保存命令类型和数据长度
	RS485_AirReveal_INFO Rs485AirReveal;
	uint8_t data[RECDATALENMAX];	
	Device_Public_Information_G BUS_Public;
	uint16_t crc_val;  //校验
	
//	uint8_t AirReveal_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,"空气接收数据长度超出范围!!\r\n");
		return rev;     //长度
	}		
	
	SRAM_DMA_Read_Buff(data,len,data_addr);   //拷贝485数据
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),dev_addr);		
	SRAM_DMA_Read_Buff((uint8_t *)&Rs485AirReveal,sizeof(RS485_AirReveal_INFO),dev_addr+Dev_Privately);
	
	if((data[0] != BUS_Public.addr)|| ((data[1] != 0x63)&&(data[1] != 0x66)))	   //地址不对  长度不对
	{
		//Dbg_Print(DBG_BIT_DEVICE_STATUS_bit,"空气质量显示数据处理判断未通过!!\r\n");
		return rev;				//校验文件头
	}
//	Dbg_Print(DBG_BIT_DEVICE_STATUS_bit,"空气质量显示数据处理!!\r\n");

	crc_val = data[len-1];//和校验
//	crc_val = SOR_CRC(data, len-1);  

	if(crc_val == SOR_CRC(data, len-1))   //校验通过
	{
//		SRAM_DMA_Read_Buff((uint8_t *)&Rs485AirReveal,sizeof(RS485_AirReveal_INFO),dev_addr+Dev_Privately);
//		temp = SRAM_Read_Byte(data_addr + 6);
		//Dbg_Print(DBG_BIT_DEVICE_STATUS_bit,"空气质量显示数据处理判断通过!!\r\n");
	
		BLV_Communication_Record(&Rs485AirReveal.comm_record,0x02,0x01);		//记录通讯成功
		
		switch(data[1])  
		{
			case 0x63:   //读取寄存器状态回复

				rev = IsMyData;
				BLW_AirReveal_Ack(&BUS_Public, &Rs485AirReveal);//回复空气参数
			
//				if(AirRevealDataProc(&BUS_Public, data, &Rs485AirReveal))
//				{
//					Dbg_Print(DBG_BIT_DEVICE_STATUS_bit,"BLV Rs485 AirReveal Reply Change\r\n");
//					LOG_Device_COMM_ASK_TO_Reply_Record2(dev_addr+Dev_port,dev_addr+Dev_baud,(SysTick_1ms - Rs485AirReveal.inquire_tick),data,len);
//				}
				if(Rs485AirReveal.DevOffline != DEV_IS_ONLINE)
				{
					Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"%s Device_Online",__func__);
					LOG_Device_Online_Record(DEV_AirRevealTYPE,SRAM_Read_Byte(dev_addr+Dev_Addr),LogInfo_Device_Online);	//记录设备在线
				}
				
				Rs485AirReveal.DevSendCnt = 0x00;
				Rs485AirReveal.DevOffline = DEV_IS_ONLINE;    //设备在线
				if(Rs485AirReveal.DevOffline != Rs485AirReveal.DevOfflineLast)	//当前在线状态不等于上一次在线状态
				{
					Rs485AirReveal.DevOfflineLast = Rs485AirReveal.DevOffline;	//更新上一次在线状态
					Write_Device_Fault_State(BUS_Public.type,BUS_Public.addr,In_ErrFun_LineState,DEV_IS_ONLINE);	//保存设备在线状态到SRAM
				}
//				Rs485AirReveal.AirRevealCtrlFlag = 0x00;
				
				/*日志记录*/
				break;	
			case	0x66:
				rev = IsMyData;
				Rs485AirReveal.AirRevealSynFlag = 0x00;	
				Rs485AirReveal.DevSendCnt = 0x00;
				Rs485AirReveal.DevOffline = DEV_IS_ONLINE;    //设备在线
				if(Rs485AirReveal.DevOffline != Rs485AirReveal.DevOfflineLast)	//当前在线状态不等于上一次在线状态
				{
					Rs485AirReveal.DevOfflineLast = Rs485AirReveal.DevOffline;	//更新上一次在线状态
					Write_Device_Fault_State(BUS_Public.type,BUS_Public.addr,In_ErrFun_LineState,DEV_IS_ONLINE);	//保存设备在线状态到SRAM
				}
				break;
		}
		
				/*将数据保存进入SRAM中*/
		SRAM_DMA_Write_Buff((uint8_t *)&Rs485AirReveal,sizeof(RS485_AirReveal_INFO),dev_addr+Dev_Privately);
		
		/*计算校验值*/
		SRAM_Write_Byte(0x00,dev_addr+Dev_Check);
		temp = sizeof(RS485_AirReveal_INFO) + Dev_Privately;
		crc_val = Dev_CheckSum(dev_addr,temp);
		SRAM_Write_Byte(crc_val,dev_addr+Dev_Check);		//校验值重新写入
	}
//	else {
//		Dbg_Print(DBG_BIT_DEVICE_STATUS_bit,"SRAM_CRC16 Fail ,crc_val:%02X SRAM_CRC16:%02X!!\r\n",crc_val,SRAM_CRC16(data_addr,len - 2));
//		
//	}
	
	return rev;
}



/*******************************************************************************
* Function Name  : BLW_AirReveal_Control_State 
* Description    : BLWAirReveal空气质量显示状态控制函数
* Input          : 
	devaddr	: 设备信息地址
  loop    ：回路
	start   ：状态
* Return  : 无
* attention : temp1非0才会进行保存
*******************************************************************************/
//void BLW_AirReveal_Control_State(uint32_t devaddr,uint8_t loop,uint8_t start)
//{
//	uint16_t temp = 0;
//	uint8_t crc_val = 0,temp1 = 0;
//	
//	if(devaddr == 0x00)	return;
//	
//	RS485_AirReveal_INFO Rs485AirReveal;

//	SRAM_DMA_Read_Buff((uint8_t *)&Rs485AirReveal,sizeof(RS485_AirReveal_INFO),devaddr+Dev_Privately);
//	
//	if(loop < Rs485AirReveal.AirRevealInputValidNum)  //有效回路
//	{
//		if(Rs485AirReveal.DevSendBuf[loop] != start)  //不同
//		{
//			Rs485AirReveal.DevSendBuf[loop] = start; 
//			Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"DevSendBuf loop:%d,start:%d\r\n",loop,start);
//			temp1++;
//		}
//	}
//	
//	if(temp1 != 0x00)
//	{
//		/*将数据保存进入SRAM中*/
//		SRAM_DMA_Write_Buff((uint8_t *)&Rs485AirReveal,sizeof(RS485_AirReveal_INFO),devaddr+Dev_Privately);
//		
//		/*计算校验值*/
//		SRAM_Write_Byte(0x00,devaddr+Dev_Check);
//		temp = sizeof(RS485_AirReveal_INFO) + Dev_Privately;
//		crc_val = Dev_CheckSum(devaddr,temp);
//		SRAM_Write_Byte(crc_val,devaddr+Dev_Check);		//校验值重新写入
//	}
//}

/*******************************************************************************
* Function Name  : BLW_AirReveal_Read_State 
* Description    : BLWAirReveal空气质量显示状态读取函数
* Input          : 
	devaddr	: 设备信息地址
  loop    ：回路
* Return  : 无
*******************************************************************************/
//uint8_t BLW_AirReveal_Read_State(uint32_t devaddr,uint8_t loop)
//{
//	if(devaddr == 0x00)
//		return 0x00;
//	
//	RS485_AirReveal_INFO Rs485AirReveal;
//	
//	SRAM_DMA_Read_Buff((uint8_t *)&Rs485AirReveal,sizeof(RS485_AirReveal_INFO),devaddr+Dev_Privately);	
//	
//	if(loop >= Rs485AirReveal.AirRevealInputValidNum)
//	{
//		return 0x00;    //超出范围 直接返回0
//	}
//	
//	return Rs485AirReveal.DevReadBuf[loop];
//	
//}

/*获取AirReveal空气质量显示在线状态*/
//uint8_t Get_BLV485_AirReveal_Online_Status(uint32_t devaddr)
//{
//	RS485_AirReveal_INFO Rs485AirReveal;
//	
//	SRAM_DMA_Read_Buff((uint8_t *)&Rs485AirReveal,sizeof(RS485_AirReveal_INFO),devaddr+Dev_Privately);
//	
//	if(Rs485AirReveal.DevOffline == DEV_IS_ONLINE)
//	{
//		return 0x01;
//	}
//	return 0x02;
//}


#define A9EXPANDCTRLLEN    12    //空气质量显示控制报文
/**
	* @name BLW 4路 集中空气质量显示控制函数
	* @para
	*   dev_addr     设备起始地址   
	* @return         无
  * @attention      发送循环里调用，变量已经被判断
	*/
//void BLW_Rs485_AirReveal_4LOOP_Ctrl(uint32_t dev_addr, RS485_AirReveal_INFO* Rs485AirRevealInfo)
//{
//	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(&Rs485AirRevealInfo->AirRevealSn, AirRevealSnMin, AirRevealSnMax);
//												 
//	SendBuf[3] = DevAddr;   
////	memcpy(&SendBuf[6], &RS485RelayExpandInfo.DevRelayBuf[SwiAddr*2], 2); //空气质量显示， 下标从5开始
//	for(i = 0; i < Rs485AirRevealInfo->AirRevealInputValidNum; i++)
//	{
//		switch(Rs485AirRevealInfo->DevSendBuf[i])
//		{
//			case 0x01:
//				SendBuf[7+i/4] |= 0x02<<((i%4)*2);       //开
//				break;
//			case 0x02:
//				SendBuf[7+i/4] |= 0x01<<((i%4)*2);      //关
//				break;
//		}
//	}
//	SendBuf[5] = CheckSum_Overlook_Check(SendBuf, A9EXPANDCTRLLEN, 5);	
//		
//	MCU485_SendString(port,SendBuf,A9EXPANDCTRLLEN);
//	
//	/*日志记录*/
//	LOG_Device_COMM_Send_Control_Record2(dev_addr+Dev_port,dev_addr+Dev_baud,SendBuf,A9EXPANDCTRLLEN);	
//}


/**@name  空气质量显示状态变化得
	*@para  无
  *@ret   为1 有变化   为0  无变化
	*/
//uint8_t AirReveal_State_Change_Get(uint32_t devaddr)
//{
//	uint8_t i;
//	uint8_t Ret = 0x00;
//	uint16_t temp = 0;
//	uint8_t crc_val = 0,temp1 = 0;

//	if(devaddr == 0x00)	return 0;
//	
//	RS485_AirReveal_INFO Rs485AirRevealInfo;

//	SRAM_DMA_Read_Buff((uint8_t *)&Rs485AirRevealInfo,sizeof(RS485_AirReveal_INFO),devaddr+Dev_Privately);
//	
//	for(i = 0; i < Rs485AirRevealInfo.AirRevealInputValidNum; i ++)
//	{
//		if(Rs485AirRevealInfo.DevSendBufNext[i] != Rs485AirRevealInfo.DevSendBuf[i])   //状态变化
//		{
//			Ret = 0x01;     //有变化	
//			temp1++;
//			Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"AirReveal空气质量显示状态变化 DevSendBuf loop:%d,start:%d\r\n", i, Rs485AirRevealInfo.DevSendBuf[i]);  //空气质量显示状态变化打印
//			
//			memcpy(Rs485AirRevealInfo.DevSendBufNext, Rs485AirRevealInfo.DevSendBuf, Rs485AirRevealInfo.AirRevealInputValidNum); //同步状态
//			break;
//		}
//	}
//	
//	if(temp1 != 0x00)
//	{
//		/*将数据保存进入SRAM中*/
//		SRAM_DMA_Write_Buff((uint8_t *)&Rs485AirRevealInfo,sizeof(RS485_AirReveal_INFO),devaddr+Dev_Privately);
//		
//		/*计算校验值*/
//		SRAM_Write_Byte(0x00,devaddr+Dev_Check);
//		temp = sizeof(RS485_AirReveal_INFO) + Dev_Privately;
//		crc_val = Dev_CheckSum(devaddr,temp);
//		SRAM_Write_Byte(crc_val,devaddr+Dev_Check);		//校验值重新写入
//	}
//	
//	return Ret;        
//}

	



/*******************************************************************************
* Function Name  : BLW_AirReveal_Control_State 
* Description    : BLWAirReveal空气质量显示状态控制函数
* Input          : 
	devaddr	: 设备信息地址
  loop    ：回路
	start   ：状态
* Return  : 无
* attention : temp1非0才会进行保存
*******************************************************************************/
//void Dev_AirReveal_Ctrl(uint32_t CfgDevAddIn, uint16_t DevInputAddr, uint32_t devaddr,uint16_t loop,uint16_t start)
//{
//	uint16_t temp = 0;
//	uint8_t crc_val = 0,temp1 = 0;
//	
//	if(devaddr == 0x00)	return;
//	
//	RS485_AirReveal_INFO Rs485AirReveal;

//	SRAM_DMA_Read_Buff((uint8_t *)&Rs485AirReveal,sizeof(RS485_AirReveal_INFO),devaddr+Dev_Privately);
//	
//	if(loop < Rs485AirReveal.AirRevealInputValidNum)  //有效回路
//	{
//		if(Rs485AirReveal.DevSendBuf[loop] != start)  //不同
//		{
//			Rs485AirReveal.DevSendBuf[loop] = start; 
//			Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"DevSendBuf loop:%d,start:%d\r\n",loop,start);
//			temp1++;
//		}
//	}
//	
//	if(temp1 != 0x00)
//	{
//		/*将数据保存进入SRAM中*/
//		SRAM_DMA_Write_Buff((uint8_t *)&Rs485AirReveal,sizeof(RS485_AirReveal_INFO),devaddr+Dev_Privately);
//		
//		/*计算校验值*/
//		SRAM_Write_Byte(0x00,devaddr+Dev_Check);
//		temp = sizeof(RS485_AirReveal_INFO) + Dev_Privately;
//		crc_val = Dev_CheckSum(devaddr,temp);
//		SRAM_Write_Byte(crc_val,devaddr+Dev_Check);		//校验值重新写入
//	}
//}


/*******************************************************************************
* Function Name  : BLW_AirReveal_Read_State 
* Description    : BLWAirReveal空气质量显示状态读取函数
* Input          : 
	devaddr	: 设备信息地址
  loop    ：回路
* Return  : 无
*******************************************************************************/
//uint16_t Dev_AirReveal_Read(uint32_t devaddr,uint16_t loop)
//{
//	if(devaddr == 0x00)
//		return 0x00;
//	
//	RS485_AirReveal_INFO Rs485AirReveal;
//	
//	SRAM_DMA_Read_Buff((uint8_t *)&Rs485AirReveal,sizeof(RS485_AirReveal_INFO),devaddr+Dev_Privately);	
//	
//	if(loop >= Rs485AirReveal.AirRevealInputValidNum)
//	{
//		return 0x00;    //超出范围 直接返回0
//	}
//	
//	return Rs485AirReveal.DevReadBuf[loop];
//	
//}








