#ifndef _BLV_BUS_DEV_C5IOFUN_H_
#define _BLV_BUS_DEV_C5IOFUN_H_

#include "stdint.h"
#include "BLV_485_Protocol.h"
#include "BLV_All_Dev_Init.h"

#define C5IOTYPE			0xF1		//C5IO设备类型 - 这设备类型只用于BLV_BUS协议通讯使用
#define DEV_C5IO_Type		0xF1		//用于设备列表中的设备类型

#define C5IO_REPEATSENDTIMEMAX 0x02    //重发次数
#define C5IO_SEND_WAIT_TIME    0x28    //等待时间     2024-02-23

#define C5IO_RecvData_Len_MAX		0x28	//接收数据长度目前最长30Byte


/*控制命令*/
#define BLV_C5IO_Inquire_CMD				0x20		//询问命令
#define BLV_C5IO_Set_Relay_CMD				0x21		//设置继电器命令
#define BLV_C5IO_Set_Relay_Inching_CMD		0x22		//设置继电器点动命令
#define BLV_C5IO_Set_Do_CMD					0x23		//设置DO命令
#define BLV_C5IO_Set_Do_Inching_CMD			0x24		//设置DO点动命令
#define BLV_C5IO_Set_Di_CMD					0x25		//设置DI命令
#define BLV_C5IO_SetRTC_CMD                 0x27        //
#define BLV_C5IO_Reply_CMD					0xA0		//回复命令
               

#define BLV_C5IO_Reply_Result				0x00		//查询结果
#define BLV_C5IO_Relay_Result				0x01		//继电器执行成功
#define BLV_C5IO_Relay_Inching_Result		0x02		//继电器点动执行成功
#define BLV_C5IO_Do_Result					0x03		//Do执行成功
#define BLV_C5IO_Do_Inching_Result			0x04		//DO点动执行成功
#define BLV_C5IO_Di_Result					0x05		//DI执行成功
#define BLV_C5IO_Error_Result				0xF0		//数据格式错误

#define BUS_C5IO_DI_Key_Type		0x01	//DI输入检测 - 按键类型
#define BUS_C5IO_DI_Pir_Type		0x02	//DI输入检测 - PIR类型
#define BUS_C5IO_DI_Dry_Type		0x03	//DI输入检测 - 干接点
#define BUS_C5IO_DI_Level_HIGH		0x20	//DI输入检测为高电平
#define BUS_C5IO_DI_Level_LOW		0x10	//DI输入检测为低电平

#define BUS_C5IO_DO_Common_Mode		0x00	//DO控制模式 - 正常控制模式
#define BUS_C5IO_DO_Inching_Mode	0x01	//DO控制模式 - 点动控制模式
#define BUS_C5IO_OUT_LOW			0x01
#define BUS_C5IO_OUT_HIGH			0x02
#define BUS_C5IO_OUT_TOGGLE			0x03
#define BUS_C5IO_Right_Inching		0x01
#define BUS_C5IO_Reverse_Inching	0x02
#define BUS_C5IO_Toggle_Inching		0x03

typedef enum{
	C5IO_Relay_CH1,
	C5IO_Relay_CH2,
	C5IO_Relay_CH3,
	C5IO_Relay_CH4,
	C5IO_Relay_CH5,
	C5IO_Relay_CH6,
	C5IO_Relay_CH7,
	C5IO_Relay_CH8,
	C5IO_Relay_CH9,
	C5IO_Relay_CH10,
	C5IO_Relay_CH11,
	C5IO_Relay_CH12,
	C5IO_Relay_CH13,
//#if (USE_FLOOR_TYPE == 1)  //C5底板
	C5IO_Relay_CH14,
	C5IO_Relay_CH15,
	C5IO_Relay_CH16,
	C5IO_Relay_CH17,
	C5IO_Relay_CH18,
	C5IO_Relay_CH19,
	C5IO_Relay_CH20,
//#endif
	C5IO_Relay_CH21,
	C5IO_Relay_CH22,
	C5IO_Relay_CH23,
	C5IO_Relay_CH24,
	C5IO_Relay_CH_MAX,
}BUS_C5IO_RELAY_NUM_E;

typedef enum{
	C5IO_DO_CH1,
	C5IO_DO_CH2,
	C5IO_DO_CH3,
	C5IO_DO_CH4,
	C5IO_DO_CH5,
	C5IO_DO_CH_MAX,
}BUS_C5IO_DO_NUM_E;

typedef enum{
	C5IO_DI_CH1,
	C5IO_DI_CH2,
	C5IO_DI_CH3,
	C5IO_DI_CH4,
	C5IO_DI_CH5,
	C5IO_DI_CH6,
	C5IO_DI_CH7,
//#if (USE_FLOOR_TYPE == 2)  //C12底板
	C5IO_DI_CH8,
	C5IO_DI_CH9,
	C5IO_DI_CH10,
	C5IO_DI_CH11,
	C5IO_DI_CH12,
	C5IO_DI_CH13,
//#endif
	C5IO_DI_CH_MAX,
}BUS_C5IO_DI_NUM_E;

