資源簡介
MultiButton 是一個小巧簡單易用的事件驅動型按鍵驅動模塊,可無限量擴展按鍵,按鍵事件的回調異步處理方式可以簡化你的程序結構,去除冗余的按鍵處理硬編碼,讓你的按鍵業務邏輯更清晰。
【使用方法】
1.先申請一個按鍵結構
- struct Button button1;
2.初始化按鍵對象,綁定按鍵的GPIO電平讀取接口read_button_pin() ,后一個參數設置有效觸發電平
- button_init(&button1, read_button_pin, 0);
3.注冊按鍵事件
- button_attach(&button1, SINGLE_CLICK, Callback_SINGLE_CLICK_Handler);
- button_attach(&button1, DOUBLE_CLICK, Callback_DOUBLE_Click_Handler);
- ...
4.啟動按鍵
- button_start(&button1);
5.設置一個5ms間隔的定時器循環調用后臺處理函數
- while(1) {
- ...
- if(timer_ticks == 5) {
- timer_ticks = 0;
- button_ticks();
- }
- }
特性
MultiButton 使用C語言實現,基于面向對象方式設計思路,每個按鍵對象單獨用一份數據結構管理:
- struct Button {
- uint16_t ticks;
- uint8_t repeat: 4;
- uint8_t event : 4;
- uint8_t state : 3;
- uint8_t debounce_cnt : 3;
- uint8_t active_level : 1;
- uint8_t button_level : 1;
- uint8_t (*hal_button_Level)(void);
- BtnCallback cb[number_of_event];
- struct Button* next;
- };
這樣每個按鍵使用單向鏈表相連,依次進入 button_handler(struct Button* handle) 狀態機處理,所以每個按鍵的狀態彼此獨立。
按鍵事件
事件 | 說明 |
---|---|
PRESS_DOWN | 按鍵按下,每次按下都觸發 |
PRESS_UP | 按鍵彈起,每次松開都觸發 |
PRESS_REPEAT | 重復按下觸發,變量repeat計數連擊次數 |
SINGLE_CLICK | 單擊按鍵事件 |
DOUBLE_CLICK | 雙擊按鍵事件 |
LONG_RRESS_START | 達到長按時間閾值時觸發一次 |
LONG_PRESS_HOLD | 長按期間一直觸發 |
Examples
- #include "button.h"
- struct Button btn1;
- int read_button1_GPIO()
- {
- return HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin);
- }
- int main()
- {
- button_init(&btn1, read_button1_GPIO, 0);
- button_attach(&btn1, PRESS_DOWN, BTN1_PRESS_DOWN_Handler);
- button_attach(&btn1, PRESS_UP, BTN1_PRESS_UP_Handler);
- button_attach(&btn1, PRESS_REPEAT, BTN1_PRESS_REPEAT_Handler);
- button_attach(&btn1, SINGLE_CLICK, BTN1_SINGLE_Click_Handler);
- button_attach(&btn1, DOUBLE_CLICK, BTN1_DOUBLE_Click_Handler);
- button_attach(&btn1, LONG_RRESS_START, BTN1_LONG_RRESS_START_Handler);
- button_attach(&btn2, LONG_PRESS_HOLD, BTN1_LONG_PRESS_HOLD_Handler);
- button_start(&btn1);
- //make the timer invoking the button_ticks() interval 5ms.
- //This function is implemented by yourself.
- __timer_start(button_ticks, 0, 5);
- while(1)
- {}
- }
- void BTN1_PRESS_DOWN_Handler(void* btn)
- {
- //do something...
- }
- void BTN1_PRESS_UP_Handler(void* btn)
- {
- //do something...
- }
- ...
代碼片段和文件信息
/*
?*?Copyright?(c)?2016?Zibin?Zheng?
?*?All?rights?reserved
?*/
#include?“multi_button.h“
#define?EVENT_CB(ev)???if(handle->cb[ev])handle->cb[ev]((Button*)handle)
//button?handle?list?head.
static?struct?Button*?head_handle?=?NULL;
/**
??*?@brief??Initializes?the?button?struct?handle.
??*?@param??handle:?the?button?handle?strcut.
??*?@param??pin_level:?read?the?HAL?GPIO?of?the?connet?button?level.
??*?@param??active_level:?pressed?GPIO?level.
??*?@retval?None
??*/
void?button_init(struct?Button*?handle?uint8_t(*pin_level)()?uint8_t?active_level)
{
memset(handle?0?sizeof(struct?Button));
handle->event?=?(uint8_t)NONE_PRESS;
handle->hal_button_Level?=?pin_level;
handle->button_level?=?handle->hal_button_Level();
handle->active_level?=?active_level;
}
/**
??*?@br
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2019-12-16?01:46??MultiButton-master\
?????文件????????1068??2019-12-16?01:46??MultiButton-master\LICENSE
?????文件????????2904??2019-12-16?01:46??MultiButton-master\README.md
?????目錄???????????0??2019-12-16?01:46??MultiButton-master\examples\
?????文件????????1628??2019-12-16?01:46??MultiButton-master\examples\example_callback.c
?????文件?????????705??2019-12-16?01:46??MultiButton-master\examples\example_poll.c
?????文件????????4892??2019-12-16?01:46??MultiButton-master\multi_button.c
?????文件????????1298??2019-12-16?01:46??MultiButton-master\multi_button.h
- 上一篇:92-1602液晶靜態顯示.zip
- 下一篇:c++ 推箱子源碼
評論
共有 條評論