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



#ifdef _cplusplus
extern "C"
{
#endif


/* Includes ------------------------------------------------------------------*/
#include "UWater_valve_driver.h"
#include "UWater_timer1uS_driver.h"	
#include "UWater_adc_driver.h"
#include "UWater_eeprom_driver.h"
#include "UWater_rtcc_driver.h"
#include "eeprom_app.h"
#include "UWater_frame_app.h"
#include "UWater_valve_app.h"
#if defined(VALVE)
/*---------------------------------????--------------------------------------------*/
/*????*/
static u8 s_g_valve_s = VALVE_IDLE;       	/*记录状态机的状态*/
static u8 s_g_adc_valve_s = 0; 			 	/*记录状态机的状态*/
//static u8 s_g_valve_state = 0;           	/*记录阀门的状态*/
static u8 s_g_valve_open_close = 0;      	/*记录收到的开关阀指令*/
//static u8 s_g_valve_msg = 0;            	/*阀控单元抛出的msg*/
u8 g_valve_message_temp = 0;            	/*写EE信息变量*/
//static u8 s_g_valve_wait_ee_flag = 0;    	/*等待写EE时，防止指令重复被执行的标志*/
static u8 s_g_error_count = 0;          	/*记录阀门堵转次数*/
//static u8 s_g_valve_flag = 0;           	/*半开阀操作时记录当前阀门是开启还是关闭，1为开启，2为关闭*/
//static u16 valve_half_time = 0;        		/*半开阀时间*/
static u8 valve_if_op = 0;
static u8 valve_if_cl = 0;
//static u8 valve_power_order = 0xAA;     	//供电顺序,默认正确
//static u8 valve_inplace_order = 0xAA;   	//到位顺序，默认正确
static u16 valve_time_ms = 20*200;          //控阀的时间
u16 valve_adc_voltage_thd = 30*4*10; 

static u8 valve_adc_check_msg = 0;
static u8 valve_adc_count = 0;
u32 g_valve_adc_energy;
u8 valve_adc_complete;
u8 g_valve_store_rcv_state_flag = 0;
u8 g_valve_store_err_state_flag = 0;
//u8 g_valve_store_rcv_state = 0;
static u8 g_valve_init_flag = 0;
static u8 s_g_valve_init_s = 0;
static u8 s_g_valve_store_s = 0;

void ValvePowerOnOrder(u8 IsOpen,u8 IsClose);
void ValveGetInplaceOrder(void);
void ValveSetState(void);
void ValveStoreEeDriver(void);
void ValveInitOperate(void);
/*************************************************************************************
 * @fun_name    :ValveInit
 * @brief       :valve???
 * @param[in] 	:none
 * @param[out] 	:None
 * @retval     	:None
 * @other       :
 *************************************************************************************/
void ValveInit(void)
{ 
	/*nSleep*/
	stc_gpio_cfg_t stcGpioCfg;

	DDL_ZERO_STRUCT(stcGpioCfg);
	stcGpioCfg.enDir = GpioDirOut;
	///< 端口驱动能力配置->低驱动能力
	stcGpioCfg.enDrv =GpioDrvL;
	///< 端口上下拉配置->无
	stcGpioCfg.enPu = GpioPuDisable;
	stcGpioCfg.enPd = GpioPdDisable;
	///< 端口开漏输出配置->开漏输出关闭
	stcGpioCfg.enOD = GpioOdDisable;
	///< GPIO IO 初始化     NSLEEP
	Gpio_Init(NEW_VALVE_NSLEEP_PORT, NEW_VALVE_NSLEEP_PIN, &stcGpioCfg);	
	Gpio_Init(NEW_VALVE_OPEN_PORT, NEW_VALVE_OPEN_PIN, &stcGpioCfg);	
	Gpio_Init(NEW_VALVE_CLOSE_PORT, NEW_VALVE_CLOSE_PIN, &stcGpioCfg);
	Gpio_Init(NEW_STA_PORT, NEW_STA_PIN, &stcGpioCfg);	
	

	/*还有三个是读输入的口*/	
	stcGpioCfg.enDir = GpioDirIn;	//输入
	Gpio_Init(NEW_VALVE_V_CHECK_PORT, NEW_VALVE_V_CHECK_PIN, &stcGpioCfg);	
	Gpio_Init(NEW_STA_CL_PORT, NEW_STA_CL_PIN, &stcGpioCfg);	
	Gpio_Init(NEW_STA_OP_PORT, NEW_STA_OP_PIN, &stcGpioCfg);	
	
	/*初始化io*/
	Gpio_WriteOutputIO(NEW_VALVE_NSLEEP_PORT, NEW_VALVE_NSLEEP_PIN, FALSE);	
	Gpio_WriteOutputIO(NEW_VALVE_OPEN_PORT, NEW_VALVE_OPEN_PIN, FALSE);
	Gpio_WriteOutputIO(NEW_VALVE_CLOSE_PORT, NEW_VALVE_CLOSE_PIN, FALSE);
	Gpio_WriteOutputIO(NEW_STA_PORT, NEW_STA_PIN, FALSE);
	
	if( (VALVE_STATE_OPEN != Sys_data_object.P1.ies_valve_paras.valve_state) && 
		(VALVE_STATE_OPEN_EMERGENCY != Sys_data_object.P1.ies_valve_paras.valve_state) && 
		(VALVE_STATE_CLOSE != Sys_data_object.P1.ies_valve_paras.valve_state)&& 
		(VALVE_STATE_CLOSE_EMERGENCY != Sys_data_object.P1.ies_valve_paras.valve_state) && 
		(VALVE_STATE_CLOSE_LOCK != Sys_data_object.P1.ies_valve_paras.valve_state))
	{
		Sys_data_object.P1.ies_valve_paras.valve_state = VALVE_STATE_OPEN;			//默认普通开阀
		Sys_data_object.P1.ies_valve_paras.valve_err = 0;
	}
	if(Sys_data_object.P1.ies_valve_paras.valve_total_time<7)
	{
		Sys_data_object.P1.ies_valve_paras.valve_total_time = 20;
	}
	if((Sys_data_object.P1.ies_valve_paras.valve_AD<VALVE_HOGH_ADC_VALUE_MIN)||
	   (Sys_data_object.P1.ies_valve_paras.valve_AD>VALVE_HOGH_ADC_VALUE_MAX))
	{
		Sys_data_object.P1.ies_valve_paras.valve_AD = VALVE_HOGH_ADC_VALUE;
	}
	valve_time_ms = Sys_data_object.P1.ies_valve_paras.valve_total_time * 200;			//最大控阀时间
	valve_adc_voltage_thd = Sys_data_object.P1.ies_valve_paras.valve_AD *2;
	
  //关阀上报的格式判断
    if((Sys_data_object.P1.ies_valve_up_times.valve_up_times_pre > 36) || (Sys_data_object.P1.ies_valve_up_times.valve_up_times_pre < 1)  || (Sys_data_object.P1.ies_valve_up_times.valve_up_times_total > 250))
    {
        Sys_data_object.P1.ies_valve_up_times.valve_up_times_pre = 2;    //默认2*30min
        Sys_data_object.P1.ies_valve_up_times.valve_up_times_total = 48;    //默认48次
    }
    if(Sys_data_object.P1.ies_valve_up_times.valve_up_times_realtime > (Sys_data_object.P1.ies_valve_up_times.valve_up_times_total + 1))
    {
        Sys_data_object.P1.ies_valve_up_times.valve_up_times_realtime = 0;
    }
    if((VALVE_AUTO_CHECK_OPEN != Sys_data_object.P1.ies_valve_up_times.valve_auto_check) && (VALVE_AUTO_CHECK_CLOSE != Sys_data_object.P1.ies_valve_up_times.valve_auto_check))
    {
        Sys_data_object.P1.ies_valve_up_times.valve_auto_check = VALVE_AUTO_CHECK_OPEN;
    }
	if(Sys_data_object.P1.ies_pulse_emergency.datas == 0 || Sys_data_object.P1.ies_pulse_emergency.datas > MAX_EMERGENCY_NUM)	//应急用水量
	{
		Sys_data_object.P1.ies_pulse_emergency.datas = 2;
	}   
}
/*************************************************************************************
 * @fun_name    :ValveIfIdle
 * @brief       :valve????idle??
 * @param[in] 	:NONE
 * @param[out] 	:None
 * @retval     	:
 * @other       :
 *************************************************************************************/
u8 ValveIfIdle(void)
{
    if (VALVE_IDLE == s_g_valve_s)
    {
        return (1);
    }
    else
    {
        return (0);
    }
}
/*************************************************************************************
 * @fun_name   ：valve_start_task
 * @brief       ：启动阀控任务
 * @param[in] 	：VALVE_TO_OPEN/ VALVE_TO_CLOSE/VALVE_HALF_OPEN
 * @param[out] 	:None
 * @retval     	:SUCCESS/ERROR
 * @other       :阀门在动作过程中时，不接收新的指令
 *************************************************************************************/
void ValveStartTask(u8 state)
{
    /*先进行参数的合法性判断*/
    if (VALVE_TO_OPEN != state && VALVE_TO_CLOSE != state && VALVE_TO_HALF_OPEN != state)
    {
        return;
    }
    else
    {
        s_g_valve_open_close = state; /*记录收到的开关阀指令*/
		s_g_error_count = 0;
		g_valve_store_err_state_flag = 0;
		Sys_data_object.P1.ies_valve_paras.valve_err = 0;
		Gpio_WriteOutputIO(NEW_STA_PORT, NEW_STA_PIN,TRUE);
		Gpio_WriteOutputIO(NEW_VALVE_NSLEEP_PORT, NEW_VALVE_NSLEEP_PIN, TRUE);	
		ValveSetState();	//状态机由idle变为非idle
    }
}

void ValveSetState(void)
{
	s_g_valve_s = VALVE_S1;
}



/*************************************************************************************
 * @fun_name    : ValveGetState
 * @brief       :??????
 * @param[in] 	:NONE
 * @param[out] 	:None
 * @retval     	:s_g_valve_state 
 * @other       :
 *************************************************************************************/
u8 ValveGetState(void)
{
    return Sys_data_object.P1.ies_valve_paras.valve_state;
}

void ValveMachineDriver()
{
    ValveActMachineDriver();
    ValveAdcMachineDriver();
	ValveStoreEeDriver();
	ValveInitOperate();

}


/*************************************************************************************
 * @fun_name    :ValveActMachineDriver
 * @brief       :???????
 * @param[in] 	:NONE
 * @param[out] 	:NONE
 * @retval     	:None
 * @other       :
 *************************************************************************************/
void VALVE_V_STA(u8 sta)
{
	(sta==1)?Gpio_WriteOutputIO(NEW_STA_PORT, NEW_STA_PIN, TRUE):Gpio_WriteOutputIO(NEW_STA_PORT, NEW_STA_PIN, FALSE);
}

void VALVE_OPEN(u8 sta)
{
	(sta==1)?Gpio_WriteOutputIO(NEW_VALVE_OPEN_PORT, NEW_VALVE_OPEN_PIN, TRUE):Gpio_WriteOutputIO(NEW_VALVE_OPEN_PORT, NEW_VALVE_OPEN_PIN, FALSE);
}

void VALVE_CLOSE(u8 sta)
{
	(sta==1)?Gpio_WriteOutputIO(NEW_VALVE_CLOSE_PORT, NEW_VALVE_CLOSE_PIN, TRUE):Gpio_WriteOutputIO(NEW_VALVE_CLOSE_PORT, NEW_VALVE_CLOSE_PIN, FALSE);
}
void ValveClearAdcMessage(void) 
{
    valve_adc_check_msg = VALVE_NORMAL_POWER;
}

//用于上电或复位时，对阀门的防护
void ValveInitOperate()
{
    switch(s_g_valve_init_s)
    {
        case 0:
        {
            if(1 == g_valve_init_flag)
            {
                g_valve_init_flag = 2;
                Gpio_WriteOutputIO(NEW_STA_PORT, NEW_STA_PIN, TRUE); /*给到位检测供电*/
				__NOP();
                __NOP();
                __NOP();
                __NOP();
                __NOP();
                ValveGetInplaceOrder();
                if((VALVE_STATE_OPEN == Sys_data_object.P1.ies_valve_paras.valve_state) || (VALVE_STATE_OPEN_EMERGENCY == Sys_data_object.P1.ies_valve_paras.valve_state))
                {
                    if(VALVE_IN_PLACE != valve_if_op)   //未开阀到位
                    {
                        Set5msTimer(TIMER_5MS_VALVE_INIT,200);
                        s_g_valve_init_s = 1;
                    }
                    else
                    {
                        Gpio_WriteOutputIO(NEW_STA_PORT, NEW_STA_PIN, FALSE);  /*给到位检测断电*/
						Sys_data_object.P1.ies_valve_paras.valve_err = 0;
                    }
                }
                else if((VALVE_STATE_CLOSE == Sys_data_object.P1.ies_valve_paras.valve_state) || (VALVE_STATE_CLOSE_EMERGENCY == Sys_data_object.P1.ies_valve_paras.valve_state) || (VALVE_STATE_CLOSE_LOCK == Sys_data_object.P1.ies_valve_paras.valve_state))
                {
                    if(VALVE_IN_PLACE != valve_if_cl)   //未关阀到位
                    {
                        Set5msTimer(TIMER_5MS_VALVE_INIT,200);
                        s_g_valve_init_s = 2;
                    }
                    else
                    {
                        Gpio_WriteOutputIO(NEW_STA_PORT, NEW_STA_PIN, FALSE);  /*给到位检测断电*/
						Sys_data_object.P1.ies_valve_paras.valve_err = 0;
                    }
                }
                else
                {
                    Gpio_WriteOutputIO(NEW_STA_PORT, NEW_STA_PIN, FALSE);  /*给到位检测断电*/
                }
            }
        }
        break;
        
        case 1:
        {
            if(0 == Check5msTimer(TIMER_5MS_VALVE_INIT))
            {
                if(ValveIfIdle())
                {
                    ValveStartTask(VALVE_TO_OPEN);
                    s_g_valve_init_s = 0;
                }
            }
        }
        break;
        
        case 2:
        {
            if(0 == Check5msTimer(TIMER_5MS_VALVE_INIT))
            {
                if(ValveIfIdle())
                {
                    ValveStartTask(VALVE_TO_CLOSE);
                    s_g_valve_init_s = 0;
                }
            }
        }
        break;
    }
}

void ValveStoreEeDriver(void)
{
    switch(s_g_valve_store_s)
    {
        case 0:
        {
            if(0xAA == g_valve_store_rcv_state_flag)//阀门状态存EE
            {
                s_g_valve_store_s = 1;
            }
			else if(0xAA == g_valve_store_err_state_flag)//阀门异常状态存EE
			{
				s_g_valve_store_s = 2;
			}
			else if(FrameCheckMsgApp(MsgChangcha_ValveUploadCnts))//关阀上报次数改变存EE
			{
				FrameClearMsgApp(MsgChangcha_ValveUploadCnts);
				s_g_valve_store_s = 3;
			}
			else if(FrameCheckMsgApp(MsgValve_EmergencyFlowStore))//应到达的应急用水量存EE
			{
				FrameClearMsgApp(MsgValve_EmergencyFlowStore);
				s_g_valve_store_s = 4;
			}
        }
		break;

        case 1:
        {
            if(EepIfIdle())
            {
                g_valve_store_rcv_state_flag = 0;
                g_valve_message_temp = 0;
				EepWrite(&Sys_data_object.P1.ies_valve_paras.valve_state,ee_addr_compute((u8*)(&Sys_data_object.P1.ies_valve_paras.valve_state)),1,&g_valve_message_temp);
                s_g_valve_store_s = 10;
            }
        }
        break;
		
		case 2:
		{
			if(EepIfIdle())
            {
                g_valve_store_err_state_flag = 0;
                g_valve_message_temp = 0;
				EepWrite(&Sys_data_object.P1.ies_valve_paras.valve_err,ee_addr_compute((u8*)(&Sys_data_object.P1.ies_valve_paras.valve_err)),1,&g_valve_message_temp);
                s_g_valve_store_s = 10;
            }
		}
		break;
		
        case 3:
		{
			if(EepIfIdle())
            {
                g_valve_message_temp = 0;
				EepWrite(&Sys_data_object.P1.ies_valve_up_times.valve_up_times_realtime,ee_addr_compute((u8*)(&Sys_data_object.P1.ies_valve_up_times.valve_up_times_realtime)),1,&g_valve_message_temp);
                s_g_valve_store_s = 10;
            }
		}
		break;
		
		case 4:
		{
			if(EepIfIdle())
            {
                g_valve_message_temp = 0;
				EepWrite(&Sys_data_object.P1.ies_pulse_remain.data_buf[0],ee_addr_compute((u8*)(&Sys_data_object.P1.ies_pulse_remain.datas)),4,&g_valve_message_temp);
                s_g_valve_store_s = 10;
            }
		}
		break;
		
        case 10:
        {
			if(EepIfIdle())
			{
				s_g_valve_store_s = 0;   
			}    
        }
		break;
		
        default:
		{
            s_g_valve_store_s = 0;
		}
        break;
    }
}

void ValveActMachineDriver(void)
{
	switch (s_g_valve_s)
	{
		/*空闲*/
		case VALVE_IDLE:
		{
			;
		}
		break;
		
		case VALVE_S1:
		{
			if (VALVE_TO_OPEN == s_g_valve_open_close) 			//收到开阀指令
			{
				Set5msTimer(TIMER_5MS_VALVE_MAIN, valve_time_ms); 	//最大开阀时间
                Set5msTimer(TIMER_5MS_VALVE, VALVE_CHECK_TIME); 	//大电流检测时间
                ValvePowerOnOrder(OPEN_POWER_ON,CLOSE_POWER_OFF);			
                s_g_valve_s = VALVE_S2;
			}
			else if (VALVE_TO_CLOSE == s_g_valve_open_close)	//关阀操作
			{
				Set5msTimer(TIMER_5MS_VALVE_MAIN, valve_time_ms);
                Set5msTimer(TIMER_5MS_VALVE, VALVE_CHECK_TIME); 	//大电流检测时间
                ValvePowerOnOrder(OPEN_POWER_OFF,CLOSE_POWER_ON);
                s_g_valve_s = VALVE_S2;
			}
			else if (VALVE_TO_HALF_OPEN == s_g_valve_open_close) 	//半开
			{
				ValveGetInplaceOrder();     						//判断当前线序
                if (VALVE_IN_PLACE == valve_if_cl)  				//关阀到位
                {
                    Set5msTimer(TIMER_5MS_VALVE_MAIN, 600); 		//3s
                    Set5msTimer(TIMER_5MS_VALVE, VALVE_CHECK_TIME); //大电流检测时间
                    ValvePowerOnOrder(OPEN_POWER_ON,CLOSE_POWER_OFF);
                    s_g_valve_s = VALVE_S2;
                }
                else if(VALVE_IN_PLACE == valve_if_op)  			//开阀到位
                {
                    Set5msTimer(TIMER_5MS_VALVE_MAIN, 500);			//2.5s
                    Set5msTimer(TIMER_5MS_VALVE, VALVE_CHECK_TIME); //大电流检测时间
                    ValvePowerOnOrder(OPEN_POWER_OFF,CLOSE_POWER_ON);
                    s_g_valve_s = VALVE_S2;
                }
                else												//开、关阀门到位线均未到位时，不动作
                {
                    VALVE_V_STA(0);
                    s_g_valve_open_close = 0;
                    s_g_valve_s = VALVE_IDLE;
                }
			}
		}
		break;
		
		case VALVE_S2:
		{
			if(0 == Check5msTimer(TIMER_5MS_VALVE)) 						//大电流检测时间
            {
                if (0 == Check5msTimer(TIMER_5MS_VALVE_MAIN)) 				//最长动作时间
                {
                    ValvePowerOnOrder(OPEN_POWER_ON,CLOSE_POWER_ON);    	//都置低
					VALVE_V_STA(0);
                    s_g_valve_open_close = 0;
                    s_g_error_count = 0;
					if(VALVE_TO_HALF_OPEN != s_g_valve_open_close)			//非半开指令
					{
						Sys_data_object.P1.ies_valve_paras.valve_err = 1;		//超时异常
					}
                    s_g_valve_s = VALVE_S4;       
                }
                else
                {
					ValveGetInplaceOrder();     							//判断当前线序
					if(VALVE_IN_PLACE == valve_if_op) 						//开到位
                    {
						if(VALVE_TO_OPEN == s_g_valve_open_close)
						{
							ValvePowerOnOrder(OPEN_POWER_ON,CLOSE_POWER_ON);	//都置低
							VALVE_V_STA(0);
							s_g_error_count = 0;
							s_g_valve_open_close = 0;
							s_g_valve_s = VALVE_S4;
						}
                    }
					else if(VALVE_IN_PLACE == valve_if_cl)					//关到位
					{
						if(VALVE_TO_CLOSE == s_g_valve_open_close)
						{
							ValvePowerOnOrder(OPEN_POWER_ON,CLOSE_POWER_ON);	//都置低
							VALVE_V_STA(0);
							s_g_error_count = 0;
							s_g_valve_open_close = 0;
							s_g_valve_s = VALVE_S4;
						}
					}
					else
					{
						if (valve_adc_check_msg == VALVE_HIGH_POWER) 					//是过电流
						{
							ValveClearAdcMessage();
							ValvePowerOnOrder(OPEN_POWER_ON,CLOSE_POWER_ON);    //都置低
                            s_g_error_count++;
							if (s_g_error_count >= VALVE_ERROR_MAX)
                            {
								ValvePowerOnOrder(OPEN_POWER_ON,CLOSE_POWER_ON);	//都置低
                                VALVE_V_STA(0);
                                s_g_valve_open_close = 0;
                                s_g_error_count = 0;			
								Sys_data_object.P1.ies_valve_paras.valve_err = 1;		//堵转置异常
                                s_g_valve_s = VALVE_S4;
                            }
							else /*堵转次数小于预设值*/
                            {
								Set5msTimer(TIMER_5MS_VALVE, VALVE_PAUSE_TIME);
								s_g_valve_s = VALVE_S3;
                            }
						}
					}
                }
            }
		}
		break;
		
		//等待返回VALVE_S1
		case VALVE_S3:
		{
			if(0 == Check5msTimer(TIMER_5MS_VALVE))
            {
                s_g_valve_s = VALVE_S1;
            }
		}
		break;
		
		//等待返回VALVE_IDLE
		case VALVE_S4:
		{
			//FrameSetMsgApp(MsgNbUploadByValveStateChange);
			g_valve_store_err_state_flag = 0xAA;		//阀门动作完毕，更新EE阀门是否异常标志
			s_g_valve_s = VALVE_IDLE;
		}
		break;
		
		default:
		{
			ValvePowerOnOrder(OPEN_POWER_ON,CLOSE_POWER_ON);    //都置低
            VALVE_V_STA(0);
            s_g_valve_s = VALVE_IDLE;
		}
		break;
		
	}
}

/*************************************************************************************
 * @fun_name    :ValveAdcMachineDriver
 * @brief       :??????
 * @param[in] 	:NONE
 * @param[out] 	:NONE
 * @retval     	:1/0
 * @other       :
 *************************************************************************************/
void ValveAdcMachineDriver(void) 
{
	switch (s_g_adc_valve_s) 
	{
		case 0:
		{
			if (ValveIfIdle() == 0) 
			{
				valve_adc_check_msg = VALVE_NORMAL_POWER;
				valve_adc_count = 0;
				s_g_adc_valve_s = 1;
				Set5msTimer(TIMER_5MS_VALVE_ADC_CHECK, VALVE_CHECK_TIME);
			}
			break;
		}
		case 1:
		{
			if (0 == Check5msTimer(TIMER_5MS_VALVE_ADC_CHECK)) //?????????50ms????AD??
			{
				s_g_adc_valve_s = 2;
				Set5msTimer(TIMER_5MS_VALVE_ADC_CHECK, 10);
			}
			break;
		}
		case 2:
		{
			if (AdcIfIdle()) 
			{ //ADC idle??ADC??????????AD??,??50ms??ad????????        
				valve_adc_count++;
				AdcStartConvert(CHANNEL_VALVE_CHECK,4,0,&g_valve_adc_energy, &valve_adc_complete); //?????5???????????,??????
				Set5msTimer(TIMER_5MS_VALVE_ADC_CHECK, 10);
				s_g_adc_valve_s = 3;
			} 
			else if (0 == Check5msTimer(TIMER_5MS_VALVE_ADC_CHECK))//??ADC???idle        
			{
				s_g_adc_valve_s = 0;
			}
			break;
		}
		case 3:
		{
			if (TRUE == valve_adc_complete) /*????,???????*/
			{
				s_g_adc_valve_s = 4;
			} 
			else if (0 == Check5msTimer(TIMER_5MS_VALVE_ADC_CHECK))//??ad????????
			{
				s_g_adc_valve_s = 0;
			}
			break;
		}
		case 4:
		{
			if (valve_adc_voltage_thd > g_valve_adc_energy) 
			{
				valve_adc_check_msg = VALVE_NORMAL_POWER; //0913
				valve_adc_count = 0;
				s_g_adc_valve_s = 5;
			} 
			else 
			{
				s_g_adc_valve_s = 2;
				if (valve_adc_count > 3)//???????????,??????
				{
					valve_adc_count = 0;
					valve_adc_check_msg = VALVE_HIGH_POWER; //0913
					//                        Set5msTimer(TIMER_5MS_VALVE_ADC_CHECK, 2);
					s_g_adc_valve_s = 5;
				}
			}
			break;
		}

		case 5://
		{
			if (ValveIfIdle() == 0) 
			{
				Set5msTimer(TIMER_5MS_VALVE_ADC_CHECK, 2);
				s_g_adc_valve_s = 1;
			} 
			else 
			{
				Gpio_WriteOutputIO(NEW_VALVE_NSLEEP_PORT, NEW_VALVE_NSLEEP_PIN, FALSE);	
				s_g_adc_valve_s = 0;
			}
			break;
		}
		default:
		{
			Gpio_WriteOutputIO(NEW_VALVE_NSLEEP_PORT, NEW_VALVE_NSLEEP_PIN, FALSE);	
			s_g_adc_valve_s = 0;
			break;
		}
	}
}

/*************************************************************************************
 * @fun_name    :ValveIfSleep
 * @brief       :阀控单元是否允许休眠
 * @param[in] 	:NONE
 * @param[out] 	:NONE
 * @retval     	:1/0
 * @other       :
 *************************************************************************************/
u8 ValveIfSleep(void)
{
    if (VALVE_IDLE == s_g_valve_s && (0 == s_g_adc_valve_s) && (0 == s_g_valve_store_s) && (0 == s_g_valve_init_s))
    {
        return (1);
    }
    else
    {
        return (0);
    }
}

/*************************************************************************************
 * @fun_name    :ValvePreSleep
 * @brief       :阀控单元休眠处理
 * @param[in] 	?NONE
 * @param[out] 	:NONE
 * @retval     	:None
 * @other       :
 *************************************************************************************/
void ValvePreSleep(void)
{
	    /*nSleep*/
	stc_gpio_cfg_t stcGpioCfg;

	DDL_ZERO_STRUCT(stcGpioCfg);
	stcGpioCfg.enDir = GpioDirOut;
	///< 端口驱动能力配置->低驱动能力
	stcGpioCfg.enDrv = GpioDrvL;
	///< 端口上下拉配置->无
	stcGpioCfg.enPu = GpioPuDisable;
	stcGpioCfg.enPd = GpioPdDisable;
	///< 端口开漏输出配置->开漏输出关闭
	stcGpioCfg.enOD = GpioOdDisable;
	///< GPIO IO 初始化     NSLEEP
	Gpio_Init(NEW_VALVE_NSLEEP_PORT, NEW_VALVE_NSLEEP_PIN, &stcGpioCfg);	
    Gpio_WriteOutputIO(NEW_VALVE_NSLEEP_PORT, NEW_VALVE_NSLEEP_PIN, FALSE);//NSLEEP清零，PT2466进休眠
	if(0 == g_valve_init_flag)
	{
		g_valve_init_flag = 1;
	}
}

/*************************************************************************************
 * @fun_name    :ValveWakeSleep
 * @brief       :唤醒阀控单元
 * @param[in] 	:NONE
 * @param[out] 	:NONE
 * @retval     	:None
 * @other       :
 *************************************************************************************/
void ValveWakeSleep(void)
{
    ;
}

//获取阀门硬件的实际状态
u8  ValveGetLineInSpace(void)
{
    u8 temp = 0;
	VALVE_V_STA(1);		//给到位检测供电
	__NOP();
	__NOP();
	__NOP();
	__NOP();
	__NOP();
	__NOP();
	__NOP();
	__NOP();
	__NOP();
	__NOP();
    ValveGetInplaceOrder();     //判断当前线序
    if((VALVE_IN_PLACE != valve_if_cl) && (VALVE_IN_PLACE == valve_if_op))		//开阀到位
    {
        temp = 0x00;
    }
    else if((VALVE_IN_PLACE == valve_if_cl) && (VALVE_IN_PLACE != valve_if_op)) //关阀到位
    {
        temp = 0x10;
    }
    else 
    {
        temp = 0x30;
    }
    if(ValveIfIdle())
    {
        VALVE_V_STA(0);	
    }
    return temp;
}

void ValveUpdatePara(void)
{
//	valve_power_order = Sys_data_object.P1.ies_valve_paras.valve_power_order;	//供电线序
//	valve_inplace_order = Sys_data_object.P1.ies_valve_paras.valve_inplace_order;//到位线序
	valve_time_ms = Sys_data_object.P1.ies_valve_paras.valve_total_time * 200;			//最大控阀时间
	valve_adc_voltage_thd = (Sys_data_object.P1.ies_valve_paras.valve_AD << 8) * 5 / 1000;		//AD值
}

void ValvePowerOnOrder(u8 IsOpen,u8 IsClose)
{
//    switch(valve_power_order)
//    {
//        case 0xBB:
//        {
			VALVE_OPEN(IsOpen);
			VALVE_CLOSE(IsClose);
//            break;
//        }
//        case 0xAA:
//        {
//            VALVE_OPEN(IsClose);
//			VALVE_CLOSE(IsOpen);
//            break;
//        }
//        default:
//        {
//            VALVE_OPEN(IsClose);
//			VALVE_CLOSE(IsOpen);
//            break;
//        }
//    }
}

void ValveGetInplaceOrder(void)
{
//    switch(valve_inplace_order) //判断当前线序
//    {
//        case 0xBB:
//        {
			valve_if_op = Gpio_GetInputIO(NEW_STA_OP_PORT, NEW_STA_OP_PIN);
            valve_if_cl = Gpio_GetInputIO(NEW_STA_CL_PORT, NEW_STA_CL_PIN);
//            break;
//        }
//        case 0xAA:
//        {
//			valve_if_op = Gpio_GetInputIO(NEW_STA_CL_PORT, NEW_STA_CL_PIN);
//            valve_if_cl = Gpio_GetInputIO(NEW_STA_OP_PORT, NEW_STA_OP_PIN);
//            break;
//        }
//        default:
//        {
//			valve_if_op = Gpio_GetInputIO(NEW_STA_CL_PORT, NEW_STA_CL_PIN);
//            valve_if_cl = Gpio_GetInputIO(NEW_STA_OP_PORT, NEW_STA_OP_PIN);
//            break;
//        }
//    }
}

void ValveStoreRcvState(u8 state)
{
    if((VALVE_STATE_CLOSE == state) || (VALVE_STATE_CLOSE_EMERGENCY == state) || (VALVE_STATE_CLOSE_LOCK == state) || (VALVE_STATE_OPEN_EMERGENCY == state) || (VALVE_STATE_OPEN == state))
    {
        g_valve_store_rcv_state_flag =0xAA;
		Sys_data_object.P1.ies_valve_paras.valve_state = state;
    }
}
#endif

#ifdef __cplusplus
}
#endif

/***************************************************************END OF FILE****/

