#ifndef _RLY_CONTROL_H_
#define _RLY_CONTROL_H_

#include "apt32f102.h"
#include "apt32f102_gpio.h"

#define A9EXPANDTYPE		0x0E//14	    //A9RELAY类型

#define CMD_SET_RLYSTATE			0x20	//设置继电器状态
#define CMD_READ_RLYSTATE			0x24	//读取继电器状态
#define CMD_SET_WINDOUTVOL			0x27	//设置各个档位的电压输出，0-10000mV
#define CMD_READ_WINDOUTVOL			0x29	//读取各个档位的电压输出，0-10000mV
#define CMD_SET_DEVPORT				0x28	//设置端口模式
#define CMD_READ_DEVPORT			0x2A	//读取端口模式

#define CMD_SET_RLYSTATE_REPLY		0x30	//回复 --  设置继电器状态
#define CMD_READ_RLYSTATE_REPLY		0x34	//回复 --  读取继电器状态
#define CMD_SET_WINDOUTVOL_REPLY	0x37	//回复 --  设置各个档位的电压输出，0-10000mV
#define CMD_READ_WINDOUTVOL_REPLY	0x39	//回复 --  读取各个档位的电压输出，0-10000mV
#define CMD_SET_DEVPORT_REPLY		0x38	//回复 --  设置端口模式
#define CMD_READ_DEVPORT_REPLY		0x3A	//回复 --  读取端口模式

#define RLY_1_OPEN		GPIO_Write_High(GPIOA0,13);
#define RLY_1_CLOSE		GPIO_Write_Low(GPIOA0,13);	

#define RLY_2_OPEN		GPIO_Write_High(GPIOA0,12);
#define RLY_2_CLOSE		GPIO_Write_Low(GPIOA0,12);	



#define WIND_LOW	0x01	
#define WIND_MID	0x02
#define WIND_HIGH	0x03
#define WIND_STOP	0x00	
	
	
#define Control_OFF		0x00	//继电器
#define Control_ON		0x01	//
#define Cnotrol_RES		0x02

#define ACTIVE_PORT                 0x02      //主动端口
#define POLLING_PORT                0x01      //轮训端口

//继电器数量
typedef enum{
	CTRL_RLY1 = 0x00,
	CTRL_RLY2,
	WINDRLY_LOW,
	WINDRLY_MID,
	WINDRLY_HIGH,
	
	RLY_MAX,
}RLY_g;

//继电器控制状态
typedef enum{
	NO_CTRL = 0x00,	//继电器不控制
	RLY_OFF,		//继电器关闭
	RLY_ON,			//继电器打开
	RLY_RES,		//继电器翻转
	
}RLY_CTRL_g;

typedef enum{
	SEND_ADDR1 = 0x00,	
	SEND_SN,		
	SEND_TYPE,			
	SEND_ADDR2,
	SEND_LEN,
	SEND_SUM,
	SEND_CMD,
	SEND_PARA,
	
}RECV_g;


typedef struct{
	
	U8_T rly_control;				//继电器控制标志位，0x00：不控制继电器，0x01：直接控制
	U8_T rly_state[RLY_MAX];		//存储继电器状态，Control_ON，Control_OFF
	U8_T rly_ctrl_state[RLY_MAX];
	
	U8_T dev_port;			//端口模式，0x01,轮询，0x02,主动
	U8_T SN;
	
	U8_T wind;
	U8_T wind_last;
	
	U16_T wind_STOP_vol;
	U16_T wind_LOW_vol;	//低风速电压记录,0-10000mV
	U16_T wind_MID_vol;
	U16_T wind_HIGH_vol;
	
}ZERO_CTRL_RLY;

extern ZERO_CTRL_RLY c_rly;

void Relay_Init(void);
void BLV_RLY_Ctrl_Purpose(U8_T rly_id,U8_T state);
U8_T CheckSum(U8_T *data,U16_T len);
U8_T CheckSum2(U8_T *data,U16_T len);
U8_T BLV_RLY_RS485_Pro(U8_T *RecData, U16_T Len);
void BLV_RLY_Task(void);
U8_T Change_OUTV(U16_T VolOut);
void CTRL_LEDStatus_Task(void);
#endif

