

/**
  ******************************************************************************
  * @file    BLV_Nor_Dev_LVoutputFun.c
  * @author  BLW RCU  development Team
  * @version V1.0.0
  * @date    2021年11月01日
  * @brief   服务信息初始化   设备服务信息控制
	* @attention 设备服务信息
	*									输入检测：  有
	*                 输入类型：  有	
	*                 执行函数：  有		
	*            			执行方式：
	*															开 
	*															关
  ******************************************************************************
  */
  
  
#include "includes.h"

#include "BLV_Nor_Dev_LVoutputFun.h"     
#include "BLV_Nor_DevFun.h"

void Dev_LVoutput_Dis(uint32_t DevAddr);
void BLW_LVoutput_Control_State(uint32_t CfgDevAddIn, uint16_t DevInputAddr, uint32_t devaddr,uint16_t DevOutputLoop,uint16_t start);
uint16_t LVoutput_Loop_State(uint32_t devaddr, uint16_t DevOutputLoop);

/*服务信息新增到普通设备链表*/
void BLV_Nor_Dev_LVoutput_Init(uint8_t devaddr)
{
	Device_Public_Information_G BUS_Public;
	NOR_LVOUTPUT_INFO DevLVoutputInfo;       //服务信息局部变量
	
	memset(&BUS_Public,0,sizeof(Device_Public_Information_G));
	memset(&DevLVoutputInfo,0,sizeof(NOR_LVOUTPUT_INFO));
	
	BUS_Public.addr = devaddr;			//设备地址
	BUS_Public.type = Dev_Host_LVoutput;		//设备类型
//	BUS_Public.port = Active_Port;			//设备发送端口
//	BUS_Public.baud = 9600;			//设备发送波特率
//	BUS_Public.retry_num = 0x03;		//设备数据重发次数
//	BUS_Public.wait_time = 0x0064;		//设备数据发送等待回复时间
//	BUS_Public.polling_cf = (uint32_t)&BLW_Touch_SwitchCycleDis;
//	BUS_Public.processing_cf = (uint32_t)&BLW_Rs485_Touch_Swi_Check;
	
	BUS_Public.DevFunInfo.Dev_Data_Process = Dev_LVoutput_Dis;                 //
	BUS_Public.DevFunInfo.Dev_Output_Ctrl = BLW_LVoutput_Control_State;        //
	BUS_Public.DevFunInfo.Dev_Output_Loop_State_Get = LVoutput_Loop_State;     //
	
	DevLVoutputInfo.LVoutputLoopValidNum = C1_LVOUTPUTNUMMAX;
	DevLVoutputInfo.DevC5IOAddr = Find_AllDevice_List_Information(DEV_C5IO_Type, 0x00);
	
	Add_Nor_Device_To_List2(&BUS_Public,(uint8_t *)&DevLVoutputInfo,sizeof(NOR_LVOUTPUT_INFO));
}




/**
  * @name  指定服务信息回路状态得到
  * @param  
  *    devaddr             当前设备的地址
  *    DevOutputLoop       需要查找的服务信息回路
  * @retval 
  *    ret                 回路状态返回    1 开   2关   0为不存在
  * @brief

  */
uint16_t LVoutput_Loop_State(uint32_t devaddr, uint16_t DevOutputLoop)
{	
	NOR_LVOUTPUT_INFO DevLVoutputInfo;       //服务信息局部变量

	if(devaddr == 0x00)
		return 0x00;
	
	SRAM_DMA_Read_Buff((uint8_t *)&DevLVoutputInfo,sizeof(NOR_LVOUTPUT_INFO),devaddr+Dev_Privately);	
	
	if(DevOutputLoop >= DevLVoutputInfo.LVoutputLoopValidNum)
	{
		return 0x00;
	}
	
	if(DevLVoutputInfo.DevLVoutputState[DevOutputLoop] == 0x01)
	{
		return 0x01;
	}
	else
	{
		return 0x02;
	}
	
}