typedef struct{
	BLV_COMM_RECORD_G comm_record;						//通讯记录
	
	uint8_t DO_Mode[C5IO_DO_CH_MAX];					//DO控制模式
	uint8_t DO_Control[C5IO_DO_CH_MAX];					//Do控制
	uint16_t Last_DO_Level_Start;						//之前DO实际电平状态
	uint16_t DO_Level_Actual_Start;						//DO实际电平状态
	uint16_t DO_Level_Perfect_Start;						//DO理想电平状态
	uint16_t DO_Inching_Tick[C5IO_DO_CH_MAX];			//DO点动时间戳
	uint16_t DO_Inching_Time[C5IO_DO_CH_MAX];			//DO点动时间
	
	uint8_t Relay_Mode[C5IO_Relay_CH_MAX];				//继电器控制模式
	uint8_t Relay_Control[C5IO_Relay_CH_MAX];			//继电器控制
	uint32_t Last_Relay_Level_Start;					//之前继电器实际电平状态
	uint32_t Relay_Level_Actual_Start;					//继电器实际状态
	uint32_t Relay_Level_Perfect_Start;					//继电器理想状态
	uint16_t Relay_Inching_Tick[C5IO_Relay_CH_MAX];		//继电器点动时间戳
	uint16_t Relay_Inching_Time[C5IO_Relay_CH_MAX];		//继电器点动时间
	uint8_t Relay_feedback;								//继电器反馈标志位
	
	uint8_t DI_Type[C5IO_DI_CH_MAX];					//DI输入类型 0~3:检测类型 4~7:检测电平
	uint16_t DI_Level_Actual_Start;	
	uint32_t DI_Actual_State;
	uint32_t DI_Perfect_State;
	uint8_t DI_Start[C5IO_DI_CH_MAX];					//DI动作状态
	uint8_t DI_LastStart[C5IO_DI_CH_MAX];
	uint8_t DI_Detection_Time[C5IO_DI_CH_MAX];			//DI消抖时间
	
	uint8_t DevSendCnt;									//设备发送计数   计数达到重发次数都没有回复，判为离线
	uint8_t DevOffline;       							//设备离线标志 1，设备离线   0，设备在线
	uint8_t DevOfflineLast;								//设备上一次离线标志 1，设备离线   0，设备在线
	uint8_t DevSendSN;									//发送SN号
	
	uint8_t Send_Type;									//发送数据类型
 	uint16_t DI_Control_Flag;							//DI控制标志位
	uint16_t Last_DI_Control_Flag;						//DI上次控制标志位
	uint8_t DO_Control_Flag;							//DO控制标志位
	uint8_t	Last_DO_Control_Flag;						//DO上次控制标志位
	uint8_t DO_Inching_Control_Flag;					//DO点动控制标志
	uint8_t	Last_DO_Inching_Control_Flag;				//DO点动上次控制标志
	uint32_t Relay_Control_Flag;						//继电器控制标志位
	uint32_t Last_Relay_Control_Flag;					//继电器上次控制标志
	uint32_t Relay_Inching_Control_Flag;				//继电器点动控制标志位
	uint32_t Last_Relay_Inching_Control_Flag;			//继电器点动上次控制标志位
	
	uint8_t C5IO_Version;       //IO程序版本 - 用于区分C5 C12 CSIO
    uint8_t comm_version;       //CSIO通讯协议中 - 自带的版本号
	uint32_t inquire_tick;		//询问时间戳
	uint16_t CxIO_DI_Control_Flag;       //记录需要设置的DI回路
	uint8_t  DI_Reset_Flag;        //DI重设标记
	uint32_t DI_Reset_Tick;        //DI重设时间戳
	uint8_t  Relay_Reset_Flag;     //继电器状态恢复标记
    uint8_t  rtc_set_flag;          
	uint8_t  DI_Init_flag;			//DI初始化标志位	2025-08-07 增加上电后，不产生动作
}__attribute__ ((__packed__)) BUS_C5IO_INFO;

void BLV_BUS_CSIO_For_Logic_Init(LOGICFILE_DEVICE_INFO *dev_info,uint8_t *data,uint16_t data_len);
void BLV_BUS_CSIO_DI_For_Logic_Init(LOGICFILE_DEVICE_INFO *dev_info,uint8_t *data,uint16_t data_len);
uint8_t BLV_BUS_C5IO_Cycle_Call(uint32_t dev_addr);
uint8_t BLV_BUS_C5IO_Data_Processing(uint32_t dev_addr,uint32_t data_addr,uint16_t len);
void BUS_C5IO_DI_Control_Datasend(uint32_t dev_addr,BUS_C5IO_INFO *Dev_Info);
void BUS_C5IO_DO_Control_Datasend(uint32_t dev_addr,BUS_C5IO_INFO *Dev_Info);
void BUS_C5IO_DO_Inching_Control_Datasend(uint32_t dev_addr,BUS_C5IO_INFO *Dev_Info);
void BUS_C5IO_Relay_Control_Datasend(uint32_t dev_addr,BUS_C5IO_INFO *Dev_Info);
void BUS_C5IO_Relay_Inching_Control_Datasend(uint32_t dev_addr,BUS_C5IO_INFO *Dev_Info);
void BUS_CSIO_SetRTCTime_Datasend(uint32_t dev_addr,BUS_C5IO_INFO *Dev_Info);
void BUS_C5IO_Inquire_Datasend(uint32_t dev_addr,BUS_C5IO_INFO *Dev_Info);
//void BUS_C5IO_Test(void);
void BUS_C5IO_Control_Relay(uint32_t dev_addr,uint8_t loop,uint8_t start);
void BUS_C5IO_Control_Do(uint32_t dev_addr,uint8_t loop,uint8_t start);
void BUS_C5IO_Control_Relay_Inching(uint32_t dev_addr,uint8_t loop,uint8_t start,uint16_t d_time);
void BUS_CSIO_Set_RTC_Time(uint32_t dev_addr);
void BUS_C5IO_Group_Control_Relay(uint32_t dev_addr,uint32_t loop,uint32_t start);
uint32_t Get_BUS_C5IO_Realy_Status(uint32_t devaddr);

uint8_t Get_BUS_C5IO_Online_Status(uint32_t devaddr);

uint8_t Get_Bus_CSIO_COMM_Version(uint32_t devaddr);        //2024-05-22







#endif
