Skip to content

Commit eb28327

Browse files
committed
Merge branch 'feat/button_add_button_end_event' into 'master'
feat(button): support enter power save manually See merge request ae_group/esp-iot-solution!1056
2 parents 91fbc63 + 171db2a commit eb28327

File tree

10 files changed

+159
-32
lines changed

10 files changed

+159
-32
lines changed

components/button/CHANGELOG.md

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

3+
## v3.3.0 - 2024-8-7
4+
5+
### Enhancements:
6+
7+
* Add Callback **button_power_save_cb_t** to support enter power save manually.
8+
* Increase the maximum polling interval supported by the button from 20ms to 50ms.
9+
310
## v3.2.3 - 2024-7-2
411

512
* Fixed the issue where the GPIO button in low-power mode continuously woke up the CPU after being pressed, causing abnormal power consumption.

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 20
5+
range 2 50
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.2.3"
1+
version: "3.3.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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ extern "C" {
1818
#endif
1919

2020
typedef void (* button_cb_t)(void *button_handle, void *usr_data);
21+
22+
#if CONFIG_GPIO_BUTTON_SUPPORT_POWER_SAVE
23+
typedef void (* button_power_save_cb_t)(void *usr_data);
24+
25+
/**
26+
* @brief Structs to store power save callback info
27+
*
28+
*/
29+
typedef struct {
30+
button_power_save_cb_t enter_power_save_cb;
31+
void *usr_data;
32+
} button_power_save_config_t;
33+
#endif
34+
2135
typedef void *button_handle_t;
2236

2337
/**
@@ -301,6 +315,21 @@ esp_err_t iot_button_resume(void);
301315
*/
302316
esp_err_t iot_button_stop(void);
303317

318+
#if CONFIG_GPIO_BUTTON_SUPPORT_POWER_SAVE
319+
/**
320+
* @brief Register a callback function for power saving.
321+
* The config->enter_power_save_cb function will be called when all keys stop working.
322+
*
323+
* @param config Button power save config
324+
* @return
325+
* - ESP_OK on success
326+
* - ESP_ERR_INVALID_STATE No button registered
327+
* - ESP_ERR_INVALID_ARG Arguments is invalid
328+
* - ESP_ERR_NO_MEM Not enough memory
329+
*/
330+
esp_err_t iot_button_register_power_save_cb(const button_power_save_config_t *config);
331+
#endif
332+
304333
#ifdef __cplusplus
305334
}
306335
#endif

components/button/iot_button.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ typedef struct Button {
7070
static button_dev_t *g_head_handle = NULL;
7171
static esp_timer_handle_t g_button_timer_handle = NULL;
7272
static bool g_is_timer_running = false;
73+
#if CONFIG_GPIO_BUTTON_SUPPORT_POWER_SAVE
74+
static button_power_save_config_t power_save_usr_cfg = {0};
75+
#endif
7376

7477
#define TICKS_INTERVAL CONFIG_BUTTON_PERIOD_TIME_MS
7578
#define DEBOUNCE_TICKS CONFIG_BUTTON_DEBOUNCE_TICKS //MAX 8
@@ -318,6 +321,10 @@ static void button_cb(void *args)
318321
button_gpio_enable_gpio_wakeup((uint32_t)(target->hardware_data), target->active_level, true);
319322
}
320323
}
324+
/*!< Notify the user that the Button has entered power save mode by calling this callback function. */
325+
if (power_save_usr_cfg.enter_power_save_cb) {
326+
power_save_usr_cfg.enter_power_save_cb(power_save_usr_cfg.usr_data);
327+
}
321328
}
322329
#endif
323330
}
@@ -764,3 +771,15 @@ esp_err_t iot_button_stop(void)
764771
g_is_timer_running = false;
765772
return ESP_OK;
766773
}
774+
775+
#if CONFIG_GPIO_BUTTON_SUPPORT_POWER_SAVE
776+
esp_err_t iot_button_register_power_save_cb(const button_power_save_config_t *config)
777+
{
778+
BTN_CHECK(g_head_handle, "No button registered", ESP_ERR_INVALID_STATE);
779+
BTN_CHECK(config->enter_power_save_cb, "Enter power save callback is invalid", ESP_ERR_INVALID_ARG);
780+
781+
power_save_usr_cfg.enter_power_save_cb = config->enter_power_save_cb;
782+
power_save_usr_cfg.usr_data = config->usr_data;
783+
return ESP_OK;
784+
}
785+
#endif

