Compare commits
2 Commits
7dc1ade08f
...
957f440ac9
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
957f440ac9 | ||
|
|
03e7e4577e |
@ -0,0 +1,6 @@
|
||||
# The following five lines of boilerplate have to be in your project's
|
||||
# CMakeLists in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(button_power_save)
|
||||
@ -0,0 +1,43 @@
|
||||
## Button Power Save Example
|
||||
|
||||
This example demonstrates how to utilize the `button` component in conjunction with the light sleep low-power mode.
|
||||
|
||||
* `button` [Component Introduction](https://docs.espressif.com/projects/esp-iot-solution/en/latest/input_device/button.html)
|
||||
|
||||
## Hardware
|
||||
|
||||
* Any GPIO on any development board can be used in this example.
|
||||
|
||||
## Build and Flash
|
||||
|
||||
Build the project and flash it to the board, then run the monitor tool to view the serial output:
|
||||
|
||||
* Run `. ./export.sh` to set IDF environment
|
||||
* Run `idf.py set-target esp32xx` to set target chip
|
||||
* Run `idf.py -p PORT flash monitor` to build, flash and monitor the project
|
||||
|
||||
(To exit the serial monitor, type `Ctrl-]`.)
|
||||
|
||||
See the Getting Started Guide for all the steps to configure and use the ESP-IDF to build projects.
|
||||
|
||||
## Example Output
|
||||
|
||||
```
|
||||
I (1139) pm: Frequency switching config: CPU_MAX: 160, APB_MAX: 80, APB_MIN: 80, Light sleep: ENABLED
|
||||
I (1149) sleep: Code start at 42000020, total 119.03 KiB, data start at 3c000000, total 49152.00 KiB
|
||||
I (1159) button: IoT Button Version: 3.2.0
|
||||
I (1163) gpio: GPIO[0]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
|
||||
I (2922) button_power_save: Button event BUTTON_PRESS_DOWN
|
||||
I (3017) button_power_save: Button event BUTTON_PRESS_UP
|
||||
I (3017) button_power_save: Wake up from light sleep, reason 4
|
||||
I (3200) button_power_save: Button event BUTTON_SINGLE_CLICK
|
||||
I (3200) button_power_save: Wake up from light sleep, reason 4
|
||||
I (3202) button_power_save: Button event BUTTON_PRESS_REPEAT_DONE
|
||||
I (3208) button_power_save: Wake up from light sleep, reason 4
|
||||
I (3627) button_power_save: Button event BUTTON_PRESS_DOWN
|
||||
I (3702) button_power_save: Button event BUTTON_PRESS_UP
|
||||
I (3702) button_power_save: Wake up from light sleep, reason 4
|
||||
I (3887) button_power_save: Button event BUTTON_SINGLE_CLICK
|
||||
I (3887) button_power_save: Wake up from light sleep, reason 4
|
||||
I (3889) button_power_save: Button event BUTTON_PRESS_REPEAT_DONE
|
||||
```
|
||||
@ -0,0 +1,2 @@
|
||||
idf_component_register(SRCS "main.c"
|
||||
INCLUDE_DIRS ".")
|
||||
@ -0,0 +1,87 @@
|
||||
menu "Example Configuration"
|
||||
|
||||
choice ENTER_LIGHT_SLEEP_MODE
|
||||
prompt "Enter light sleep mode"
|
||||
default ENTER_LIGHT_SLEEP_AUTO
|
||||
depends on PM_ENABLE
|
||||
help
|
||||
Enable light sleep mode to save power.
|
||||
|
||||
config ENTER_LIGHT_SLEEP_AUTO
|
||||
bool "Auto enter Light Sleep"
|
||||
config ENTER_LIGHT_SLEEP_MODE_MANUALLY
|
||||
bool "Manually enter Light Sleep"
|
||||
endchoice
|
||||
|
||||
choice EXAMPLE_MAX_CPU_FREQ
|
||||
prompt "Maximum CPU frequency"
|
||||
default EXAMPLE_MAX_CPU_FREQ_80 if !IDF_TARGET_ESP32H2
|
||||
default EXAMPLE_MAX_CPU_FREQ_96 if IDF_TARGET_ESP32H2
|
||||
depends on PM_ENABLE
|
||||
help
|
||||
Maximum CPU frequency to use for dynamic frequency scaling.
|
||||
|
||||
config EXAMPLE_MAX_CPU_FREQ_80
|
||||
bool "80 MHz"
|
||||
config EXAMPLE_MAX_CPU_FREQ_96
|
||||
bool "96 MHz"
|
||||
depends on IDF_TARGET_ESP32H2
|
||||
config EXAMPLE_MAX_CPU_FREQ_120
|
||||
bool "120 MHz"
|
||||
depends on IDF_TARGET_ESP32C2
|
||||
config EXAMPLE_MAX_CPU_FREQ_160
|
||||
bool "160 MHz"
|
||||
depends on !IDF_TARGET_ESP32C2
|
||||
config EXAMPLE_MAX_CPU_FREQ_240
|
||||
bool "240 MHz"
|
||||
depends on IDF_TARGET_ESP32 || IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32S3
|
||||
endchoice
|
||||
|
||||
config EXAMPLE_MAX_CPU_FREQ_MHZ
|
||||
int
|
||||
default 80 if EXAMPLE_MAX_CPU_FREQ_80
|
||||
default 96 if EXAMPLE_MAX_CPU_FREQ_96
|
||||
default 120 if EXAMPLE_MAX_CPU_FREQ_120
|
||||
default 160 if EXAMPLE_MAX_CPU_FREQ_160
|
||||
default 240 if EXAMPLE_MAX_CPU_FREQ_240
|
||||
|
||||
choice EXAMPLE_MIN_CPU_FREQ
|
||||
prompt "Minimum CPU frequency"
|
||||
default EXAMPLE_MIN_CPU_FREQ_10M if !IDF_TARGET_ESP32H2
|
||||
default EXAMPLE_MIN_CPU_FREQ_32M if IDF_TARGET_ESP32H2
|
||||
depends on PM_ENABLE
|
||||
help
|
||||
Minimum CPU frequency to use for dynamic frequency scaling.
|
||||
Should be set to XTAL frequency or XTAL frequency divided by integer.
|
||||
|
||||
config EXAMPLE_MIN_CPU_FREQ_40M
|
||||
bool "40 MHz (use with 40MHz XTAL)"
|
||||
depends on XTAL_FREQ_40 || XTAL_FREQ_AUTO || ESP32_XTAL_FREQ_40 || ESP32_XTAL_FREQ_AUTO || !IDF_TARGET_ESP32
|
||||
config EXAMPLE_MIN_CPU_FREQ_20M
|
||||
bool "20 MHz (use with 40MHz XTAL)"
|
||||
depends on XTAL_FREQ_40 || XTAL_FREQ_AUTO || ESP32_XTAL_FREQ_40 || ESP32_XTAL_FREQ_AUTO || !IDF_TARGET_ESP32
|
||||
config EXAMPLE_MIN_CPU_FREQ_10M
|
||||
bool "10 MHz (use with 40MHz XTAL)"
|
||||
depends on XTAL_FREQ_40 || XTAL_FREQ_AUTO || ESP32_XTAL_FREQ_40 || ESP32_XTAL_FREQ_AUTO || !IDF_TARGET_ESP32
|
||||
config EXAMPLE_MIN_CPU_FREQ_26M
|
||||
bool "26 MHz (use with 26MHz XTAL)"
|
||||
depends on XTAL_FREQ_26 || XTAL_FREQ_AUTO || ESP32_XTAL_FREQ_26 || ESP32_XTAL_FREQ_AUTO
|
||||
config EXAMPLE_MIN_CPU_FREQ_13M
|
||||
bool "13 MHz (use with 26MHz XTAL)"
|
||||
depends on XTAL_FREQ_26 || XTAL_FREQ_AUTO || ESP32_XTAL_FREQ_26 || ESP32_XTAL_FREQ_AUTO
|
||||
config EXAMPLE_MIN_CPU_FREQ_32M
|
||||
bool "32 MHz (use with 32MHz XTAL)"
|
||||
depends on IDF_TARGET_ESP32H2
|
||||
depends on XTAL_FREQ_32 || XTAL_FREQ_AUTO
|
||||
endchoice
|
||||
|
||||
config EXAMPLE_MIN_CPU_FREQ_MHZ
|
||||
int
|
||||
default 40 if EXAMPLE_MIN_CPU_FREQ_40M
|
||||
default 20 if EXAMPLE_MIN_CPU_FREQ_20M
|
||||
default 10 if EXAMPLE_MIN_CPU_FREQ_10M
|
||||
default 26 if EXAMPLE_MIN_CPU_FREQ_26M
|
||||
default 13 if EXAMPLE_MIN_CPU_FREQ_13M
|
||||
default 32 if EXAMPLE_MIN_CPU_FREQ_32M
|
||||
|
||||
endmenu
|
||||
@ -0,0 +1,6 @@
|
||||
version: "0.0.1"
|
||||
dependencies:
|
||||
idf: ">=4.4"
|
||||
button:
|
||||
version: "*"
|
||||
override_path: "../../../../components/button"
|
||||
@ -0,0 +1,139 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_pm.h"
|
||||
#include "iot_button.h"
|
||||
#include "esp_sleep.h"
|
||||
#include "esp_idf_version.h"
|
||||
|
||||
/* Most development boards have "boot" button attached to GPIO0.
|
||||
* You can also change this to another pin.
|
||||
*/
|
||||
#if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32C2 || CONFIG_IDF_TARGET_ESP32H2 || CONFIG_IDF_TARGET_ESP32C6
|
||||
#define BOOT_BUTTON_NUM 9
|
||||
#else
|
||||
#define BOOT_BUTTON_NUM 0
|
||||
#endif
|
||||
#define BUTTON_ACTIVE_LEVEL 0
|
||||
|
||||
static const char *TAG = "button_power_save";
|
||||
|
||||
const char *button_event_table[] = {
|
||||
"BUTTON_PRESS_DOWN",
|
||||
"BUTTON_PRESS_UP",
|
||||
"BUTTON_PRESS_REPEAT",
|
||||
"BUTTON_PRESS_REPEAT_DONE",
|
||||
"BUTTON_SINGLE_CLICK",
|
||||
"BUTTON_DOUBLE_CLICK",
|
||||
"BUTTON_MULTIPLE_CLICK",
|
||||
"BUTTON_LONG_PRESS_START",
|
||||
"BUTTON_LONG_PRESS_HOLD",
|
||||
"BUTTON_LONG_PRESS_UP",
|
||||
"BUTTON_PRESS_END",
|
||||
};
|
||||
|
||||
static void button_event_cb(void *arg, void *data)
|
||||
{
|
||||
ESP_LOGI(TAG, "Button event %s", button_event_table[(button_event_t)data]);
|
||||
esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause();
|
||||
if (cause != ESP_SLEEP_WAKEUP_UNDEFINED) {
|
||||
ESP_LOGI(TAG, "Wake up from light sleep, reason %d", cause);
|
||||
}
|
||||
}
|
||||
|
||||
#if CONFIG_ENTER_LIGHT_SLEEP_MODE_MANUALLY
|
||||
void button_enter_power_save(void *usr_data)
|
||||
{
|
||||
ESP_LOGI(TAG, "Can enter power save now");
|
||||
esp_light_sleep_start();
|
||||
}
|
||||
#endif
|
||||
|
||||
void button_init(uint32_t button_num)
|
||||
{
|
||||
button_config_t btn_cfg = {
|
||||
.type = BUTTON_TYPE_GPIO,
|
||||
.gpio_button_config = {
|
||||
.gpio_num = button_num,
|
||||
.active_level = BUTTON_ACTIVE_LEVEL,
|
||||
#if CONFIG_GPIO_BUTTON_SUPPORT_POWER_SAVE
|
||||
.enable_power_save = true,
|
||||
#endif
|
||||
},
|
||||
};
|
||||
button_handle_t btn = iot_button_create(&btn_cfg);
|
||||
assert(btn);
|
||||
esp_err_t err = iot_button_register_cb(btn, BUTTON_PRESS_DOWN, button_event_cb, (void *)BUTTON_PRESS_DOWN);
|
||||
err |= iot_button_register_cb(btn, BUTTON_PRESS_UP, button_event_cb, (void *)BUTTON_PRESS_UP);
|
||||
err |= iot_button_register_cb(btn, BUTTON_PRESS_REPEAT, button_event_cb, (void *)BUTTON_PRESS_REPEAT);
|
||||
err |= iot_button_register_cb(btn, BUTTON_PRESS_REPEAT_DONE, button_event_cb, (void *)BUTTON_PRESS_REPEAT_DONE);
|
||||
err |= iot_button_register_cb(btn, BUTTON_SINGLE_CLICK, button_event_cb, (void *)BUTTON_SINGLE_CLICK);
|
||||
err |= iot_button_register_cb(btn, BUTTON_DOUBLE_CLICK, button_event_cb, (void *)BUTTON_DOUBLE_CLICK);
|
||||
err |= iot_button_register_cb(btn, BUTTON_LONG_PRESS_START, button_event_cb, (void *)BUTTON_LONG_PRESS_START);
|
||||
err |= iot_button_register_cb(btn, BUTTON_LONG_PRESS_HOLD, button_event_cb, (void *)BUTTON_LONG_PRESS_HOLD);
|
||||
err |= iot_button_register_cb(btn, BUTTON_LONG_PRESS_UP, button_event_cb, (void *)BUTTON_LONG_PRESS_UP);
|
||||
err |= iot_button_register_cb(btn, BUTTON_PRESS_END, button_event_cb, (void *)BUTTON_PRESS_END);
|
||||
|
||||
#if CONFIG_ENTER_LIGHT_SLEEP_MODE_MANUALLY
|
||||
/*!< For enter Power Save */
|
||||
button_power_save_config_t config = {
|
||||
.enter_power_save_cb = button_enter_power_save,
|
||||
};
|
||||
err |= iot_button_register_power_save_cb(&config);
|
||||
#endif
|
||||
|
||||
ESP_ERROR_CHECK(err);
|
||||
}
|
||||
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 0)
|
||||
void power_save_init(void)
|
||||
{
|
||||
esp_pm_config_t pm_config = {
|
||||
.max_freq_mhz = CONFIG_EXAMPLE_MAX_CPU_FREQ_MHZ,
|
||||
.min_freq_mhz = CONFIG_EXAMPLE_MIN_CPU_FREQ_MHZ,
|
||||
#if CONFIG_FREERTOS_USE_TICKLESS_IDLE
|
||||
.light_sleep_enable = true
|
||||
#endif
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_pm_configure(&pm_config));
|
||||
}
|
||||
#else
|
||||
void power_save_init(void)
|
||||
{
|
||||
#if CONFIG_IDF_TARGET_ESP32
|
||||
esp_pm_config_esp32_t pm_config = {
|
||||
#elif CONFIG_IDF_TARGET_ESP32S2
|
||||
esp_pm_config_esp32s2_t pm_config = {
|
||||
#elif CONFIG_IDF_TARGET_ESP32C3
|
||||
esp_pm_config_esp32c3_t pm_config = {
|
||||
#elif CONFIG_IDF_TARGET_ESP32S3
|
||||
esp_pm_config_esp32s3_t pm_config = {
|
||||
#elif CONFIG_IDF_TARGET_ESP32C2
|
||||
esp_pm_config_esp32c2_t pm_config = {
|
||||
#endif
|
||||
.max_freq_mhz = CONFIG_EXAMPLE_MAX_CPU_FREQ_MHZ,
|
||||
.min_freq_mhz = CONFIG_EXAMPLE_MIN_CPU_FREQ_MHZ,
|
||||
#if CONFIG_FREERTOS_USE_TICKLESS_IDLE
|
||||
.light_sleep_enable = true
|
||||
#endif
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_pm_configure(&pm_config));
|
||||
}
|
||||
#endif
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
button_init(BOOT_BUTTON_NUM);
|
||||
#if CONFIG_ENTER_LIGHT_SLEEP_AUTO
|
||||
power_save_init();
|
||||
#else
|
||||
esp_light_sleep_start();
|
||||
#endif
|
||||
}
|
||||
9
components/espressif__button/test_apps/CMakeLists.txt
Normal file
9
components/espressif__button/test_apps/CMakeLists.txt
Normal file
@ -0,0 +1,9 @@
|
||||
|
||||
# The following lines of boilerplate have to be in your project's CMakeLists
|
||||
# in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
set(EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/unit-test-app/components"
|
||||
"../../button")
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(button_test)
|
||||
@ -0,0 +1,9 @@
|
||||
if("${IDF_VERSION_MAJOR}.${IDF_VERSION_MINOR}" VERSION_GREATER_EQUAL "5.0")
|
||||
list(APPEND PRIVREQ esp_adc)
|
||||
else()
|
||||
list(APPEND PRIVREQ esp_adc_cal)
|
||||
endif()
|
||||
|
||||
idf_component_register(SRC_DIRS "."
|
||||
PRIV_INCLUDE_DIRS "."
|
||||
PRIV_REQUIRES unity test_utils button ${PRIVREQ})
|
||||
767
components/espressif__button/test_apps/main/button_test.c
Normal file
767
components/espressif__button/test_apps/main/button_test.c
Normal file
@ -0,0 +1,767 @@
|
||||
/* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "stdio.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "freertos/timers.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "esp_idf_version.h"
|
||||
#include "esp_log.h"
|
||||
#include "unity.h"
|
||||
#include "iot_button.h"
|
||||
#include "sdkconfig.h"
|
||||
|
||||
static const char *TAG = "BUTTON TEST";
|
||||
|
||||
#define TEST_MEMORY_LEAK_THRESHOLD (-400)
|
||||
#define BUTTON_IO_NUM 0
|
||||
#define BUTTON_ACTIVE_LEVEL 0
|
||||
#define BUTTON_NUM 16
|
||||
|
||||
static size_t before_free_8bit;
|
||||
static size_t before_free_32bit;
|
||||
static button_handle_t g_btns[BUTTON_NUM] = {0};
|
||||
|
||||
static int get_btn_index(button_handle_t btn)
|
||||
{
|
||||
for (size_t i = 0; i < BUTTON_NUM; i++) {
|
||||
if (btn == g_btns[i]) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void button_press_down_cb(void *arg, void *data)
|
||||
{
|
||||
TEST_ASSERT_EQUAL_HEX(BUTTON_PRESS_DOWN, iot_button_get_event(arg));
|
||||
ESP_LOGI(TAG, "BTN%d: BUTTON_PRESS_DOWN", get_btn_index((button_handle_t)arg));
|
||||
}
|
||||
|
||||
static void button_press_up_cb(void *arg, void *data)
|
||||
{
|
||||
TEST_ASSERT_EQUAL_HEX(BUTTON_PRESS_UP, iot_button_get_event(arg));
|
||||
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));
|
||||
}
|
||||
|
||||
static void button_press_repeat_cb(void *arg, void *data)
|
||||
{
|
||||
ESP_LOGI(TAG, "BTN%d: BUTTON_PRESS_REPEAT[%d]", get_btn_index((button_handle_t)arg), iot_button_get_repeat((button_handle_t)arg));
|
||||
}
|
||||
|
||||
static void button_single_click_cb(void *arg, void *data)
|
||||
{
|
||||
TEST_ASSERT_EQUAL_HEX(BUTTON_SINGLE_CLICK, iot_button_get_event(arg));
|
||||
ESP_LOGI(TAG, "BTN%d: BUTTON_SINGLE_CLICK", get_btn_index((button_handle_t)arg));
|
||||
}
|
||||
|
||||
static void button_double_click_cb(void *arg, void *data)
|
||||
{
|
||||
TEST_ASSERT_EQUAL_HEX(BUTTON_DOUBLE_CLICK, iot_button_get_event(arg));
|
||||
ESP_LOGI(TAG, "BTN%d: BUTTON_DOUBLE_CLICK", get_btn_index((button_handle_t)arg));
|
||||
}
|
||||
|
||||
static void button_long_press_start_cb(void *arg, void *data)
|
||||
{
|
||||
TEST_ASSERT_EQUAL_HEX(BUTTON_LONG_PRESS_START, iot_button_get_event(arg));
|
||||
ESP_LOGI(TAG, "BTN%d: BUTTON_LONG_PRESS_START", get_btn_index((button_handle_t)arg));
|
||||
}
|
||||
|
||||
static void button_long_press_hold_cb(void *arg, void *data)
|
||||
{
|
||||
TEST_ASSERT_EQUAL_HEX(BUTTON_LONG_PRESS_HOLD, iot_button_get_event(arg));
|
||||
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));
|
||||
}
|
||||
|
||||
static void button_press_repeat_done_cb(void *arg, void *data)
|
||||
{
|
||||
TEST_ASSERT_EQUAL_HEX(BUTTON_PRESS_REPEAT_DONE, iot_button_get_event(arg));
|
||||
ESP_LOGI(TAG, "BTN%d: BUTTON_PRESS_REPEAT_DONE[%d]", get_btn_index((button_handle_t)arg), iot_button_get_repeat((button_handle_t)arg));
|
||||
}
|
||||
|
||||
static esp_err_t custom_button_gpio_init(void *param)
|
||||
{
|
||||
button_gpio_config_t *cfg = (button_gpio_config_t *)param;
|
||||
|
||||
return button_gpio_init(cfg);
|
||||
}
|
||||
|
||||
static uint8_t custom_button_gpio_get_key_value(void *param)
|
||||
{
|
||||
button_gpio_config_t *cfg = (button_gpio_config_t *)param;
|
||||
|
||||
return button_gpio_get_key_level((void *)cfg->gpio_num);
|
||||
}
|
||||
|
||||
static esp_err_t custom_button_gpio_deinit(void *param)
|
||||
{
|
||||
button_gpio_config_t *cfg = (button_gpio_config_t *)param;
|
||||
|
||||
return button_gpio_deinit(cfg->gpio_num);
|
||||
}
|
||||
|
||||
TEST_CASE("custom button test", "[button][iot]")
|
||||
{
|
||||
button_gpio_config_t *gpio_cfg = calloc(1, sizeof(button_gpio_config_t));
|
||||
gpio_cfg->active_level = 0;
|
||||
gpio_cfg->gpio_num = 0;
|
||||
|
||||
button_config_t cfg = {
|
||||
.type = BUTTON_TYPE_CUSTOM,
|
||||
.long_press_time = CONFIG_BUTTON_LONG_PRESS_TIME_MS,
|
||||
.short_press_time = CONFIG_BUTTON_SHORT_PRESS_TIME_MS,
|
||||
.custom_button_config = {
|
||||
.button_custom_init = custom_button_gpio_init,
|
||||
.button_custom_deinit = custom_button_gpio_deinit,
|
||||
.button_custom_get_key_value = custom_button_gpio_get_key_value,
|
||||
.active_level = 0,
|
||||
.priv = gpio_cfg,
|
||||
},
|
||||
};
|
||||
|
||||
g_btns[0] = iot_button_create(&cfg);
|
||||
TEST_ASSERT_NOT_NULL(g_btns[0]);
|
||||
iot_button_register_cb(g_btns[0], BUTTON_PRESS_DOWN, button_press_down_cb, NULL);
|
||||
iot_button_register_cb(g_btns[0], BUTTON_PRESS_UP, button_press_up_cb, NULL);
|
||||
iot_button_register_cb(g_btns[0], BUTTON_PRESS_REPEAT, button_press_repeat_cb, NULL);
|
||||
iot_button_register_cb(g_btns[0], BUTTON_SINGLE_CLICK, button_single_click_cb, NULL);
|
||||
iot_button_register_cb(g_btns[0], BUTTON_DOUBLE_CLICK, button_double_click_cb, NULL);
|
||||
iot_button_register_cb(g_btns[0], BUTTON_LONG_PRESS_START, button_long_press_start_cb, NULL);
|
||||
iot_button_register_cb(g_btns[0], BUTTON_LONG_PRESS_HOLD, button_long_press_hold_cb, NULL);
|
||||
iot_button_register_cb(g_btns[0], BUTTON_PRESS_REPEAT_DONE, button_press_repeat_done_cb, NULL);
|
||||
|
||||
while (1) {
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
}
|
||||
|
||||
iot_button_delete(g_btns[0]);
|
||||
}
|
||||
|
||||
TEST_CASE("gpio button test", "[button][iot]")
|
||||
{
|
||||
button_config_t cfg = {
|
||||
.type = BUTTON_TYPE_GPIO,
|
||||
.long_press_time = CONFIG_BUTTON_LONG_PRESS_TIME_MS,
|
||||
.short_press_time = CONFIG_BUTTON_SHORT_PRESS_TIME_MS,
|
||||
.gpio_button_config = {
|
||||
.gpio_num = 0,
|
||||
.active_level = 0,
|
||||
},
|
||||
};
|
||||
g_btns[0] = iot_button_create(&cfg);
|
||||
TEST_ASSERT_NOT_NULL(g_btns[0]);
|
||||
iot_button_register_cb(g_btns[0], BUTTON_PRESS_DOWN, button_press_down_cb, NULL);
|
||||
iot_button_register_cb(g_btns[0], BUTTON_PRESS_UP, button_press_up_cb, NULL);
|
||||
iot_button_register_cb(g_btns[0], BUTTON_PRESS_REPEAT, button_press_repeat_cb, NULL);
|
||||
iot_button_register_cb(g_btns[0], BUTTON_SINGLE_CLICK, button_single_click_cb, NULL);
|
||||
iot_button_register_cb(g_btns[0], BUTTON_DOUBLE_CLICK, button_double_click_cb, NULL);
|
||||
iot_button_register_cb(g_btns[0], BUTTON_LONG_PRESS_START, button_long_press_start_cb, NULL);
|
||||
iot_button_register_cb(g_btns[0], BUTTON_LONG_PRESS_HOLD, button_long_press_hold_cb, NULL);
|
||||
iot_button_register_cb(g_btns[0], BUTTON_PRESS_REPEAT_DONE, button_press_repeat_done_cb, NULL);
|
||||
|
||||
uint8_t level = 0;
|
||||
level = iot_button_get_key_level(g_btns[0]);
|
||||
ESP_LOGI(TAG, "button level is %d", level);
|
||||
|
||||
while (1) {
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
}
|
||||
|
||||
iot_button_delete(g_btns[0]);
|
||||
}
|
||||
|
||||
TEST_CASE("gpio button get event test", "[button][iot]")
|
||||
{
|
||||
const char *EVENT_STR[] = {"BUTTON_PRESS_DOWN",
|
||||
"BUTTON_PRESS_UP",
|
||||
"BUTTON_PRESS_REPEAT",
|
||||
"BUTTON_PRESS_REPEAT_DONE",
|
||||
"BUTTON_SINGLE_CLICK",
|
||||
"BUTTON_DOUBLE_CLICK",
|
||||
"BUTTON_MULTIPLE_CLICK",
|
||||
"BUTTON_LONG_PRESS_START",
|
||||
"BUTTON_LONG_PRESS_HOLD",
|
||||
"BUTTON_LONG_PRESS_UP",
|
||||
"BUTTON_PRESS_END",
|
||||
};
|
||||
|
||||
button_config_t cfg = {
|
||||
.type = BUTTON_TYPE_GPIO,
|
||||
.long_press_time = CONFIG_BUTTON_LONG_PRESS_TIME_MS,
|
||||
.short_press_time = CONFIG_BUTTON_SHORT_PRESS_TIME_MS,
|
||||
.gpio_button_config = {
|
||||
.gpio_num = 0,
|
||||
.active_level = 0,
|
||||
},
|
||||
};
|
||||
g_btns[0] = iot_button_create(&cfg);
|
||||
TEST_ASSERT_NOT_NULL(g_btns[0]);
|
||||
|
||||
uint8_t level = 0;
|
||||
level = iot_button_get_key_level(g_btns[0]);
|
||||
ESP_LOGI(TAG, "button level is %d", level);
|
||||
|
||||
while (1) {
|
||||
button_event_t event = iot_button_get_event(g_btns[0]);
|
||||
if (event != BUTTON_NONE_PRESS) {
|
||||
printf("event is %s\n", EVENT_STR[event]);
|
||||
}
|
||||
vTaskDelay(pdMS_TO_TICKS(1));
|
||||
}
|
||||
|
||||
iot_button_delete(g_btns[0]);
|
||||
}
|
||||
|
||||
TEST_CASE("gpio button test power save", "[button][iot][power save]")
|
||||
{
|
||||
button_config_t cfg = {
|
||||
.type = BUTTON_TYPE_GPIO,
|
||||
.long_press_time = CONFIG_BUTTON_LONG_PRESS_TIME_MS,
|
||||
.short_press_time = CONFIG_BUTTON_SHORT_PRESS_TIME_MS,
|
||||
.gpio_button_config = {
|
||||
.gpio_num = 0,
|
||||
.active_level = 0,
|
||||
},
|
||||
};
|
||||
g_btns[0] = iot_button_create(&cfg);
|
||||
TEST_ASSERT_NOT_NULL(g_btns[0]);
|
||||
iot_button_register_cb(g_btns[0], BUTTON_PRESS_DOWN, button_press_down_cb, NULL);
|
||||
iot_button_register_cb(g_btns[0], BUTTON_PRESS_UP, button_press_up_cb, NULL);
|
||||
iot_button_register_cb(g_btns[0], BUTTON_PRESS_REPEAT, button_press_repeat_cb, NULL);
|
||||
iot_button_register_cb(g_btns[0], BUTTON_SINGLE_CLICK, button_single_click_cb, NULL);
|
||||
iot_button_register_cb(g_btns[0], BUTTON_DOUBLE_CLICK, button_double_click_cb, NULL);
|
||||
iot_button_register_cb(g_btns[0], BUTTON_LONG_PRESS_START, button_long_press_start_cb, NULL);
|
||||
iot_button_register_cb(g_btns[0], BUTTON_LONG_PRESS_HOLD, button_long_press_hold_cb, NULL);
|
||||
iot_button_register_cb(g_btns[0], BUTTON_PRESS_REPEAT_DONE, button_press_repeat_done_cb, NULL);
|
||||
|
||||
TEST_ASSERT_EQUAL(ESP_OK, iot_button_stop());
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
TEST_ASSERT_EQUAL(ESP_OK, iot_button_resume());
|
||||
|
||||
while (1) {
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
}
|
||||
|
||||
iot_button_delete(g_btns[0]);
|
||||
}
|
||||
|
||||
TEST_CASE("matrix keyboard button test", "[button][matrix key]")
|
||||
{
|
||||
int32_t row_gpio[4] = {4, 5, 6, 7};
|
||||
int32_t col_gpio[4] = {3, 8, 16, 15};
|
||||
button_config_t cfg = {
|
||||
.type = BUTTON_TYPE_MATRIX,
|
||||
.long_press_time = CONFIG_BUTTON_LONG_PRESS_TIME_MS,
|
||||
.short_press_time = CONFIG_BUTTON_SHORT_PRESS_TIME_MS,
|
||||
.matrix_button_config = {
|
||||
.row_gpio_num = 0,
|
||||
.col_gpio_num = 0,
|
||||
}
|
||||
};
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
cfg.matrix_button_config.row_gpio_num = row_gpio[i];
|
||||
for (int j = 0; j < 4; j++) {
|
||||
cfg.matrix_button_config.col_gpio_num = col_gpio[j];
|
||||
g_btns[i * 4 + j] = iot_button_create(&cfg);
|
||||
TEST_ASSERT_NOT_NULL(g_btns[i * 4 + j]);
|
||||
iot_button_register_cb(g_btns[i * 4 + j], BUTTON_PRESS_DOWN, button_press_down_cb, NULL);
|
||||
iot_button_register_cb(g_btns[i * 4 + j], BUTTON_PRESS_UP, button_press_up_cb, NULL);
|
||||
iot_button_register_cb(g_btns[i * 4 + j], BUTTON_PRESS_REPEAT, button_press_repeat_cb, NULL);
|
||||
iot_button_register_cb(g_btns[i * 4 + j], BUTTON_SINGLE_CLICK, button_single_click_cb, NULL);
|
||||
iot_button_register_cb(g_btns[i * 4 + j], BUTTON_DOUBLE_CLICK, button_double_click_cb, NULL);
|
||||
iot_button_register_cb(g_btns[i * 4 + j], BUTTON_LONG_PRESS_START, button_long_press_start_cb, NULL);
|
||||
iot_button_register_cb(g_btns[i * 4 + j], BUTTON_LONG_PRESS_HOLD, button_long_press_hold_cb, NULL);
|
||||
iot_button_register_cb(g_btns[i * 4 + j], BUTTON_PRESS_REPEAT_DONE, button_press_repeat_done_cb, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
while (1) {
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
}
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
for (int j = 0; j < 4; j++) {
|
||||
iot_button_delete(g_btns[i * 4 + j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if CONFIG_SOC_ADC_SUPPORTED
|
||||
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0)
|
||||
TEST_CASE("adc button test", "[button][iot]")
|
||||
{
|
||||
/** ESP32-S3-Korvo board */
|
||||
const uint16_t vol[6] = {380, 820, 1180, 1570, 1980, 2410};
|
||||
button_config_t cfg = {0};
|
||||
cfg.type = BUTTON_TYPE_ADC;
|
||||
cfg.long_press_time = CONFIG_BUTTON_LONG_PRESS_TIME_MS;
|
||||
cfg.short_press_time = CONFIG_BUTTON_SHORT_PRESS_TIME_MS;
|
||||
for (size_t i = 0; i < 6; i++) {
|
||||
cfg.adc_button_config.adc_channel = 7,
|
||||
cfg.adc_button_config.button_index = i;
|
||||
if (i == 0) {
|
||||
cfg.adc_button_config.min = (0 + vol[i]) / 2;
|
||||
} else {
|
||||
cfg.adc_button_config.min = (vol[i - 1] + vol[i]) / 2;
|
||||
}
|
||||
|
||||
if (i == 5) {
|
||||
cfg.adc_button_config.max = (vol[i] + 3000) / 2;
|
||||
} else {
|
||||
cfg.adc_button_config.max = (vol[i] + vol[i + 1]) / 2;
|
||||
}
|
||||
|
||||
g_btns[i] = iot_button_create(&cfg);
|
||||
TEST_ASSERT_NOT_NULL(g_btns[i]);
|
||||
iot_button_register_cb(g_btns[i], BUTTON_PRESS_DOWN, button_press_down_cb, NULL);
|
||||
iot_button_register_cb(g_btns[i], BUTTON_PRESS_UP, button_press_up_cb, NULL);
|
||||
iot_button_register_cb(g_btns[i], BUTTON_PRESS_REPEAT, button_press_repeat_cb, NULL);
|
||||
iot_button_register_cb(g_btns[i], BUTTON_SINGLE_CLICK, button_single_click_cb, NULL);
|
||||
iot_button_register_cb(g_btns[i], BUTTON_DOUBLE_CLICK, button_double_click_cb, NULL);
|
||||
iot_button_register_cb(g_btns[i], BUTTON_LONG_PRESS_START, button_long_press_start_cb, NULL);
|
||||
iot_button_register_cb(g_btns[i], BUTTON_LONG_PRESS_HOLD, button_long_press_hold_cb, NULL);
|
||||
iot_button_register_cb(g_btns[i], BUTTON_PRESS_REPEAT_DONE, button_press_repeat_done_cb, NULL);
|
||||
}
|
||||
|
||||
while (1) {
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
}
|
||||
for (size_t i = 0; i < 6; i++) {
|
||||
iot_button_delete(g_btns[i]);
|
||||
}
|
||||
}
|
||||
#else
|
||||
|
||||
#include "esp_adc/adc_cali.h"
|
||||
|
||||
static esp_err_t adc_calibration_init(adc_unit_t unit, adc_atten_t atten, adc_cali_handle_t *out_handle)
|
||||
{
|
||||
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
|
||||
#define ADC_BUTTON_WIDTH SOC_ADC_RTC_MAX_BITWIDTH
|
||||
#else
|
||||
#define ADC_BUTTON_WIDTH ADC_WIDTH_MAX - 1
|
||||
#endif
|
||||
|
||||
adc_cali_handle_t handle = NULL;
|
||||
esp_err_t ret = ESP_FAIL;
|
||||
bool calibrated = false;
|
||||
|
||||
#if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED
|
||||
if (!calibrated) {
|
||||
ESP_LOGI(TAG, "calibration scheme version is %s", "Curve Fitting");
|
||||
adc_cali_curve_fitting_config_t cali_config = {
|
||||
.unit_id = unit,
|
||||
.atten = atten,
|
||||
.bitwidth = ADC_BUTTON_WIDTH,
|
||||
};
|
||||
ret = adc_cali_create_scheme_curve_fitting(&cali_config, &handle);
|
||||
if (ret == ESP_OK) {
|
||||
calibrated = true;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if ADC_CALI_SCHEME_LINE_FITTING_SUPPORTED
|
||||
if (!calibrated) {
|
||||
ESP_LOGI(TAG, "calibration scheme version is %s", "Line Fitting");
|
||||
adc_cali_line_fitting_config_t cali_config = {
|
||||
.unit_id = unit,
|
||||
.atten = atten,
|
||||
.bitwidth = ADC_BUTTON_WIDTH,
|
||||
};
|
||||
ret = adc_cali_create_scheme_line_fitting(&cali_config, &handle);
|
||||
if (ret == ESP_OK) {
|
||||
calibrated = true;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
*out_handle = handle;
|
||||
if (ret == ESP_OK) {
|
||||
ESP_LOGI(TAG, "Calibration Success");
|
||||
} else if (ret == ESP_ERR_NOT_SUPPORTED || !calibrated) {
|
||||
ESP_LOGW(TAG, "eFuse not burnt, skip software calibration");
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Invalid arg or no memory");
|
||||
}
|
||||
|
||||
return calibrated ? ESP_OK : ESP_FAIL;
|
||||
}
|
||||
|
||||
TEST_CASE("adc button idf5 drive test", "[button][iot]")
|
||||
{
|
||||
adc_oneshot_unit_handle_t adc1_handle;
|
||||
adc_cali_handle_t adc1_cali_handle;
|
||||
adc_oneshot_unit_init_cfg_t init_config = {
|
||||
.unit_id = ADC_UNIT_1,
|
||||
};
|
||||
esp_err_t ret = adc_oneshot_new_unit(&init_config, &adc1_handle);
|
||||
TEST_ASSERT_TRUE(ret == ESP_OK);
|
||||
/*!< use atten 11db or 12db */
|
||||
adc_calibration_init(ADC_UNIT_1, 3, &adc1_cali_handle);
|
||||
|
||||
/** ESP32-S3-Korvo board */
|
||||
const uint16_t vol[6] = {380, 820, 1180, 1570, 1980, 2410};
|
||||
button_config_t cfg = {0};
|
||||
cfg.type = BUTTON_TYPE_ADC;
|
||||
cfg.long_press_time = CONFIG_BUTTON_LONG_PRESS_TIME_MS;
|
||||
cfg.short_press_time = CONFIG_BUTTON_SHORT_PRESS_TIME_MS;
|
||||
for (size_t i = 0; i < 6; i++) {
|
||||
cfg.adc_button_config.adc_handle = &adc1_handle;
|
||||
cfg.adc_button_config.adc_channel = 7,
|
||||
cfg.adc_button_config.button_index = i;
|
||||
if (i == 0) {
|
||||
cfg.adc_button_config.min = (0 + vol[i]) / 2;
|
||||
} else {
|
||||
cfg.adc_button_config.min = (vol[i - 1] + vol[i]) / 2;
|
||||
}
|
||||
|
||||
if (i == 5) {
|
||||
cfg.adc_button_config.max = (vol[i] + 3000) / 2;
|
||||
} else {
|
||||
cfg.adc_button_config.max = (vol[i] + vol[i + 1]) / 2;
|
||||
}
|
||||
|
||||
g_btns[i] = iot_button_create(&cfg);
|
||||
TEST_ASSERT_NOT_NULL(g_btns[i]);
|
||||
iot_button_register_cb(g_btns[i], BUTTON_PRESS_DOWN, button_press_down_cb, NULL);
|
||||
iot_button_register_cb(g_btns[i], BUTTON_PRESS_UP, button_press_up_cb, NULL);
|
||||
iot_button_register_cb(g_btns[i], BUTTON_PRESS_REPEAT, button_press_repeat_cb, NULL);
|
||||
iot_button_register_cb(g_btns[i], BUTTON_SINGLE_CLICK, button_single_click_cb, NULL);
|
||||
iot_button_register_cb(g_btns[i], BUTTON_DOUBLE_CLICK, button_double_click_cb, NULL);
|
||||
iot_button_register_cb(g_btns[i], BUTTON_LONG_PRESS_START, button_long_press_start_cb, NULL);
|
||||
iot_button_register_cb(g_btns[i], BUTTON_LONG_PRESS_HOLD, button_long_press_hold_cb, NULL);
|
||||
iot_button_register_cb(g_btns[i], BUTTON_PRESS_REPEAT_DONE, button_press_repeat_done_cb, NULL);
|
||||
}
|
||||
|
||||
while (1) {
|
||||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
}
|
||||
for (size_t i = 0; i < 6; i++) {
|
||||
iot_button_delete(g_btns[i]);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endif // CONFIG_SOC_ADC_SUPPORTED
|
||||
|
||||
#define GPIO_OUTPUT_IO_45 45
|
||||
static EventGroupHandle_t g_check = NULL;
|
||||
static SemaphoreHandle_t g_auto_check_pass = NULL;
|
||||
static const char* button_event_str[BUTTON_EVENT_MAX] = {
|
||||
"BUTTON_PRESS_DOWN",
|
||||
"BUTTON_PRESS_UP",
|
||||
"BUTTON_PRESS_REPEAT",
|
||||
"BUTTON_PRESS_REPEAT_DONE",
|
||||
"BUTTON_SINGLE_CLICK",
|
||||
"BUTTON_DOUBLE_CLICK",
|
||||
"BUTTON_MULTIPLE_CLICK",
|
||||
"BUTTON_LONG_PRESS_START",
|
||||
"BUTTON_LONG_PRESS_HOLD",
|
||||
"BUTTON_LONG_PRESS_UP",
|
||||
"BUTTON_PRESS_END",
|
||||
};
|
||||
|
||||
static button_event_t state = BUTTON_PRESS_DOWN;
|
||||
|
||||
static void button_auto_press_test_task(void *arg)
|
||||
{
|
||||
// test BUTTON_PRESS_DOWN
|
||||
xEventGroupWaitBits(g_check, BIT(0) | BIT(1), pdTRUE, pdTRUE, portMAX_DELAY);
|
||||
gpio_set_level(GPIO_OUTPUT_IO_45, 0);
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
|
||||
// // test BUTTON_PRESS_UP
|
||||
xEventGroupWaitBits(g_check, BIT(0) | BIT(1), pdTRUE, pdTRUE, portMAX_DELAY);
|
||||
gpio_set_level(GPIO_OUTPUT_IO_45, 1);
|
||||
vTaskDelay(pdMS_TO_TICKS(200));
|
||||
|
||||
// test BUTTON_PRESS_REPEAT
|
||||
xEventGroupWaitBits(g_check, BIT(0) | BIT(1), pdTRUE, pdTRUE, portMAX_DELAY);
|
||||
gpio_set_level(GPIO_OUTPUT_IO_45, 0);
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
gpio_set_level(GPIO_OUTPUT_IO_45, 1);
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
gpio_set_level(GPIO_OUTPUT_IO_45, 0);
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
|
||||
// test BUTTON_PRESS_REPEAT_DONE
|
||||
xEventGroupWaitBits(g_check, BIT(0) | BIT(1), pdTRUE, pdTRUE, portMAX_DELAY);
|
||||
gpio_set_level(GPIO_OUTPUT_IO_45, 1);
|
||||
vTaskDelay(pdMS_TO_TICKS(200));
|
||||
|
||||
// test BUTTON_SINGLE_CLICK
|
||||
xEventGroupWaitBits(g_check, BIT(0) | BIT(1), pdTRUE, pdTRUE, portMAX_DELAY);
|
||||
gpio_set_level(GPIO_OUTPUT_IO_45, 0);
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
gpio_set_level(GPIO_OUTPUT_IO_45, 1);
|
||||
vTaskDelay(pdMS_TO_TICKS(200));
|
||||
|
||||
// test BUTTON_DOUBLE_CLICK
|
||||
xEventGroupWaitBits(g_check, BIT(0) | BIT(1), pdTRUE, pdTRUE, portMAX_DELAY);
|
||||
gpio_set_level(GPIO_OUTPUT_IO_45, 0);
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
gpio_set_level(GPIO_OUTPUT_IO_45, 1);
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
gpio_set_level(GPIO_OUTPUT_IO_45, 0);
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
gpio_set_level(GPIO_OUTPUT_IO_45, 1);
|
||||
vTaskDelay(pdMS_TO_TICKS(200));
|
||||
|
||||
// test BUTTON_MULTIPLE_CLICK
|
||||
xEventGroupWaitBits(g_check, BIT(0) | BIT(1), pdTRUE, pdTRUE, portMAX_DELAY);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
gpio_set_level(GPIO_OUTPUT_IO_45, 0);
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
gpio_set_level(GPIO_OUTPUT_IO_45, 1);
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
}
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
|
||||
// test BUTTON_LONG_PRESS_START
|
||||
xEventGroupWaitBits(g_check, BIT(0) | BIT(1), pdTRUE, pdTRUE, portMAX_DELAY);
|
||||
gpio_set_level(GPIO_OUTPUT_IO_45, 0);
|
||||
vTaskDelay(pdMS_TO_TICKS(1600));
|
||||
|
||||
// test BUTTON_LONG_PRESS_HOLD and BUTTON_LONG_PRESS_UP
|
||||
xEventGroupWaitBits(g_check, BIT(0) | BIT(1), pdTRUE, pdTRUE, portMAX_DELAY);
|
||||
gpio_set_level(GPIO_OUTPUT_IO_45, 1);
|
||||
|
||||
ESP_LOGI(TAG, "Auto Press Success!");
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
static void button_auto_check_cb_1(void *arg, void *data)
|
||||
{
|
||||
if (iot_button_get_event(g_btns[0]) == state) {
|
||||
xEventGroupSetBits(g_check, BIT(1));
|
||||
}
|
||||
}
|
||||
static void button_auto_check_cb(void *arg, void *data)
|
||||
{
|
||||
if (iot_button_get_event(g_btns[0]) == state) {
|
||||
ESP_LOGI(TAG, "Auto check: button event %s pass", button_event_str[state]);
|
||||
xEventGroupSetBits(g_check, BIT(0));
|
||||
if (++state >= BUTTON_EVENT_MAX) {
|
||||
xSemaphoreGive(g_auto_check_pass);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("gpio button auto-test", "[button][iot][auto]")
|
||||
{
|
||||
state = BUTTON_PRESS_DOWN;
|
||||
g_check = xEventGroupCreate();
|
||||
g_auto_check_pass = xSemaphoreCreateBinary();
|
||||
xEventGroupSetBits(g_check, BIT(0) | BIT(1));
|
||||
button_config_t cfg = {
|
||||
.type = BUTTON_TYPE_GPIO,
|
||||
.long_press_time = CONFIG_BUTTON_LONG_PRESS_TIME_MS,
|
||||
.short_press_time = CONFIG_BUTTON_SHORT_PRESS_TIME_MS,
|
||||
.gpio_button_config = {
|
||||
.gpio_num = 0,
|
||||
.active_level = 0,
|
||||
},
|
||||
};
|
||||
g_btns[0] = iot_button_create(&cfg);
|
||||
TEST_ASSERT_NOT_NULL(g_btns[0]);
|
||||
|
||||
/* register iot_button callback for all the button_event */
|
||||
for (uint8_t i = 0; i < BUTTON_EVENT_MAX; i++) {
|
||||
if (i == BUTTON_MULTIPLE_CLICK) {
|
||||
button_event_config_t btn_cfg;
|
||||
btn_cfg.event = i;
|
||||
btn_cfg.event_data.multiple_clicks.clicks = 4;
|
||||
iot_button_register_event_cb(g_btns[0], btn_cfg, button_auto_check_cb_1, NULL);
|
||||
iot_button_register_event_cb(g_btns[0], btn_cfg, button_auto_check_cb, NULL);
|
||||
} else {
|
||||
iot_button_register_cb(g_btns[0], i, button_auto_check_cb_1, NULL);
|
||||
iot_button_register_cb(g_btns[0], i, button_auto_check_cb, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_ASSERT_EQUAL(ESP_OK, iot_button_set_param(g_btns[0], BUTTON_LONG_PRESS_TIME_MS, (void *)1500));
|
||||
|
||||
gpio_config_t io_conf = {
|
||||
.intr_type = GPIO_INTR_DISABLE,
|
||||
.mode = GPIO_MODE_OUTPUT,
|
||||
.pin_bit_mask = (1ULL << GPIO_OUTPUT_IO_45),
|
||||
.pull_down_en = 0,
|
||||
.pull_up_en = 0,
|
||||
};
|
||||
gpio_config(&io_conf);
|
||||
gpio_set_level(GPIO_OUTPUT_IO_45, 1);
|
||||
|
||||
xTaskCreate(button_auto_press_test_task, "button_auto_press_test_task", 1024 * 4, NULL, 10, NULL);
|
||||
|
||||
TEST_ASSERT_EQUAL(pdTRUE, xSemaphoreTake(g_auto_check_pass, pdMS_TO_TICKS(6000)));
|
||||
|
||||
for (uint8_t i = 0; i < BUTTON_EVENT_MAX; i++) {
|
||||
button_event_config_t btn_cfg;
|
||||
btn_cfg.event = i;
|
||||
if (i == BUTTON_MULTIPLE_CLICK) {
|
||||
btn_cfg.event_data.multiple_clicks.clicks = 4;
|
||||
} else if (i == BUTTON_LONG_PRESS_UP || i == BUTTON_LONG_PRESS_START) {
|
||||
btn_cfg.event_data.long_press.press_time = 1500;
|
||||
}
|
||||
iot_button_unregister_event(g_btns[0], btn_cfg, button_auto_check_cb);
|
||||
iot_button_unregister_event(g_btns[0], btn_cfg, button_auto_check_cb_1);
|
||||
}
|
||||
|
||||
TEST_ASSERT_EQUAL(ESP_OK, iot_button_delete(g_btns[0]));
|
||||
vEventGroupDelete(g_check);
|
||||
vSemaphoreDelete(g_auto_check_pass);
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
}
|
||||
|
||||
#define TOLERANCE CONFIG_BUTTON_LONG_PRESS_TOLERANCE_MS
|
||||
|
||||
uint16_t long_press_time[5] = {2000, 2500, 3000, 3500, 4000};
|
||||
static SemaphoreHandle_t long_press_check = NULL;
|
||||
static SemaphoreHandle_t long_press_auto_check_pass = NULL;
|
||||
unsigned int status = 0;
|
||||
|
||||
static void button_auto_long_press_test_task(void *arg)
|
||||
{
|
||||
// Test for BUTTON_LONG_PRESS_START
|
||||
for (int i = 0; i < 5; i++) {
|
||||
xSemaphoreTake(long_press_check, portMAX_DELAY);
|
||||
gpio_set_level(GPIO_OUTPUT_IO_45, 0);
|
||||
status = (BUTTON_LONG_PRESS_START << 16) | long_press_time[i];
|
||||
if (i > 0) {
|
||||
vTaskDelay(pdMS_TO_TICKS(long_press_time[i] - long_press_time[i - 1]));
|
||||
} else {
|
||||
vTaskDelay(pdMS_TO_TICKS(long_press_time[i]));
|
||||
}
|
||||
}
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
gpio_set_level(GPIO_OUTPUT_IO_45, 1);
|
||||
xSemaphoreGive(long_press_auto_check_pass);
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
// Test for BUTTON_LONG_PRESS_UP
|
||||
for (int i = 0; i < 5; i++) {
|
||||
xSemaphoreTake(long_press_check, portMAX_DELAY);
|
||||
status = (BUTTON_LONG_PRESS_UP << 16) | long_press_time[i];
|
||||
gpio_set_level(GPIO_OUTPUT_IO_45, 0);
|
||||
vTaskDelay(pdMS_TO_TICKS(long_press_time[i] + 10));
|
||||
gpio_set_level(GPIO_OUTPUT_IO_45, 1);
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "Auto Long Press Success!");
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
static void button_long_press_auto_check_cb(void *arg, void *data)
|
||||
{
|
||||
uint32_t value = (uint32_t)data;
|
||||
uint16_t event = (0xffff0000 & value) >> 16;
|
||||
uint16_t time = 0xffff & value;
|
||||
uint16_t ticks_time = iot_button_get_ticks_time(g_btns[0]);
|
||||
if (status == value && abs(ticks_time - time) <= TOLERANCE) {
|
||||
ESP_LOGI(TAG, "Auto check: button event: %s and time: %d pass", button_event_str[event], time);
|
||||
|
||||
if (event == BUTTON_LONG_PRESS_UP && time == long_press_time[4]) {
|
||||
xSemaphoreGive(long_press_auto_check_pass);
|
||||
}
|
||||
|
||||
xSemaphoreGive(long_press_check);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("gpio button long_press auto-test", "[button][long_press][auto]")
|
||||
{
|
||||
ESP_LOGI(TAG, "Starting the test");
|
||||
long_press_check = xSemaphoreCreateBinary();
|
||||
long_press_auto_check_pass = xSemaphoreCreateBinary();
|
||||
xSemaphoreGive(long_press_check);
|
||||
button_config_t cfg = {
|
||||
.type = BUTTON_TYPE_GPIO,
|
||||
.long_press_time = CONFIG_BUTTON_LONG_PRESS_TIME_MS,
|
||||
.short_press_time = CONFIG_BUTTON_SHORT_PRESS_TIME_MS,
|
||||
.gpio_button_config = {
|
||||
.gpio_num = 0,
|
||||
.active_level = 0,
|
||||
},
|
||||
};
|
||||
g_btns[0] = iot_button_create(&cfg);
|
||||
TEST_ASSERT_NOT_NULL(g_btns[0]);
|
||||
|
||||
button_event_config_t btn_cfg;
|
||||
btn_cfg.event = BUTTON_LONG_PRESS_START;
|
||||
for (int i = 0; i < 5; i++) {
|
||||
btn_cfg.event_data.long_press.press_time = long_press_time[i];
|
||||
uint32_t data = (btn_cfg.event << 16) | long_press_time[i];
|
||||
iot_button_register_event_cb(g_btns[0], btn_cfg, button_long_press_auto_check_cb, (void*)data);
|
||||
}
|
||||
|
||||
gpio_config_t io_conf = {
|
||||
.intr_type = GPIO_INTR_DISABLE,
|
||||
.mode = GPIO_MODE_OUTPUT,
|
||||
.pin_bit_mask = (1ULL << GPIO_OUTPUT_IO_45),
|
||||
.pull_down_en = 0,
|
||||
.pull_up_en = 0,
|
||||
};
|
||||
gpio_config(&io_conf);
|
||||
gpio_set_level(GPIO_OUTPUT_IO_45, 1);
|
||||
xTaskCreate(button_auto_long_press_test_task, "button_auto_long_press_test_task", 1024 * 4, NULL, 10, NULL);
|
||||
|
||||
xSemaphoreTake(long_press_auto_check_pass, portMAX_DELAY);
|
||||
iot_button_unregister_cb(g_btns[0], BUTTON_LONG_PRESS_START);
|
||||
btn_cfg.event = BUTTON_LONG_PRESS_UP;
|
||||
for (int i = 0; i < 5; i++) {
|
||||
btn_cfg.event_data.long_press.press_time = long_press_time[i];
|
||||
uint32_t data = (btn_cfg.event << 16) | long_press_time[i];
|
||||
iot_button_register_event_cb(g_btns[0], btn_cfg, button_long_press_auto_check_cb, (void*)data);
|
||||
}
|
||||
TEST_ASSERT_EQUAL(pdTRUE, xSemaphoreTake(long_press_auto_check_pass, pdMS_TO_TICKS(17000)));
|
||||
TEST_ASSERT_EQUAL(ESP_OK, iot_button_delete(g_btns[0]));
|
||||
vSemaphoreDelete(long_press_check);
|
||||
vSemaphoreDelete(long_press_auto_check_pass);
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
}
|
||||
|
||||
static void check_leak(size_t before_free, size_t after_free, const char *type)
|
||||
{
|
||||
ssize_t delta = after_free - before_free;
|
||||
printf("MALLOC_CAP_%s: Before %u bytes free, After %u bytes free (delta %d)\n", type, before_free, after_free, delta);
|
||||
TEST_ASSERT_MESSAGE(delta >= TEST_MEMORY_LEAK_THRESHOLD, "memory leak");
|
||||
}
|
||||
|
||||
void setUp(void)
|
||||
{
|
||||
before_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
|
||||
before_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
|
||||
}
|
||||
|
||||
void tearDown(void)
|
||||
{
|
||||
size_t after_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
|
||||
size_t after_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
|
||||
check_leak(before_free_8bit, after_free_8bit, "8BIT");
|
||||
check_leak(before_free_32bit, after_free_32bit, "32BIT");
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
/*
|
||||
* ____ _ _ _______ _
|
||||
*| _ \ | | | | |__ __| | |
|
||||
*| |_) | _ _ | |_ | |_ ___ _ __ | | ___ ___ | |_
|
||||
*| _ < | | | || __|| __|/ _ \ | '_ \ | | / _ \/ __|| __|
|
||||
*| |_) || |_| || |_ | |_| (_) || | | | | || __/\__ \| |_
|
||||
*|____/ \__,_| \__| \__|\___/ |_| |_| |_| \___||___/ \__|
|
||||
*/
|
||||
printf(" ____ _ _ _______ _ \n");
|
||||
printf(" | _ \\ | | | | |__ __| | | \n");
|
||||
printf(" | |_) | _ _ | |_ | |_ ___ _ __ | | ___ ___ | |_ \n");
|
||||
printf(" | _ < | | | || __|| __|/ _ \\ | '_ \\ | | / _ \\/ __|| __|\n");
|
||||
printf(" | |_) || |_| || |_ | |_| (_) || | | | | || __/\\__ \\| |_ \n");
|
||||
printf(" |____/ \\__,_| \\__| \\__|\\___/ |_| |_| |_| \\___||___/ \\__|\n");
|
||||
unity_run_menu();
|
||||
}
|
||||
29
components/espressif__button/test_apps/pytest_button.py
Normal file
29
components/espressif__button/test_apps/pytest_button.py
Normal file
@ -0,0 +1,29 @@
|
||||
# SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
'''
|
||||
Steps to run these cases:
|
||||
- Build
|
||||
- . ${IDF_PATH}/export.sh
|
||||
- pip install idf_build_apps
|
||||
- python tools/build_apps.py components/button/test_apps -t esp32s3
|
||||
- Test
|
||||
- pip install -r tools/requirements/requirement.pytest.txt
|
||||
- pytest components/button/test_apps --target esp32s3
|
||||
'''
|
||||
|
||||
import pytest
|
||||
from pytest_embedded import Dut
|
||||
|
||||
@pytest.mark.target('esp32s3')
|
||||
@pytest.mark.env('button')
|
||||
@pytest.mark.parametrize(
|
||||
'config',
|
||||
[
|
||||
'defaults',
|
||||
],
|
||||
)
|
||||
def test_button(dut: Dut)-> None:
|
||||
dut.expect_exact('Press ENTER to see the list of tests.')
|
||||
dut.write('[auto]')
|
||||
dut.expect_unity_test_output(timeout = 300)
|
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user