From 58afcd9d81b30b2ccc8ec3dcc7f460c0b5229a39 Mon Sep 17 00:00:00 2001 From: JiXieShi Date: Sun, 23 Jun 2024 14:14:35 +0800 Subject: [PATCH] =?UTF-8?q?UP=20=E4=BC=98=E5=8C=96key=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo/oled/test.c | 21 +++++++-------------- lib/inc/key/key.h | 13 +++++++------ lib/src/key/key.cpp | 3 ++- 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/demo/oled/test.c b/demo/oled/test.c index 54a70e2..9d75329 100644 --- a/demo/oled/test.c +++ b/demo/oled/test.c @@ -77,21 +77,14 @@ void Get_Key(void *pVoid) { key_init(&k6, SIM_Key_SET, 1, SIM_KEY_SET); key_init(&k7, SIM_Key_RESET, 1, SIM_KEY_RESET); - key_attach(&k1, KEY_ALL_EVENT, Key_Call_E); - key_attach(&k2, KEY_ALL_EVENT, Key_Call_E); - key_attach(&k3, KEY_ALL_EVENT, Key_Call_E); - key_attach(&k4, KEY_ALL_EVENT, Key_Call_E); - key_attach(&k5, KEY_ALL_EVENT, Key_Call_E); - key_attach(&k6, KEY_ALL_EVENT, Key_Call_E); - key_attach(&k7, KEY_ALL_EVENT, Key_Call_E); + key_attach(&k1, KEY_ALL_EVENT, Key_Call_E, true); + key_attach(&k2, KEY_ALL_EVENT, Key_Call_E, true); + key_attach(&k3, KEY_ALL_EVENT, Key_Call_E, true); + key_attach(&k4, KEY_ALL_EVENT, Key_Call_E, true); + key_attach(&k5, KEY_ALL_EVENT, Key_Call_E, true); + key_attach(&k6, KEY_ALL_EVENT, Key_Call_E, true); + key_attach(&k7, KEY_ALL_EVENT, Key_Call_E, true); - key_start(&k1); - key_start(&k2); - key_start(&k3); - key_start(&k4); - key_start(&k5); - key_start(&k6); - key_start(&k7); while (1) { // 每5ms调用一次key_ticks函数 key_ticks(); diff --git a/lib/inc/key/key.h b/lib/inc/key/key.h index a9dd2c0..1c5d73b 100644 --- a/lib/inc/key/key.h +++ b/lib/inc/key/key.h @@ -56,14 +56,15 @@ struct Key_List { void key_init(Key_t *key, uint8_t(*pin_level)(uint8_t), uint8_t active_level, uint8_t key_id); /** - * @brief 绑定按键事件回调函数 - * @param key: [输入] 指向按键结构体的指针 + * @brief 为按键设置回调函数 + * @param key: [输入] 按键结构体指针 * @param event: [输入] 按键事件类型 - * @param cb: [输入] 按键事件回调函数 + * @param cb: [输入] 回调函数指针 + * @param start: [输入] 是否立即启用 * @return void - * @example key_attach(&my_key, PRESS_DOWN, key_press_callback); - */ -void key_attach(Key_t *key, PressEvent event, Key_Callback_t cb); + * @example key_attach(&my_key, PRESS_EVENT_LONG_PRESS, key_callback_func, true); +**/ +void key_attach(Key_t *key, PressEvent event, Key_Callback_t cb, bool start); /** * @brief 获取按键当前事件 diff --git a/lib/src/key/key.cpp b/lib/src/key/key.cpp index 5bf21b6..5338bb1 100644 --- a/lib/src/key/key.cpp +++ b/lib/src/key/key.cpp @@ -21,7 +21,7 @@ void key_init(Key_t *key, uint8_t(*pin_level)(uint8_t), uint8_t active_level, ui } -void key_attach(Key_t *key, PressEvent event, Key_Callback_t cb) { +void key_attach(Key_t *key, PressEvent event, Key_Callback_t cb, bool start) { // 如果事件类型为ALL_EVENT,则将回调函数cb分别赋值给所有事件类型 if (event == KEY_ALL_EVENT) { for (uint8_t i = KEY_PRESS_UP; i < number_of_event; i++) { @@ -31,6 +31,7 @@ void key_attach(Key_t *key, PressEvent event, Key_Callback_t cb) { // 否则,将回调函数cb赋值给指定的事件类型 key->cb[event] = cb; } + if (start)key_start(key); } PressEvent get_key_event(Key_t *key) {