docs/en/input_device/button.rst

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,34 @@ Button event
2121

2222
Triggering conditions for each button event are enlisted in the table below:
2323

24-
25-
+--------------------------+-----------------------------------+
26-
| Event | Trigger Condition |
27-
+==========================+===================================+
28-
| BUTTON_PRESS_DOWN | Pressed |
29-
+--------------------------+-----------------------------------+
30-
| BUTTON_PRESS_UP | Released |
31-
+--------------------------+-----------------------------------+
32-
| BUTTON_PRESS_REPEAT | Pressed and released >= 2 times |
33-
+--------------------------+-----------------------------------+
34-
| BUTTON_PRESS_REPEAT_DONE | Repeated press completed |
35-
+--------------------------+-----------------------------------+
36-
| BUTTON_SINGLE_CLICK | Pressed and released once |
37-
+--------------------------+-----------------------------------+
38-
| BUTTON_DOUBLE_CLICK | Pressed and released twice |
39-
+--------------------------+-----------------------------------+
40-
| BUTTON_MULTIPLE_CLICK | Pressed and released N times |
41-
| | specified, triggers when achieved |
42-
+--------------------------+-----------------------------------+
43-
| BUTTON_LONG_PRESS_START | Instant when held for a threshold |
44-
| | duration of time |
45-
+--------------------------+-----------------------------------+
46-
| BUTTON_LONG_PRESS_HOLD | Triggered continuously during |
47-
| | long press |
48-
+--------------------------+-----------------------------------+
49-
| BUTTON_LONG_PRESS_UP | Released after a long press |
50-
+--------------------------+-----------------------------------+
51-
| BUTTON_PRESS_REPEAT_DONE | Repeated press and release ended |
52-
+--------------------------+-----------------------------------+
24+
+--------------------------+--------------------------------------+
25+
| Event | Trigger Condition |
26+
+==========================+======================================+
27+
| BUTTON_PRESS_DOWN | Pressed |
28+
+--------------------------+--------------------------------------+
29+
| BUTTON_PRESS_UP | Released |
30+
+--------------------------+--------------------------------------+
31+
| BUTTON_PRESS_REPEAT | Pressed and released >= 2 times |
32+
+--------------------------+--------------------------------------+
33+
| BUTTON_PRESS_REPEAT_DONE | Repeated press completed |
34+
+--------------------------+--------------------------------------+
35+
| BUTTON_SINGLE_CLICK | Pressed and released once |
36+
+--------------------------+--------------------------------------+
37+
| BUTTON_DOUBLE_CLICK | Pressed and released twice |
38+
+--------------------------+--------------------------------------+
39+
| BUTTON_MULTIPLE_CLICK | Pressed and released N times |
40+
| | specified, triggers when achieved |
41+
+--------------------------+--------------------------------------+
42+
| BUTTON_LONG_PRESS_START | Instant when held for a threshold |
43+
| | duration of time |
44+
+--------------------------+--------------------------------------+
45+
| BUTTON_LONG_PRESS_HOLD | Triggered continuously during |
46+
| | long press |
47+
+--------------------------+--------------------------------------+
48+
| BUTTON_LONG_PRESS_UP | Released after a long press |
49+
+--------------------------+--------------------------------------+
50+
| BUTTON_PRESS_REPEAT_DONE | Repeated press and release ended |
51+
+--------------------------+--------------------------------------+
5352

5453
Each button supports **call-back** and **pooling** mode.
5554

@@ -277,6 +276,25 @@ As shown, low-power mode results in more power savings.
277276
};
278277
button_handle_t btn = iot_button_create(&btn_cfg);
279278
279+
When to Enter Light Sleep
280+
281+
- Using Auto Light Sleep: The device will enter Light Sleep automatically after the button closes the esp_timer.
282+
283+
- User-Controlled Light Sleep: The device can enter Light Sleep when ``enter_power_save_cb`` is called.
284+
285+
.. code:: c
286+
287+
void btn_enter_power_save(void *usr_data)
288+
{
289+
ESP_LOGI(TAG, "Can enter power save now");
290+
}
291+
292+
button_power_save_config_t config = {
293+
.enter_power_save_cb = btn_enter_power_save,
294+
};
295+
296+
iot_button_register_power_save_cb(&config);
297+
280298
Stop and resume
281299
^^^^^^^^^^^^^^^^^
282300

