UP 优化key库

main
JiXieShi 2024-06-23 14:14:35 +08:00
parent 8bf40f0d9c
commit 58afcd9d81
3 changed files with 16 additions and 21 deletions

View File

@ -77,21 +77,14 @@ void Get_Key(void *pVoid) {
key_init(&k6, SIM_Key_SET, 1, SIM_KEY_SET); key_init(&k6, SIM_Key_SET, 1, SIM_KEY_SET);
key_init(&k7, SIM_Key_RESET, 1, SIM_KEY_RESET); key_init(&k7, SIM_Key_RESET, 1, SIM_KEY_RESET);
key_attach(&k1, 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); key_attach(&k2, KEY_ALL_EVENT, Key_Call_E, true);
key_attach(&k3, KEY_ALL_EVENT, Key_Call_E); key_attach(&k3, KEY_ALL_EVENT, Key_Call_E, true);
key_attach(&k4, KEY_ALL_EVENT, Key_Call_E); key_attach(&k4, KEY_ALL_EVENT, Key_Call_E, true);
key_attach(&k5, KEY_ALL_EVENT, Key_Call_E); key_attach(&k5, KEY_ALL_EVENT, Key_Call_E, true);
key_attach(&k6, KEY_ALL_EVENT, Key_Call_E); key_attach(&k6, KEY_ALL_EVENT, Key_Call_E, true);
key_attach(&k7, KEY_ALL_EVENT, Key_Call_E); 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) { while (1) {
// 每5ms调用一次key_ticks函数 // 每5ms调用一次key_ticks函数
key_ticks(); key_ticks();

View File

@ -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); void key_init(Key_t *key, uint8_t(*pin_level)(uint8_t), uint8_t active_level, uint8_t key_id);
/** /**
* @brief * @brief
* @param key: [] * @param key: []
* @param event: [] * @param event: []
* @param cb: [] * @param cb: []
* @param start: []
* @return void * @return void
* @example key_attach(&my_key, PRESS_DOWN, key_press_callback); * @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); void key_attach(Key_t *key, PressEvent event, Key_Callback_t cb, bool start);
/** /**
* @brief * @brief

View File

@ -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分别赋值给所有事件类型 // 如果事件类型为ALL_EVENT则将回调函数cb分别赋值给所有事件类型
if (event == KEY_ALL_EVENT) { if (event == KEY_ALL_EVENT) {
for (uint8_t i = KEY_PRESS_UP; i < number_of_event; i++) { 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赋值给指定的事件类型 // 否则将回调函数cb赋值给指定的事件类型
key->cb[event] = cb; key->cb[event] = cb;
} }
if (start)key_start(key);
} }
PressEvent get_key_event(Key_t *key) { PressEvent get_key_event(Key_t *key) {