Skip to content

Commit

Permalink
Merge branch 'feature/button_support_power_save' into 'master'
Browse files Browse the repository at this point in the history
feat: button support power save

Closes AEG-1177

See merge request ae_group/esp-iot-solution!931
  • Loading branch information
leeebo committed Jan 9, 2024
2 parents 74e2deb + 3eba386 commit 082913b
Show file tree
Hide file tree
Showing 29 changed files with 625 additions and 48 deletions.
12 changes: 12 additions & 0 deletions .gitlab/ci/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,18 @@ build_example_get_started_blink:
EXAMPLE_DIR: examples/get-started/blink
IMAGE: espressif/idf:release-v4.4

build_example_get_started_button_power_save:
extends:
- .build_examples_template
- .rules:build:example_get_started_button_power_save
parallel:
matrix:
- IMAGE: espressif/idf:release-v4.4
- IMAGE: espressif/idf:release-v5.0
- IMAGE: espressif/idf:latest
variables:
EXAMPLE_DIR: examples/get-started/button_power_save

build_example_gprof_gprof_simple:
extends:
- .build_examples_template
Expand Down
15 changes: 15 additions & 0 deletions .gitlab/ci/rules.yml
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,9 @@
.patterns-example_get_started_blink: &patterns-example_get_started_blink
- "examples/get-started/blink/**/*"

.patterns-example_get_started_button_power_save: &patterns-example_get_started_button_power_save
- "examples/get-started/button_power_save/**/*"

.patterns-example_lighting_lightbulb: &patterns-example_lighting_lightbulb
- "examples/lighting/lightbulb/**/*"

Expand Down Expand Up @@ -637,6 +640,18 @@
- <<: *if-dev-push
changes: *patterns-example_get_started_blink

.rules:build:example_get_started_button_power_save:
rules:
- <<: *if-protected
- <<: *if-label-build
- <<: *if-trigger-job
- <<: *if-dev-push
changes: *patterns-build_system
- <<: *if-dev-push
changes: *patterns-components_button
- <<: *if-dev-push
changes: *patterns-example_get_started_button_power_save

.rules:build:example_gprof_gprof_simple:
rules:
- <<: *if-protected
Expand Down
7 changes: 7 additions & 0 deletions components/button/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# ChangeLog


## v3.2.0 - 2023-11-13

### Enhancements:

* The power consumption of GPIO buttons is lower during light sleep mode.

## v3.1.3 - 2023-11-13

* Resolved issue 'ADC_ATTEN_DB_11 is deprecated'.
Expand Down
12 changes: 11 additions & 1 deletion components/button/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ menu "IoT Button"

config BUTTON_DEBOUNCE_TICKS
int "BUTTON DEBOUNCE TICKS"
range 1 8
range 1 7
default 2
help
"One CONFIG_BUTTON_DEBOUNCE_TICKS equal to CONFIG_BUTTON_PERIOD_TIME_MS"
Expand All @@ -35,6 +35,16 @@ menu "IoT Button"
help
"Serial trigger interval"

config GPIO_BUTTON_SUPPORT_POWER_SAVE
bool "GPIO BUTTON SUPPORT POWER SAVE"
default n
help
Enable GPIO button power save

The function enables the use of GPIO buttons during light sleep,
but enabling this function prevents the simultaneous use of other
types of buttons.

config ADC_BUTTON_MAX_CHANNEL
int "ADC BUTTON MAX CHANNEL"
range 1 5
Expand Down
10 changes: 9 additions & 1 deletion components/button/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ List of supported events:
There are three ways this driver can handle buttons:
1. Buttons connected to standard digital GPIO
2. Multiple buttons connected to single ADC channel
3. Custom button connect to any driver
3. Matrix keyboard employs multiple GPIOs for operation.
4. Custom button connect to any driver

The component supports the following functionalities:
1. Creation of an unlimited number of buttons, accommodating various types simultaneously.
2. Multiple callback functions for a single event.
3. Allowing customization of the consecutive key press count to any desired number.
4. Facilitating the setup of callbacks for any specified long-press duration.
5. Support power save mode (Only for gpio button)

## Add component to your project

Expand Down
33 changes: 33 additions & 0 deletions components/button/button_gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "esp_log.h"
#include "driver/gpio.h"
#include "button_gpio.h"
#include "esp_sleep.h"

static const char *TAG = "gpio button";

