Skip to content

Commit 7d48b81

Browse files
committed
feat(button): support longger polling intervals
1 parent db62dff commit 7d48b81

File tree

7 files changed

+34
-22
lines changed

7 files changed

+34
-22
lines changed

components/button/CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# ChangeLog
22

3+
## v3.4.0 - 2024-10-22
4+
5+
### Enhancements:
6+
7+
* Supports a maximum button polling interval of 500ms.
8+
* Fixed a potential counter overflow issue.
9+
10+
### Break change:
11+
12+
* The return value of `iot_button_get_ticks_time` has been changed from `uint16_t` to `uint32_t`.
13+
314
## v3.3.2 - 2024-8-28
415

516
### Enhancements:

components/button/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ menu "IoT Button"
22

33
config BUTTON_PERIOD_TIME_MS
44
int "BUTTON PERIOD TIME (MS)"
5-
range 2 50
5+
range 2 500
66
default 5
77
help
88
"Button scan interval"

components/button/idf_component.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: "3.3.2"
1+
version: "3.4.0"
22
description: GPIO and ADC button driver
33
url: https://github.com/espressif/esp-iot-solution/tree/master/components/button
44
repository: https://github.com/espressif/esp-iot-solution.git

components/button/include/iot_button.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ uint8_t iot_button_get_repeat(button_handle_t btn_handle);
292292
*
293293
* @return Actual time from press down to up (ms).
294294
*/
295-
uint16_t iot_button_get_ticks_time(button_handle_t btn_handle);
295+
uint32_t iot_button_get_ticks_time(button_handle_t btn_handle);
296296

