#include "ys32md211.h"
#include "ys32md211.h"
#include "motor.h"
#include "KEY.h"
typedef struct {
	  uint16_t   key_long_time;
	  uint16_t   key_old_state;
    uint16_t   key_cureent_state;          // : 1;  // bit0:????
    uint16_t   key_state;     //  : 1;  // bit1:????
    uint16_t   key_cnt;
	  
} G_key;
G_key   Key;  

//*************************************************************************************//
void IO_Configuration(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
	 RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_GPIOA, ENABLE);
	  RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_GPIOB, ENABLE);
    RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_GPIOF, ENABLE);
     /* Configure Pb7 in input mode */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;      //??PA7??
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;      //????
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_VeryHigh;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
	  
	  GPIO_InitStructure.GPIO_Pin = PIN_LED;                             //LED??
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;                      //????
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_VeryHigh;                  //????
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;                     //????
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;                   //????(??)
    GPIO_Init(PORT_LED, &GPIO_InitStructure);
	
//	   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;      //??PA7??
//    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;      //????
//    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
//	  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_VeryHigh;
//    GPIO_Init(GPIOB, &GPIO_InitStructure);
}
//**************************************************************************************//
void key_process(void)
{
         
   Key.key_cureent_state=(GPIOA->IDR&GPIO_Pin_6);//Key_state.key_cureent_state=(GPIOB->IDR&GPIO_Pin_7);////=pa&0x80;
  
   if(Key.key_cureent_state==Key.key_old_state)
   {

	   if((GPIOA->IDR&GPIO_Pin_6)==RESET)
		  {
         if(Key.key_long_time>=1500)
			    {
			      goto  gotonext;//	return;
			    }
		     Key.key_long_time++;
		
		     if(Key.key_long_time>=1400)
		     {

           Key.key_long_time=2500;
			     if(Key.key_state)
				    {
//				       Key_state.key_state=0;
//							 m_par_set.Motor_state=M_STOP;
//							 Fan_Stop_Func();
//			         stop_mos();
//							 TIM_Cmd(TIM17, ENABLE);
				   
				    }
			     else
				    {
//			         Key_state.key_state=1;
//							 if(m_par_set.Motor_state!=M_STOP)
//							  {
//							        m_par_set.Motor_state=1;
//							      
//							  }
//							  else
//								{
//									    com_set_PWM(m_par_set.hx_temp);
//							        Start_Sensorless_BLDC();
//										  m_par_set.Motor_state=M_START_OFF;
//										  LED_TOGGLE;	
//								}
//								TIM_Cmd(TIM17, DISABLE);
				    }
		    }

	   }
	   else
	   {
           if(Key.key_long_time>=2500)
		       {
              Key.key_long_time=0;
		       }
	   } 
gotonext:
	  if(Key.key_cnt>200)
		   return;
 	  Key.key_cnt++;
    if(Key.key_cnt>10)
     {
        if(GPIOA->IDR&GPIO_Pin_6)
	       {

            if((Key.key_long_time<1200)&&(Key.key_long_time>=20))//&&(Key_state.key_state)) 
			      {
		   	       #if(M_Control_SET == 1)
							 if(Key.key_state==0)
							 {
							    m_par_set.Motor_state=1; 
							    		
							 }
		 	         Key.key_state++;
			         if(Key.key_state>4)
			         {
								  Key.key_state=0;
                  m_par_set.Motor_state=M_STOP;
							    
			         }
	            #endif       
			      }
			    Key.key_long_time=0;
	        Key.key_cnt=250;
        }
     }
	 }
   else
   {
      Key.key_cnt=0;
	    Key.key_old_state=Key.key_cureent_state;
   }
 
}
//********************************************************************//
uint8_t read_key_state(void)
{
    return Key.key_state;
}
//**********************************//
void clr_key_state(void)
{
//	   if(m_par_set.Motor_state==M_STOP)
//			 return;
     Key.key_state=0;
	  // m_par_set.Motor_state=M_INIT;
}