Expand Down Expand Up @@ -34,6 +35,16 @@ esp_err_t button_gpio_init(const button_gpio_config_t *config)
}
gpio_config(&gpio_conf);

#if CONFIG_GPIO_BUTTON_SUPPORT_POWER_SAVE
if (config->enable_power_save) {
/* Enable wake up from GPIO */
esp_err_t ret = gpio_wakeup_enable(config->gpio_num, config->active_level == 0 ? GPIO_INTR_LOW_LEVEL : GPIO_INTR_HIGH_LEVEL);
GPIO_BTN_CHECK(ret == ESP_OK, "Enable gpio wakeup failed", ESP_FAIL);
ret = esp_sleep_enable_gpio_wakeup();
GPIO_BTN_CHECK(ret == ESP_OK, "Configure gpio as wakeup source failed", ESP_FAIL);
}
#endif

return ESP_OK;
}

Expand All @@ -46,3 +57,25 @@ uint8_t button_gpio_get_key_level(void *gpio_num)
{
return (uint8_t)gpio_get_level((uint32_t)gpio_num);
}

esp_err_t button_gpio_set_intr(int gpio_num, gpio_int_type_t intr_type, gpio_isr_t isr_handler, void *args)
{
static bool isr_service_installed = false;
gpio_set_intr_type(gpio_num, intr_type);
if (!isr_service_installed) {
gpio_install_isr_service(ESP_INTR_FLAG_IRAM);
isr_service_installed = true;
}
gpio_isr_handler_add(gpio_num, isr_handler, args);
return ESP_OK;
}

esp_err_t button_gpio_intr_control(int gpio_num, bool enable)
{
if (enable) {
gpio_intr_enable(gpio_num);
} else {
gpio_intr_disable(gpio_num);
}
return ESP_OK;
}
4 changes: 3 additions & 1 deletion components/button/idf_component.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
version: "3.1.3"
version: "3.2.0"
description: GPIO and ADC button driver
url: https://github.com/espressif/esp-iot-solution/tree/master/components/button
repository: https://github.com/espressif/esp-iot-solution.git
documentation: https://docs.espressif.com/projects/esp-iot-solution/en/latest/input_device/button.html
issues: https://github.com/espressif/esp-iot-solution/issues
examples:
- path: ../../examples/get-started/button_power_save
dependencies:
idf: ">=4.0"
cmake_utilities: "0.*"
25 changes: 24 additions & 1 deletion components/button/include/button_gpio.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
/* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -18,6 +18,9 @@ extern "C" {
typedef struct {
int32_t gpio_num; /**< num of gpio */
uint8_t active_level; /**< gpio level when press down */
#if CONFIG_GPIO_BUTTON_SUPPORT_POWER_SAVE
bool enable_power_save; /**< enable power save mode */
#endif
} button_gpio_config_t;

/**
Expand Down Expand Up @@ -49,6 +52,26 @@ esp_err_t button_gpio_deinit(int gpio_num);
*/
uint8_t button_gpio_get_key_level(void *gpio_num);

/**
* @brief Sets up interrupt for GPIO button.
*
* @param gpio_num gpio number of button
* @param intr_type The type of GPIO interrupt.
* @param isr_handler The ISR (Interrupt Service Routine) handler function.
* @param args Arguments to be passed to the ISR handler function.
* @return Always return ESP_OK
*/
esp_err_t button_gpio_set_intr(int gpio_num, gpio_int_type_t intr_type, gpio_isr_t isr_handler, void *args);

/**
* @brief Enable or disable interrupt for GPIO button.
*
* @param gpio_num gpio number of button
* @param enable enable or disable
* @return Always return ESP_OK
*/
esp_err_t button_gpio_intr_control(int gpio_num, bool enable);

#ifdef __cplusplus
}
#endif
12 changes: 11 additions & 1 deletion components/button/include/iot_button.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
/* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -268,6 +268,16 @@ uint16_t iot_button_get_long_press_hold_cnt(button_handle_t btn_handle);
*/
esp_err_t iot_button_set_param(button_handle_t btn_handle, button_param_t param, void *value);

/**
* @brief Get button key level
*
* @param btn_handle Button handle
* @return
* - 1 if key is pressed
* - 0 if key is released or invalid button handle
*/
uint8_t iot_button_get_key_level(button_handle_t btn_handle);

/**
* @brief resume button timer, if button timer is stopped. Make sure iot_button_create() is called before calling this API.
*
Expand Down
Loading

0 comments on commit 082913b

Please sign in to comment.