docs/zh_CN/input_device/button.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,25 @@ Button 组件支持为多个事件注册回调函数,每个事件都可以注
277277
};
278278
button_handle_t btn = iot_button_create(&btn_cfg);
279279
280+
什么时候进入 Light Sleep
281+
282+
- 使用 Auto Light Sleep: 会在 button 自动关闭 esp_timer 后进入 Light Sleep
283+
284+
- 用户控制 Light Sleep: 需要在 ``enter_power_save_cb`` 回调到来时进入 Light Sleep
285+
286+
.. code:: c
287+
288+
void btn_enter_power_save(void *usr_data)
289+
{
290+
ESP_LOGI(TAG, "Can enter power save now");
291+
}
292+
293+
button_power_save_config_t config = {
294+
.enter_power_save_cb = btn_enter_power_save,
295+
};
296+
297+
iot_button_register_power_save_cb(&config);
298+
280299
开启和关闭
281300
^^^^^^^^^^^^^
282301

examples/get-started/button_power_save/main/Kconfig.projbuild

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
menu "Example Configuration"
22

3+
choice ENTER_LIGHT_SLEEP_MODE
4+
prompt "Enter light sleep mode"
5+
default ENTER_LIGHT_SLEEP_AUTO
6+
depends on PM_ENABLE
7+
help
8+
Enable light sleep mode to save power.
9+
10+
config ENTER_LIGHT_SLEEP_AUTO
11+
bool "Auto enter Light Sleep"
12+
config ENTER_LIGHT_SLEEP_MODE_MANUALLY
13+
bool "Manually enter Light Sleep"
14+
endchoice
15+
316
choice EXAMPLE_MAX_CPU_FREQ
417
prompt "Maximum CPU frequency"
518
default EXAMPLE_MAX_CPU_FREQ_80 if !IDF_TARGET_ESP32H2

examples/get-started/button_power_save/main/main.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ static void button_event_cb(void *arg, void *data)
4747
}
4848
}
4949

50+
#if CONFIG_ENTER_LIGHT_SLEEP_MODE_MANUALLY
51+
void button_enter_power_save(void *usr_data)
52+
{
53+
ESP_LOGI(TAG, "Can enter power save now");
54+
esp_light_sleep_start();
55+
}
56+
#endif
57+
5058
void button_init(uint32_t button_num)
5159
{
5260
button_config_t btn_cfg = {
@@ -70,6 +78,15 @@ void button_init(uint32_t button_num)
7078
err |= iot_button_register_cb(btn, BUTTON_LONG_PRESS_START, button_event_cb, (void *)BUTTON_LONG_PRESS_START);
7179
err |= iot_button_register_cb(btn, BUTTON_LONG_PRESS_HOLD, button_event_cb, (void *)BUTTON_LONG_PRESS_HOLD);
7280
err |= iot_button_register_cb(btn, BUTTON_LONG_PRESS_UP, button_event_cb, (void *)BUTTON_LONG_PRESS_UP);
81+
82+
#if CONFIG_ENTER_LIGHT_SLEEP_MODE_MANUALLY
83+
/*!< For enter Power Save */
84+
button_power_save_config_t config = {
85+
.enter_power_save_cb = button_enter_power_save,
86+
};
87+
err |= iot_button_register_power_save_cb(&config);
88+
#endif
89+
7390
ESP_ERROR_CHECK(err);
7491
}
7592

@@ -111,6 +128,10 @@ void power_save_init(void)
111128

112129
void app_main(void)
113130
{
114-
power_save_init();
115131
button_init(BOOT_BUTTON_NUM);
132+
#if CONFIG_ENTER_LIGHT_SLEEP_AUTO
133+
power_save_init();
134+
#else
135+
esp_light_sleep_start();
136+
#endif
116137
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_ENTER_LIGHT_SLEEP_MODE_MANUALLY=y\

0 commit comments

Comments
 (0)