From 0b8875b90a8abd6abcc438ad4c4d8409c6937037 Mon Sep 17 00:00:00 2001 From: Sheikah <> Date: Fri, 27 Jan 2023 22:37:42 +0800 Subject: [PATCH] add click and long_press event --- Core/App/Graphic/ui.h | 1 + Core/App/Input/keypad.c | 23 +++++++++++++++++++++++ Core/App/Input/keypad.h | 4 ++++ Core/App/app.c | 5 +++-- 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/Core/App/Graphic/ui.h b/Core/App/Graphic/ui.h index 8ff14f9..e44d51f 100644 --- a/Core/App/Graphic/ui.h +++ b/Core/App/Graphic/ui.h @@ -4,6 +4,7 @@ void ui_text_number18x32(uint32_t num, uint16_t x, uint16_t y, uint16_t color); /* 绘制界面元素 */ +void ui_com_title_bar(uint8_t clear, uint8_t *text, uint32_t bytes_len); void ui_com_freq_digital(uint8_t clear); void ui_com_fm_am(uint8_t clear); void ui_com_vol_bar(uint8_t clear); diff --git a/Core/App/Input/keypad.c b/Core/App/Input/keypad.c index c9e9c9b..48500a5 100644 --- a/Core/App/Input/keypad.c +++ b/Core/App/Input/keypad.c @@ -2,6 +2,7 @@ #include "keypad.h" #include "gpio.h" #include "RotaryCoder.h" +#include "time.h" static uint8_t key_status = 0; static uint16_t encoder_value = 0; @@ -32,6 +33,15 @@ static uint8_t keys[] = { kp_KEY6, kp_KEYENCODER }; +static int32_t pressed_at[] = { + -1, + -1, + -1, + -1, + -1, + -1, + -1 +}; #define K_GET(x) (((0b1 << x) & key_status) >> x) #define K_SET0(x) (key_status = (~(0b1 << x)) & key_status) @@ -41,6 +51,7 @@ static uint8_t keys[] = { uint8_t kp_query() { // Scan keypad + int32_t now = ticks_ms(); uint16_t rota_value = EC11_COUNT(); uint16_t differ = rota_value - encoder_value; uint8_t direction = kp_ROTATE_LEFT; @@ -61,11 +72,23 @@ uint8_t kp_query() { mstate = K_GET(key_n); if (state && (!mstate)) { K_SET1(key_n); + pressed_at[key_n] = now; return K_EVENT(kp_KEY_DOWN, keys[key_n]); } else if ((!state) && mstate) { + if (pressed_at[key_n] >= 0) { + pressed_at[key_n] = -1; + return K_EVENT(kp_SHORT_CLICK, keys[key_n]); + } K_SET0(key_n); return K_EVENT(kp_KEY_UP, keys[key_n]); } + // ticks pressed time + if (pressed_at[key_n] >= 0 && state) { + if (ticks_diff(now, pressed_at[key_n]) > kp_LONG_PRESS_TIMEOUT_MS) { + pressed_at[key_n] = -1; + return K_EVENT(kp_LONG_PRESS, keys[key_n]); + } + } } return K_EVENT(kp_NOP, 0); } diff --git a/Core/App/Input/keypad.h b/Core/App/Input/keypad.h index 796b790..f8787c5 100644 --- a/Core/App/Input/keypad.h +++ b/Core/App/Input/keypad.h @@ -5,6 +5,8 @@ #define kp_KEY_UP 0x2 #define kp_ROTATE_LEFT 0x3 #define kp_ROTATE_RIGHT 0x4 +#define kp_SHORT_CLICK 0x5 +#define kp_LONG_PRESS 0x6 #define kp_KEY1 0x0 #define kp_KEY2 0x1 @@ -17,5 +19,7 @@ #define kp_Type(x) (x & 0b1111) #define kp_Value(x) (x >> 4) +#define kp_LONG_PRESS_TIMEOUT_MS 250 + // query and return keypad event uint8_t kp_query(); diff --git a/Core/App/app.c b/Core/App/app.c index fa3b4bd..ee056d7 100644 --- a/Core/App/app.c +++ b/Core/App/app.c @@ -3,7 +3,6 @@ #include "ui.h" #include "keypad.h" #include "global.h" -#include "time.h" void app_init() { // 程序开始时执行一次 @@ -33,7 +32,7 @@ void app_main_loop() { } printf("Freq: %d\n", global_data.freq); ui_com_freq_digital(1); - } else if (event_type == kp_KEY_DOWN) { + } else if (event_type == kp_SHORT_CLICK) { if (event_value == kp_KEY1) { // 切换FM和AM if (global_data.rf_mode == G_RF_MODE_AM) { @@ -46,6 +45,8 @@ void app_main_loop() { ui_com_fm_am(1); ui_com_freq_digital(1); } + } else if (event_type == kp_LONG_PRESS) { + // } else if (event_type != kp_NOP) { printf("event: %d, key: %d\n", event_type, event_value); }