EmbeddedButton
簡(jiǎn)介
EmbeddedButton是一個(gè)輕量級(jí)簡(jiǎn)單易用的嵌入式按鍵驅(qū)動(dòng)模塊,可無(wú)限拓展按鍵,支持多連擊、長(zhǎng)按、短按長(zhǎng)按等多種按鍵事件;該模塊通過(guò)異步回調(diào)方式來(lái)簡(jiǎn)化程序結(jié)構(gòu),根據(jù)幾個(gè)簡(jiǎn)單原則完成了整個(gè)代碼邏輯的支撐。
使用方法
1.定義按鍵實(shí)體
struct button_obj_t button1;
2.建立鍵值映射表(設(shè)置回調(diào)事件)
const key_value_map_t button1_map[] =
{
{
.key_value = SINGLE_CLICK_KV,
.kv_func_cb = single_press_handle
},
{
.key_value = LONG_PRESEE_START,
.kv_func_cb = long_press_handle
},
{
.key_value = SINGLE_CLICK_THEN_LONG_PRESS_KV,
.kv_func_cb = single_press_then_long_press_handle
},
};
3.初始化按鍵對(duì)象,參數(shù)含義分別為
按鍵實(shí)體
綁定按鍵的GPIO電平讀取接口read_button1_pin()
設(shè)置有效觸發(fā)電平
按鍵ID
鍵值映射表
鍵值映射表大小
button_init(&button1, read_button1_pin, 0, 0, button1_map, ARRAY_SIZE(button1_map));
4.啟動(dòng)按鍵
button_start(&button1);
5.設(shè)置一個(gè)5ms間隔的定時(shí)器循環(huán)調(diào)用按鍵后臺(tái)處理函數(shù)
while(1) {
...
if(timer_ticks == 5) {
timer_ticks = 0;
button_ticks();
}
}
特性
1.依靠簡(jiǎn)單幾個(gè)原則,支持起整個(gè)按鍵判斷邏輯
只要鍵值非零,時(shí)間tick++
只要按鍵狀態(tài)發(fā)生變化,改變一次鍵值(__append_bit()),tick時(shí)間清零(確保tick為按下或抬起的時(shí)間)
以tick時(shí)間的長(zhǎng)短及按鍵抬起作為一次狀態(tài)結(jié)束的判斷依據(jù),可以很好的實(shí)現(xiàn)短按長(zhǎng)按等操作;
2.使用C語(yǔ)言實(shí)現(xiàn),巧妙利用位運(yùn)算來(lái)實(shí)現(xiàn)每個(gè)按鍵鍵值的二進(jìn)制記錄表示,1代表按下,0代表松開(kāi)
3.利用數(shù)據(jù)驅(qū)動(dòng)思想完成對(duì)應(yīng)按鍵事件的調(diào)用:
typedef struct {
key_value_type_t key_value;
void ( kv_func_cb)(void );
} key_value_map_t;
const key_value_map_t button1_map[] =
{
{
.key_value = SINGLE_CLICK_KV,
.kv_func_cb = single_press_handle
},
{
.key_value = LONG_PRESEE_START,
.kv_func_cb = long_press_handle
},
{
.key_value = SINGLE_CLICK_THEN_LONG_PRESS_KV,
.kv_func_cb = single_press_then_long_press_handle
},
};
for(size_t i = 0; i < button->map_size; i++) {
if((button->map_ptr[i].key_value == button->key_value)
&& (button->map_ptr[i].kv_func_cb))
{
button->map_ptr[i].kv_func_cb(button);
}
}
4.基于面向?qū)ο蠓绞皆O(shè)計(jì)思路,每個(gè)按鍵對(duì)象單獨(dú)用一份數(shù)據(jù)結(jié)構(gòu)管理:
typedef struct button_obj_t {
uint8_t debounce_cnt : 4;
uint8_t active_level : 1;
uint8_t read_level : 1;
uint8_t read_level_update : 1;
uint8_t event_analyze_en : 1;
uint8_t id;
uint16_t ticks;
state_bits_type_t state_bits;
key_value_type_t key_value;
uint8_t (* read_button_func_ptr)(uint8_t button_id );
const key_value_map_t map_ptr;
size_t map_size;
struct button_obj_t next;
}button_obj_t;
Examples
#include "embedded_button.h"
struct button_obj_t button1;
uint8_t read_button_pin(uint8_t button_id)
{
// you can share the GPIO read function with multiple Buttons
switch(button_id)
{
case 0:
return get_button1_value(); //Require self implementation
break;
default:
return 0;
break;
}
return 0;
}
void single_click_handle(void* btn)
{
//do something...
printf("/ single click /rn");
}
void double_click_handle(void* btn)
{
//do something...
printf("/ double click /rn");
}
void long_press_handle(void* btn)
{
//do something...
printf("/ long press /rn");
}
void single_click_then_long_press_handle(void* btn)
{
//do something...
printf("/ single click and long press /rn");
}
void double_click_then_long_press_handle(void* btn)
{
//do something...
printf("/ double click and long press /rn");
}
const key_value_map_t button1_map[] =
{
{
.key_value = SINGLE_CLICK_KV,
.kv_func_cb = single_click_handle
},
{
.key_value = DOUBLE_CLICK_KV,
.kv_func_cb = double_click_handle
},
{
.key_value = LONG_PRESEE_START,
.kv_func_cb = long_press_handle
},
{
.key_value = SINGLE_CLICK_THEN_LONG_PRESS_KV,
.kv_func_cb = single_click_then_long_press_handle
},
{
.key_value = DOUBLE_CLICK_THEN_LONG_PRESS_KV,
.kv_func_cb = double_click_then_long_press_handle
}
};
...
int main()
{
button_init(&button1, read_button_pin, 0, 0, button1_map, ARRAY_SIZE(button1_map));
button_start(&button1);
//make the timer invoking the button_ticks() interval 5ms.
//This function is implemented by yourself.
__timer_start(button_ticks, 0, 5);
while(1)
{}
}
-
嵌入式系統(tǒng)
+關(guān)注
關(guān)注
41文章
3568瀏覽量
129234 -
定時(shí)器
+關(guān)注
關(guān)注
23文章
3240瀏覽量
114477 -
狀態(tài)機(jī)
+關(guān)注
關(guān)注
2文章
492瀏覽量
27478 -
GPIO
+關(guān)注
關(guān)注
16文章
1196瀏覽量
51919 -
按鍵驅(qū)動(dòng)
+關(guān)注
關(guān)注
0文章
11瀏覽量
7126
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論