/**
  ******************************************************************************
  * @file    BLV_Nor_Dev_SceneFun.c
  * @author  BLW RCU  development Team
  * @version V1.0.0
  * @date    2021年10月23日
  * @brief  拥有 所有设备控制函数，唯一接口  能控制所有设备，能得到所有设备的状态
  ******************************************************************************
  */

#include "includes.h"

#include "BLV_Nor_DevFun.h"

/*普通设备驱动头文件开始*/
#include "BLV_Nor_Dev_ServiceFun.h"
#include "BLV_Nor_Dev_LVinputFun.h"
#include "BLV_Nor_Dev_HVoutFun.h"
#include "BLV_Nor_Dev_LVoutputFun.h"     

/*普通设备驱动头文件结束*/

//#include "DBG.h"

void BLV_Nor_Dev_Polling_Task(void);


BLV_NORDEV_Manage_G NorDevInfoGlobal;      /*普通设备全局变量*/

/*******************************************************************************
* Function Name  : Add_Nor_Device_To_List2 
* Description    : 添加普通设备到链表
* Input          : 
	Device_Public_Information_G *dev_info	: 添加的设备公共数据
	dev_data		：设备的私有数据
	data_len			: 设备私有数据长度

* Return         : None
*******************************************************************************/
void Add_Nor_Device_To_List2(Device_Public_Information_G *dev_info,uint8_t *dev_data,uint16_t data_len)
{
	/*向设备链表添加普通设备*/

//	uint16_t write_len = 
	dev_info->data_len = data_len + Dev_Privately;	//设备私有数据长度加上公共数据的长度
	
	uint32_t list_addr = SRAM_Read_DW(SRAM_NORMAL_Device_List_Addr);	//获取设备信息保存地址
	uint32_t Start_addr = SRAM_Read_DW(SRAM_ACTIVE_Device_List_Addr);	//获取主动设备信息起始地址
	
	if((Start_addr < SRAM_Device_List_Start_Addr) || (Start_addr > SRAM_Device_List_End_Addr))	
	{
		Start_addr = SRAM_Device_List_Start_Addr;
		SRAM_Write_DW(Start_addr,SRAM_ACTIVE_Device_List_Addr);
	}
	if((list_addr < Start_addr) || (list_addr > SRAM_Device_List_End_Addr))	list_addr = Start_addr;
	
	/*清空数据*/
	for(uint16_t i = 0;i<SRAM_Device_List_Size;i++)
	{
		SRAM_Write_Byte(0x00,list_addr+i);	
	}
	
	/*添加公有数据*/
	SRAM_DMA_Write_Buff((uint8_t *)dev_info,Dev_Privately,list_addr);   
	
	/*添加设备私有变量*/
	SRAM_DMA_Write_Buff(dev_data,data_len,list_addr+Dev_Privately);  
	
	/*计算校验值*/
	dev_info->check = Dev_CheckSum(list_addr,dev_info->data_len);
	SRAM_Write_Byte(dev_info->check,list_addr+Dev_Check);		//校验值重新写入
	
//	Dbg_Println(DBG_BIT_SYS_STATUS_bit,"data_len :%d ,dev Buffer:" , write_len);
//	for(uint16_t i = 0;i < write_len;i++)
//	{
//		Dbg_Println(DBG_BIT_SYS_STATUS_bit,"%02X " , SRAM_Read_Byte(list_addr + i));
//	}
//	Dbg_Println(DBG_BIT_SYS_STATUS_bit,"\r\n");
	
	/*设备添加完毕,设备链表地址偏移*/
	list_addr += SRAM_Device_List_Size;
	SRAM_Write_DW(list_addr,SRAM_NORMAL_Device_List_Addr);
}


/*******************************************************************************
* Function Name  : BLV_Nor_Device_Init 
* Description    : 普通设备全局遍历初始化 普通设备接在主动设备的后面
*******************************************************************************/
void BLV_Nor_Device_Init(void)
{
	/*普通设备初始化*/
	memset((void *)&NorDevInfoGlobal,0,sizeof(BLV_NORDEV_Manage_G));
	
	/*开始添加普通设备，就接在主动设备的后面*/
	BLV_Nor_Dev_Service_Init(0x00);   //添加服务信息设备
	
	/*添加设备 - 设置公共属性*/

	
//	BLV_RS485_C7T_Data_Init(0x00,0x10);		//这一个初始化添加了2个设备
//	NorDev_Info.device_num += 2;

	
//	BLV_485_Dev_Touch_Switch_Init(0x01);
//	NorDev_Info.device_num += 1;
	


	/*初始化主动端口*/
//	USART_INIT(USART_2,9600);
}

/*******************************************************************************
* Function Name  : BLV_NormalPort_ModeTask 
* Description    : 
*******************************************************************************/
void BLV_Nor_Dev_ModeTask(void)
{
	static uint32_t dev_Nor_processing_tick = 0;
	
	if( SysTick_1ms - dev_Nor_processing_tick > 4)		//5ms处理一个设备
	{
		dev_Nor_processing_tick = SysTick_1ms;
		BLV_Nor_Dev_Polling_Task();
	}
}

/*******************************************************************************
* Function Name  : BLV_Nor_Dev_Polling_Task 
* Description    : 普通设备扫描任务 之后会用一个函数遍历所有的设备
*******************************************************************************/
void BLV_Nor_Dev_Polling_Task(void)
{
	uint16_t i;
	uint32_t Start_addr = SRAM_Read_DW(SRAM_ACTIVE_Device_List_Addr);	//获取主动设备信息起始地址
	uint32_t List_addr;
	static uint8_t FlagDis = 0x01;

	if(0x01 == FlagDis) 
	{
		FlagDis = 0x00;
		Dbg_Println(DBG_BIT_LOGIC_STATUS_bit,"普通设备遍历开始:%08X,设备个数:%d",Start_addr,NorDevInfoGlobal.NorDeviceNum);

	}
	
	for(i = 0; i < NorDevInfoGlobal.NorDeviceNum; i++)   //遍历所有普通设备
	{
		List_addr = Start_addr + i*SRAM_Device_List_Size;
		{
			Device_Public_Information_G BUS_Public;   //共用
			SRAM_DMA_Read_Buff((uint8_t *)&BUS_Public,sizeof(Device_Public_Information_G),List_addr);	//拷贝所有的函数指针
			switch(BUS_Public.type)
			{
//				case Dev_Host_HVout:        //强电继电器             输出状态
				case Dev_Host_LVinput:      //弱电输入               输入状态
				case Dev_Host_LVoutput:     //弱电输出            输出状态
				case Dev_Host_Service:      //服务信息   可配置数量  输出状态
				case Dev_NodeCurtain:       //干节点窗帘 可配置数量  输出状态		
				case Dev_Rs485_PB20_LD:
				case Dev_Rs485_PB20_LS:
				case Dev_Rs485_PB20_Relay:
				case DEV_Virtual_NoCard:
				case DEV_Virtual_Time:
				case DEV_Virtual_Card:
				case DEV_Virtual_ColorTemp:
				case DEV_Carbon_Saved:
				case Dev_Scene_Restore:
				case Dev_Virtual_GlobalSet:
					if(NULL != BUS_Public.DevFunInfo.Dev_Data_Process)   //函数指针不为0
					{
						BUS_Public.DevFunInfo.Dev_Data_Process(List_addr);    //就跳转
					}
			}
		}
	}
}









