diff --git a/main/button_handling.c b/main/button_handling.c index 2616552..d58f7a0 100644 --- a/main/button_handling.c +++ b/main/button_handling.c @@ -1,155 +1,86 @@ #include "button_handling.h" +#include "button_gpio.h" +#include "common.h" #include "driver/gpio.h" -#include "driver/i2c.h" #include "esp_err.h" #include "esp_log.h" #include "esp_mac.h" -#include "esp_timer.h" #include "freertos/FreeRTOS.h" -#include "freertos/queue.h" -#include "freertos/task.h" +#include "iot_button.h" #include "sdkconfig.h" #include #include -#include "common.h" - static const char *TAG = "button_handling"; -#define DEBOUNCE_TIME_MS (50) // Shorter debounce time for timer-based debouncing -#define BUTTON_QUEUE_LENGTH 5 -#define BUTTON_QUEUE_ITEM_SIZE sizeof(uint8_t) +const uint8_t gpios[] = {BUTTON_DOWN, BUTTON_UP, BUTTON_LEFT, BUTTON_RIGHT, BUTTON_SELECT, BUTTON_BACK}; -const uint8_t pins[] = {BUTTON_DOWN, BUTTON_UP, BUTTON_LEFT, BUTTON_RIGHT, BUTTON_SELECT, BUTTON_BACK}; +typedef struct +{ + uint8_t gpio; +} button_user_data_t; + +static button_user_data_t button_data[6]; QueueHandle_t buttonQueue = NULL; -// Structure for button state -typedef struct +static void button_event_cb(void *arg, void *usr_data) { - uint8_t pin; - esp_timer_handle_t timer; - bool is_pressed; - int64_t last_interrupt_time; -} button_state_t; - -// Array for button states -static button_state_t button_states[6]; - -// Timer callback for debouncing -static void button_timer_callback(void *arg) -{ - button_state_t *button = (button_state_t *)arg; - - // Check current GPIO state - int level = gpio_get_level(button->pin); - - // Button is pressed (LOW) and was not pressed before - if (level == 0 && !button->is_pressed) + if (buttonQueue == NULL) { - button->is_pressed = true; - - // Send button press to queue - uint8_t press_signal = button->pin; - xQueueSend(buttonQueue, &press_signal, 0); - - ESP_LOGD(TAG, "Button %d pressed", button->pin); + ESP_LOGE(TAG, "Button queue not initialized!"); + return; } - // Button is released (HIGH) and was pressed before - else if (level == 1 && button->is_pressed) + button_user_data_t *data = (button_user_data_t *)usr_data; + uint8_t gpio_num = data->gpio; + + ESP_LOGI(TAG, "Button pressed on GPIO %d", gpio_num); + + if (xQueueSend(buttonQueue, &gpio_num, 0) != pdTRUE) { - button->is_pressed = false; - ESP_LOGD(TAG, "Button %d released", button->pin); + ESP_LOGW(TAG, "Failed to send button press to queue"); } } -// ISR Handler - only starts the timer -void IRAM_ATTR button_isr_handler(void *arg) +static void create_button(uint8_t gpio, int index) { - button_state_t *button = (button_state_t *)arg; - int64_t now = esp_timer_get_time(); - - // Simple time-based debouncing in ISR - if ((now - button->last_interrupt_time) > (DEBOUNCE_TIME_MS * 1000)) + const button_config_t btn_cfg = {0}; + const button_gpio_config_t btn_gpio_cfg = { + .gpio_num = gpio, + .active_level = 0, + .enable_power_save = true, + }; + button_handle_t gpio_btn = NULL; + const esp_err_t ret = iot_button_new_gpio_device(&btn_cfg, &btn_gpio_cfg, &gpio_btn); + if (ret != ESP_OK) { - button->last_interrupt_time = now; - - // Start/restart the timer - esp_timer_stop(button->timer); - esp_timer_start_once(button->timer, DEBOUNCE_TIME_MS * 1000); + ESP_LOGE(TAG, "Button create failed"); } + + button_data[index].gpio = gpio; + iot_button_register_cb(gpio_btn, BUTTON_SINGLE_CLICK, NULL, button_event_cb, &button_data[index]); } void setup_buttons(void) { - buttonQueue = xQueueCreate(BUTTON_QUEUE_LENGTH, BUTTON_QUEUE_ITEM_SIZE); + buttonQueue = xQueueCreate(10, sizeof(uint8_t)); if (buttonQueue == NULL) { - ESP_LOGE(TAG, "Error while Queue creation!"); + ESP_LOGE(TAG, "Failed to create button queue"); return; } - ESP_LOGI(TAG, "Button Queue created."); - esp_err_t isr_service_err = gpio_install_isr_service(ESP_INTR_FLAG_IRAM); - if (isr_service_err != ESP_OK && isr_service_err != ESP_ERR_INVALID_STATE) + ESP_LOGI(TAG, "Button queue created successfully"); + for (int i = 0; i < sizeof(gpios) / sizeof(gpios[0]); i++) { - ESP_LOGE(TAG, "Error in gpio_install_isr_service: %s", esp_err_to_name(isr_service_err)); - } - - // Timer configuration - esp_timer_create_args_t timer_args = {.callback = button_timer_callback, .name = "button_debounce"}; - - for (int i = 0; i < sizeof(pins) / sizeof(pins[0]); i++) - { - const uint8_t pin = pins[i]; - - // Initialize button state - button_states[i].pin = pin; - button_states[i].is_pressed = false; - button_states[i].last_interrupt_time = 0; - - // Create timer for this button - timer_args.arg = &button_states[i]; - esp_err_t timer_err = esp_timer_create(&timer_args, &button_states[i].timer); - if (timer_err != ESP_OK) - { - ESP_LOGE(TAG, "Failed to create timer for button %d: %s", pin, esp_err_to_name(timer_err)); - continue; - } - - // GPIO configuration - gpio_config_t io_conf = {.intr_type = GPIO_INTR_ANYEDGE, // React to both edges - .pin_bit_mask = (1ULL << pin), - .mode = GPIO_MODE_INPUT, - .pull_up_en = GPIO_PULLUP_ENABLE, - .pull_down_en = GPIO_PULLDOWN_DISABLE}; - gpio_config(&io_conf); - - // Add ISR handler - esp_err_t add_isr_err = gpio_isr_handler_add(pin, button_isr_handler, &button_states[i]); - if (add_isr_err != ESP_OK) - { - ESP_LOGE(TAG, "Error in gpio_isr_handler_add: %s", esp_err_to_name(add_isr_err)); - } - - ESP_LOGD(TAG, "Button interrupt configured for GPIO %d", pin); + create_button(gpios[i], i); } } // Cleanup function (optional) void cleanup_buttons(void) { - for (int i = 0; i < sizeof(pins) / sizeof(pins[0]); i++) - { - if (button_states[i].timer != NULL) - { - esp_timer_stop(button_states[i].timer); - esp_timer_delete(button_states[i].timer); - } - gpio_isr_handler_remove(button_states[i].pin); - } - if (buttonQueue != NULL) { vQueueDelete(buttonQueue); diff --git a/main/button_handling.h b/main/button_handling.h index a5f87dc..620048a 100644 --- a/main/button_handling.h +++ b/main/button_handling.h @@ -1,82 +1,10 @@ -/** - * @file button_handling.h - * @brief Button input handling system for user interface interaction - * @details This header defines the button handling subsystem which manages - * hardware button inputs, debouncing, interrupt processing, and - * event queue management. It provides a robust foundation for - * reliable user input processing in embedded applications. - * @author System Control Team - * @date 2025-06-20 - */ - #pragma once #ifdef __cplusplus extern "C" { #endif - /** - * @brief Initializes the button handling subsystem and configures hardware - * - * @details This function sets up the complete button handling infrastructure: - * - GPIO configuration for button input pins with pull-up resistors - * - Interrupt service routine installation for responsive input - * - Debouncing timer creation and configuration - * - FreeRTOS queue creation for button event buffering - * - Button state tracking structure initialization - * - * The function configures all defined button pins to trigger interrupts - * on both rising and falling edges, enabling detection of both press - * and release events. Each button uses a dedicated timer for debouncing - * to ensure reliable input processing even with mechanical switch bounce. - * - * Button events are queued using FreeRTOS queues to ensure no input - * events are lost during high system activity periods. The queue-based - * approach also decouples interrupt handling from application processing. - * - * @pre ESP32 GPIO and timer subsystems must be available and functional - * @pre FreeRTOS must be running and queue services available - * @post All button pins are configured and ready for input detection - * @post Button event queue is created and ready for event processing - * @post Interrupt handlers are installed and active - * - * @note This function must be called during system initialization before - * any button input processing is expected - * @note The function configures hardware-specific GPIO pins as defined - * in the project configuration - * - * @see cleanup_buttons() for proper resource cleanup - */ void setup_buttons(void); - - /** - * @brief Cleans up button handling resources and disables interrupts - * - * @details This function performs complete cleanup of the button handling - * subsystem by: - * - Stopping and deleting all debouncing timers - * - Removing GPIO interrupt handlers from all button pins - * - Deleting the button event queue and freeing associated memory - * - Resetting button state tracking structures - * - * This cleanup function ensures proper resource management and prevents - * memory leaks when the button handling subsystem is no longer needed. - * It can be called during system shutdown or when reconfiguring the - * input handling subsystem. - * - * @pre Button handling subsystem must have been previously initialized - * @post All button-related interrupts are disabled and handlers removed - * @post All timers are stopped and deleted, freeing system resources - * @post Button event queue is deleted and memory is released - * @post GPIO pins are returned to default state - * - * @note This function should be called during system shutdown or when - * button handling is no longer required - * @note After calling this function, setup_buttons() must be called - * again before button input can be processed - * - * @see setup_buttons() for initialization of the button handling system - */ void cleanup_buttons(void); #ifdef __cplusplus } diff --git a/main/idf_component.yml b/main/idf_component.yml index 2565e81..e8a375e 100755 --- a/main/idf_component.yml +++ b/main/idf_component.yml @@ -3,3 +3,4 @@ dependencies: git: https://github.com/olikraus/u8g2.git # u8g2_hal: # git: https://github.com/mkfrey/u8g2-hal-esp-idf.git + espressif/button: ^4.1.3