狀態機器實現按鍵檢測

來源:互聯網
上載者:User

 

最近發現GPIO的中斷功能有點問題,查了兩天確實也沒查出來為什麼,畢竟中斷管理也不沒那麼容易,所有暫且用掃描的辦法來解決,下面這個演算法很經典,記下,備以後參考.

--------------------------------------------file "key.h"----------------------------------------------

#ifndef KEY_H
#define KEY_H

/*----------------key definition--------------*/

#define  NO_KEY           0x00

#define  PLAY_KEY         0x01         /*  vcd   key   */
#define  STOP_KEY         0x02
#define  NEXT_KEY         0x03
#define  PREV_KEY         0x04
#define  REPEAT_KEY       0x05
#define  PROGRAM_KEY      0x06
#define  REMAIN_KEY       0x07
#define  VOCAL_ASSIST_KEY 0x08
#define  VOLUME_UP_KEY    0x09
#define  VOLUME_DOWN_KEY  0x0a
#define  TV_MODE_KEY      0x0b
#define  EJECT_KEY        0x0c
#define  FB_KEY           0x0d
#define  FF_KEY           0x0e

#define  POWER_KEY        0x20         /* common key  */
#define  CLOCK_KEY        0x21
#define  SLEEP_KEY        0x22
#define  FUNCTION_KEY     0x23
#define  TONE_KEY         0x24
#define  EQ_KEY           0x25
#define  SBASS_KEY        0x26
#define  RADIO_KEY        0x27
#define  TAPE_KEY         0x28
#define  AUX_KEY          0x29
#define  VCD_KEY          0x30

#define  MEM_KEY          0x31        /*  radio key  */
#define  MEM_MINUS_KEY    0x32
#define  MEM_PLUS_KEY     0x33
#define  AM_STEP_KEY      0x34
#define  BAND_KEY         0x35
#define  SEARCH_UP_KEY    0x36
#define  SEARCH_DOWN_KEY  0x37
#define  AUTO_SEARCH_UP_KEY     0x38
#define  AUTO_SEARCH_DOWN_KEY   0x39

/*---------------end key definition-----------*/

#define  KEY_IDLE_STATE     0
#define  KEY_SHAKE_STATE    1
#define  KEY_COMPLEX_STATE  2
#define  KEY_REPEAT_STATE   3
#define  KEY_WAIT_UP        4

extern unsigned char shake_cancel_timer;
extern unsigned char complex_key_timer;
extern unsigned char repeat_key_timer;
extern unsigned char current_key;
extern unsigned char current_state;

extern void scan_key();
extern void process_panel_key();
extern unsigned char is_repeat_key(unsigned char);

#endif

------------------------------------------------file "key.c"-----------------------------------------------

#include "key.h"
#include "display.h"

unsigned char current_state;    /* RADIO /  VCD  /TAPE /AUX */
unsigned char current_key;
unsigned char key;

unsigned char is_repeat_key(unsigned char);
void scan_key();
void process_panel_key();

/*====================================================================================================
                                    掃描面板按鍵程式
====================================================================================================*/
void scan_key()
{
   set_row1;
   set_row2;
   set_row3;
   set_row4;
   key = NO_KEY;

   clear_col1;
   set_col2;
   set_col3;
   set_col4;
   if (check_row1_low)     key = SEARCH_DOWN_KEY;   
   if (check_row2_low)     key = SEARCH_UP_KEY;
   if (check_row3_low)     key = BAND_KEY;  

   if (check_row4_low)     key = EJECT_KEY; 

   set_col1;
   clear_col2;
   set_col3;
   set_col4;
   if (check_row1_low)     key = MEM_MINUS_KEY;
   if (check_row2_low)     key = MEM_PLUS_KEY;
   if (check_row3_low)     key = MEM_KEY;
   if (check_row4_low)     key = AM_STEP_KEY;

   set_col1;
   set_col2;
   clear_col3;
   set_col4;
   if (check_row1_low)     key = SLEEP_KEY;   
   if (check_row2_low)     key = SBASS_KEY; 
   if (check_row3_low)     key = CLOCK_KEY;  
   if (check_row4_low)     key = VOLUME_DOWN_KEY;         

   set_col1;
   set_col2;
   set_col3;
   clear_col4;
   if (check_row1_low)     key = POWER_KEY;
   if (check_row2_low)     key = FUNCTION_KEY;
   if (check_row3_low)     key = TONE_KEY;  
   if (check_row4_low)     key = VOLUME_UP_KEY;

}

