修改官方库,使支持长按事件

This commit is contained in:
YuXifeng 2025-04-02 13:56:11 +08:00
parent e9ffa15945
commit 075c626788

View File

@ -28,6 +28,7 @@ typedef struct {
bool btn_prev; /* Button prev state */
bool btn_next; /* Button next state */
bool btn_enter; /* Button enter state */
bool btn_longhold;
} lvgl_port_nav_btns_ctx_t;
/*******************************************************************************
@ -37,6 +38,7 @@ typedef struct {
static void lvgl_port_navigation_buttons_read(lv_indev_drv_t *indev_drv, lv_indev_data_t *data);
static void lvgl_port_btn_down_handler(void *arg, void *arg2);
static void lvgl_port_btn_up_handler(void *arg, void *arg2);
static void lvgl_port_btn_longhold_handler(void *arg, void *arg2);
/*******************************************************************************
* Public API functions
@ -90,12 +92,14 @@ lv_indev_t *lvgl_port_add_navigation_buttons(const lvgl_port_nav_btns_cfg_t *but
#else
ESP_ERROR_CHECK(iot_button_register_cb(buttons_ctx->btn[i], BUTTON_PRESS_UP, NULL, lvgl_port_btn_down_handler, buttons_ctx));
ESP_ERROR_CHECK(iot_button_register_cb(buttons_ctx->btn[i], BUTTON_PRESS_END, NULL, lvgl_port_btn_up_handler, buttons_ctx));
ESP_ERROR_CHECK(iot_button_register_cb(buttons_ctx->btn[i], BUTTON_LONG_PRESS_HOLD, NULL, lvgl_port_btn_longhold_handler, buttons_ctx));
#endif
}
buttons_ctx->btn_prev = false;
buttons_ctx->btn_next = false;
buttons_ctx->btn_enter = false;
buttons_ctx->btn_longhold = false;
/* Register a touchpad input device */
lv_indev_drv_init(&buttons_ctx->indev_drv);
@ -150,8 +154,20 @@ static void lvgl_port_navigation_buttons_read(lv_indev_drv_t *indev_drv, lv_inde
lvgl_port_nav_btns_ctx_t *ctx = (lvgl_port_nav_btns_ctx_t *)indev_drv->user_data;
assert(ctx);
/* Buttons */
if (ctx->btn_prev) {
/* 长按处理 */
if (ctx->btn_longhold) {
data->key = LV_KEY_ESC;
last_key = LV_KEY_ESC;
data->state = LV_INDEV_STATE_PRESSED;
ctx->btn_longhold = false; // 读取后清除,避免一直触发
lv_obj_t *focused_obj = lv_group_get_focused(lv_group_get_default()); // 获取当前焦点对象
if (!focused_obj) return;
// printf("检测到长按,发送 LV_KEY_ESC\n");
lv_event_send(focused_obj, LV_EVENT_VALUE_CHANGED, (void *)LV_KEY_ESC);
}
/* 普通按键处理 */
else if (ctx->btn_prev) {
data->key = LV_KEY_LEFT;
last_key = LV_KEY_LEFT;
data->state = LV_INDEV_STATE_PRESSED;
@ -169,6 +185,7 @@ static void lvgl_port_navigation_buttons_read(lv_indev_drv_t *indev_drv, lv_inde
}
}
static void lvgl_port_btn_down_handler(void *arg, void *arg2)
{
lvgl_port_nav_btns_ctx_t *ctx = (lvgl_port_nav_btns_ctx_t *) arg2;
@ -210,3 +227,28 @@ static void lvgl_port_btn_up_handler(void *arg, void *arg2)
}
}
}
static void lvgl_port_btn_longhold_handler(void *arg, void *arg2)
{
lvgl_port_nav_btns_ctx_t *ctx = (lvgl_port_nav_btns_ctx_t *) arg2;
button_handle_t button = (button_handle_t)arg;
if (ctx && button) {
// 调试输出按钮事件
iot_button_print_event(button);
/* PREV 按钮长按 */
if (button == ctx->btn[LVGL_PORT_NAV_BTN_PREV]) {
ctx->btn_longhold = true;
}
/* NEXT 按钮长按 */
else if (button == ctx->btn[LVGL_PORT_NAV_BTN_NEXT]) {
ctx->btn_longhold = true;
}
/* ENTER 按钮长按 */
else if (button == ctx->btn[LVGL_PORT_NAV_BTN_ENTER]) {
ctx->btn_longhold = true;
}
}
}