Repository navigation
Expand file tree
/
Copy pathbutton.c
More file actions
110 lines (89 loc) · 3.38 KB
/
Copy pathbutton.c
File metadata and controls
110 lines (89 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* @file button.c
* @brief Button driver with multi-click detection.
* @author Ihar Hlukhau
* @date 04/01/2026
*/
#include <stdatomic.h>
#include "button.h"
#include "comm_mngr.h"
#include "device_cmd.h"
#include "fault.h"
#include "led.h"
#include "driver/gpio.h"
#include "esp_timer.h"
#include "freertos/FreeRTOS.h"
#include "esp_log.h"
/** @brief GPIO pin for the button. */
#define BUTTON_PIN 19
/** @brief Number of clicks in the window to trigger mode switch. */
#define CLICKS_MODE_SWITCH 2
/** @brief Number of clicks in the window to trigger Matter fabric reset. */
#define CLICKS_MATTER_RESET 3
/** @brief Number of clicks in the window to trigger factory reset. */
#define CLICKS_FACTORY_RESET 4
/** @brief Click counting window duration (ms). */
#define CLICK_WINDOW_MS 2000
/** @brief Debounce duration during which button inputs are ignored (us). */
#define BUTTON_DEBOUNCE_TIME (300 * 1000)
static const char *TAG = "BUTTON";
/** @brief Atomic click counter (read/written from both ISR and task context). */
static atomic_int s_click_count = 0;
/** @brief One-shot timer that fires when the click window expires. */
static TimerHandle_t xClickTimer;
/** @brief Timer callback: evaluate click count and trigger the corresponding action. */
static void vClickTimerCallback(__unused TimerHandle_t xTimer) {
const int clicks = atomic_load(&s_click_count);
if (clicks >= CLICKS_FACTORY_RESET) {
device_cmd_factory_reset();
} else if (clicks >= CLICKS_MATTER_RESET) {
device_cmd_matter_factory_reset();
} else if (clicks >= CLICKS_MODE_SWITCH) {
led_show_pattern(LED_PATTERN_MODE_SWITCH);
comm_mngr_request_switch();
}
atomic_store(&s_click_count, 0);
}
/** @brief GPIO ISR: increment click counter and (re)start the window timer. */
static void button_isr_handler(__unused void* arg) {
static int64_t s_last_press_us = 0;
/* Software debounce */
const int64_t now = esp_timer_get_time();
if (now - s_last_press_us < BUTTON_DEBOUNCE_TIME) { return; }
s_last_press_us = now;
atomic_fetch_add(&s_click_count, 1);
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
xTimerStartFromISR(xClickTimer, &xHigherPriorityTaskWoken);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
esp_err_t button_init(void) {
esp_err_t err = ESP_OK;
const gpio_config_t io_conf = {
.intr_type = GPIO_INTR_NEGEDGE, // trigger on press (pull-up logic)
.mode = GPIO_MODE_INPUT,
.pin_bit_mask = (1ULL << BUTTON_PIN),
.pull_up_en = 1,
};
err = gpio_config(&io_conf);
if (err != ESP_OK) {
ESP_LOGE(TAG, "GPIO config failed: %s", esp_err_to_name(err));
fault_report(FAULT_BUTTON, ERROR_BUTTON_ISR_INIT, err);
return ESP_FAIL;
}
err = gpio_install_isr_service(0);
if (err != ESP_OK) {
fault_report(FAULT_BUTTON, ERROR_BUTTON_ISR_INIT, err);
return ESP_FAIL;
}
err = gpio_isr_handler_add(BUTTON_PIN, button_isr_handler, NULL);
if (err != ESP_OK) {
fault_report(FAULT_BUTTON, ERROR_BUTTON_ISR_INIT, err);
return ESP_FAIL;
}
xClickTimer = xTimerCreate("ClickTimer", pdMS_TO_TICKS(CLICK_WINDOW_MS), pdFALSE, NULL, vClickTimerCallback);
if (xClickTimer == NULL) {
fault_report(FAULT_BUTTON, ERROR_BUTTON_TIMER_INIT, ESP_ERR_NO_MEM);
return ESP_FAIL;
}
return ESP_OK;
}