之前的文章中介紹過按鍵的處理方法《多功能(單擊、輕按兩下、長按)按鍵設計》,今天再來分享另外一種方式:MultiButton。
1.Multi_Button簡介
MultiButton 是Github上的一個開源的按鍵處理元件,作者0x1abin。
GIthub位址:https://github.com/0x1abin/MultiButton
MultiButton一個小巧簡單易用的事件驅動型按鍵驅動子產品,可無限量擴充按鍵,按鍵事件的回調異步處理方式可以簡化你的程式結構,去除備援的按鍵處理寫死,讓你的按鍵業務邏輯更清晰。
按鍵支援的事件包括:
2.Multi_Button的使用
子產品隻有兩個檔案multi_button.c和multi_button.h,使用時,将.c檔案添加到工程中。這裡以STM32為例。
2.1.包含頭檔案#include"multi_button.h"
2.2.定義按鍵結構體和按鍵ID,這裡定義了2個按鍵:
Button button1;
Button button2;
#define btn1_id 1
#define btn2_id 2
2.3.編寫一個讀取按鍵GPIO電平的函數:
uint8_t read_button_GPIO(uint8_t button_id)
{
// you can share the GPIO read function with multiple Buttons
switch(button_id)
{
case btn1_id:
return HAL_GPIO_ReadPin(KEY0_GPIO_Port, KEY0_Pin);
break;
case btn2_id:
return HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin);
break;
default:
return 0;
break;
}
}
2.4.初始化按鍵對象:
button_init(&button1, read_button_GPIO, 0, btn1_id);
button_init(&button2, read_button_GPIO, 0, btn2_id);
在button_init函數中:
第一個參數為2.2中定義的按鍵結構體指針。
第二個參數為綁定的2.3中編寫的讀取按鍵GPIO電平的函數。
第三個參數為按鍵的有效電平,0代表低電平有效,1代表高電平有效。
第四個參數為按鍵ID。
2.5.綁定按鍵回調函數:
button_attach(&button1, PRESS_DOWN, BTN_PRESS_DOWN_Handler);
button_attach(&button1, PRESS_UP, BTN_PRESS_UP_Handler);
button_attach(&button1, PRESS_REPEAT, BTN_PRESS_REPEAT_Handler);
button_attach(&button1, SINGLE_CLICK, BTN_SINGLE_Click_Handler);
button_attach(&button1, DOUBLE_CLICK, BTN_DOUBLE_Click_Handler);
button_attach(&button1, LONG_PRESS_START, BTN_LONG_PRESS_START_Handler);
button_attach(&button1, LONG_PRESS_HOLD, BTN_LONG_PRESS_HOLD_Handler);
button_attach(&button2, PRESS_DOWN, BTN_PRESS_DOWN_Handler);
button_attach(&button2, PRESS_UP, BTN_PRESS_UP_Handler);
button_attach(&button2, PRESS_REPEAT, BTN_PRESS_REPEAT_Handler);
button_attach(&button2, SINGLE_CLICK, BTN_SINGLE_Click_Handler);
button_attach(&button2, DOUBLE_CLICK, BTN_DOUBLE_Click_Handler);
button_attach(&button2, LONG_PRESS_START, BTN_LONG_PRESS_START_Handler);
button_attach(&button2, LONG_PRESS_HOLD, BTN_LONG_PRESS_HOLD_Handler);
這裡綁定了所有的按鍵功能,使用者可根據實際需求進行删減。不同的按鍵回調函數可以相同,以按鍵按下的回調函數為例,函數如下:
void BTN_PRESS_DOWN_Handler(void* btn)
{
Button *temp_button = (Button *)btn;
switch(temp_button->button_id)
{
case btn1_id:
printf("btn1 press down\r\n");
break;
case btn2_id:
printf("btn2 press down\r\n");
break;
default:
break;
}
}
2.6.調用啟動函數:
button_start(&button1);
button_start(&button2);
2.7.最後,需要在一個定時任務中循環調用按鍵掃描函數:
void scan_key()
{
if(key_tick < TICKS_INTERVAL)return;
key_tick = 0;
button_ticks();
}
需要注意的是,按鍵的掃描周期、長按、短按、輕按兩下的時間定義可以在.h檔案中修改:
//According to your need to modify the constants.
#define TICKS_INTERVAL 10 //ms
#define DEBOUNCE_TICKS 3 //MAX 8
#define SHORT_TICKS (300 /TICKS_INTERVAL)
#define LONG_TICKS (1000 /TICKS_INTERVAL)
最後來測試一下效果,按鍵的按下、彈起、單擊、輕按兩下、長按等都能被檢測到,用起來還是挺友善的。
這個開源按鍵子產品的源代碼隻有200行左右,有興趣的朋友可以自己研究一下。
測試工程連結:
連結:https://pan.baidu.com/s/1wiw5Ajoooc7WZFgwO87zqw
提取碼:wola