use button component from espressif

Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
2025-06-28 00:12:57 +02:00
parent f97f67422a
commit ca996d1c13
3 changed files with 41 additions and 181 deletions

View File

@@ -1,155 +1,86 @@
#include "button_handling.h" #include "button_handling.h"
#include "button_gpio.h"
#include "common.h"
#include "driver/gpio.h" #include "driver/gpio.h"
#include "driver/i2c.h"
#include "esp_err.h" #include "esp_err.h"
#include "esp_log.h" #include "esp_log.h"
#include "esp_mac.h" #include "esp_mac.h"
#include "esp_timer.h"
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/queue.h" #include "iot_button.h"
#include "freertos/task.h"
#include "sdkconfig.h" #include "sdkconfig.h"
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "common.h"
static const char *TAG = "button_handling"; static const char *TAG = "button_handling";
#define DEBOUNCE_TIME_MS (50) // Shorter debounce time for timer-based debouncing const uint8_t gpios[] = {BUTTON_DOWN, BUTTON_UP, BUTTON_LEFT, BUTTON_RIGHT, BUTTON_SELECT, BUTTON_BACK};
#define BUTTON_QUEUE_LENGTH 5
#define BUTTON_QUEUE_ITEM_SIZE sizeof(uint8_t)
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; QueueHandle_t buttonQueue = NULL;
// Structure for button state static void button_event_cb(void *arg, void *usr_data)
typedef struct
{ {
uint8_t pin; if (buttonQueue == NULL)
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; ESP_LOGE(TAG, "Button queue not initialized!");
return;
// 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)
{
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);
} }
// Button is released (HIGH) and was pressed before button_user_data_t *data = (button_user_data_t *)usr_data;
else if (level == 1 && button->is_pressed) 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_LOGW(TAG, "Failed to send button press to queue");
ESP_LOGD(TAG, "Button %d released", button->pin);
} }
} }
// ISR Handler - only starts the timer static void create_button(uint8_t gpio, int index)
void IRAM_ATTR button_isr_handler(void *arg)
{ {
button_state_t *button = (button_state_t *)arg; const button_config_t btn_cfg = {0};
int64_t now = esp_timer_get_time(); const button_gpio_config_t btn_gpio_cfg = {
.gpio_num = gpio,
// Simple time-based debouncing in ISR .active_level = 0,
if ((now - button->last_interrupt_time) > (DEBOUNCE_TIME_MS * 1000)) .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; ESP_LOGE(TAG, "Button create failed");
// Start/restart the timer
esp_timer_stop(button->timer);
esp_timer_start_once(button->timer, DEBOUNCE_TIME_MS * 1000);
} }
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) void setup_buttons(void)
{ {
buttonQueue = xQueueCreate(BUTTON_QUEUE_LENGTH, BUTTON_QUEUE_ITEM_SIZE); buttonQueue = xQueueCreate(10, sizeof(uint8_t));
if (buttonQueue == NULL) if (buttonQueue == NULL)
{ {
ESP_LOGE(TAG, "Error while Queue creation!"); ESP_LOGE(TAG, "Failed to create button queue");
return; return;
} }
ESP_LOGI(TAG, "Button Queue created.");
esp_err_t isr_service_err = gpio_install_isr_service(ESP_INTR_FLAG_IRAM); ESP_LOGI(TAG, "Button queue created successfully");
if (isr_service_err != ESP_OK && isr_service_err != ESP_ERR_INVALID_STATE) 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)); create_button(gpios[i], i);
}
// 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);
} }
} }
// Cleanup function (optional) // Cleanup function (optional)
void cleanup_buttons(void) 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) if (buttonQueue != NULL)
{ {
vQueueDelete(buttonQueue); vQueueDelete(buttonQueue);

View File

@@ -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 #pragma once
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"
{ {
#endif #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); 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); void cleanup_buttons(void);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@@ -3,3 +3,4 @@ dependencies:
git: https://github.com/olikraus/u8g2.git git: https://github.com/olikraus/u8g2.git
# u8g2_hal: # u8g2_hal:
# git: https://github.com/mkfrey/u8g2-hal-esp-idf.git # git: https://github.com/mkfrey/u8g2-hal-esp-idf.git
espressif/button: ^4.1.3