Skip to content

Commit 9b3e52e

Browse files
committed
applications: gesture_recognition: Update button handling
Refactor button click handler to use state based press detection instead of detecting edges as it consumes less energy. JIRA: NCSDK-37833 Signed-off-by: Jan Zyczkowski <jan.zyczkowski@nordicsemi.no>
1 parent 326ffc4 commit 9b3e52e

3 files changed

Lines changed: 29 additions & 15 deletions

File tree

applications/gesture_recognition/src/hw_modules/button/button.c

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,43 @@ LOG_MODULE_REGISTER(button, CONFIG_LOG_DEFAULT_LEVEL);
2020
#error "Unsupported board: sw0 devicetree alias is not defined"
2121
#endif
2222

23+
#define DEBOUNCE_MS 1000
2324

2425
static const struct gpio_dt_spec button_sw0 = GPIO_DT_SPEC_GET_OR(SW0_NODE, gpios, {0});
2526
static struct gpio_callback button_cb_data;
2627
static button_click_handler_t button_click_handler;
28+
static struct k_timer reenable_timer;
2729

28-
29-
static bool is_pressed(void)
30+
static void reenable_timer_expiry(struct k_timer *timer)
3031
{
31-
return gpio_pin_get_dt(&button_sw0) > 0;
32+
ARG_UNUSED(timer);
33+
int ret = gpio_pin_interrupt_configure_dt(&button_sw0, GPIO_INT_LEVEL_ACTIVE);
34+
35+
if (ret != 0) {
36+
LOG_ERR("Error %d: failed to re-enable interrupt on %s pin %u",
37+
ret, button_sw0.port->name, button_sw0.pin);
38+
}
3239
}
3340

34-
void button_interrupt(const struct device *dev, struct gpio_callback *cb, uint32_t pins)
41+
static void button_interrupt(const struct device *dev, struct gpio_callback *cb, uint32_t pins)
3542
{
3643
ARG_UNUSED(dev);
3744
ARG_UNUSED(cb);
3845
ARG_UNUSED(pins);
3946

40-
if (button_click_handler) {
41-
button_click_handler(is_pressed());
47+
int ret = gpio_pin_interrupt_configure_dt(&button_sw0, GPIO_INT_DISABLE);
48+
if (ret != 0) {
49+
LOG_ERR("Error %d: failed to disable interrupt on %s pin %u",
50+
ret, button_sw0.port->name, button_sw0.pin);
51+
}
52+
53+
if (button_click_handler != NULL) {
54+
button_click_handler();
55+
} else {
56+
LOG_WRN("Button interrupt fired but no click handler is registered");
4257
}
58+
59+
k_timer_start(&reenable_timer, K_MSEC(DEBOUNCE_MS), K_NO_WAIT);
4360
}
4461

4562
int button_init(void)
@@ -58,7 +75,9 @@ int button_init(void)
5875
return ret;
5976
}
6077

61-
ret = gpio_pin_interrupt_configure_dt(&button_sw0, GPIO_INT_EDGE_BOTH);
78+
k_timer_init(&reenable_timer, reenable_timer_expiry, NULL);
79+
80+
ret = gpio_pin_interrupt_configure_dt(&button_sw0, GPIO_INT_LEVEL_ACTIVE);
6281

6382
if (ret != 0) {
6483
LOG_ERR("Error %d: failed to configure interrupt on %s pin %u",

applications/gesture_recognition/src/hw_modules/button/button.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,8 @@ extern "C" {
2323

2424
/**
2525
* @brief Button click handler type
26-
*
27-
* @param is_pressed Indicates if the button is currently pressed (true) or released (false).
2826
*/
29-
30-
typedef void (*button_click_handler_t)(bool is_pressed);
27+
typedef void (*button_click_handler_t)(void);
3128

3229
/**
3330
* @brief Initialize button module

applications/gesture_recognition/src/main.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,9 @@ static void button_work_handler(struct k_work *work)
297297
#endif
298298
}
299299

300-
static void button_click_handler(bool pressed)
300+
static void button_click_handler(void)
301301
{
302-
if (pressed) {
303-
k_work_submit(&button_work);
304-
}
302+
k_work_submit(&button_work);
305303
}
306304

307305
static void hw_modules_init(void)

0 commit comments

Comments
 (0)