ChaosDMX
Open-Source DMX-Interface
Loading...
Searching...
No Matches
button_actions.c
Go to the documentation of this file.
1
6
10#define LOG_TAG "BUTTON"
11
12#include "button_actions.h"
13#include "button_gpio.h"
14#include "config.h"
15#include "driver/gpio.h"
16#include "esp_system.h"
17#include "iot_button.h"
18#include "led.h"
19#include "logger.h"
20
24#define BUTTON_GPIO_NUM GPIO_NUM_5
25
29#define RESET_HOLD_TIME_MS 3000
30
34static button_handle_t s_btn_handle = NULL;
35
45static void execute_button_action(config_button_action_t assigned_action) {
46 switch (assigned_action) {
48 LOGI("Button action: Toggle LED Brightness");
49
50 // TODO: Extract in component/led
51
52 static const uint8_t levels[] = {0, 1, 20, 100, 255};
53 const uint8_t num_levels = sizeof(levels) / sizeof(levels[0]);
54
55 uint8_t current_brightness = config_get_led_brightness();
56 uint8_t next_brightness = levels[0];
57
58 for (uint8_t i = 0; i < num_levels; i++) {
59 if (current_brightness < levels[i]) {
60 next_brightness = levels[i];
61 break;
62 }
63 }
64
65 config_set_led_brightness(next_brightness);
67 break;
68 }
69
71 LOGI("Button action: Reboot");
72 fflush(stdout);
73 esp_restart();
74 break;
75
77 default:
78 LOGI("No action or APP_BUTTON_ACTION_NONE assigned to this event.");
79 break;
80 }
81}
82
83/* --- Internal Driver Event Callbacks --- */
84
90static void iot_button_single_click_cb(void *arg, void *usr_data) {
91 LOGI("Single click detected.");
94}
95
101static void iot_button_double_click_cb(void *arg, void *usr_data) {
102 LOGI("Double click detected.");
105}
106
112static void iot_button_multiple_click_cb(void *arg, void *usr_data) {
113 LOGI("Multiple click detected.");
116}
117
118/* --- Public API --- */
119
120esp_err_t button_init(void) {
121 LOGI("Initializing runtime-configurable hardware button handler...");
122
123 // 1. General button configuration (Logic)
124 button_config_t btn_cfg = {
125 .long_press_time = RESET_HOLD_TIME_MS,
126 };
127
128 // 2. Hardware-specific configuration (GPIO driver)
129 button_gpio_config_t gpio_cfg = {
130 .gpio_num = BUTTON_GPIO_NUM,
131 .active_level = 0, /* Active Low */
132 };
133
134 // 3. Create GPIO button device
135 esp_err_t err =
136 iot_button_new_gpio_device(&btn_cfg, &gpio_cfg, &s_btn_handle);
137 if (err != ESP_OK || s_btn_handle == NULL) {
138 LOGE("Failed to instantiate IoT button driver!");
139 return ESP_FAIL;
140 }
141
142 // 4. Register event callbacks
143 // Note: MULTIPLE_CLICK requires arguments to define the click count.
144 // Using static to ensure the driver has a stable pointer to the
145 // configuration.
146 static button_event_args_t multi_args = {.multiple_clicks = {.clicks = 3}};
147
148 ESP_ERROR_CHECK(iot_button_register_cb(s_btn_handle, BUTTON_SINGLE_CLICK,
150 NULL));
151 ESP_ERROR_CHECK(iot_button_register_cb(s_btn_handle, BUTTON_DOUBLE_CLICK,
153 NULL));
154
155 ESP_ERROR_CHECK(iot_button_register_cb(s_btn_handle, BUTTON_MULTIPLE_CLICK,
156 &multi_args,
158
159 LOGI("Dynamic button router successfully running.");
160 return ESP_OK;
161}
162
164 if (s_btn_handle == NULL) {
165 return false;
166 }
167 uint8_t level = iot_button_get_key_level(s_btn_handle);
168 return level == 1;
169}
static void execute_button_action(config_button_action_t assigned_action)
Executes the action compiled into the configuration for the triggered event.
bool button_is_pressed(void)
Checks if the button is currently physically pressed.
esp_err_t button_init(void)
Initializes the hardware button configuration and event callbacks.
#define BUTTON_GPIO_NUM
GPIO number assigned to the hardware button.
static void iot_button_single_click_cb(void *arg, void *usr_data)
Callback for single click events.
static button_handle_t s_btn_handle
Internal handle for the IoT button instance.
static void iot_button_multiple_click_cb(void *arg, void *usr_data)
Callback for multiple click events.
#define RESET_HOLD_TIME_MS
Duration (ms) the button must be held for a reset trigger.
static void iot_button_double_click_cb(void *arg, void *usr_data)
Callback for double click events.
Hardware button abstraction layer for short and long press detection.
Thread-safe configuration component utilizing NVS for ESP32.
bool config_set_led_brightness(uint8_t brightness)
Sets the status LED brightness level in RAM.
Definition config.c:299
config_button_action_t
Actions that can be dynamically assigned to button events.
Definition config.h:46
@ APP_BUTTON_ACTION_REBOOT
Definition config.h:49
@ APP_BUTTON_ACTION_NONE
Definition config.h:47
@ APP_BUTTON_ACTION_TOGGLE_LED
Definition config.h:48
uint8_t config_get_led_brightness(void)
Gets the current status LED brightness level.
Definition config.c:290
@ APP_BUTTON_EVENT_SINGLE_CLICK
Definition config.h:35
@ APP_BUTTON_EVENT_MULTIPLE_CLICK
Definition config.h:37
@ APP_BUTTON_EVENT_DOUBLE_CLICK
Definition config.h:36
config_button_action_t config_get_button_action(app_button_event_t event)
Retrieves the action assigned to a specific button event.
Definition config.c:447
esp_err_t config_save(void)
Flushes all staged RAM changes permanently to the non-volatile storage.
Definition config.c:204
LED control component with flexible blinking and breathing modes.
Project-wide logging macros based on ESP-IDF's logging library.
#define LOGI(...)
Log a message at Info level.
Definition logger.h:42
#define LOGE(...)
Log a message at Error level.
Definition logger.h:38