diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 24f639e..6e843d6 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -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" diff --git a/main/app/app_main.c b/main/app/app_main.c index fa78053..27d60ab 100644 --- a/main/app/app_main.c +++ b/main/app/app_main.c @@ -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(); } diff --git a/main/app/ui_handlers.c b/main/app/ui_handlers.c index 12a25bb..84fd8e0 100644 --- a/main/app/ui_handlers.c +++ b/main/app/ui_handlers.c @@ -1,6 +1,31 @@ #include "ui_handlers.h" + +#include + +#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; diff --git a/main/app/ui_handlers.h b/main/app/ui_handlers.h index 74423bd..339e2a4 100644 --- a/main/app/ui_handlers.h +++ b/main/app/ui_handlers.h @@ -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); diff --git a/main/bsp/board_config.h b/main/bsp/board_config.h index 80d9869..5519d45 100644 --- a/main/bsp/board_config.h +++ b/main/bsp/board_config.h @@ -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 diff --git a/main/bsp/boot_button.c b/main/bsp/boot_button.c new file mode 100644 index 0000000..3883d59 --- /dev/null +++ b/main/bsp/boot_button.c @@ -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); +} diff --git a/main/bsp/boot_button.h b/main/bsp/boot_button.h new file mode 100644 index 0000000..2a84782 --- /dev/null +++ b/main/bsp/boot_button.h @@ -0,0 +1,5 @@ +#pragma once + +typedef void (*boot_button_cb_t)(void); + +void boot_button_init(boot_button_cb_t on_press);