/*====================================================================================
                                   處理面板按鍵程式
=====================================================================================*/
unsigned char shake_cancel_timer;
unsigned char complex_key_timer;
unsigned char repeat_key_timer;
unsigned char scan_key_state;
unsigned char key_bak;
unsigned char repeat_key;

void process_panel_key()
{
  scan_key();
  switch (scan_key_state)
  {
    case KEY_IDLE_STATE:   //按鍵空閑狀態
       if (key!=NO_KEY)  //如果有按鍵按下,則進入按鍵消抖狀態。
     {
         key_bak = key;          
         shake_cancel_timer = 1;  //消抖時間設定為20ms
         scan_key_state = KEY_SHAKE_STATE;
       }else            //如果沒有按鍵按下,繼續為空白閑檢測狀態。
       {
         scan_key_state = KEY_IDLE_STATE;
       }
     break;
    case KEY_SHAKE_STATE: //按鍵去抖狀態
      if (!shake_cancel_timer)
      {
         if (key == key_bak)     //消抖後,如果按鍵未變,進入複合檢測狀態。
         {
            scan_key_state = KEY_COMPLEX_STATE;
            complex_key_timer = 100;  //複合判斷時間為200ms 小於200ms為原功能,大於200ms為複合功能。
         }else                  //確認為按鍵抖動。
         {
            scan_key_state = KEY_IDLE_STATE;
         }
      }
     break;
   case KEY_COMPLEX_STATE: //按鍵複合處理狀態
      if (!complex_key_timer)
      {
        if (key == key_bak)  //複合檢測時間到,按鍵仍按下。
        {
            if (key == SEARCH_UP_KEY)       //執行next的複合功能。
            {
              current_key = AUTO_SEARCH_UP_KEY;
            }else if (key == SEARCH_DOWN_KEY) //執行prev的複合功能。
            {
              current_key = AUTO_SEARCH_DOWN_KEY;
            }else                     //執行沒有複合功能的按鍵。
            {
              current_key = key;
            }

            if (is_repeat_key(key))   //檢測為可重複按鍵時,進入重複狀態。
           {
             repeat_key = current_key;
             scan_key_state = KEY_REPEAT_STATE;
             repeat_key_timer = 20; // 按鍵重複時間為200ms,如果連續按鍵,每200ms執行重複按鍵動作。
            }else                    //檢測為不可重複按鍵時,進入按鍵抬起等待狀態。
            {
             scan_key_state = KEY_WAIT_UP;
            }
         }else       //按鍵已經抬起,執行未複合功能。
         {
           current_key = key_bak;
           scan_key_state = KEY_IDLE_STATE;
         }
       }else         
       {
          if (key != key_bak)          //按鍵抬起,執行未複合功能。
          {
             current_key = key_bak;
             scan_key_state = KEY_IDLE_STATE;
           }
       }
      break;
    case KEY_REPEAT_STATE:    //按鍵重複處理狀態
      if (key == key_bak)
      {
        if (!repeat_key_timer)    //按鍵重複時間到,再次執行按鍵功能。
        {
           repeat_key_timer = 20; // 按鍵重複時間為200ms,如果連續按鍵,每200ms執行重複按鍵動作。
           current_key = repeat_key;
        }
      }else                     //按鍵抬起,回到按鍵空閑檢測狀態。
      {
           scan_key_state = KEY_IDLE_STATE;
      }
     break;
   case KEY_WAIT_UP:             //按鍵等待抬起狀態
     if (key!= key_bak)
     {
        scan_key_state = KEY_IDLE_STATE;   //按鍵抬起,返回按鍵空閑檢測狀態。
     }
    break;
  }

}
/*------------------------------------------------------------------------------------------------*/
unsigned char is_repeat_key(unsigned char r_key)    //重複按鍵檢測通行函數。
{
   if ((r_key == VOLUME_UP_KEY)||(r_key == VOLUME_DOWN_KEY)||(r_key == SEARCH_UP_KEY)||(r_key == SEARCH_DOWN_KEY))
   {
       return 1;
   }else
   {
       return 0;
   }
}
/*-----------------------------------------------------------------------------------------------*/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.