/*******************************************************************************
* Function Name  : BLW_LVoutput_Control_State 
* Description    : BLW服务信息状态控制函数
* Input          : 
	devaddr	: 设备信息地址
  loop    ：回路      回路都是从0
	start   ：状态    0x01开  0x02关
* Return  : 无
* attention : temp1非0才会进行保存
*******************************************************************************/
void BLW_LVoutput_Control_State(uint32_t CfgDevAddIn, uint16_t DevInputAddr, uint32_t devaddr,uint16_t DevOutputLoop,uint16_t DevOutputType)
{
	uint8_t temp1 = 0;
	uint8_t state;    //0关  1开
	uint8_t CtrlWay;
	Device_Public_Information_G BUS_Public;   //共用
	NOR_LVOUTPUT_INFO DevLVoutputInfo;       //弱电输出局部变量
	
	if(devaddr == 0x00)	return;
	
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),devaddr);	//拷贝所有共有数据	
	SRAM_DMA_Read_Buff((uint8_t *)&DevLVoutputInfo,sizeof(NOR_LVOUTPUT_INFO),devaddr+Dev_Privately);
	
	if(DevOutputLoop >= DevLVoutputInfo.LVoutputLoopValidNum)//有效回路
	{
		return ;
	}
	CtrlWay = DevOutputType&0x00ff;   //取低字节

	switch(CtrlWay)
	{
		case 0x01:state = OPEN;break;
		case 0x02:state = CLOSE;break;
		case 0x04:      //取反
			if(0x01 == DevLVoutputInfo.DevLVoutputState[DevOutputLoop])
			{
				state = 0x00;
			}else{
				state = 0x01;
			}	
			Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"弱电输出闪烁赋值,start:%d",state);
			break;
		default:return;
	}

	
	if( DevLVoutputInfo.DevLVoutputState[DevOutputLoop] != state )
	{
		switch(state)
		{
			case 0x00:  //指定位清0
				DevLVoutputInfo.DevLVoutputState[DevOutputLoop] = 0x00;
				break;
			case 0x01:   //指定位置一
				DevLVoutputInfo.DevLVoutputState[DevOutputLoop] = 0x01;				
				break;
		}
		
		Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"LVoutput loop:%d,state:%d",DevOutputLoop,state);
		temp1++;
	}

	if(temp1 != 0x00)
	{
		BUS_Public.check = 0x00;
		BUS_Public.check = DoubleData_CheckSum((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G), (uint8_t *)&DevLVoutputInfo, sizeof(NOR_LVOUTPUT_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_Public, sizeof(Device_Public_Information_G),devaddr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&DevLVoutputInfo,sizeof(NOR_LVOUTPUT_INFO),devaddr+Dev_Privately);
					
	}	
}



/**
*@name 继电器扫描函数
*@para
*  DevAddr      设备地址
*@brief 拿到继电器的状态，将继电器的状态同步给C5IO设备
*/

