Key.c 8.94 KB
Newer Older
崔立宝's avatar
崔立宝 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
/******************************************************************************
文 件 名:Key.c
功能描述:按键检测函数库文件
作    者:张暄
版    本:V1.0
日    期:2016.11.21
******************************************************************************/

#include "Key.h"

uint8_t KEY_RIGHT_TURN;     
uint8_t KEY_LEFT_TURN ;   
RotateKeyAttrStruct KeyMenu ;


uint8_t KeyDelaytimer1 ;    //20MS 
uint8_t KeyDelaytimer2 ;    //20MS 

KeyDetectStruct  KeyStatus[KEY_TOTAL_NUMBER];

/******************************************************************************
函数名:Key_Status_Init
功  能:初始化旋转按键状态   
参  数:无
返回值:无
******************************************************************************/
void Key_Status_Init(void)
{
  uint8_t i;
  
  for (i = 0; i < KEY_TOTAL_NUMBER; i++)
  {
    KeyStatus[i].UpdateEnable  = 1;
    KeyStatus[i].Timer         = 0;
    KeyStatus[i].IdleTimer     = 0;
    KeyStatus[i].FinalStatus   = KEY_IDLE;
    KeyStatus[i].CurrentStatus = KEY_IDLE;
  }

}

/******************************************************************************
函数名:Key_Status_Detect_Service
功  能:按键检测服务函数,用于检测并更新按键的状态
参  数:无
返回值:无
******************************************************************************
注  意:该服务函数必须每20ms被调用一次
******************************************************************************/
void Key_Status_Detect_Service(void)
{
  uint8_t   i;
  volatile  uint8_t   *pSig;

  for (i = 0; i < KEY_TOTAL_NUMBER; i++)
  {
    pSig = KeyAttrTable[i].pInputSignal;

    if (*pSig)
    {
      if (KeyStatus[i].Timer < 0xFFFF)
        KeyStatus[i].Timer++;

      switch (KeyAttrTable[i].HoldDetMode)
      {
      case KEY_HOLD_DISABLE     :
        if (KeyAttrTable[i].DoubleDetEn)
        {
          if (KeyStatus[i].IdleTimer)
          {
            KeyStatus[i].CurrentStatus = KEY_DOUBLE;
            if (KeyStatus[i].UpdateEnable)
            {
              KeyStatus[i].FinalStatus  = KEY_DOUBLE;
              KeyStatus[i].UpdateEnable = 0;
            }
          }
          else
            KeyStatus[i].CurrentStatus = KEY_PRESS;
        }
        else
        {
          KeyStatus[i].CurrentStatus = KEY_PRESS;
          if (KeyStatus[i].UpdateEnable)
          {
            KeyStatus[i].FinalStatus  = KEY_PRESS;
            KeyStatus[i].UpdateEnable = 0;
          }
        }
        break;

      case KEY_HOLD_ONLY        :
        if (KeyStatus[i].Timer < KEY_HOLD_THRESHOLD / 20)
        {
          if (KeyStatus[i].IdleTimer)
            KeyStatus[i].CurrentStatus = KEY_DOUBLE;
          else
            KeyStatus[i].CurrentStatus = KEY_PRESS;
        }
        else
        {
          KeyStatus[i].CurrentStatus = KEY_HOLD;
          if (KeyStatus[i].UpdateEnable)
          {
            KeyStatus[i].FinalStatus  = KEY_HOLD;
            KeyStatus[i].UpdateEnable = 0;
          }
        }
        break;

      case KEY_STUCK_ONLY       :
        if (KeyStatus[i].Timer < KEY_STUCK_THRESHOLD / 20)
        {
          if (KeyStatus[i].IdleTimer)
            KeyStatus[i].CurrentStatus = KEY_DOUBLE;
          else
            KeyStatus[i].CurrentStatus = KEY_PRESS;
        }
        else
        {
          KeyStatus[i].CurrentStatus = KEY_STUCK;
          if (KeyStatus[i].UpdateEnable)
          {
            KeyStatus[i].FinalStatus  = KEY_STUCK;
            KeyStatus[i].UpdateEnable = 0;
          }
        }
        break;

      case KEY_HOLD_STUCK       :
        if (KeyStatus[i].Timer < KEY_HOLD_THRESHOLD / 20)
        {
          if (KeyStatus[i].IdleTimer)
            KeyStatus[i].CurrentStatus = KEY_DOUBLE;
          else
            KeyStatus[i].CurrentStatus = KEY_PRESS;
        }
        else if (KeyStatus[i].Timer < KEY_STUCK_THRESHOLD / 20)
        {
          KeyStatus[i].CurrentStatus = KEY_HOLD;
        }
        else
        {
          KeyStatus[i].CurrentStatus = KEY_STUCK;
          if (KeyStatus[i].UpdateEnable)
          {
            KeyStatus[i].FinalStatus  = KEY_STUCK;
            KeyStatus[i].UpdateEnable = 0;
          }
        }
        break;

      case KEY_HOLD_LONG        :
        if (KeyStatus[i].Timer < KEY_HOLD_THRESHOLD / 20)
        {
          if (KeyStatus[i].IdleTimer)
            KeyStatus[i].CurrentStatus = KEY_DOUBLE;
          else
            KeyStatus[i].CurrentStatus = KEY_PRESS;
        }
        else if (KeyStatus[i].Timer < KEY_LONG_THRESHOLD / 20)
        {
          KeyStatus[i].CurrentStatus = KEY_HOLD;
        }
        else
        {
          KeyStatus[i].CurrentStatus = KEY_LONG;
          if (KeyStatus[i].UpdateEnable)
          {
            KeyStatus[i].FinalStatus  = KEY_LONG;
            KeyStatus[i].UpdateEnable = 0;
          }
        }

        break;

      case KEY_HOLD_LONG_STUCK  :
        if (KeyStatus[i].Timer < KEY_HOLD_THRESHOLD / 20)
        {
          if (KeyStatus[i].IdleTimer)
            KeyStatus[i].CurrentStatus = KEY_DOUBLE;
          else
            KeyStatus[i].CurrentStatus = KEY_PRESS;
        }
        else if (KeyStatus[i].Timer < KEY_LONG_THRESHOLD / 20)
        {
          KeyStatus[i].CurrentStatus = KEY_HOLD;
        }
        else if (KeyStatus[i].Timer < KEY_STUCK_THRESHOLD / 20)
        {
          KeyStatus[i].CurrentStatus = KEY_LONG;
        }
        else
        {
          KeyStatus[i].CurrentStatus = KEY_STUCK;
          if (KeyStatus[i].UpdateEnable)
          {
            KeyStatus[i].FinalStatus  = KEY_STUCK;
            KeyStatus[i].UpdateEnable = 0;
          }
        }
        break;

      default                    :
        break;
      }
    }
    else
    {
      if (KeyAttrTable[i].DoubleDetEn)
      {
        if (KeyStatus[i].CurrentStatus == KEY_PRESS)
        {
          if (KeyStatus[i].Timer)
            KeyStatus[i].IdleTimer = KEY_DOUBLE_INTERVAL / 20;
        }
        else
          KeyStatus[i].IdleTimer = 0;

        if (KeyStatus[i].IdleTimer)
          KeyStatus[i].IdleTimer--;
        else
        {
          if ((KeyStatus[i].CurrentStatus != KEY_IDLE) && (KeyStatus[i].UpdateEnable != 0))
            KeyStatus[i].FinalStatus = KeyStatus[i].CurrentStatus;
          KeyStatus[i].CurrentStatus = KEY_IDLE;
          KeyStatus[i].UpdateEnable = 1;
        }
      }
      else
      {
        if ((KeyStatus[i].CurrentStatus != KEY_IDLE) && (KeyStatus[i].UpdateEnable != 0))
          KeyStatus[i].FinalStatus = KeyStatus[i].CurrentStatus;
        KeyStatus[i].CurrentStatus = KEY_IDLE;
        KeyStatus[i].UpdateEnable = 1;
      }
      KeyStatus[i].Timer = 0;
    }
  }
}