297297
/**
298298
* @brief Get button long press hold count

components/button/iot_button.c

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,10 @@ typedef struct {
6060
*
6161
*/
6262
typedef struct Button {
63-
uint16_t ticks;
64-
uint16_t long_press_ticks; /*! Trigger ticks for long press*/
65-
uint16_t short_press_ticks; /*! Trigger ticks for repeat press*/
66-
uint16_t long_press_hold_cnt; /*! Record long press hold count*/
67-
uint16_t long_press_ticks_default;
63+
uint32_t ticks; /*!< Count for the current button state. */
64+
uint32_t long_press_ticks; /*!< Trigger ticks for long press, */
65+
uint32_t short_press_ticks; /*!< Trigger ticks for repeat press */
66+
uint32_t long_press_hold_cnt; /*!< Record long press hold count */
6867
uint8_t repeat;
6968
uint8_t state: 3;
7069
uint8_t debounce_cnt: 3;
@@ -153,9 +152,10 @@ static void button_handler(button_dev_t *btn)
153152
btn->event = (uint8_t)BUTTON_LONG_PRESS_START;
154153
btn->state = 4;
155154
/** Calling callbacks for BUTTON_LONG_PRESS_START */
156-
uint16_t ticks_time = iot_button_get_ticks_time(btn);
155+
uint32_t ticks_time = iot_button_get_ticks_time(btn);
156+
int32_t diff = ticks_time - btn->long_press_ticks * TICKS_INTERVAL;
157157
if (btn->cb_info[btn->event] && btn->count[0] == 0) {
158-
if (abs(ticks_time - (btn->long_press_ticks * TICKS_INTERVAL)) <= TOLERANCE && btn->cb_info[btn->event][btn->count[0]].event_data.long_press.press_time == (btn->long_press_ticks * TICKS_INTERVAL)) {
158+
if (abs(diff) <= TOLERANCE && btn->cb_info[btn->event][btn->count[0]].event_data.long_press.press_time == (btn->long_press_ticks * TICKS_INTERVAL)) {
159159
do {
160160
btn->cb_info[btn->event][btn->count[0]].cb(btn, btn->cb_info[btn->event][btn->count[0]].usr_data);
161161
btn->count[0]++;
@@ -234,7 +234,7 @@ static void button_handler(button_dev_t *btn)
234234
CALL_EVENT_CB(BUTTON_LONG_PRESS_HOLD);
235235

236236
/** Calling callbacks for BUTTON_LONG_PRESS_START based on press_time */
237-
uint16_t ticks_time = iot_button_get_ticks_time(btn);
237+
uint32_t ticks_time = iot_button_get_ticks_time(btn);
238238
if (btn->cb_info[BUTTON_LONG_PRESS_START]) {
239239
button_cb_info_t *cb_info = btn->cb_info[BUTTON_LONG_PRESS_START];
240240
uint16_t time = cb_info[btn->count[0]].event_data.long_press.press_time;
@@ -247,7 +247,7 @@ static void button_handler(button_dev_t *btn)
247247
}
248248
}
249249
}
250-
if (btn->count[0] < btn->size[BUTTON_LONG_PRESS_START] && abs(ticks_time - time) <= TOLERANCE) {
250+
if (btn->count[0] < btn->size[BUTTON_LONG_PRESS_START] && abs((int)ticks_time - (int)time) <= TOLERANCE) {
251251
btn->event = (uint8_t)BUTTON_LONG_PRESS_START;
252252
do {
253253
cb_info[btn->count[0]].cb(btn, cb_info[btn->count[0]].usr_data);
@@ -272,7 +272,7 @@ static void button_handler(button_dev_t *btn)
272272
}
273273
}
274274
}
275-
if (btn->count[1] + 1 < btn->size[BUTTON_LONG_PRESS_UP] && abs(ticks_time - time) <= TOLERANCE) {
275+
if (btn->count[1] + 1 < btn->size[BUTTON_LONG_PRESS_UP] && abs((int)ticks_time - (int)time) <= TOLERANCE) {
276276
do {
277277
btn->count[1]++;
278278
if (btn->count[1] + 1 >= btn->size[BUTTON_LONG_PRESS_UP]) {
@@ -377,7 +377,6 @@ static button_dev_t *button_create_com(uint8_t active_level, uint8_t (*hal_get_k
377377
btn->hal_button_Level = hal_get_key_state;
378378
btn->button_level = !active_level;
379379
btn->long_press_ticks = long_press_ticks;
380-
btn->long_press_ticks_default = btn->long_press_ticks;
381380
btn->short_press_ticks = short_press_ticks;
382381

383382
/** Add handle to list */
@@ -541,7 +540,7 @@ esp_err_t iot_button_register_cb(button_handle_t btn_handle, button_event_t even
541540
};
542541

543542
if ((event == BUTTON_LONG_PRESS_START || event == BUTTON_LONG_PRESS_UP) && !event_cfg.event_data.long_press.press_time) {
544-
event_cfg.event_data.long_press.press_time = btn->long_press_ticks_default * TICKS_INTERVAL;
543+
event_cfg.event_data.long_press.press_time = btn->long_press_ticks * TICKS_INTERVAL;
545544
}
546545

547546
return iot_button_register_event_cb(btn_handle, event_cfg, cb, usr_data);
@@ -746,7 +745,7 @@ uint8_t iot_button_get_repeat(button_handle_t btn_handle)
746745
return btn->repeat;
747746
}
748747

749-
uint16_t iot_button_get_ticks_time(button_handle_t btn_handle)
748+
uint32_t iot_button_get_ticks_time(button_handle_t btn_handle)
750749
{
751750
BTN_CHECK(NULL != btn_handle, "Pointer of handle is invalid", 0);
752751
button_dev_t *btn = (button_dev_t *) btn_handle;

components/button/test_apps/main/button_test.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
#include "stdio.h"
6+
#include <stdio.h>
7+
#include <inttypes.h>
78
#include "freertos/FreeRTOS.h"
89
#include "freertos/task.h"
910
#include "freertos/queue.h"
@@ -46,7 +47,7 @@ static void button_press_down_cb(void *arg, void *data)
4647
static void button_press_up_cb(void *arg, void *data)
4748
{
4849
TEST_ASSERT_EQUAL_HEX(BUTTON_PRESS_UP, iot_button_get_event(arg));
49-
ESP_LOGI(TAG, "BTN%d: BUTTON_PRESS_UP[%d]", get_btn_index((button_handle_t)arg), iot_button_get_ticks_time((button_handle_t)arg));
50+
ESP_LOGI(TAG, "BTN%d: BUTTON_PRESS_UP[%"PRIu32"]", get_btn_index((button_handle_t)arg), iot_button_get_ticks_time((button_handle_t)arg));
5051
}
5152

5253
static void button_press_repeat_cb(void *arg, void *data)
@@ -75,7 +76,7 @@ static void button_long_press_start_cb(void *arg, void *data)
7576
static void button_long_press_hold_cb(void *arg, void *data)
7677
{
7778
TEST_ASSERT_EQUAL_HEX(BUTTON_LONG_PRESS_HOLD, iot_button_get_event(arg));
78-
ESP_LOGI(TAG, "BTN%d: BUTTON_LONG_PRESS_HOLD[%d],count is [%d]", get_btn_index((button_handle_t)arg), iot_button_get_ticks_time((button_handle_t)arg), iot_button_get_long_press_hold_cnt((button_handle_t)arg));
79+
ESP_LOGI(TAG, "BTN%d: BUTTON_LONG_PRESS_HOLD[%"PRIu32"],count is [%d]", get_btn_index((button_handle_t)arg), iot_button_get_ticks_time((button_handle_t)arg), iot_button_get_long_press_hold_cnt((button_handle_t)arg));
7980
}
8081

8182
static void button_press_repeat_done_cb(void *arg, void *data)
@@ -636,8 +637,9 @@ static void button_long_press_auto_check_cb(void *arg, void *data)
636637
uint32_t value = (uint32_t)data;
637638
uint16_t event = (0xffff0000 & value) >> 16;
638639
uint16_t time = 0xffff & value;
639-
uint16_t ticks_time = iot_button_get_ticks_time(g_btns[0]);
640-
if (status == value && abs(ticks_time - time) <= TOLERANCE) {
640+
uint32_t ticks_time = iot_button_get_ticks_time(g_btns[0]);
641+
int32_t diff = ticks_time - time;
642+
if (status == value && abs(diff) <= TOLERANCE) {
641643
ESP_LOGI(TAG, "Auto check: button event: %s and time: %d pass", iot_button_get_event_str(state), time);
642644

643645
if (event == BUTTON_LONG_PRESS_UP && time == long_press_time[4]) {

examples/usb/device/usb_surface_dial/main/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ static void _button_press_down_cb(void *arg, void *data)
6767

6868
static void _button_press_up_cb(void *arg, void *data)
6969
{
70-
ESP_LOGI(TAG, "BTN: BUTTON_PRESS_UP[%d]", iot_button_get_ticks_time((button_handle_t)arg));
70+
ESP_LOGI(TAG, "BTN: BUTTON_PRESS_UP[%"PRIu32"]", iot_button_get_ticks_time((button_handle_t)arg));
7171
surface_dial_report(DIAL_RELEASE);
7272
}
7373

0 commit comments

Comments
 (0)