void Dev_LVoutput_Dis(uint32_t DevAddr)
{
	Device_Public_Information_G BUS_PublicLVoutput;   //共用
	NOR_LVOUTPUT_INFO DevLVoutputInfo;       //继电器局部变量
	Device_Public_Information_G BUS_PublicC5IO;   //共用	
	BUS_C5IO_INFO C5IO_Info;

	uint8_t KeepFlag = 0x00;

	
	if(NULL == DevAddr)
	{
		return ;
	}
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_PublicLVoutput,sizeof(Device_Public_Information_G),DevAddr);	//拷贝所有共有数据		
	SRAM_DMA_Read_Buff((uint8_t *)&DevLVoutputInfo,sizeof(NOR_LVOUTPUT_INFO),DevAddr+Dev_Privately); 
	
	if(NULL == DevLVoutputInfo.DevC5IOAddr)
	{
		return ;
	}
	SRAM_DMA_Read_Buff((uint8_t *)&BUS_PublicC5IO,sizeof(Device_Public_Information_G),DevLVoutputInfo.DevC5IOAddr);	//拷贝所有共有数据			
	SRAM_DMA_Read_Buff((uint8_t *)&C5IO_Info,sizeof(BUS_C5IO_INFO),DevLVoutputInfo.DevC5IOAddr+Dev_Privately);

	if( (DevLVoutputInfo.init_flag == 0x00) && (C5IO_Info.DI_Init_flag == 0x01) ){
		/*DI初始化完成 - 读取继电器状态，作为初始化状态*/
		DevLVoutputInfo.init_flag = 0x01;
		
		for(uint32_t i = 0; i < DevLVoutputInfo.LVoutputLoopValidNum; i++)
		{
			if( (C5IO_Info.Relay_Level_Actual_Start & (0x01<<i)) != 0x00 )
			{
				DevLVoutputInfo.DevLVoutputState[i] = 0x01;
				DevLVoutputInfo.DevLVoutputStateLast[i] = 0x01;
				
				Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"DO 初始化:第%d路 开", i);
			}else {
				DevLVoutputInfo.DevLVoutputState[i] = 0x00;
				DevLVoutputInfo.DevLVoutputStateLast[i] = 0x00;
				
				Dbg_Println(DBG_BIT_DEVICE_STATUS_bit,"DO 初始化:第%d路 关", i);
			}
		}
		
		KeepFlag = 1;
	}
	
	for(uint16_t i = 0; i < DevLVoutputInfo.LVoutputLoopValidNum; i++)
	{
		
		if(DevLVoutputInfo.DevLVoutputStateLast[i] != DevLVoutputInfo.DevLVoutputState[i])
		{
			KeepFlag = 0x01;
			DevLVoutputInfo.DevLVoutputStateLast[i] = DevLVoutputInfo.DevLVoutputState[i];
			
			Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"准备同步弱电输出的状态，回路:%d 状态:%d", i, DevLVoutputInfo.DevLVoutputState[i]);
			switch(DevLVoutputInfo.DevLVoutputState[i])  //
			{
				case OPEN:       //开
					C5IO_Info.DO_Control[i] = BUS_C5IO_OUT_HIGH;
					C5IO_Info.DO_Control_Flag |= 0x01<<i;
					break;
				case CLOSE:      //关
					C5IO_Info.DO_Control[i] = BUS_C5IO_OUT_LOW;
					C5IO_Info.DO_Control_Flag |= 0x01<<i;				
					break;
			}
		}
	}

	if(0x01 == KeepFlag)
	{
		if(g_pc_test.test_flag == 0x12)		
		{
			BLV_PC_Testing_Data_Reported(0x02,DEV_C5IO_Type,0x00,SRAM_LOG_Device_C5IO_Relay_Status,4);
		}		
		BUS_PublicLVoutput.check = 0x00;
		BUS_PublicLVoutput.check = DoubleData_CheckSum((uint8_t *)&BUS_PublicLVoutput, sizeof(Device_Public_Information_G), (uint8_t *)&DevLVoutputInfo, sizeof(NOR_LVOUTPUT_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_PublicLVoutput, sizeof(Device_Public_Information_G),DevAddr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&DevLVoutputInfo,sizeof(NOR_LVOUTPUT_INFO),DevAddr+Dev_Privately);
				
		
		BUS_PublicC5IO.check = 0x00;
		BUS_PublicC5IO.check = DoubleData_CheckSum((uint8_t *)&BUS_PublicC5IO, sizeof(Device_Public_Information_G), (uint8_t *)&C5IO_Info, sizeof(BUS_C5IO_INFO));
		SRAM_DMA_Write_Buff((uint8_t *)&BUS_PublicC5IO, sizeof(Device_Public_Information_G),DevLVoutputInfo.DevC5IOAddr);/*将数据保存*/
		SRAM_DMA_Write_Buff((uint8_t *)&C5IO_Info,sizeof(BUS_C5IO_INFO),DevLVoutputInfo.DevC5IOAddr+Dev_Privately);
						
	}
	
	
}