/******************************************************************************
函数名:Reset_Key_Status
功  能:复位某一按键的按键检测状态
参  数:Key:需要复位的按键
返回值:无
******************************************************************************/
void Reset_Key_Status(uint8_t Key)
{
  KeyStatus[Key].UpdateEnable  = 1;
  KeyStatus[Key].Timer         = 0;
  KeyStatus[Key].IdleTimer     = 0;
  KeyStatus[Key].FinalStatus   = KEY_IDLE;
  KeyStatus[Key].CurrentStatus = KEY_IDLE;
}

/******************************************************************************
函数名:Key_Get_Status
功  能:获取某一按键的检测结果
        按键的检测结果在按键的状态可以被确定时更新
        经由此函数获取检测结果后,按键检测结果被自动清除至KEY_IDLE状态
参  数:Key:需要获取结果的按键
返回值:无
******************************************************************************/
KeyStatusEnum Key_Get_Status(uint8_t Key)
{
  KeyStatusEnum   Status;
  Status = KeyStatus[Key].FinalStatus;
  KeyStatus[Key].FinalStatus = KEY_IDLE;
  return Status;
}

/******************************************************************************
函数名:Key_Get_Current_Status
功  能:获取某一按键的当前状态
        返回的结果反映按键当前所处的状态,并不判定按键的最终状态,在按键符合某一
        状态的条件时实时变化为该状态(包括按键释放后恢复为KEY_IDLE状态)
参  数:Key:需要获取状态的按键
返回值:无
******************************************************************************/
KeyStatusEnum Key_Get_Current_Status(uint8_t Key)
{
  return KeyStatus[Key].CurrentStatus;
}
/*****************************************************************************
函数名:Key_Knob
功  能:获取某一旋钮按键的当前状态
        返回的结果反映按键当前所处的状态,并不判定按键的最终状态,在按键符合某一
        状态的条件时实时变化为该状态()
参  数:KnobSwitch.Key_Step:当前旋钮的步数

返回值:无
*******************************************************************************/
void RotaryKey_Status_Detect_Service(RotateKeyAttrStruct *KeyMenu)
{	
#if 0
  if(!RotaryKEY0_In1 && !RotaryKEY0_In2)	
	{
	    KeyMenu->RotateState = keep_local;//默认
	    KeyMenu->RotateValid = 1;
	}
	
	if((RotaryKEY0_In1 && RotaryKEY0_In2)&&(KeyMenu->RotateValid == 1))  
	{
			KeyMenu->RotateValid = 0;
			
			if( KeyMenu->Rotary2Old == 0 ) 
			{
			    KeyDelaytimer1++ ; 
			    KEY_LEFT_TURN = 1 ;
			}   
			else if( KeyMenu->Rotary1Old == 0 ) 
			{
			    KeyDelaytimer2++ ;    
			    KEY_RIGHT_TURN = 1; 
			}
	}
	KeyMenu->Rotary1Old = RotaryKEY0_In1;
	KeyMenu->Rotary2Old = RotaryKEY0_In2;  	
#endif				   
}