/*******************************************************************************
* @Copyright (c)    :(C)2017, Qingdao ieslab Co., Ltd
* @FileName         :GP22Gas_valve_driver.h
* @Author           :Kv-L
* @Version          :V1.0
* @Date             :2017年5月9日 13:30:23
* @Description      :the function of the entity of GP22Gas_valve_driver.h
*******************************************************************************/

/* Define to prevent recursive inclusion -------------------------------------*/
#include "sys_config.h"

#ifndef VALVE_DRIVER_H
#define	VALVE_DRIVER_H

#ifdef	__cplusplus
extern "C" {
#endif 
	
/* Includes ------------------------------------------------------------------*/
#include "sys_config.h"
#include "ddl.h"
#include "type.h"
//#include "UWater_frame_app.h"

/*EEP地址映射*/
//#define VALVE_ADDR_BASE     EEP_BASE_VALVE      /*跟系统确认*/
//#define VALVE_ERR_BASE    (VALVE_ADDR_BASE + 1) 

#define CHANNEL_VALVE_CHECK     13			/*ref ADC_channels   HD 2020-09-16*/

#define VALVE_PAUSE_TIME	200        	/*暂停时间1s，选用5ms当量定时器TIMER_5MS_VALVE*/
#define VALVE_CHECK_TIME	100	 		/*大电流检测时间0.5s，选用5ms当量定时器TIMER_5MS_VALVE*/
#define VALVE_ERROR_MAX		2	 		/*最大堵转次数*/

//阀门adc电压信息
#define VALVE_HIGH_POWER          1         //堵转
#define VALVE_NORMAL_POWER        0         //正常
//堵转电流AD值
#define VALVE_HOGH_ADC_VALUE      (280/2) //(实测堵转ADC=310,采样电阻0.5R->堵转电流370mA;设定堵转电流280mA-->0.5R采样电阻--->ADC=230;150mA--->0.5R--->adc=123)
#define VALVE_HOGH_ADC_VALUE_MIN  (123/2)//123--->150mA
#define VALVE_HOGH_ADC_VALUE_MAX  (287/2)//287--->350mA

#if (MODULE_ML302_TCP|MODULE_ML307A_TCP)
	#define NEW_STA_PORT			GpioPortA
	#define NEW_STA_PIN				GpioPin4
		
	#define NEW_STA_OP_PORT		    GpioPortF
	#define NEW_STA_OP_PIN			GpioPin1
#else
	#define NEW_STA_PORT	              	GpioPortC	//到位检测供电	
	#define NEW_STA_PIN                 	GpioPin1
	
	#define NEW_STA_OP_PORT		        	GpioPortA
	#define NEW_STA_OP_PIN    	        	GpioPin7
#endif
#define NEW_VALVE_NSLEEP_PORT		  	GpioPortB
#define NEW_VALVE_NSLEEP_PIN		  	GpioPin7
	
#define NEW_VALVE_OPEN_PORT			    GpioPortF
#define NEW_VALVE_OPEN_PIN			  	GpioPin5
	
#define NEW_VALVE_CLOSE_PORT		  	GpioPortF
#define NEW_VALVE_CLOSE_PIN	    		GpioPin4  

#define NEW_VALVE_V_CHECK_PORT	    	GpioPortC	
#define NEW_VALVE_V_CHECK_PIN       	GpioPin3

#define NEW_STA_CL_PORT		          	GpioPortA
#define NEW_STA_CL_PIN              	GpioPin6

/*状态机状态*/
enum{
	VALVE_IDLE = 0,
	VALVE_S1,
	VALVE_S2,
	VALVE_S3,
	VALVE_S4
};


/*/控制指令*/
#define VALVE_TO_OPEN       		0x55       /*开阀指令*/
#define VALVE_TO_CLOSE      		0x99       /*关阀指令*/
#define VALVE_TO_HALF_OPEN  		0xAA       /*半开阀指令*/
#define VALBE_CLOSE_GENERAL			0xBB		//普通关阀
#define VALBE_CLOSE_LOCK			0xCC		//权限关阀


/*阀门状态*/
enum{
	VALVE_STATE_OPEN = 1,				//普通开阀状态
	VALVE_STATE_OPEN_EMERGENCY,			//应急开阀状态
	VALVE_STATE_CLOSE,					//普通关阀
	VALVE_STATE_CLOSE_EMERGENCY,		//应急关阀
	VALVE_STATE_CLOSE_LOCK				//权限关阀	
};


/*到位*/
#define VALVE_IN_PLACE          0       /*阀门到位*/
#define VALVE_NOT_IN_PLACE      1       /*阀门到位*/

/**检测到位*/
#define VALVE_STA_CL        Gpio_GetInputIO(NEW_STA_CL_PORT, NEW_STA_CL_PIN)
#define VALVE_STA_OP        Gpio_GetInputIO(NEW_STA_OP_PORT, NEW_STA_OP_PIN)


#define OPEN_POWER_ON           0
#define OPEN_POWER_OFF          1    
#define CLOSE_POWER_ON          0
#define CLOSE_POWER_OFF         1 


void ValveStartTask(u8 state);
void ValveInit(void);
void ValveActMachineDriver(void);
void ValveMachineDriver(void);
void ValveAdcMachineDriver(void); 
u8 ValveIfIdle(void);
u8 ValveIfSleep(void);
void ValvePreSleep(void);
void ValveWakeSleep(void);
u8 ValveGetState(void);
u8  ValveGetLineInSpace(void);
void ValveUpdatePara(void);
void ValveStoreRcvState(u8 state);
#ifdef __cplusplus
}
#endif
#endif

