feat: 添加启动按钮功能与 UI 事件处理

- 在 CMakeLists.txt 中添加 boot_button.c 源文件以支持启动按钮功能
- 新增 ui_handlers.c 和 ui_handlers.h,处理 UI 事件并初始化启动按钮
- 实现 boot_button.c,配置 GPIO 中断和任务以响应启动按钮按下事件
- 更新 board_config.h,定义启动按钮的 GPIO 引脚
This commit is contained in:
2026-07-01 22:10:11 +08:00
parent 73e62c4dfd
commit ebe62c5284
7 changed files with 109 additions and 0 deletions
+1
View File
@@ -4,6 +4,7 @@ idf_component_register(SRCS
"bsp/display.c"
"bsp/touch.c"
"bsp/lvgl_port.c"
"bsp/boot_button.c"
"services/wifi_manager.c"
"ui/ui.c"
"ui/ui_helpers.c"
+3
View File
@@ -6,6 +6,7 @@
#include "lvgl_port.h"
#include "touch.h"
#include "ui.h"
#include "ui_handlers.h"
#include "wifi_manager.h"
static const char *TAG = "app";
@@ -35,6 +36,8 @@ void app_main(void)
ui_init();
lvgl_port_unlock();
ui_handlers_init();
ESP_LOGI(TAG, "Starting WiFi");
wifi_manager_init();
}
+25
View File
@@ -1,6 +1,31 @@
#include "ui_handlers.h"
#include <stdbool.h>
#include "boot_button.h"
#include "lvgl_port.h"
#include "ui.h"
#include "ui_events.h"
static bool s_showing_screen1;
static void ui_toggle_screen(void)
{
lvgl_port_lock();
if (s_showing_screen1) {
_ui_screen_change(&ui_Screen2, LV_SCR_LOAD_ANIM_FADE_ON, 200, 0, ui_Screen2_screen_init);
} else {
_ui_screen_change(&ui_Screen1, LV_SCR_LOAD_ANIM_FADE_ON, 200, 0, ui_Screen1_screen_init);
}
s_showing_screen1 = !s_showing_screen1;
lvgl_port_unlock();
}
void ui_handlers_init(void)
{
boot_button_init(ui_toggle_screen);
}
void update_year_action(lv_event_t * e)
{
(void)e;
+1
View File
@@ -1,3 +1,4 @@
#pragma once
/* UI event callbacks declared in ui_events.h are implemented in ui_handlers.c */
void ui_handlers_init(void);
+1
View File
@@ -41,6 +41,7 @@
#define BOARD_TOUCH_ENABLED 1
/* User IO */
#define BOARD_PIN_BOOT 0
#define BOARD_PIN_KEY 4
#define BOARD_PIN_LED 16
+73
View File
@@ -0,0 +1,73 @@
#include "boot_button.h"
#include "board_config.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "freertos/task.h"
static const char *TAG = "boot_btn";
#define BOOT_DEBOUNCE_MS 50
static boot_button_cb_t s_on_press;
static QueueHandle_t s_gpio_evt_queue;
static void IRAM_ATTR gpio_isr_handler(void *arg)
{
uint32_t gpio_num = (uint32_t)arg;
xQueueSendFromISR(s_gpio_evt_queue, &gpio_num, NULL);
}
static void boot_button_task(void *arg)
{
(void)arg;
uint32_t io_num;
while (1) {
if (xQueueReceive(s_gpio_evt_queue, &io_num, portMAX_DELAY) != pdTRUE) {
continue;
}
vTaskDelay(pdMS_TO_TICKS(BOOT_DEBOUNCE_MS));
if (gpio_get_level(BOARD_PIN_BOOT) != 0) {
continue;
}
while (gpio_get_level(BOARD_PIN_BOOT) == 0) {
vTaskDelay(pdMS_TO_TICKS(10));
}
if (s_on_press != NULL) {
s_on_press();
}
}
}
void boot_button_init(boot_button_cb_t on_press)
{
s_on_press = on_press;
gpio_config_t io_conf = {
.pin_bit_mask = 1ULL << BOARD_PIN_BOOT,
.mode = GPIO_MODE_INPUT,
.pull_up_en = GPIO_PULLUP_ENABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTR_NEGEDGE,
};
ESP_ERROR_CHECK(gpio_config(&io_conf));
s_gpio_evt_queue = xQueueCreate(10, sizeof(uint32_t));
esp_err_t err = gpio_install_isr_service(0);
if (err != ESP_OK && err != ESP_ERR_INVALID_STATE) {
ESP_ERROR_CHECK(err);
}
ESP_ERROR_CHECK(gpio_isr_handler_add(BOARD_PIN_BOOT, gpio_isr_handler,
(void *)BOARD_PIN_BOOT));
xTaskCreate(boot_button_task, "boot_btn", 2048, NULL, 5, NULL);
ESP_LOGI(TAG, "BOOT button ready on GPIO%d", BOARD_PIN_BOOT);
}
+5
View File
@@ -0,0 +1,5 @@
#pragma once
typedef void (*boot_button_cb_t)(void);
void boot_button_init(boot_button_cb_t on_press);