diff --git a/components/led/led_indicator/CMakeLists.txt b/components/led/led_indicator/CMakeLists.txt index c25bd43dbe..1515c7ddd1 100644 --- a/components/led/led_indicator/CMakeLists.txt +++ b/components/led/led_indicator/CMakeLists.txt @@ -1,12 +1,16 @@ -set(srcs "src/led_ledc.c" +set(srcs "src/led_indicator_ledc.c" "src/led_gamma.c" - "src/led_gpio.c" + "src/led_indicator_gpio.c" "src/led_indicator.c" - "src/led_indicator_blink_default.c" - "src/led_rgb.c" - "src/led_strips.c" + "src/led_indicator_rgb.c" + "src/led_indicator_strips.c" "src/led_convert.c" ) +if(CONFIG_USE_MI_BLINK_DEFAULT) + list(APPEND srcs "src/mi_led_indicator_blink_default.c") +else() + list(APPEND srcs "src/led_indicator_blink_default.c") +endif() idf_component_register( SRCS ${srcs} INCLUDE_DIRS "include" diff --git a/components/led/led_indicator/Kconfig b/components/led/led_indicator/Kconfig index bf297dde2c..4e56963dbe 100644 --- a/components/led/led_indicator/Kconfig +++ b/components/led/led_indicator/Kconfig @@ -9,6 +9,10 @@ menu "LED Indicator" bool "Enable gamma correction" default "y" + config USE_MI_BLINK_DEFAULT + bool "Use Xiaomi blink default" + default "n" + menu "LEDC Config" choice LEDC_SPEED_MODE diff --git a/components/led/led_indicator/include/led_convert.h b/components/led/led_indicator/include/led_convert.h index b91020ac2b..be33488a87 100644 --- a/components/led/led_indicator/include/led_convert.h +++ b/components/led/led_indicator/include/led_convert.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ diff --git a/components/led/led_indicator/include/led_custom.h b/components/led/led_indicator/include/led_custom.h deleted file mode 100644 index 911cbc12ce..0000000000 --- a/components/led/led_indicator/include/led_custom.h +++ /dev/null @@ -1,58 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#pragma once - -#ifdef __cplusplus -extern "C" { -#endif - -#include -#include -#include "esp_err.h" - -/** - * @brief LED duty should be consistent with the physical resolution of the indicator. - * eg. LED_GPIO_MODE should with LED_DUTY_1_BIT - * - */ -typedef enum { - LED_DUTY_1_BIT = 1, /*!< LED duty resolution of 1 bits */ - LED_DUTY_2_BIT, /*!< LED duty resolution of 2 bits */ - LED_DUTY_3_BIT, /*!< LED duty resolution of 3 bits */ - LED_DUTY_4_BIT, /*!< LED duty resolution of 4 bits */ - LED_DUTY_5_BIT, /*!< LED duty resolution of 5 bits */ - LED_DUTY_6_BIT, /*!< LED duty resolution of 6 bits */ - LED_DUTY_7_BIT, /*!< LED duty resolution of 7 bits */ - LED_DUTY_8_BIT, /*!< LED duty resolution of 8 bits */ - LED_DUTY_9_BIT, /*!< LED duty resolution of 9 bits */ - LED_DUTY_10_BIT, /*!< LED duty resolution of 10 bits */ - LED_DUTY_11_BIT, /*!< LED duty resolution of 11 bits */ - LED_DUTY_12_BIT, /*!< LED duty resolution of 12 bits */ - LED_DUTY_13_BIT, /*!< LED duty resolution of 13 bits */ - LED_DUTY_14_BIT, /*!< LED duty resolution of 14 bits */ - LED_DUTY_15_BIT, /*!< LED duty resolution of 15 bits */ -} led_indicator_duty_t; - -/** - * @brief LED custom configuration - * - */ -typedef struct { - bool is_active_level_high; /*!< Set true if GPIO level is high when light is ON, otherwise false, values should be modified based on the actual scenario.*/ - led_indicator_duty_t duty_resolution; /*!< Resolution of duty setting in number of bits. The range of duty values is [0, (2**duty_resolution) -1]. If the brightness cannot be set, set this as 1. */ - esp_err_t (*hal_indicator_init)(void *hardware_data); /*!< pointer functions for initialization*/ - esp_err_t (*hal_indicator_set_on_off)(void *hardware_data, bool on_off); /*!< pointer functions for setting on or off */ - esp_err_t (*hal_indicator_deinit)(void *hardware_data); /*!< pointer functions for deinitialization */ - esp_err_t (*hal_indicator_set_brightness)(void *hardware_data, uint32_t brightness); /*!< pointer functions for setting brightness, must be supported by hardware */ - esp_err_t (*hal_indicator_set_rgb)(void *hardware, uint32_t rgb_value); /*!< Pointer function for setting rgb, must be supported by hardware */ - esp_err_t (*hal_indicator_set_hsv)(void *hardware, uint32_t hsv_value); /*!< Pointer function for setting rgb, must be supported by hardware */ - void *hardware_data; /*!< user hardware data*/ -} led_indicator_custom_config_t; - -#ifdef __cplusplus -} -#endif diff --git a/components/led/led_indicator/include/led_gpio.h b/components/led/led_indicator/include/led_gpio.h deleted file mode 100644 index a12a2507d7..0000000000 --- a/components/led/led_indicator/include/led_gpio.h +++ /dev/null @@ -1,59 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#pragma once - -#include "driver/gpio.h" - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * @brief LED GPIO configuration - * - */ -typedef struct { - bool is_active_level_high; /*!< Set true if GPIO level is high when light is ON, otherwise false. */ - int32_t gpio_num; /*!< num of GPIO */ -} led_indicator_gpio_config_t; - -/** - * @brief Initialize the specific GPIO to work as a LED indicator - * - * @param param led_indicator_gpio_config_t - * @param ret_handle gpio_handle - * @return esp_err_t - * - ESP_OK success - * - ESP_ERR_INVALID_ARG Parameter error - */ -esp_err_t led_indicator_gpio_init(void *param, void **ret_handle); - -/** - * @brief Deinitialize the specific GPIO that works as a LED indicator - * - * @param handle GPIO handle - * @return esp_err_t - * - ESP_OK success - */ -esp_err_t led_indicator_gpio_deinit(void *handle); - -/** - * @brief Set the specific GPIO's level to make the LED indicator ON or OFF - * - * @param handle GPIO handle - * @param on_off Set number to control the GPIO's level. If the LED's positive side is connected to this GPIO, then setting number greater than 0 will make the LED OFF, - * and setting 0 will make the LED ON. If the LED's negative side is connected to this GPIO, then setting 0 will make the LED OFF, and - * setting number greater than 0 will make the LED ON. - * @return esp_err_t - * - ESP_OK Success - * - ESP_ERR_INVALID_ARG GPIO number error - */ -esp_err_t led_indicator_gpio_set_on_off(void *handle, bool on_off); - -#ifdef __cplusplus -} -#endif diff --git a/components/led/led_indicator/include/led_indicator.h b/components/led/led_indicator/include/led_indicator.h index a232f79fa9..40950abcc6 100644 --- a/components/led/led_indicator/include/led_indicator.h +++ b/components/led/led_indicator/include/led_indicator.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -12,90 +12,44 @@ extern "C" { #include #include +#include "freertos/FreeRTOS.h" +#include "freertos/timers.h" +#include "freertos/semphr.h" #include "esp_err.h" -#include "led_gpio.h" -#include "led_ledc.h" -#include "led_rgb.h" -#include "led_strips.h" #include "led_convert.h" -#include "led_custom.h" +#include "led_types.h" -/** - * @brief LED state: 0-100, only hardware that supports to set brightness can adjust brightness. - * - */ -enum { - LED_STATE_OFF = 0, /*!< turn off the LED */ - LED_STATE_25_PERCENT = 64, /*!< 25% brightness, must support to set brightness */ - LED_STATE_50_PERCENT = 128, /*!< 50% brightness, must support to set brightness */ - LED_STATE_75_PERCENT = 191, /*!< 75% brightness, must support to set brightness */ - LED_STATE_ON = UINT8_MAX, /*!< turn on the LED */ -}; +#define LED_INDICATOR_CHECK(a, str, action) if(!(a)) { \ + ESP_LOGE(TAG,"%s:%d (%s):%s", __FILE__, __LINE__, __FUNCTION__, str); \ + action; \ + } -/** - * @brief actions in this type - * - */ -typedef enum { - LED_BLINK_STOP = -1, /*!< stop the blink */ - LED_BLINK_HOLD, /*!< hold the on-off state */ - LED_BLINK_BREATHE, /*!< breathe state */ - LED_BLINK_BRIGHTNESS, /*!< set the brightness, it will transition from the old brightness to the new brightness */ - LED_BLINK_RGB, /*!< color change with R(0-255) G(0-255) B(0-255) */ - LED_BLINK_RGB_RING, /*!< Gradual color transition from old color to new color in a color ring */ - LED_BLINK_HSV, /*!< color change with H(0-360) S(0-255) V(0-255) */ - LED_BLINK_HSV_RING, /*!< Gradual color transition from old color to new color in a color ring */ - LED_BLINK_LOOP, /*!< loop from first step */ -} blink_step_type_t; +#define LED_INDICATOR_CHECK_WARNING(a, str, action) if(!(a)) { \ + ESP_LOGW(TAG,"%s:%d (%s):%s", __FILE__, __LINE__, __FUNCTION__, str); \ + action; \ + } /** - * @brief one blink step, a meaningful signal consists of a group of steps + * @brief Creates a new LED indicator instance with the specified configuration. * - */ -typedef struct { - blink_step_type_t type; /*!< action type in this step */ - uint32_t value; /*!< hold on or off, set 0 if LED_BLINK_STOP() or LED_BLINK_LOOP */ - uint32_t hold_time_ms; /*!< hold time(ms), set 0 if not LED_BLINK_HOLD */ -} blink_step_t; - -/** - * @brief LED indicator blink mode, as a member of led_indicator_config_t + * @param cfg Pointer to a configuration structure of type _led_indicator_com_config_t + * containing initialization parameters for the LED indicator. * + * @return Pointer to the created _led_indicator_t instance on success, or NULL on failure. */ -typedef enum { - LED_GPIO_MODE, /*!< blink with max brightness */ - LED_LEDC_MODE, /*!< blink with LEDC driver */ - LED_RGB_MODE, /*!< blink with RGB driver */ - LED_STRIPS_MODE, /*!< blink with LEDC strips driver */ - LED_CUSTOM_MODE, /*!< blink with custom driver */ -} led_indicator_mode_t; +_led_indicator_t *_led_indicator_create_com(_led_indicator_com_config_t *cfg); /** - * @brief LED indicator specified configurations, as a arg when create a new indicator + * @brief Add a new node to the LED indicator instance. * - */ -typedef struct { - led_indicator_mode_t mode; /*!< LED work mode, eg. GPIO or pwm mode */ - union { - led_indicator_gpio_config_t *led_indicator_gpio_config; /*!< LED GPIO configuration */ - led_indicator_ledc_config_t *led_indicator_ledc_config; /*!< LED LEDC configuration */ - led_indicator_rgb_config_t *led_indicator_rgb_config; /*!< LED RGB configuration */ - led_indicator_strips_config_t *led_indicator_strips_config; /*!< LED LEDC rgb configuration */ - led_indicator_custom_config_t *led_indicator_custom_config; /*!< LED custom configuration */ - }; /**< LED configuration */ - blink_step_t const **blink_lists; /*!< user defined LED blink lists */ - uint16_t blink_list_num; /*!< number of blink lists */ -} led_indicator_config_t; - -typedef void *led_indicator_handle_t; /*!< LED indicator operation handle */ - -/** - * @brief create a LED indicator instance with GPIO number and configuration + * @param[in,out] p_led_indicator Pointer to the LED indicator instance to which the node will be added. * - * @param config configuration of the LED, eg. GPIO level when LED off - * @return led_indicator_handle_t handle of the LED indicator, NULL if create failed. + * @return esp_err_t + * - ESP_ERR_INVALID_ARG if parameter is invalid + * - ESP_ERR_NOT_FOUND no predefined blink_type found + * - ESP_OK Success */ -led_indicator_handle_t led_indicator_create(const led_indicator_config_t *config); +esp_err_t _led_indicator_add_node(_led_indicator_t *p_led_indicator); /** * @brief delete the LED indicator and release resource diff --git a/components/led/led_indicator/include/led_indicator_blink_default.h b/components/led/led_indicator/include/led_indicator_blink_default.h index 81cd71e2d4..20b46cf544 100644 --- a/components/led/led_indicator/include/led_indicator_blink_default.h +++ b/components/led/led_indicator/include/led_indicator_blink_default.h @@ -1,11 +1,11 @@ /* - * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ #pragma once - +#include "led_indicator.h" #ifdef __cplusplus extern "C" { #endif @@ -14,6 +14,27 @@ extern "C" { * @brief The blink type with smaller index has the higher priority * eg. BLINK_FACTORY_RESET priority is higher than BLINK_UPDATING */ +#ifdef CONFIG_USE_MI_BLINK_DEFAULT +enum { + BLINK_MI_WAIT_CONNECT, /*!< Device waiting for connection */ + BLINK_MI_CONNECTING, /*!< Connecting to network */ + BLINK_MI_ONLINE, /*!< Device online */ + BLINK_MI_FAULT, /*!< Device fault (connection failed/network error/disconnected) */ + BLINK_MI_RECONNECTING, /*!< Reconnecting to network */ + BLINK_MI_OTA_UPDATING, /*!< OTA updating */ + BLINK_MI_WIFI_PROVISION_OFF, /*!< Wi-Fi provision function off */ + BLINK_MI_WORKING, /*!< Device working normally */ + BLINK_MI_WORK_ABNORMAL, /*!< Device abnormal or high-risk alarm */ + BLINK_MI_ENV_GOOD, /*!< Environment status good */ + BLINK_MI_ENV_MODERATE, /*!< Environment moderate pollution or call action */ + BLINK_MI_ENV_SEVERE, /*!< Environment severe pollution or device fault */ + BLINK_MI_SETTING, /*!< Device in setting mode */ + BLINK_MI_LOW_BATTERY, /*!< Low battery */ + BLINK_MI_CHARGING, /*!< Charging */ + BLINK_MI_CHARGED, /*!< Fully charged */ + BLINK_MAX, /*!< INVALID type */ +}; +#else enum { BLINK_FACTORY_RESET, /*!< restoring factory settings */ BLINK_UPDATING, /*!< updating software */ @@ -24,6 +45,7 @@ enum { BLINK_PROVISIONING, /*!< provisioning */ BLINK_MAX, /*!< INVALID type */ }; +#endif extern const int DEFAULT_BLINK_LIST_NUM; extern blink_step_t const *default_led_indicator_blink_lists[]; diff --git a/components/led/led_indicator/include/led_indicator_gpio.h b/components/led/led_indicator/include/led_indicator_gpio.h new file mode 100644 index 0000000000..c29d15b73b --- /dev/null +++ b/components/led/led_indicator/include/led_indicator_gpio.h @@ -0,0 +1,40 @@ +/* + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include "driver/gpio.h" +#include "led_indicator.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief LED GPIO configuration + * + */ +typedef struct { + bool is_active_level_high; /*!< Set true if GPIO level is high when light is ON, otherwise false. */ + int32_t gpio_num; /*!< num of GPIO */ +} led_indicator_gpio_config_t; + +/** + * @brief Create a new LED indicator device using a GPIO pin. + * + * + * @param led_config Pointer to the LED configuration structure. + * @param gpio_cfg Pointer to the GPIO configuration structure for the LED. + * @param handle pointer to LED indicator handle + * @return esp_err_t + * - ESP_ERR_INVALID_ARG if parameter is invalid + * - ESP_OK Success + * - ESP_FAIL Delete fail + */ +esp_err_t led_indicator_new_gpio_device(const led_indicator_config_t *led_config, const led_indicator_gpio_config_t *gpio_cfg, led_indicator_handle_t *handle); +#ifdef __cplusplus +} +#endif diff --git a/components/led/led_indicator/include/led_indicator_ledc.h b/components/led/led_indicator/include/led_indicator_ledc.h new file mode 100644 index 0000000000..e5b4764fef --- /dev/null +++ b/components/led/led_indicator/include/led_indicator_ledc.h @@ -0,0 +1,40 @@ +/* + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include "driver/ledc.h" +#include "led_indicator.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct { + bool is_active_level_high; /*!< Set true if GPIO level is high when light is ON, otherwise false. */ + bool timer_inited; /*!< Set true if LEDC timer is inited, otherwise false. */ + ledc_timer_t timer_num; /*!< The timer source of channel */ + int32_t gpio_num; /*!< num of gpio */ + ledc_channel_t channel; /*!< LEDC channel */ +} led_indicator_ledc_config_t; + +/** + * @brief Create a new LED indicator device using the LEDC (LED Controller) peripheral. + * + * + * @param led_config Pointer to the general LED configuration structure. + * @param ledc_cfg Pointer to the LEDC-specific configuration structure. + * @param handle pointer to LED indicator handle + * @return esp_err_t + * - ESP_ERR_INVALID_ARG if parameter is invalid + * - ESP_OK Success + * - ESP_FAIL Delete fail + */ +esp_err_t led_indicator_new_ledc_device(const led_indicator_config_t *led_config, const led_indicator_ledc_config_t *ledc_cfg, led_indicator_handle_t *handle); + +#ifdef __cplusplus +} +#endif diff --git a/components/led/led_indicator/include/led_indicator_rgb.h b/components/led/led_indicator/include/led_indicator_rgb.h new file mode 100644 index 0000000000..ec6079df60 --- /dev/null +++ b/components/led/led_indicator/include/led_indicator_rgb.h @@ -0,0 +1,47 @@ +/* + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#pragma once + +#include "driver/ledc.h" +#include "led_indicator.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define LEDC_MODE CONFIG_LEDC_SPEED_MODE_VALUE +#define LEDC_DUTY_RES CONFIG_LEDC_TIMER_BIT_NUM +#define LEDC_FREQ_HZ CONFIG_LEDC_TIMER_FREQ_HZ + +typedef struct { + bool is_active_level_high; /*!< Set true if GPIO level is high when light is ON, otherwise false. */ + bool timer_inited; /*!< Set true if LEDC timer is inited, otherwise false. */ + ledc_timer_t timer_num; /*!< The timer source of channel */ + int32_t red_gpio_num; /*!< Red LED pwm gpio number */ + int32_t green_gpio_num; /*!< Green LED pwm gpio number */ + int32_t blue_gpio_num; /*!< Blue LED pwm gpio number */ + ledc_channel_t red_channel; /*!< Red LED LEDC channel */ + ledc_channel_t green_channel; /*!< Green LED LEDC channel */ + ledc_channel_t blue_channel; /*!< Blue LED LEDC channel */ +} led_indicator_rgb_config_t; + +/** + * @brief Create a new RGB LED device instance. + * + * + * @param led_config Pointer to the general LED configuration structure. + * @param rgb_cfg Pointer to the RGB-specific configuration structure. + * @param handle pointer to LED indicator handle + * @return esp_err_t + * - ESP_ERR_INVALID_ARG if parameter is invalid + * - ESP_OK Success + * - ESP_FAIL Delete fail + */ +esp_err_t led_indicator_new_rgb_device(const led_indicator_config_t *led_config, const led_indicator_rgb_config_t *rgb_cfg, led_indicator_handle_t *handle); + +#ifdef __cplusplus +} +#endif diff --git a/components/led/led_indicator/include/led_indicator_strips.h b/components/led/led_indicator/include/led_indicator_strips.h new file mode 100644 index 0000000000..26bd2a2a81 --- /dev/null +++ b/components/led/led_indicator/include/led_indicator_strips.h @@ -0,0 +1,56 @@ +/* + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include "led_strip.h" +#include "led_strip_types.h" +#include "esp_idf_version.h" +#include "led_indicator.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief led strip driver type + * + */ +typedef enum { + LED_STRIP_RMT, +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 0) + LED_STRIP_SPI, +#endif + LED_STRIP_MAX, +} led_strip_driver_t; + +typedef struct { + led_strip_config_t led_strip_cfg; /*!< LED Strip Configuration. */ + led_strip_driver_t led_strip_driver; /*!< led strip control type */ + union { + led_strip_rmt_config_t led_strip_rmt_cfg; /*!< RMT (Remote Control) Configuration for the LED strip. */ +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 0) + led_strip_spi_config_t led_strip_spi_cfg; /*!< SPI Configuration for the LED strip. */ +#endif + }; +} led_indicator_strips_config_t; + +/** + * @brief Create a new LED indicator device using LED strips. + * + * + * @param led_config Pointer to the LED configuration structure. + * @param strips_cfg Pointer to the LED strips specific configuration structure. + * @param handle pointer to LED indicator handle + * @return esp_err_t + * - ESP_ERR_INVALID_ARG if parameter is invalid + * - ESP_OK Success + * - ESP_FAIL Delete fail + */ +esp_err_t led_indicator_new_strips_device(const led_indicator_config_t *led_config, const led_indicator_strips_config_t *strips_cfg, led_indicator_handle_t *handle); +#ifdef __cplusplus +} +#endif diff --git a/components/led/led_indicator/include/led_ledc.h b/components/led/led_indicator/include/led_ledc.h deleted file mode 100644 index abdfbaa003..0000000000 --- a/components/led/led_indicator/include/led_ledc.h +++ /dev/null @@ -1,70 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#pragma once - -#include "driver/ledc.h" - -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct { - bool is_active_level_high; /*!< Set true if GPIO level is high when light is ON, otherwise false. */ - bool timer_inited; /*!< Set true if LEDC timer is inited, otherwise false. */ - ledc_timer_t timer_num; /*!< The timer source of channel */ - int32_t gpio_num; /*!< num of gpio */ - ledc_channel_t channel; /*!< LEDC channel */ -} led_indicator_ledc_config_t; - -/** - * @brief Initialize LEDC-related configurations for this LED indicator. - * - * @param param LEDC config: ledc_timer_config & ledc_channel_config - * @return esp_err_t - * - ESP_OK Success - * - ESP_FAIL Deinit fail - * - ESP_ERR_NO_MEM failed to request memory - */ -esp_err_t led_indicator_ledc_init(void *param); - -/** - * @brief Deinitialize LEDC which is used by the LED indicator. - * - * @param ledc_handle LED indicator LEDC operation handle - * @return esp_err_t - * - ESP_OK Success - * - ESP_FAIL Deinit fail - */ -esp_err_t led_indicator_ledc_deinit(void *ledc_handle); - -/** - * @brief Set the specific LEDC's level to make the LED indicator ON or OFF - * - * @param ledc_handle LED indicator LEDC operation handle - * @param on_off Set 0 to control the LEDC's output level low, while values greater than 0 set the LEDC's output level high.. - * @return esp_err_t - * - ESP_OK Success - * - ESP_ERR_INVALID_ARG Parameter error - * - ESP_FAIL LEDC channel doesn't init - */ -esp_err_t led_indicator_ledc_set_on_off(void *ledc_handle, bool on_off); - -/** - * @brief Set LEDC duty cycle - * - * @param ledc_handle LED indicator LEDC operation handle - * @param brightness duty cycle of LEDC [0-255] - * @return esp_err_t - * - ESP_OK Success - * - ESP_ERR_INVALID_ARG Parameter error - * - ESP_FAIL Set brightness fail - */ -esp_err_t led_indicator_ledc_set_brightness(void *ledc_handle, uint32_t brightness); - -#ifdef __cplusplus -} -#endif diff --git a/components/led/led_indicator/include/led_rgb.h b/components/led/led_indicator/include/led_rgb.h deleted file mode 100644 index 46d5c7f308..0000000000 --- a/components/led/led_indicator/include/led_rgb.h +++ /dev/null @@ -1,102 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ -#pragma once - -#include "driver/ledc.h" - -#ifdef __cplusplus -extern "C" { -#endif - -#define LEDC_MODE CONFIG_LEDC_SPEED_MODE_VALUE -#define LEDC_DUTY_RES CONFIG_LEDC_TIMER_BIT_NUM -#define LEDC_FREQ_HZ CONFIG_LEDC_TIMER_FREQ_HZ - -typedef struct { - bool is_active_level_high; /*!< Set true if GPIO level is high when light is ON, otherwise false. */ - bool timer_inited; /*!< Set true if LEDC timer is inited, otherwise false. */ - ledc_timer_t timer_num; /*!< The timer source of channel */ - int32_t red_gpio_num; /*!< Red LED pwm gpio number */ - int32_t green_gpio_num; /*!< Green LED pwm gpio number */ - int32_t blue_gpio_num; /*!< Blue LED pwm gpio number */ - ledc_channel_t red_channel; /*!< Red LED LEDC channel */ - ledc_channel_t green_channel; /*!< Green LED LEDC channel */ - ledc_channel_t blue_channel; /*!< Blue LED LEDC channel */ -} led_indicator_rgb_config_t; - -/** - * @brief Initialize the RGB LED indicator (WS2812 SK6812). - * - * @param param Pointer to initialization parameters. - * @param ret_rgb Pointer to a variable that will hold the LED RGB instance. - * @return esp_err_t - * - ESP_OK: Success - * - ESP_FAIL: Initialization failed - * - ESP_ERR_INVALID_ARG: Invalid argument provided - * - ESP_ERR_NO_MEM: Memory allocation failed - */ -esp_err_t led_indicator_rgb_init(void *param, void **ret_rgb); - -/** - * @brief Deinitialize led RGB which is used by the LED indicator. - * - * @param rgb_handle LED indicator LED RGB operation handle - * @return esp_err_t - * - ESP_OK Success - * - ESP_FAIL Deinit fail - */ -esp_err_t led_indicator_rgb_deinit(void *rgb_handle); - -/** - * @brief Turn the LED indicator on or off. - * - * @param rgb_handle LED indicator LED RGB operation handle. - * @param on_off Set to 0 or 1 to control the LED (0 for off, 1 for on). - * @return esp_err_t - * - ESP_OK: Success - * - ESP_ERR_INVALID_ARG: Invalid argument provided - * - ESP_FAIL: LEDC channel initialization failed - */ -esp_err_t led_indicator_rgb_set_on_off(void *rgb_handle, bool on_off); - -/** - * @brief Set the RGB color for the LED indicator. - * - * @param rgb_handle LED indicator LED RGB operation handle. - * @param rgb_value RGB color value to set. (R: 0-255, G: 0-255, B: 0-255) - * @return esp_err_t - * - ESP_OK: Success - * - ESP_ERR_INVALID_ARG: Invalid argument provided - * - ESP_FAIL: Failed to set brightness - */ -esp_err_t led_indicator_rgb_set_rgb(void *rgb_handle, uint32_t rgb_value); - -/** - * @brief Set the HSV color for the LED indicator. - * - * @param rgb_handle LED indicator LED RGB operation handle. - * @param hsv_value HSV color value to set. (H: 0-360, S: 0-255, V: 0-255) - * @return esp_err_t - * - ESP_OK: Success - * - ESP_ERR_INVALID_ARG: Invalid argument provided - * - ESP_FAIL: Failed to set brightness - */ -esp_err_t led_indicator_rgb_set_hsv(void *rgb_handle, uint32_t hsv_value); - -/** - * @brief Set the brightness for the LED indicator. - * - * @param rgb_handle Pointer to the LED indicator handle. - * @param brightness Brightness value to set. - * @return esp_err_t - * - ESP_OK: Success - * - ESP_FAIL: Failed to set brightness - */ -esp_err_t led_indicator_rgb_set_brightness(void *rgb_handle, uint32_t brightness); - -#ifdef __cplusplus -} -#endif diff --git a/components/led/led_indicator/include/led_strips.h b/components/led/led_indicator/include/led_strips.h deleted file mode 100644 index d826883fd7..0000000000 --- a/components/led/led_indicator/include/led_strips.h +++ /dev/null @@ -1,112 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#pragma once - -#include "led_strip.h" -#include "led_strip_types.h" -#include "esp_idf_version.h" - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * @brief led strip driver type - * - */ -typedef enum { - LED_STRIP_RMT, -#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 0) - LED_STRIP_SPI, -#endif - LED_STRIP_MAX, -} led_strip_driver_t; - -typedef struct { - led_strip_config_t led_strip_cfg; /*!< LED Strip Configuration. */ - led_strip_driver_t led_strip_driver; /*!< led strip control type */ - union { - led_strip_rmt_config_t led_strip_rmt_cfg; /*!< RMT (Remote Control) Configuration for the LED strip. */ -#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 0) - led_strip_spi_config_t led_strip_spi_cfg; /*!< SPI Configuration for the LED strip. */ -#endif - }; -} led_indicator_strips_config_t; - -/** - * @brief Initialize the RGB LED indicator (WS2812 SK6812). - * - * @param param Pointer to initialization parameters. - * @param ret_strips Pointer to a variable that will hold the LED strip instance. - * @return esp_err_t - * - ESP_OK: Success - * - ESP_FAIL: Initialization failed - * - ESP_ERR_INVALID_ARG: Invalid argument provided - * - ESP_ERR_NO_MEM: Memory allocation failed - */ -esp_err_t led_indicator_strips_init(void *param, void **ret_strips); - -/** - * @brief Deinitialize led strip which is used by the LED indicator. - * - * @param strips LED indicator LED Strips operation handle - * @return esp_err_t - * - ESP_OK Success - * - ESP_FAIL Deinit fail - */ -esp_err_t led_indicator_strips_deinit(void *strips); - -/** - * @brief Turn the LED indicator on or off. - * - * @param strips LED indicator LED Strips operation handle. - * @param on_off Set to 0 or 1 to control the LED (0 for off, 1 for on). - * @return esp_err_t - * - ESP_OK: Success - * - ESP_ERR_INVALID_ARG: Invalid argument provided - * - ESP_FAIL: LEDC channel initialization failed - */ -esp_err_t led_indicator_strips_set_on_off(void *strips, bool on_off); - -/** - * @brief Set the RGB color for the LED indicator. - * - * @param strips LED indicator LED Strips operation handle. - * @param rgb_value RGB color value to set. (I: 0-127 R: 0-255, G: 0-255, B: 0-255) - * @return esp_err_t - * - ESP_OK: Success - * - ESP_ERR_INVALID_ARG: Invalid argument provided - * - ESP_FAIL: Failed to set brightness - */ -esp_err_t led_indicator_strips_set_rgb(void *strips, uint32_t irgb_value); - -/** - * @brief Set the HSV color for the LED indicator. - * - * @param strips LED indicator LED Strips operation handle. - * @param hsv_value HSV color value to set. (I: 0-127 H: 0-360, S: 0-255, V: 0-255) - * @return esp_err_t - * - ESP_OK: Success - * - ESP_ERR_INVALID_ARG: Invalid argument provided - * - ESP_FAIL: Failed to set brightness - */ -esp_err_t led_indicator_strips_set_hsv(void *strips, uint32_t ihsv_value); - -/** - * @brief Set the brightness for the LED indicator. - * - * @param strips Pointer to the LED indicator handle. - * @param brightness Brightness value to set. (I: 0-127 B: 0-255) - * @return esp_err_t - * - ESP_OK: Success - * - ESP_FAIL: Failed to set brightness - */ -esp_err_t led_indicator_strips_set_brightness(void *strips, uint32_t ibrightness); - -#ifdef __cplusplus -} -#endif diff --git a/components/led/led_indicator/include/led_types.h b/components/led/led_indicator/include/led_types.h new file mode 100644 index 0000000000..4e19590f7e --- /dev/null +++ b/components/led/led_indicator/include/led_types.h @@ -0,0 +1,115 @@ +/* + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif +/** + * @brief LED duty should be consistent with the physical resolution of the indicator. + * eg. LED_GPIO_MODE should with LED_DUTY_1_BIT + * + */ +typedef enum { + LED_DUTY_1_BIT = 1, /*!< LED duty resolution of 1 bits */ + LED_DUTY_2_BIT, /*!< LED duty resolution of 2 bits */ + LED_DUTY_3_BIT, /*!< LED duty resolution of 3 bits */ + LED_DUTY_4_BIT, /*!< LED duty resolution of 4 bits */ + LED_DUTY_5_BIT, /*!< LED duty resolution of 5 bits */ + LED_DUTY_6_BIT, /*!< LED duty resolution of 6 bits */ + LED_DUTY_7_BIT, /*!< LED duty resolution of 7 bits */ + LED_DUTY_8_BIT, /*!< LED duty resolution of 8 bits */ + LED_DUTY_9_BIT, /*!< LED duty resolution of 9 bits */ + LED_DUTY_10_BIT, /*!< LED duty resolution of 10 bits */ + LED_DUTY_11_BIT, /*!< LED duty resolution of 11 bits */ + LED_DUTY_12_BIT, /*!< LED duty resolution of 12 bits */ + LED_DUTY_13_BIT, /*!< LED duty resolution of 13 bits */ + LED_DUTY_14_BIT, /*!< LED duty resolution of 14 bits */ + LED_DUTY_15_BIT, /*!< LED duty resolution of 15 bits */ +} led_indicator_duty_t; + +/** + * @brief LED state: 0-100, only hardware that supports to set brightness can adjust brightness. + * + */ +enum { + LED_STATE_OFF = 0, /*!< turn off the LED */ + LED_STATE_25_PERCENT = 64, /*!< 25% brightness, must support to set brightness */ + LED_STATE_50_PERCENT = 128, /*!< 50% brightness, must support to set brightness */ + LED_STATE_75_PERCENT = 191, /*!< 75% brightness, must support to set brightness */ + LED_STATE_ON = UINT8_MAX, /*!< turn on the LED */ +}; + +/** + * @brief actions in this type + * + */ +typedef enum { + LED_BLINK_STOP = -1, /*!< stop the blink */ + LED_BLINK_HOLD, /*!< hold the on-off state */ + LED_BLINK_BREATHE, /*!< breathe state */ + LED_BLINK_BRIGHTNESS, /*!< set the brightness, it will transition from the old brightness to the new brightness */ + LED_BLINK_RGB, /*!< color change with R(0-255) G(0-255) B(0-255) */ + LED_BLINK_RGB_RING, /*!< Gradual color transition from old color to new color in a color ring */ + LED_BLINK_HSV, /*!< color change with H(0-360) S(0-255) V(0-255) */ + LED_BLINK_HSV_RING, /*!< Gradual color transition from old color to new color in a color ring */ + LED_BLINK_LOOP, /*!< loop from first step */ +} blink_step_type_t; + +/** + * @brief one blink step, a meaningful signal consists of a group of steps + * + */ +typedef struct { + blink_step_type_t type; /*!< action type in this step */ + uint32_t value; /*!< hold on or off, set 0 if LED_BLINK_STOP() or LED_BLINK_LOOP */ + uint32_t hold_time_ms; /*!< hold time(ms), set 0 if not LED_BLINK_HOLD */ +} blink_step_t; + +typedef struct { + esp_err_t (*hal_indicator_set_on_off)(void *hardware_data, bool on_off); /*!< Pointer function for setting on or off */ + esp_err_t (*hal_indicator_deinit)(void *hardware_data); /*!< Pointer function for Deinitialization */ + esp_err_t (*hal_indicator_set_brightness)(void *hardware_data, uint32_t brightness); /*!< Pointer function for setting brightness, must be supported by hardware */ + esp_err_t (*hal_indicator_set_rgb)(void *hardware, uint32_t rgb_value); /*!< Pointer function for setting rgb, must be supported by hardware */ + esp_err_t (*hal_indicator_set_hsv)(void *hardware, uint32_t hsv_value); /*!< Pointer function for setting hsv, must be supported by hardware */ + void *hardware_data; /*!< Hardware data of the LED indicator */ + int active_blink; /*!< Active blink list*/ + int preempt_blink; /*!< Highest priority blink list*/ + int *p_blink_steps; /*!< Stage of each blink list */ + led_indicator_ihsv_t current_fade_value; /*!< Current fade value */ + led_indicator_ihsv_t last_fade_value; /*!< Save the last value. */ + uint16_t fade_value_count; /*!< Count the number of fade */ + uint16_t fade_step; /*!< Step of fade */ + uint16_t fade_total_step; /*!< Total step of fade */ + uint32_t max_duty; /*!< Max duty cycle from duty_resolution : 2^duty_resolution -1 */ + SemaphoreHandle_t mutex; /*!< Mutex to achieve thread-safe */ + TimerHandle_t h_timer; /*!< LED timer handle, invalid if works in pwm mode */ + blink_step_t const **blink_lists; /*!< User defined LED blink lists */ + uint16_t blink_list_num; /*!< Number of blink lists */ +} _led_indicator_t; + +typedef struct _led_indicator_com_config { + esp_err_t (*hal_indicator_set_on_off)(void *hardware_data, bool on_off); /*!< Pointer function for setting on or off */ + esp_err_t (*hal_indicator_deinit)(void *hardware_data); /*!< Pointer function for Deinitialization */ + esp_err_t (*hal_indicator_set_brightness)(void *hardware_data, uint32_t brightness); /*!< Pointer function for setting brightness, must be supported by hardware */ + esp_err_t (*hal_indicator_set_rgb)(void *hardware, uint32_t rgb_value); /*!< Pointer function for setting rgb, must be supported by hardware */ + esp_err_t (*hal_indicator_set_hsv)(void *hardware, uint32_t hsv_value); /*!< Pointer function for setting hsv, must be supported by hardware */ + void *hardware_data; /*!< GPIO number of the LED indicator */ + blink_step_t const **blink_lists; /*!< User defined LED blink lists */ + uint16_t blink_list_num; /*!< Number of blink lists */ + led_indicator_duty_t duty_resolution; /*!< Resolution of duty setting in number of bits. The range of duty values is [0, (2**duty_resolution) -1]. If the brightness cannot be set, set this as 1. */ +} _led_indicator_com_config_t; + +typedef struct { + blink_step_t const **blink_lists; /*!< user defined LED blink lists */ + uint16_t blink_list_num; /*!< number of blink lists */ +} led_indicator_config_t; + +typedef void *led_indicator_handle_t; /*!< LED indicator operation handle */ + +#ifdef __cplusplus +} +#endif diff --git a/components/led/led_indicator/src/led_gpio.c b/components/led/led_indicator/src/led_gpio.c deleted file mode 100644 index 2610e9749b..0000000000 --- a/components/led/led_indicator/src/led_gpio.c +++ /dev/null @@ -1,70 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include "driver/gpio.h" -#include "esp_log.h" -#include "led_gpio.h" - -typedef struct { - bool is_active_level_high; /*!< Set true if GPIO level is high when light is ON, otherwise false. */ - uint32_t io_num; -} led_gpio_t; - -esp_err_t led_indicator_gpio_init(void *param, void **ret_handle) -{ - const led_indicator_gpio_config_t *cfg = (const led_indicator_gpio_config_t *)param; - - gpio_config_t io_conf = {0}; - //disable interrupt - io_conf.intr_type = GPIO_INTR_DISABLE; - //set as output mode - io_conf.mode = GPIO_MODE_OUTPUT; - //bit mask of the pins that you want to set - io_conf.pin_bit_mask = 1ULL << (uint32_t)cfg->gpio_num; - //disable pull-down mode - io_conf.pull_down_en = 0; - //disable pull-up mode - io_conf.pull_up_en = 0; - //configure GPIO with the given settings - esp_err_t ret = gpio_config(&io_conf); - if (ret != ESP_OK) { - return ret; - } - - led_gpio_t *handle = calloc(1, sizeof(led_gpio_t)); - handle->is_active_level_high = cfg->is_active_level_high; - handle->io_num = cfg->gpio_num; - *ret_handle = (void *)handle; - return ESP_OK; -} - -esp_err_t led_indicator_gpio_deinit(void *handle) -{ - if (handle == NULL) { - return ESP_ERR_INVALID_ARG; - } - led_gpio_t *p_gpio = (led_gpio_t *)handle; - esp_err_t ret = gpio_reset_pin(p_gpio->io_num); - - if (ret != ESP_OK) { - return ret; - } - free(p_gpio); - - return ESP_OK; -} - -esp_err_t led_indicator_gpio_set_on_off(void *handle, bool on_off) -{ - if (handle == NULL) { - return ESP_ERR_INVALID_ARG; - } - led_gpio_t *p_gpio = (led_gpio_t *)handle; - if (!p_gpio->is_active_level_high) { - on_off = !on_off; - } - return gpio_set_level(p_gpio->io_num, on_off); -} diff --git a/components/led/led_indicator/src/led_indicator.c b/components/led/led_indicator/src/led_indicator.c index 99368bda18..a9038dcc84 100644 --- a/components/led/led_indicator/src/led_indicator.c +++ b/components/led/led_indicator/src/led_indicator.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -10,31 +10,17 @@ #include #include #include "esp_log.h" -#include "freertos/FreeRTOS.h" -#include "freertos/timers.h" -#include "freertos/semphr.h" -#include "led_custom.h" #include "led_indicator.h" #include "led_indicator_blink_default.h" -#include "led_gpio.h" -#include "led_ledc.h" -#include "led_rgb.h" -#include "led_strips.h" +#include "led_indicator_gpio.h" +#include "led_indicator_ledc.h" +#include "led_indicator_rgb.h" +#include "led_indicator_strips.h" #include "led_gamma.h" #include "led_convert.h" static const char *TAG = "led_indicator"; -#define LED_INDICATOR_CHECK(a, str, action) if(!(a)) { \ - ESP_LOGE(TAG,"%s:%d (%s):%s", __FILE__, __LINE__, __FUNCTION__, str); \ - action; \ - } - -#define LED_INDICATOR_CHECK_WARNING(a, str, action) if(!(a)) { \ - ESP_LOGW(TAG,"%s:%d (%s):%s", __FILE__, __LINE__, __FUNCTION__, str); \ - action; \ - } - #define MAX3(x, y, z) ((x) > (y) ? ((x) > (z) ? (x) : (z)) : ((y) > (z) ? (y) : (z))) #define BRIGHTNESS_TICKS CONFIG_BRIGHTNESS_TICKS @@ -61,54 +47,19 @@ static const HS_color_t temp_table[] = { {222, 20}, {222, 20}, {222, 20}, {222, 21}, {222, 21} }; -static const char *led_indicator_mode_str[5] = {"GPIO mode", "LEDC mode", "LED RGB mode", "LED Strips mode", "custom mode"}; - /** * @brief LED indicator object * */ -typedef struct { - esp_err_t (*hal_indicator_set_on_off)(void *hardware_data, bool on_off); /*!< Pointer function for setting on or off */ - esp_err_t (*hal_indicator_deinit)(void *hardware_data); /*!< Pointer function for Deinitialization */ - esp_err_t (*hal_indicator_set_brightness)(void *hardware_data, uint32_t brightness); /*!< Pointer function for setting brightness, must be supported by hardware */ - esp_err_t (*hal_indicator_set_rgb)(void *hardware, uint32_t rgb_value); /*!< Pointer function for setting rgb, must be supported by hardware */ - esp_err_t (*hal_indicator_set_hsv)(void *hardware, uint32_t hsv_value); /*!< Pointer function for setting hsv, must be supported by hardware */ - void *hardware_data; /*!< Hardware data of the LED indicator */ - led_indicator_mode_t mode; /*!< LED work mode, eg. GPIO or pwm mode */ - int active_blink; /*!< Active blink list*/ - int preempt_blink; /*!< Highest priority blink list*/ - int *p_blink_steps; /*!< Stage of each blink list */ - led_indicator_ihsv_t current_fade_value; /*!< Current fade value */ - led_indicator_ihsv_t last_fade_value; /*!< Save the last value. */ - uint16_t fade_value_count; /*!< Count the number of fade */ - uint16_t fade_step; /*!< Step of fade */ - uint16_t fade_total_step; /*!< Total step of fade */ - uint32_t max_duty; /*!< Max duty cycle from duty_resolution : 2^duty_resolution -1 */ - SemaphoreHandle_t mutex; /*!< Mutex to achieve thread-safe */ - TimerHandle_t h_timer; /*!< LED timer handle, invalid if works in pwm mode */ - blink_step_t const **blink_lists; /*!< User defined LED blink lists */ - uint16_t blink_list_num; /*!< Number of blink lists */ -} _led_indicator_t; typedef struct _led_indicator_slist_t { SLIST_ENTRY(_led_indicator_slist_t) next; /*!< Pointer to the next element in the singly linked list */ _led_indicator_t *p_led_indicator; /*!< Pointer to the LED indicator structure */ } _led_indicator_slist_t; -typedef struct _led_indicator_com_config { - esp_err_t (*hal_indicator_set_on_off)(void *hardware_data, bool on_off); /*!< Pointer function for setting on or off */ - esp_err_t (*hal_indicator_deinit)(void *hardware_data); /*!< Pointer function for Deinitialization */ - esp_err_t (*hal_indicator_set_brightness)(void *hardware_data, uint32_t brightness); /*!< Pointer function for setting brightness, must be supported by hardware */ - esp_err_t (*hal_indicator_set_rgb)(void *hardware, uint32_t rgb_value); /*!< Pointer function for setting rgb, must be supported by hardware */ - esp_err_t (*hal_indicator_set_hsv)(void *hardware, uint32_t hsv_value); /*!< Pointer function for setting hsv, must be supported by hardware */ - void *hardware_data; /*!< GPIO number of the LED indicator */ - blink_step_t const **blink_lists; /*!< User defined LED blink lists */ - uint16_t blink_list_num; /*!< Number of blink lists */ - led_indicator_duty_t duty_resolution; /*!< Resolution of duty setting in number of bits. The range of duty values is [0, (2**duty_resolution) -1]. If the brightness cannot be set, set this as 1. */ -} _led_indicator_com_config_t; static SLIST_HEAD(_led_indicator_head_t, _led_indicator_slist_t) s_led_indicator_slist_head = SLIST_HEAD_INITIALIZER(s_led_indicator_slist_head); -static esp_err_t _led_indicator_add_node(_led_indicator_t *p_led_indicator) +esp_err_t _led_indicator_add_node(_led_indicator_t *p_led_indicator) { LED_INDICATOR_CHECK(p_led_indicator != NULL, "pointer can not be NULL", return ESP_ERR_INVALID_ARG); _led_indicator_slist_t *node = calloc(1, sizeof(_led_indicator_slist_t)); @@ -502,7 +453,7 @@ static void _blink_list_runner(TimerHandle_t xTimer) xSemaphoreGive(p_led_indicator->mutex); } -static _led_indicator_t *_led_indicator_create_com(_led_indicator_com_config_t *cfg) +_led_indicator_t *_led_indicator_create_com(_led_indicator_com_config_t *cfg) { LED_INDICATOR_CHECK(NULL != cfg, "com config can't be NULL", return NULL); @@ -550,108 +501,6 @@ static _led_indicator_t *_led_indicator_create_com(_led_indicator_com_config_t * return NULL; } -led_indicator_handle_t led_indicator_create(const led_indicator_config_t *config) -{ - esp_err_t ret = ESP_OK; - bool if_blink_default_list = false; - ESP_LOGI(TAG, "LED Indicator Version: %d.%d.%d", LED_INDICATOR_VER_MAJOR, LED_INDICATOR_VER_MINOR, LED_INDICATOR_VER_PATCH); - LED_INDICATOR_CHECK(config != NULL, "invalid config pointer", return NULL); - _led_indicator_com_config_t com_cfg = {0}; - _led_indicator_t *p_led_indicator = NULL; - switch (config->mode) { - case LED_GPIO_MODE: { - void *hardware_data = NULL; - const led_indicator_gpio_config_t *cfg = config->led_indicator_gpio_config; - ret = led_indicator_gpio_init((void *)cfg, &hardware_data); - LED_INDICATOR_CHECK(ESP_OK == ret, "GPIO mode init failed", return NULL); - - com_cfg.hardware_data = hardware_data; - com_cfg.hal_indicator_set_on_off = led_indicator_gpio_set_on_off; - com_cfg.hal_indicator_deinit = led_indicator_gpio_deinit; - com_cfg.hal_indicator_set_brightness = NULL; - com_cfg.duty_resolution = LED_DUTY_1_BIT; - break; - } - case LED_LEDC_MODE: { - const led_indicator_ledc_config_t *cfg = config->led_indicator_ledc_config; - ret = led_indicator_ledc_init((void *)cfg); - LED_INDICATOR_CHECK(ESP_OK == ret, "LEDC mode init failed", return NULL); - com_cfg.hardware_data = (void *)cfg->channel; - com_cfg.hal_indicator_set_on_off = led_indicator_ledc_set_on_off; - com_cfg.hal_indicator_deinit = led_indicator_ledc_deinit; - com_cfg.hal_indicator_set_brightness = led_indicator_ledc_set_brightness; - com_cfg.duty_resolution = LED_DUTY_8_BIT; - break; - } - case LED_RGB_MODE: { - void *hardware_data = NULL; - const led_indicator_rgb_config_t *cfg = config->led_indicator_rgb_config; - ret = led_indicator_rgb_init((void *)cfg, &hardware_data); - LED_INDICATOR_CHECK(ESP_OK == ret, "LEDC mode init failed", return NULL); - com_cfg.hardware_data = hardware_data; - com_cfg.hal_indicator_set_on_off = led_indicator_rgb_set_on_off; - com_cfg.hal_indicator_deinit = led_indicator_rgb_deinit; - com_cfg.hal_indicator_set_brightness = led_indicator_rgb_set_brightness; - com_cfg.hal_indicator_set_rgb = led_indicator_rgb_set_rgb; - com_cfg.hal_indicator_set_hsv = led_indicator_rgb_set_hsv; - com_cfg.duty_resolution = LED_DUTY_8_BIT; - break; - } - case LED_STRIPS_MODE: { - const led_indicator_strips_config_t *cfg = config->led_indicator_strips_config; - void *hardware_data = NULL; - ret = led_indicator_strips_init((void *)cfg, &hardware_data); - LED_INDICATOR_CHECK(ESP_OK == ret, "LED rgb init failed", return NULL); - com_cfg.hardware_data = hardware_data; - com_cfg.hal_indicator_set_on_off = led_indicator_strips_set_on_off; - com_cfg.hal_indicator_deinit = led_indicator_strips_deinit; - com_cfg.hal_indicator_set_brightness = led_indicator_strips_set_brightness; - com_cfg.hal_indicator_set_rgb = led_indicator_strips_set_rgb; - com_cfg.hal_indicator_set_hsv = led_indicator_strips_set_hsv; - com_cfg.duty_resolution = LED_DUTY_8_BIT; - break; - } - case LED_CUSTOM_MODE: { - const led_indicator_custom_config_t *cfg = config->led_indicator_custom_config; - LED_INDICATOR_CHECK_WARNING(cfg->hal_indicator_init != NULL, "LED indicator does not have hal_indicator_init ", goto without_init); - ret = cfg->hal_indicator_init(cfg->hardware_data); - LED_INDICATOR_CHECK(ret == ESP_OK, "LED indicator init failed", return NULL); -without_init: - com_cfg.hardware_data = cfg->hardware_data; - com_cfg.hal_indicator_set_on_off = cfg->hal_indicator_set_on_off; - com_cfg.hal_indicator_deinit = cfg->hal_indicator_deinit; - com_cfg.hal_indicator_set_brightness = cfg->hal_indicator_set_brightness; - com_cfg.hal_indicator_set_rgb = cfg->hal_indicator_set_rgb; - com_cfg.hal_indicator_set_hsv = cfg->hal_indicator_set_hsv; - com_cfg.duty_resolution = cfg->duty_resolution; - break; - } - default: - ESP_LOGE(TAG, "Unsupported indicator mode"); - goto unsupported_mode; - break; - } - - if (config->blink_lists == NULL) { - ESP_LOGI(TAG, "blink_lists is null, use default blink list"); - com_cfg.blink_lists = default_led_indicator_blink_lists; - com_cfg.blink_list_num = DEFAULT_BLINK_LIST_NUM; - if_blink_default_list = true; - } else { - com_cfg.blink_lists = config->blink_lists; - com_cfg.blink_list_num = config->blink_list_num; - } - - p_led_indicator = _led_indicator_create_com(&com_cfg); - -unsupported_mode: - LED_INDICATOR_CHECK(NULL != p_led_indicator, "LED indicator create failed", return NULL); - p_led_indicator->mode = config->mode; - _led_indicator_add_node(p_led_indicator); - ESP_LOGI(TAG, "Indicator create successfully. type:%s, hardware_data:%p, blink_lists:%s", led_indicator_mode_str[p_led_indicator->mode], p_led_indicator->hardware_data, if_blink_default_list ? "default" : "custom"); - return (led_indicator_handle_t)p_led_indicator; -} - static esp_err_t _led_indicator_delete_com(_led_indicator_t *p_led_indicator) { esp_err_t err; diff --git a/components/led/led_indicator/src/led_indicator_gpio.c b/components/led/led_indicator/src/led_indicator_gpio.c new file mode 100644 index 0000000000..ae82cbb2bd --- /dev/null +++ b/components/led/led_indicator/src/led_indicator_gpio.c @@ -0,0 +1,111 @@ +/* + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "driver/gpio.h" +#include "esp_log.h" +#include "led_indicator_gpio.h" +#include "led_indicator_blink_default.h" + +#define TAG "led_gpio" + +typedef struct { + bool is_active_level_high; /*!< Set true if GPIO level is high when light is ON, otherwise false. */ + uint32_t io_num; +} led_gpio_t; + +static esp_err_t led_indicator_gpio_init(void *param, void **ret_handle) +{ + const led_indicator_gpio_config_t *cfg = (const led_indicator_gpio_config_t *)param; + + gpio_config_t io_conf = {0}; + //disable interrupt + io_conf.intr_type = GPIO_INTR_DISABLE; + //set as output mode + io_conf.mode = GPIO_MODE_OUTPUT; + //bit mask of the pins that you want to set + io_conf.pin_bit_mask = 1ULL << (uint32_t)cfg->gpio_num; + //disable pull-down mode + io_conf.pull_down_en = 0; + //disable pull-up mode + io_conf.pull_up_en = 0; + //configure GPIO with the given settings + esp_err_t ret = gpio_config(&io_conf); + if (ret != ESP_OK) { + return ret; + } + + led_gpio_t *handle = calloc(1, sizeof(led_gpio_t)); + handle->is_active_level_high = cfg->is_active_level_high; + handle->io_num = cfg->gpio_num; + *ret_handle = (void *)handle; + return ESP_OK; +} + +static esp_err_t led_indicator_gpio_deinit(void *handle) +{ + if (handle == NULL) { + return ESP_ERR_INVALID_ARG; + } + led_gpio_t *p_gpio = (led_gpio_t *)handle; + esp_err_t ret = gpio_reset_pin(p_gpio->io_num); + + if (ret != ESP_OK) { + return ret; + } + free(p_gpio); + + return ESP_OK; +} + +static esp_err_t led_indicator_gpio_set_on_off(void *handle, bool on_off) +{ + if (handle == NULL) { + return ESP_ERR_INVALID_ARG; + } + led_gpio_t *p_gpio = (led_gpio_t *)handle; + if (!p_gpio->is_active_level_high) { + on_off = !on_off; + } + return gpio_set_level(p_gpio->io_num, on_off); +} + +esp_err_t led_indicator_new_gpio_device(const led_indicator_config_t *led_config, const led_indicator_gpio_config_t *gpio_cfg, led_indicator_handle_t *handle) +{ + esp_err_t ret = ESP_OK; + bool if_blink_default_list = false; + + ESP_LOGI(TAG, "LED Indicator Version: %d.%d.%d", LED_INDICATOR_VER_MAJOR, LED_INDICATOR_VER_MINOR, LED_INDICATOR_VER_PATCH); + LED_INDICATOR_CHECK(gpio_cfg != NULL, "invalid config pointer", return ESP_ERR_INVALID_ARG); + _led_indicator_com_config_t com_cfg = {0}; + _led_indicator_t *p_led_indicator = NULL; + + void *hardware_data = NULL; + ret = led_indicator_gpio_init((void *)gpio_cfg, &hardware_data); + LED_INDICATOR_CHECK(ESP_OK == ret, "LED rgb init failed", return ESP_FAIL); + com_cfg.hardware_data = hardware_data; + com_cfg.hal_indicator_set_on_off = led_indicator_gpio_set_on_off; + com_cfg.hal_indicator_deinit = led_indicator_gpio_deinit; + com_cfg.hal_indicator_set_brightness = NULL; + com_cfg.duty_resolution = LED_DUTY_1_BIT; + + if (led_config->blink_lists == NULL) { + ESP_LOGI(TAG, "blink_lists is null, use default blink list"); + com_cfg.blink_lists = default_led_indicator_blink_lists; + com_cfg.blink_list_num = DEFAULT_BLINK_LIST_NUM; + if_blink_default_list = true; + } else { + com_cfg.blink_lists = led_config->blink_lists; + com_cfg.blink_list_num = led_config->blink_list_num; + } + + p_led_indicator = _led_indicator_create_com(&com_cfg); + + LED_INDICATOR_CHECK(NULL != p_led_indicator, "LED indicator create failed", return ESP_FAIL); + _led_indicator_add_node(p_led_indicator); + ESP_LOGI(TAG, "Indicator create successfully. type:GPIO mode, hardware_data:%p, blink_lists:%s", p_led_indicator->hardware_data, if_blink_default_list ? "default" : "custom"); + *handle = (led_indicator_handle_t)p_led_indicator; + return ESP_OK; +} diff --git a/components/led/led_indicator/src/led_ledc.c b/components/led/led_indicator/src/led_indicator_ledc.c similarity index 65% rename from components/led/led_indicator/src/led_ledc.c rename to components/led/led_indicator/src/led_indicator_ledc.c index f40c1b1bac..33c413cabb 100644 --- a/components/led/led_indicator/src/led_ledc.c +++ b/components/led/led_indicator/src/led_indicator_ledc.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -7,8 +7,9 @@ #include #include "driver/ledc.h" #include "esp_log.h" -#include "led_ledc.h" +#include "led_indicator_ledc.h" #include "led_common.h" +#include "led_indicator_blink_default.h" #define TAG "led_ledc" @@ -36,7 +37,7 @@ typedef struct { static led_indicator_ledc_t *s_ledc = NULL; -esp_err_t led_indicator_ledc_init(void *param) +static esp_err_t led_indicator_ledc_init(void *param) { esp_err_t ret = ESP_OK; const led_indicator_ledc_config_t *cfg = (led_indicator_ledc_config_t *)param; @@ -71,7 +72,7 @@ esp_err_t led_indicator_ledc_init(void *param) return ret; } -esp_err_t led_indicator_ledc_deinit(void *channel) +static esp_err_t led_indicator_ledc_deinit(void *channel) { uint32_t ch = (uint32_t)channel; LED_LEDC_CHECK(NULL != s_ledc, "LEDC is not initialized", return ESP_FAIL); @@ -86,7 +87,7 @@ esp_err_t led_indicator_ledc_deinit(void *channel) return ESP_OK; } -esp_err_t led_indicator_ledc_set_on_off(void *channel, bool on_off) +static esp_err_t led_indicator_ledc_set_on_off(void *channel, bool on_off) { esp_err_t ret; uint32_t ch = (uint32_t)channel; @@ -103,7 +104,7 @@ esp_err_t led_indicator_ledc_set_on_off(void *channel, bool on_off) return ESP_OK; } -esp_err_t led_indicator_ledc_set_brightness(void *channel, uint32_t brightness) +static esp_err_t led_indicator_ledc_set_brightness(void *channel, uint32_t brightness) { esp_err_t ret; uint32_t ch = (uint32_t)channel; @@ -116,3 +117,40 @@ esp_err_t led_indicator_ledc_set_brightness(void *channel, uint32_t brightness) LED_LEDC_CHECK(ESP_OK == ret, "LEDC update duty error", return ret); return ESP_OK; } + +esp_err_t led_indicator_new_ledc_device(const led_indicator_config_t *led_config, const led_indicator_ledc_config_t *ledc_cfg, led_indicator_handle_t *handle) +{ + esp_err_t ret = ESP_OK; + bool if_blink_default_list = false; + + ESP_LOGI(TAG, "LED Indicator Version: %d.%d.%d", LED_INDICATOR_VER_MAJOR, LED_INDICATOR_VER_MINOR, LED_INDICATOR_VER_PATCH); + LED_INDICATOR_CHECK(ledc_cfg != NULL, "invalid config pointer", return ESP_ERR_INVALID_ARG); + _led_indicator_com_config_t com_cfg = {0}; + _led_indicator_t *p_led_indicator = NULL; + + ret = led_indicator_ledc_init((void *)ledc_cfg); + LED_INDICATOR_CHECK(ESP_OK == ret, "LEDC mode init failed", return ESP_FAIL); + com_cfg.hardware_data = (void *)ledc_cfg->channel; + com_cfg.hal_indicator_set_on_off = led_indicator_ledc_set_on_off; + com_cfg.hal_indicator_deinit = led_indicator_ledc_deinit; + com_cfg.hal_indicator_set_brightness = led_indicator_ledc_set_brightness; + com_cfg.duty_resolution = LED_DUTY_8_BIT; + + if (led_config->blink_lists == NULL) { + ESP_LOGI(TAG, "blink_lists is null, use default blink list"); + com_cfg.blink_lists = default_led_indicator_blink_lists; + com_cfg.blink_list_num = DEFAULT_BLINK_LIST_NUM; + if_blink_default_list = true; + } else { + com_cfg.blink_lists = led_config->blink_lists; + com_cfg.blink_list_num = led_config->blink_list_num; + } + + p_led_indicator = _led_indicator_create_com(&com_cfg); + + LED_INDICATOR_CHECK(NULL != p_led_indicator, "LED indicator create failed", return ESP_FAIL); + _led_indicator_add_node(p_led_indicator); + ESP_LOGI(TAG, "Indicator create successfully. type:LEDC mode, hardware_data:%p, blink_lists:%s", p_led_indicator->hardware_data, if_blink_default_list ? "default" : "custom"); + *handle = (led_indicator_handle_t)p_led_indicator; + return ESP_OK; +} diff --git a/components/led/led_indicator/src/led_rgb.c b/components/led/led_indicator/src/led_indicator_rgb.c similarity index 68% rename from components/led/led_indicator/src/led_rgb.c rename to components/led/led_indicator/src/led_indicator_rgb.c index 084055db8f..f3e0beae39 100644 --- a/components/led/led_indicator/src/led_rgb.c +++ b/components/led/led_indicator/src/led_indicator_rgb.c @@ -7,9 +7,10 @@ #include #include "driver/ledc.h" #include "esp_log.h" -#include "led_rgb.h" +#include "led_indicator_rgb.h" #include "led_common.h" #include "led_convert.h" +#include "led_indicator_blink_default.h" #define TAG "led_rgb" @@ -27,7 +28,7 @@ typedef struct { led_indicator_ihsv_t hsv; /*!< HSV: H [0-360] - 9 bits, S [0-255] - 8 bits, V [0-255] - 8 bits*/ } led_rgb_t; -esp_err_t led_indicator_rgb_init(void *param, void **ret_rgb) +static esp_err_t led_indicator_rgb_init(void *param, void **ret_rgb) { esp_err_t ret = ESP_OK; const led_indicator_rgb_config_t *cfg = (const led_indicator_rgb_config_t *)param; @@ -62,7 +63,7 @@ esp_err_t led_indicator_rgb_init(void *param, void **ret_rgb) return ret; } -esp_err_t led_indicator_rgb_deinit(void *rgb_handle) +static esp_err_t led_indicator_rgb_deinit(void *rgb_handle) { LED_RGB_CHECK(NULL != rgb_handle, "rgb_handle pointer invalid", return ESP_ERR_INVALID_ARG); free(rgb_handle); @@ -86,7 +87,7 @@ static esp_err_t led_indicator_rgb_set_duty(led_rgb_t *p_rgb, uint32_t rgb[]) return ESP_OK; } -esp_err_t led_indicator_rgb_set_on_off(void *rgb_handle, bool on_off) +static esp_err_t led_indicator_rgb_set_on_off(void *rgb_handle, bool on_off) { esp_err_t ret; LED_RGB_CHECK(NULL != rgb_handle, "rgb_handle pointer invalid", return ESP_ERR_INVALID_ARG); @@ -111,7 +112,7 @@ esp_err_t led_indicator_rgb_set_on_off(void *rgb_handle, bool on_off) return ESP_OK; } -esp_err_t led_indicator_rgb_set_rgb(void *rgb_handle, uint32_t rgb_value) +static esp_err_t led_indicator_rgb_set_rgb(void *rgb_handle, uint32_t rgb_value) { esp_err_t ret; led_rgb_t *p_rgb = (led_rgb_t *)rgb_handle; @@ -126,7 +127,7 @@ esp_err_t led_indicator_rgb_set_rgb(void *rgb_handle, uint32_t rgb_value) return ESP_OK; } -esp_err_t led_indicator_rgb_set_hsv(void *rgb_handle, uint32_t hsv_value) +static esp_err_t led_indicator_rgb_set_hsv(void *rgb_handle, uint32_t hsv_value) { esp_err_t ret; led_rgb_t *p_rgb = (led_rgb_t *)rgb_handle; @@ -142,7 +143,7 @@ esp_err_t led_indicator_rgb_set_hsv(void *rgb_handle, uint32_t hsv_value) return ESP_OK; } -esp_err_t led_indicator_rgb_set_brightness(void *rgb_handle, uint32_t brightness) +static esp_err_t led_indicator_rgb_set_brightness(void *rgb_handle, uint32_t brightness) { esp_err_t ret; led_rgb_t *p_rgb = (led_rgb_t *)rgb_handle; @@ -157,3 +158,43 @@ esp_err_t led_indicator_rgb_set_brightness(void *rgb_handle, uint32_t brightness LED_RGB_CHECK(ESP_OK == ret, "LEDC set duty error", return ret); return ESP_OK; } + +esp_err_t led_indicator_new_rgb_device(const led_indicator_config_t *led_config, const led_indicator_rgb_config_t *rgb_cfg, led_indicator_handle_t *handle) +{ + esp_err_t ret = ESP_OK; + bool if_blink_default_list = false; + + ESP_LOGI(TAG, "LED Indicator Version: %d.%d.%d", LED_INDICATOR_VER_MAJOR, LED_INDICATOR_VER_MINOR, LED_INDICATOR_VER_PATCH); + LED_INDICATOR_CHECK(rgb_cfg != NULL, "invalid config pointer", return ESP_ERR_INVALID_ARG); + _led_indicator_com_config_t com_cfg = {0}; + _led_indicator_t *p_led_indicator = NULL; + + void *hardware_data = NULL; + ret = led_indicator_rgb_init((void *)rgb_cfg, &hardware_data); + LED_INDICATOR_CHECK(ESP_OK == ret, "LEDC mode init failed", return ESP_FAIL); + com_cfg.hardware_data = hardware_data; + com_cfg.hal_indicator_set_on_off = led_indicator_rgb_set_on_off; + com_cfg.hal_indicator_deinit = led_indicator_rgb_deinit; + com_cfg.hal_indicator_set_brightness = led_indicator_rgb_set_brightness; + com_cfg.hal_indicator_set_rgb = led_indicator_rgb_set_rgb; + com_cfg.hal_indicator_set_hsv = led_indicator_rgb_set_hsv; + com_cfg.duty_resolution = LED_DUTY_8_BIT; + + if (led_config->blink_lists == NULL) { + ESP_LOGI(TAG, "blink_lists is null, use default blink list"); + com_cfg.blink_lists = default_led_indicator_blink_lists; + com_cfg.blink_list_num = DEFAULT_BLINK_LIST_NUM; + if_blink_default_list = true; + } else { + com_cfg.blink_lists = led_config->blink_lists; + com_cfg.blink_list_num = led_config->blink_list_num; + } + + p_led_indicator = _led_indicator_create_com(&com_cfg); + + LED_INDICATOR_CHECK(NULL != p_led_indicator, "LED indicator create failed", return ESP_FAIL); + _led_indicator_add_node(p_led_indicator); + ESP_LOGI(TAG, "Indicator create successfully. type:LED RGB mode, hardware_data:%p, blink_lists:%s", p_led_indicator->hardware_data, if_blink_default_list ? "default" : "custom"); + *handle = (led_indicator_handle_t)p_led_indicator; + return ESP_OK; +} diff --git a/components/led/led_indicator/src/led_strips.c b/components/led/led_indicator/src/led_indicator_strips.c similarity index 67% rename from components/led/led_indicator/src/led_strips.c rename to components/led/led_indicator/src/led_indicator_strips.c index 129f888d77..002de422ed 100644 --- a/components/led/led_indicator/src/led_strips.c +++ b/components/led/led_indicator/src/led_indicator_strips.c @@ -1,21 +1,22 @@ /* - * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ /* - * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ #include #include "esp_log.h" -#include "led_strips.h" +#include "led_indicator_strips.h" #include "led_strip.h" #include "led_strip_types.h" #include "led_convert.h" +#include "led_indicator_blink_default.h" #define TAG "led_strips" @@ -32,7 +33,7 @@ typedef struct { led_indicator_ihsv_t ihsv; /*!< IHSV: I [0-127] 7 bits - H [0-360] - 9 bits, S [0-255] - 8 bits, V [0-255] - 8 bits*/ } led_strips_t; -esp_err_t led_indicator_strips_init(void *param, void **ret_strips) +static esp_err_t led_indicator_strips_init(void *param, void **ret_strips) { esp_err_t ret = ESP_OK; const led_indicator_strips_config_t *cfg = (led_indicator_strips_config_t *)param; @@ -72,7 +73,7 @@ esp_err_t led_indicator_strips_init(void *param, void **ret_strips) return ret; } -esp_err_t led_indicator_strips_deinit(void *strips) +static esp_err_t led_indicator_strips_deinit(void *strips) { LED_STRIPS_CHECK(NULL != strips, "param pointer invalid", return ESP_ERR_INVALID_ARG); led_strips_t *p_strip = (led_strips_t *)strips; @@ -83,7 +84,7 @@ esp_err_t led_indicator_strips_deinit(void *strips) return ESP_OK; } -esp_err_t led_indicator_strips_set_on_off(void *strips, bool on_off) +static esp_err_t led_indicator_strips_set_on_off(void *strips, bool on_off) { led_strips_t *p_strip = (led_strips_t *)strips; p_strip->ihsv.v = on_off ? MAX_BRIGHTNESS : 0; @@ -107,7 +108,7 @@ esp_err_t led_indicator_strips_set_on_off(void *strips, bool on_off) return ESP_OK; } -esp_err_t led_indicator_strips_set_rgb(void *strips, uint32_t irgb_value) +static esp_err_t led_indicator_strips_set_rgb(void *strips, uint32_t irgb_value) { led_strips_t *p_strip = (led_strips_t *)strips; uint8_t i, r, g, b; @@ -138,7 +139,7 @@ esp_err_t led_indicator_strips_set_rgb(void *strips, uint32_t irgb_value) return ESP_OK; } -esp_err_t led_indicator_strips_set_hsv(void *strips, uint32_t ihsv_value) +static esp_err_t led_indicator_strips_set_hsv(void *strips, uint32_t ihsv_value) { led_strips_t *p_strip = (led_strips_t *)strips; p_strip->ihsv.value = ihsv_value; @@ -163,7 +164,7 @@ esp_err_t led_indicator_strips_set_hsv(void *strips, uint32_t ihsv_value) return ESP_OK; } -esp_err_t led_indicator_strips_set_brightness(void *strips, uint32_t ihsv) +static esp_err_t led_indicator_strips_set_brightness(void *strips, uint32_t ihsv) { led_strips_t *p_strip = (led_strips_t *)strips; p_strip->ihsv.i = GET_INDEX(ihsv); @@ -188,3 +189,43 @@ esp_err_t led_indicator_strips_set_brightness(void *strips, uint32_t ihsv) return ESP_OK; } + +esp_err_t led_indicator_new_strips_device(const led_indicator_config_t *led_config, const led_indicator_strips_config_t *strips_cfg, led_indicator_handle_t *handle) +{ + esp_err_t ret = ESP_OK; + bool if_blink_default_list = false; + + ESP_LOGI(TAG, "LED Indicator Version: %d.%d.%d", LED_INDICATOR_VER_MAJOR, LED_INDICATOR_VER_MINOR, LED_INDICATOR_VER_PATCH); + LED_INDICATOR_CHECK(strips_cfg != NULL, "invalid config pointer", return ESP_ERR_INVALID_ARG); + _led_indicator_com_config_t com_cfg = {0}; + _led_indicator_t *p_led_indicator = NULL; + + void *hardware_data = NULL; + ret = led_indicator_strips_init((void *)strips_cfg, &hardware_data); + LED_INDICATOR_CHECK(ESP_OK == ret, "LED rgb init failed", return ESP_FAIL); + com_cfg.hardware_data = hardware_data; + com_cfg.hal_indicator_set_on_off = led_indicator_strips_set_on_off; + com_cfg.hal_indicator_deinit = led_indicator_strips_deinit; + com_cfg.hal_indicator_set_brightness = led_indicator_strips_set_brightness; + com_cfg.hal_indicator_set_rgb = led_indicator_strips_set_rgb; + com_cfg.hal_indicator_set_hsv = led_indicator_strips_set_hsv; + com_cfg.duty_resolution = LED_DUTY_8_BIT; + + if (led_config->blink_lists == NULL) { + ESP_LOGI(TAG, "blink_lists is null, use default blink list"); + com_cfg.blink_lists = default_led_indicator_blink_lists; + com_cfg.blink_list_num = DEFAULT_BLINK_LIST_NUM; + if_blink_default_list = true; + } else { + com_cfg.blink_lists = led_config->blink_lists; + com_cfg.blink_list_num = led_config->blink_list_num; + } + + p_led_indicator = _led_indicator_create_com(&com_cfg); + + LED_INDICATOR_CHECK(NULL != p_led_indicator, "LED indicator create failed", return ESP_FAIL); + _led_indicator_add_node(p_led_indicator); + ESP_LOGI(TAG, "Indicator create successfully. type:LED Strips mode, hardware_data:%p, blink_lists:%s", p_led_indicator->hardware_data, if_blink_default_list ? "default" : "custom"); + *handle = (led_indicator_handle_t)p_led_indicator; + return ESP_OK; +} diff --git a/components/led/led_indicator/src/mi_led_indicator_blink_default.c b/components/led/led_indicator/src/mi_led_indicator_blink_default.c new file mode 100644 index 0000000000..c77192548e --- /dev/null +++ b/components/led/led_indicator/src/mi_led_indicator_blink_default.c @@ -0,0 +1,126 @@ +/* + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "led_indicator.h" +#include "led_indicator_blink_default.h" + +/*********************************** Config Blink List in Different Conditions ***********************************/ +// LED state definitions +static const blink_step_t default_mi_wait_connect[] = { // Device waiting for connection (Orange blinking) + {LED_BLINK_RGB, SET_IRGB(MAX_INDEX, 255, 69, 0), 100}, // Orange, ON for 0.1s + {LED_BLINK_RGB, SET_IRGB(MAX_INDEX, 0, 0, 0), 400}, // OFF for 0.4s + {LED_BLINK_LOOP, 0, 0}, +}; + +static const blink_step_t default_mi_connecting[] = { // Device connecting to network (Blue blinking) + {LED_BLINK_RGB, SET_IRGB(MAX_INDEX, 0, 0, 255), 100}, // Blue, ON for 0.1s + {LED_BLINK_RGB, SET_IRGB(MAX_INDEX, 0, 0, 0), 400}, // OFF for 0.4s + {LED_BLINK_LOOP, 0, 0}, +}; + +static const blink_step_t default_mi_online[] = { // Device online (Solid blue) + {LED_BLINK_RGB, SET_IRGB(MAX_INDEX, 0, 0, 255), 1000}, // Solid blue + {LED_BLINK_LOOP, 0, 0}, +}; + +static const blink_step_t default_mi_fault[] = { // Device fault state (Solid orange) + {LED_BLINK_RGB, SET_IRGB(MAX_INDEX, 255, 69, 0), 1000}, // Solid orange + {LED_BLINK_LOOP, 0, 0}, +}; + +static const blink_step_t default_mi_reconnecting[] = { // Network reconnecting (Blue blinking) + {LED_BLINK_RGB, SET_IRGB(MAX_INDEX, 0, 0, 255), 100}, // Blue, ON for 0.1s + {LED_BLINK_RGB, SET_IRGB(MAX_INDEX, 0, 0, 0), 400}, // OFF for 0.4s + {LED_BLINK_LOOP, 0, 0}, +}; + +static const blink_step_t default_mi_ota_updating[] = { // OTA updating (Blue blinking) + {LED_BLINK_RGB, SET_IRGB(MAX_INDEX, 0, 0, 255), 100}, // Blue, ON for 0.1s + {LED_BLINK_RGB, SET_IRGB(MAX_INDEX, 0, 0, 0), 400}, // OFF for 0.4s + {LED_BLINK_LOOP, 0, 0}, +}; + +static const blink_step_t default_mi_wifi_provision_off[] = { // Wi-Fi provisioning disabled (LED OFF) + {LED_BLINK_RGB, SET_IRGB(MAX_INDEX, 0, 0, 0), 1000}, // LED OFF + {LED_BLINK_STOP, 0, 0}, +}; + +static const blink_step_t default_mi_working[] = { // Normal working state (Solid white) + {LED_BLINK_RGB, SET_IRGB(MAX_INDEX, 255, 255, 255), 1000}, // Solid white + {LED_BLINK_LOOP, 0, 0}, +}; + +static const blink_step_t default_mi_work_abnormal[] = { // Working abnormal/High risk alarm (Red blinking) + {LED_BLINK_RGB, SET_IRGB(MAX_INDEX, 255, 0, 0), 100}, // Red, ON for 0.1s + {LED_BLINK_RGB, SET_IRGB(MAX_INDEX, 0, 0, 0), 400}, // OFF for 0.4s + {LED_BLINK_LOOP, 0, 0}, +}; + +static const blink_step_t default_mi_env_good[] = { // Environment good (Solid green) + {LED_BLINK_RGB, SET_IRGB(MAX_INDEX, 0, 255, 0), 1000}, // Solid green + {LED_BLINK_LOOP, 0, 0}, +}; + +static const blink_step_t default_mi_env_moderate[] = { // Environment moderate pollution (Solid orange) + {LED_BLINK_RGB, SET_IRGB(MAX_INDEX, 255, 69, 0), 1000}, // Solid orange + {LED_BLINK_LOOP, 0, 0}, +}; + +static const blink_step_t default_mi_env_severe[] = { // Environment severe pollution (Solid red) + {LED_BLINK_RGB, SET_IRGB(MAX_INDEX, 255, 0, 0), 1000}, // Solid red + {LED_BLINK_LOOP, 0, 0}, +}; + +static const blink_step_t default_mi_setting[] = { // Device setting (Solid white) + {LED_BLINK_RGB, SET_IRGB(MAX_INDEX, 255, 255, 255), 1000}, // Solid white + {LED_BLINK_LOOP, 0, 0}, +}; + +static const blink_step_t default_mi_low_battery[] = { // Low battery (Solid red) + {LED_BLINK_RGB, SET_IRGB(MAX_INDEX, 255, 0, 0), 1000}, // Solid red + {LED_BLINK_LOOP, 0, 0}, +}; + +static const blink_step_t default_mi_charging[] = { // Charging (White breathing) + {LED_BLINK_RGB, SET_IRGB(MAX_INDEX, 255, 255, 255), 250}, + {LED_BLINK_BREATHE, INSERT_INDEX(MAX_INDEX, LED_STATE_OFF), 1500}, + {LED_BLINK_BRIGHTNESS, INSERT_INDEX(MAX_INDEX, LED_STATE_OFF), 500}, + {LED_BLINK_BREATHE, INSERT_INDEX(MAX_INDEX, LED_STATE_ON), 1500}, + {LED_BLINK_BRIGHTNESS, INSERT_INDEX(MAX_INDEX, LED_STATE_ON), 250}, + {LED_BLINK_LOOP, 0, 0}, +}; + +static const blink_step_t default_mi_charged[] = { // Fully charged (LED OFF) + {LED_BLINK_RGB, SET_IRGB(MAX_INDEX, 0, 0, 0), 0}, // LED OFF + {LED_BLINK_STOP, 0, 0}, +}; + +/** + * @brief LED indicator blink lists, the index like BLINK_MI_WAIT_CONNECT defined the priority of the blink + * + */ +blink_step_t const *default_led_indicator_blink_lists[] = { + [BLINK_MI_WAIT_CONNECT] = default_mi_wait_connect, + [BLINK_MI_CONNECTING] = default_mi_connecting, + [BLINK_MI_ONLINE] = default_mi_online, + [BLINK_MI_FAULT] = default_mi_fault, + [BLINK_MI_RECONNECTING] = default_mi_reconnecting, + [BLINK_MI_OTA_UPDATING] = default_mi_ota_updating, + [BLINK_MI_WIFI_PROVISION_OFF] = default_mi_wifi_provision_off, + [BLINK_MI_WORKING] = default_mi_working, + [BLINK_MI_WORK_ABNORMAL] = default_mi_work_abnormal, + [BLINK_MI_ENV_GOOD] = default_mi_env_good, + [BLINK_MI_ENV_MODERATE] = default_mi_env_moderate, + [BLINK_MI_ENV_SEVERE] = default_mi_env_severe, + [BLINK_MI_SETTING] = default_mi_setting, + [BLINK_MI_LOW_BATTERY] = default_mi_low_battery, + [BLINK_MI_CHARGING] = default_mi_charging, + [BLINK_MI_CHARGED] = default_mi_charged, + [BLINK_MAX] = NULL, +}; + +/* LED blink_steps handling machine implementation */ +const int DEFAULT_BLINK_LIST_NUM = (sizeof(default_led_indicator_blink_lists) / sizeof(default_led_indicator_blink_lists[0]) - 1); diff --git a/components/led/led_indicator/test_apps/main/test_led_indicator.c b/components/led/led_indicator/test_apps/main/test_led_indicator.c index 446fbbf5ec..671e2194b4 100644 --- a/components/led/led_indicator/test_apps/main/test_led_indicator.c +++ b/components/led/led_indicator/test_apps/main/test_led_indicator.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -10,11 +10,14 @@ #include "esp_log.h" #include "esp_system.h" #include "esp_idf_version.h" -#include "led_indicator.h" #include "led_indicator_blink_default.h" #include "unity.h" #include "led_gamma.h" #include "led_convert.h" +#include "led_indicator_strips.h" +#include "led_indicator_gpio.h" +#include "led_indicator_ledc.h" +#include "led_indicator_rgb.h" // Some resources are lazy allocated in pulse_cnt driver, the threshold is left for that case #define TEST_MEMORY_LEAK_THRESHOLD (-200) @@ -28,6 +31,16 @@ #define TAG "LED indicator Test" static led_indicator_handle_t led_handle_0 = NULL; + +void led_indicator_deinit() +{ + ESP_LOGI(TAG, "deinit....."); + esp_err_t ret = led_indicator_delete(led_handle_0); + TEST_ASSERT(ret == ESP_OK); + led_handle_0 = NULL; +} + +#ifndef CONFIG_USE_MI_BLINK_DEFAULT static led_indicator_handle_t led_handle_1 = NULL; static led_indicator_handle_t led_handle_2 = NULL; @@ -39,22 +52,13 @@ void led_indicator_init() }; led_indicator_config_t config = { - .mode = LED_GPIO_MODE, - .led_indicator_gpio_config = &led_indicator_gpio_config, .blink_lists = (void *)NULL, .blink_list_num = 0, }; - led_handle_0 = led_indicator_create(&config); - TEST_ASSERT_NOT_NULL(led_handle_0); -} - -void led_indicator_deinit() -{ - ESP_LOGI(TAG, "deinit....."); - esp_err_t ret = led_indicator_delete(led_handle_0); + esp_err_t ret = led_indicator_new_gpio_device(&config, &led_indicator_gpio_config, &led_handle_0); TEST_ASSERT(ret == ESP_OK); - led_handle_0 = NULL; + TEST_ASSERT_NOT_NULL(led_handle_0); } void led_indicator_gpio_mode_test_all() @@ -163,19 +167,20 @@ void led_indicator_all_init() }; led_indicator_config_t config = { - .led_indicator_gpio_config = &led_indicator_gpio_config, - .mode = LED_GPIO_MODE, .blink_lists = NULL, .blink_list_num = 0, }; - led_handle_0 = led_indicator_create(&config); + esp_err_t ret = led_indicator_new_gpio_device(&config, &led_indicator_gpio_config, &led_handle_0); + TEST_ASSERT(ret == ESP_OK); TEST_ASSERT_NOT_NULL(led_handle_0); led_indicator_gpio_config.gpio_num = LED_IO_NUM_1; - led_handle_1 = led_indicator_create(&config); + ret = led_indicator_new_gpio_device(&config, &led_indicator_gpio_config, &led_handle_1); + TEST_ASSERT(ret == ESP_OK); TEST_ASSERT_NOT_NULL(led_handle_1); led_indicator_gpio_config.gpio_num = LED_IO_NUM_2; - led_handle_2 = led_indicator_create(&config); + ret = led_indicator_new_gpio_device(&config, &led_indicator_gpio_config, &led_handle_2); + TEST_ASSERT(ret == ESP_OK); TEST_ASSERT_NOT_NULL(led_handle_2); } @@ -260,6 +265,8 @@ TEST_CASE("blink three LED", "[LED][indicator]") led_indicator_all_deinit(); } +#endif + typedef enum { BLINK_25_BRIGHTNESS, BLINK_50_BRIGHTNESS, @@ -337,17 +344,16 @@ TEST_CASE("User defined blink", "[LED][indicator]") }; led_indicator_config_t config = { - .led_indicator_gpio_config = &led_indicator_gpio_config, - .mode = LED_GPIO_MODE, .blink_lists = led_blink_lst, .blink_list_num = BLINK_NUM, }; - led_handle_0 = led_indicator_create(&config); + esp_err_t ret = led_indicator_new_gpio_device(&config, &led_indicator_gpio_config, &led_handle_0); + TEST_ASSERT(ret == ESP_OK); TEST_ASSERT_NOT_NULL(led_handle_0); ESP_LOGI(TAG, "double blink....."); - esp_err_t ret = led_indicator_start(led_handle_0, BLINK_DOUBLE); + ret = led_indicator_start(led_handle_0, BLINK_DOUBLE); TEST_ASSERT(ret == ESP_OK); vTaskDelay(2000 / portTICK_PERIOD_MS); ret = led_indicator_stop(led_handle_0, BLINK_DOUBLE); @@ -373,17 +379,16 @@ TEST_CASE("Preempt blink lists test", "[LED][indicator]") }; led_indicator_config_t config = { - .led_indicator_gpio_config = &led_indicator_gpio_config, - .mode = LED_GPIO_MODE, .blink_lists = led_blink_lst, .blink_list_num = BLINK_NUM, }; - led_handle_0 = led_indicator_create(&config); + esp_err_t ret = led_indicator_new_gpio_device(&config, &led_indicator_gpio_config, &led_handle_0); + TEST_ASSERT(ret == ESP_OK); TEST_ASSERT_NOT_NULL(led_handle_0); ESP_LOGI(TAG, "double blink....."); - esp_err_t ret = led_indicator_start(led_handle_0, BLINK_DOUBLE); + ret = led_indicator_start(led_handle_0, BLINK_DOUBLE); vTaskDelay(1000 / portTICK_PERIOD_MS); TEST_ASSERT(ret == ESP_OK); @@ -416,78 +421,16 @@ TEST_CASE("breathe test", "[LED][indicator]") }; led_indicator_config_t config = { - .mode = LED_LEDC_MODE, - .led_indicator_ledc_config = &led_indicator_ledc_config, .blink_lists = led_blink_lst, .blink_list_num = BLINK_NUM, }; - led_handle_0 = led_indicator_create(&config); - TEST_ASSERT_NOT_NULL(led_handle_0); - - ESP_LOGI(TAG, "breathe 25/100 blink....."); - esp_err_t ret = led_indicator_start(led_handle_0, BLINK_25_BRIGHTNESS); - vTaskDelay(2000 / portTICK_PERIOD_MS); - TEST_ASSERT(ret == ESP_OK); - - ESP_LOGI(TAG, "breathe 50/100 blink....."); - ret = led_indicator_start(led_handle_0, BLINK_50_BRIGHTNESS); - vTaskDelay(2000 / portTICK_PERIOD_MS); - TEST_ASSERT(ret == ESP_OK); - - ESP_LOGI(TAG, "breathe 75/100 blink....."); - ret = led_indicator_start(led_handle_0, BLINK_75_BRIGHTNESS); - vTaskDelay(2000 / portTICK_PERIOD_MS); - TEST_ASSERT(ret == ESP_OK); - - ESP_LOGI(TAG, "breathe blink ....."); - ret = led_indicator_start(led_handle_0, BLINK_BREATHE); + esp_err_t ret = led_indicator_new_ledc_device(&config, &led_indicator_ledc_config, &led_handle_0); TEST_ASSERT(ret == ESP_OK); - vTaskDelay(8000 / portTICK_PERIOD_MS); - - led_indicator_deinit(); -} - -static esp_err_t custom_ledc_init(void *param) -{ - led_indicator_ledc_config_t led_indicator_ledc_config = { - .is_active_level_high = 1, - .timer_inited = false, - .timer_num = LEDC_TIMER_0, - .gpio_num = LED_IO_NUM_0, - .channel = LEDC_CHANNEL_0, - }; - - esp_err_t ret = led_indicator_ledc_init(&led_indicator_ledc_config); - return ret; -} - -TEST_CASE("custom mode test", "[LED][indicator]") -{ - led_indicator_custom_config_t led_indicator_custom_config = { - .is_active_level_high = 1, - .duty_resolution = LED_DUTY_13_BIT, - .hal_indicator_init = custom_ledc_init, - .hal_indicator_deinit = led_indicator_ledc_deinit, - .hal_indicator_set_on_off = led_indicator_ledc_set_on_off, - .hal_indicator_set_brightness = led_indicator_ledc_set_brightness, - .hal_indicator_set_rgb = NULL, - .hal_indicator_set_hsv = NULL, - .hardware_data = (void *)LEDC_CHANNEL_0, - }; - - led_indicator_config_t config = { - .led_indicator_custom_config = &led_indicator_custom_config, - .mode = LED_CUSTOM_MODE, - .blink_lists = led_blink_lst, - .blink_list_num = BLINK_NUM, - }; - - led_handle_0 = led_indicator_create(&config); TEST_ASSERT_NOT_NULL(led_handle_0); ESP_LOGI(TAG, "breathe 25/100 blink....."); - esp_err_t ret = led_indicator_start(led_handle_0, BLINK_25_BRIGHTNESS); + ret = led_indicator_start(led_handle_0, BLINK_25_BRIGHTNESS); vTaskDelay(2000 / portTICK_PERIOD_MS); TEST_ASSERT(ret == ESP_OK); @@ -504,7 +447,7 @@ TEST_CASE("custom mode test", "[LED][indicator]") ESP_LOGI(TAG, "breathe blink ....."); ret = led_indicator_start(led_handle_0, BLINK_BREATHE); TEST_ASSERT(ret == ESP_OK); - vTaskDelay(6000 / portTICK_PERIOD_MS); + vTaskDelay(8000 / portTICK_PERIOD_MS); led_indicator_deinit(); } @@ -525,18 +468,17 @@ TEST_CASE("test led preempt func with breath", "[LED][preempt][breath]") }; led_indicator_config_t config = { - .mode = LED_LEDC_MODE, - .led_indicator_ledc_config = &led_indicator_ledc_config, .blink_lists = led_blink_lst, .blink_list_num = BLINK_NUM, }; int cnt0 = 3; while (cnt0--) { - led_handle_0 = led_indicator_create(&config); + esp_err_t ret = led_indicator_new_ledc_device(&config, &led_indicator_ledc_config, &led_handle_0); + TEST_ASSERT(ret == ESP_OK); TEST_ASSERT_NOT_NULL(led_handle_0); ESP_LOGI(TAG, "breathe blink ....."); - esp_err_t ret = led_indicator_start(led_handle_0, BLINK_BREATHE); + ret = led_indicator_start(led_handle_0, BLINK_BREATHE); TEST_ASSERT(ret == ESP_OK); bool preempted = false; int cnt = 3; @@ -707,15 +649,13 @@ TEST_CASE("TEST LED RGB", "[LED RGB][RGB]") }; led_indicator_config_t config = { - .mode = LED_RGB_MODE, - .led_indicator_rgb_config = &led_grb_cfg, .blink_lists = led_rgb_blink_lst, .blink_list_num = BLINK_RGB_NUM, }; - led_handle_0 = led_indicator_create(&config); + esp_err_t ret = led_indicator_new_rgb_device(&config, &led_grb_cfg, &led_handle_0); + TEST_ASSERT(ret == ESP_OK); TEST_ASSERT_NOT_NULL(led_handle_0); - esp_err_t ret = ESP_OK; ESP_LOGI(TAG, "breathe 25/100 blink....."); ret = led_indicator_start(led_handle_0, BLINK_RGB_25_BRIGHTNESS); @@ -830,15 +770,13 @@ TEST_CASE("TEST LED Strips by RGB", "[LED Strips][RGB]") }; led_indicator_config_t config = { - .mode = LED_STRIPS_MODE, - .led_indicator_strips_config = &led_indicator_strips_config, .blink_lists = led_rgb_blink_lst, .blink_list_num = BLINK_RGB_NUM, }; - - led_handle_0 = led_indicator_create(&config); - TEST_ASSERT_NOT_NULL(led_handle_0); esp_err_t ret = ESP_OK; + ret = led_indicator_new_strips_device(&config, &led_indicator_strips_config, &led_handle_0); + TEST_ASSERT(ret == ESP_OK); + TEST_ASSERT_NOT_NULL(led_handle_0); ESP_LOGI(TAG, "breathe 25/100 blink....."); ret = led_indicator_start(led_handle_0, BLINK_RGB_25_BRIGHTNESS); @@ -950,15 +888,13 @@ TEST_CASE("TEST LED RGB control Real time ", "[LED RGB][Real time]") }; led_indicator_config_t config = { - .mode = LED_STRIPS_MODE, - .led_indicator_strips_config = &led_indicator_strips_config, .blink_lists = led_rgb_blink_lst, .blink_list_num = BLINK_RGB_NUM, }; - - led_handle_0 = led_indicator_create(&config); - esp_err_t ret = ESP_OK; + ret = led_indicator_new_strips_device(&config, &led_indicator_strips_config, &led_handle_0); + TEST_ASSERT(ret == ESP_OK); + TEST_ASSERT_NOT_NULL(led_handle_0); ESP_LOGI(TAG, "set red by rgb_value one by one ....."); @@ -988,6 +924,146 @@ TEST_CASE("TEST LED RGB control Real time ", "[LED RGB][Real time]") #endif +#ifdef CONFIG_USE_MI_BLINK_DEFAULT +TEST_CASE("TEST LED RGB MI DEFAULT", "[LED RGB][RGB]") +{ + led_indicator_rgb_config_t led_grb_cfg = { + .is_active_level_high = 1, + .timer_inited = false, + .timer_num = LEDC_TIMER_0, + .red_gpio_num = LED_RGB_RED_GPIO, + .green_gpio_num = LED_RGB_GREEN_GPIO, + .blue_gpio_num = LED_RGB_BLUE_GPIO, + .red_channel = LEDC_CHANNEL_0, + .green_channel = LEDC_CHANNEL_1, + .blue_channel = LEDC_CHANNEL_2, + }; + + led_indicator_config_t config = { + .blink_lists = NULL, + .blink_list_num = 0, + }; + + esp_err_t ret = led_indicator_new_rgb_device(&config, &led_grb_cfg, &led_handle_0); + TEST_ASSERT(ret == ESP_OK); + TEST_ASSERT_NOT_NULL(led_handle_0); + + ESP_LOGI(TAG, "MI wait connect....."); + ret = led_indicator_start(led_handle_0, BLINK_MI_WAIT_CONNECT); + TEST_ASSERT(ret == ESP_OK); + vTaskDelay(4000 / portTICK_PERIOD_MS); + ret = led_indicator_stop(led_handle_0, BLINK_MI_WAIT_CONNECT); + TEST_ASSERT(ret == ESP_OK); + + ESP_LOGI(TAG, "MI connecting....."); + ret = led_indicator_start(led_handle_0, BLINK_MI_CONNECTING); + TEST_ASSERT(ret == ESP_OK); + vTaskDelay(4000 / portTICK_PERIOD_MS); + ret = led_indicator_stop(led_handle_0, BLINK_MI_CONNECTING); + TEST_ASSERT(ret == ESP_OK); + + ESP_LOGI(TAG, "MI online....."); + ret = led_indicator_start(led_handle_0, BLINK_MI_ONLINE); + TEST_ASSERT(ret == ESP_OK); + vTaskDelay(4000 / portTICK_PERIOD_MS); + ret = led_indicator_stop(led_handle_0, BLINK_MI_ONLINE); + TEST_ASSERT(ret == ESP_OK); + + ESP_LOGI(TAG, "MI fault....."); + ret = led_indicator_start(led_handle_0, BLINK_MI_FAULT); + TEST_ASSERT(ret == ESP_OK); + vTaskDelay(4000 / portTICK_PERIOD_MS); + ret = led_indicator_stop(led_handle_0, BLINK_MI_FAULT); + TEST_ASSERT(ret == ESP_OK); + + ESP_LOGI(TAG, "MI reconnecting....."); + ret = led_indicator_start(led_handle_0, BLINK_MI_RECONNECTING); + TEST_ASSERT(ret == ESP_OK); + vTaskDelay(4000 / portTICK_PERIOD_MS); + ret = led_indicator_stop(led_handle_0, BLINK_MI_RECONNECTING); + TEST_ASSERT(ret == ESP_OK); + + ESP_LOGI(TAG, "MI ota_updating....."); + ret = led_indicator_start(led_handle_0, BLINK_MI_OTA_UPDATING); + TEST_ASSERT(ret == ESP_OK); + vTaskDelay(4000 / portTICK_PERIOD_MS); + ret = led_indicator_stop(led_handle_0, BLINK_MI_OTA_UPDATING); + TEST_ASSERT(ret == ESP_OK); + + ESP_LOGI(TAG, "MI wifi provision off....."); + ret = led_indicator_start(led_handle_0, BLINK_MI_WIFI_PROVISION_OFF); + TEST_ASSERT(ret == ESP_OK); + vTaskDelay(4000 / portTICK_PERIOD_MS); + ret = led_indicator_stop(led_handle_0, BLINK_MI_WIFI_PROVISION_OFF); + TEST_ASSERT(ret == ESP_OK); + + ESP_LOGI(TAG, "MI working....."); + ret = led_indicator_start(led_handle_0, BLINK_MI_WORKING); + TEST_ASSERT(ret == ESP_OK); + vTaskDelay(4000 / portTICK_PERIOD_MS); + ret = led_indicator_stop(led_handle_0, BLINK_MI_WORKING); + TEST_ASSERT(ret == ESP_OK); + + ESP_LOGI(TAG, "MI work abnormal....."); + ret = led_indicator_start(led_handle_0, BLINK_MI_WORK_ABNORMAL); + TEST_ASSERT(ret == ESP_OK); + vTaskDelay(4000 / portTICK_PERIOD_MS); + ret = led_indicator_stop(led_handle_0, BLINK_MI_WORK_ABNORMAL); + TEST_ASSERT(ret == ESP_OK); + + ESP_LOGI(TAG, "MI env good....."); + ret = led_indicator_start(led_handle_0, BLINK_MI_ENV_GOOD); + TEST_ASSERT(ret == ESP_OK); + vTaskDelay(4000 / portTICK_PERIOD_MS); + ret = led_indicator_stop(led_handle_0, BLINK_MI_ENV_GOOD); + TEST_ASSERT(ret == ESP_OK); + + ESP_LOGI(TAG, "MI env moderate....."); + ret = led_indicator_start(led_handle_0, BLINK_MI_ENV_MODERATE); + TEST_ASSERT(ret == ESP_OK); + vTaskDelay(4000 / portTICK_PERIOD_MS); + ret = led_indicator_stop(led_handle_0, BLINK_MI_ENV_MODERATE); + TEST_ASSERT(ret == ESP_OK); + + ESP_LOGI(TAG, "MI env severe....."); + ret = led_indicator_start(led_handle_0, BLINK_MI_ENV_SEVERE); + TEST_ASSERT(ret == ESP_OK); + vTaskDelay(4000 / portTICK_PERIOD_MS); + ret = led_indicator_stop(led_handle_0, BLINK_MI_ENV_SEVERE); + TEST_ASSERT(ret == ESP_OK); + + ESP_LOGI(TAG, "MI setting....."); + ret = led_indicator_start(led_handle_0, BLINK_MI_SETTING); + TEST_ASSERT(ret == ESP_OK); + vTaskDelay(4000 / portTICK_PERIOD_MS); + ret = led_indicator_stop(led_handle_0, BLINK_MI_SETTING); + TEST_ASSERT(ret == ESP_OK); + + ESP_LOGI(TAG, "MI low battery....."); + ret = led_indicator_start(led_handle_0, BLINK_MI_LOW_BATTERY); + TEST_ASSERT(ret == ESP_OK); + vTaskDelay(4000 / portTICK_PERIOD_MS); + ret = led_indicator_stop(led_handle_0, BLINK_MI_LOW_BATTERY); + TEST_ASSERT(ret == ESP_OK); + + ESP_LOGI(TAG, "MI charging....."); + ret = led_indicator_start(led_handle_0, BLINK_MI_CHARGING); + TEST_ASSERT(ret == ESP_OK); + vTaskDelay(10000 / portTICK_PERIOD_MS); + ret = led_indicator_stop(led_handle_0, BLINK_MI_CHARGING); + TEST_ASSERT(ret == ESP_OK); + + ESP_LOGI(TAG, "MI charged....."); + ret = led_indicator_start(led_handle_0, BLINK_MI_CHARGED); + TEST_ASSERT(ret == ESP_OK); + vTaskDelay(4000 / portTICK_PERIOD_MS); + ret = led_indicator_stop(led_handle_0, BLINK_MI_CHARGED); + TEST_ASSERT(ret == ESP_OK); + + led_indicator_deinit(); +} +#endif + static size_t before_free_8bit; static size_t before_free_32bit; diff --git a/docs/en/display/led_indicator.rst b/docs/en/display/led_indicator.rst index 52e8c8b616..7d22bc3d34 100644 --- a/docs/en/display/led_indicator.rst +++ b/docs/en/display/led_indicator.rst @@ -230,16 +230,15 @@ Create an indicator by specifying an IO and a set of configuration information. .. code:: c + led_indicator_gpio_config_t led_indicator_gpio_config = { + .gpio_num = 1, /**< num of GPIO */ + .is_active_level_high = 1, + }; led_indicator_config_t config = { - .mode = LED_GPIO_MODE, - .led_gpio_config = { - .active_level = 1, - .gpio_num = 1, - }, .blink_lists = led_indicator_get_sample_lists(), .blink_list_num = led_indicator_get_sample_lists_num(), }; - led_indicator_handle_t led_handle = led_indicator_create(8, &config); // attach to gpio 8 + esp_err_t ret = led_indicator_new_gpio_device(&config, &led_indicator_gpio_config, &led_handle); Start/stop blinking: control your indicator to start/stop a specified type of blink by calling corresponding functions. The functions are returned immediately after calling, and the blink process is controlled by the internal timer. The same indicator can perform multiple blink types in turn based on their priorities. @@ -288,14 +287,14 @@ Custom light blink [BLINK_NUM] = NULL, }; + led_indicator_gpio_config_t led_indicator_gpio_config = { + .gpio_num = 1, /**< num of GPIO */ + .is_active_level_high = 1, + }; + led_indicator_config_t config = { - .mode = LED_GPIO_MODE, - .led_gpio_config = { - .active_level = 1, - .gpio_num = 1, - }, - .blink_lists = led_blink_lst, - .blink_list_num = BLINK_MAX, + .blink_lists = led_blink_lst,, + .blink_list_num = BLINK_MAX,, }; By defining ``led_blink_lst[]`` to achieve the custom indicator. diff --git a/docs/zh_CN/display/led_indicator.rst b/docs/zh_CN/display/led_indicator.rst index 4c422d4279..1986571ef6 100644 --- a/docs/zh_CN/display/led_indicator.rst +++ b/docs/zh_CN/display/led_indicator.rst @@ -226,16 +226,15 @@ LED 指示灯是最简单的输出外设之一,可以通过不同形式的闪 .. code:: c + led_indicator_gpio_config_t led_indicator_gpio_config = { + .gpio_num = 1, /**< num of GPIO */ + .is_active_level_high = 1, + }; led_indicator_config_t config = { - .mode = LED_GPIO_MODE, - .led_gpio_config = { - .active_level = 1, - .gpio_num = 1, - }, .blink_lists = led_indicator_get_sample_lists(), .blink_list_num = led_indicator_get_sample_lists_num(), }; - led_indicator_handle_t led_handle = led_indicator_create(8, &config); // attach to gpio 8 + esp_err_t ret = led_indicator_new_gpio_device(&config, &led_indicator_gpio_config, &led_handle); 开始/停止闪烁:控制指示灯开启/停止指定闪烁类型,函数调用后立刻返回,内部由定时器控制闪烁流程。同一个指示灯可以开启多种闪烁类型,将根据闪烁类型优先级依次执行。 @@ -285,16 +284,17 @@ LED 指示灯是最简单的输出外设之一,可以通过不同形式的闪 [BLINK_NUM] = NULL, }; + led_indicator_gpio_config_t led_indicator_gpio_config = { + .gpio_num = 1, /**< num of GPIO */ + .is_active_level_high = 1, + }; + led_indicator_config_t config = { - .mode = LED_GPIO_MODE, - .led_gpio_config = { - .active_level = 1, - .gpio_num = 1, - }, - .blink_lists = led_blink_lst, - .blink_list_num = BLINK_MAX, + .blink_lists = led_blink_lst,, + .blink_list_num = BLINK_MAX,, }; + 通过定义 ``led_blink_lst[]`` 实现自定义指示灯。 gamma 曲线调光 diff --git a/examples/indicator/gpio/main/main.c b/examples/indicator/gpio/main/main.c index 2b429f4592..e644df8cba 100644 --- a/examples/indicator/gpio/main/main.c +++ b/examples/indicator/gpio/main/main.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -11,7 +11,7 @@ #include "cmd_led_indicator.h" #include "esp_idf_version.h" #include "esp_log.h" -#include "led_indicator.h" +#include "led_indicator_gpio.h" #define GPIO_LED_PIN CONFIG_EXAMPLE_GPIO_NUM #define GPIO_ACTIVE_LEVEL CONFIG_EXAMPLE_GPIO_ACTIVE_LEVEL @@ -120,13 +120,12 @@ void app_main(void) }; const led_indicator_config_t config = { - .mode = LED_GPIO_MODE, - .led_indicator_gpio_config = &gpio_config, .blink_lists = led_mode, .blink_list_num = BLINK_MAX, }; - led_handle = led_indicator_create(&config); + esp_err_t ret = led_indicator_new_gpio_device(&config, &gpio_config, &led_handle); + ESP_ERROR_CHECK(ret); assert(led_handle != NULL); #if CONFIG_EXAMPLE_ENABLE_CONSOLE_CONTROL diff --git a/examples/indicator/ledc/main/main.c b/examples/indicator/ledc/main/main.c index 7a1b820aad..12a28988b5 100644 --- a/examples/indicator/ledc/main/main.c +++ b/examples/indicator/ledc/main/main.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -11,7 +11,7 @@ #include "cmd_led_indicator.h" #include "esp_idf_version.h" #include "esp_log.h" -#include "led_indicator.h" +#include "led_indicator_ledc.h" #define GPIO_LED_PIN CONFIG_EXAMPLE_GPIO_NUM #define GPIO_ACTIVE_LEVEL CONFIG_EXAMPLE_GPIO_ACTIVE_LEVEL @@ -148,13 +148,12 @@ void app_main(void) }; const led_indicator_config_t config = { - .mode = LED_LEDC_MODE, - .led_indicator_ledc_config = &ledc_config, .blink_lists = led_mode, .blink_list_num = BLINK_MAX, }; - led_handle = led_indicator_create(&config); + esp_err_t ret = led_indicator_new_ledc_device(&config, &ledc_config, &led_handle); + ESP_ERROR_CHECK(ret); assert(led_handle != NULL); #if CONFIG_EXAMPLE_ENABLE_CONSOLE_CONTROL diff --git a/examples/indicator/rgb/main/main.c b/examples/indicator/rgb/main/main.c index c17485127b..584b330760 100644 --- a/examples/indicator/rgb/main/main.c +++ b/examples/indicator/rgb/main/main.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -11,7 +11,7 @@ #include "cmd_led_indicator.h" #include "esp_idf_version.h" #include "esp_log.h" -#include "led_indicator.h" +#include "led_indicator_rgb.h" #define GPIO_LED_RED_PIN CONFIG_EXAMPLE_GPIO_RED_NUM #define GPIO_LED_GREEN_PIN CONFIG_EXAMPLE_GPIO_GREEN_NUM @@ -182,13 +182,12 @@ void app_main(void) }; const led_indicator_config_t config = { - .mode = LED_RGB_MODE, - .led_indicator_rgb_config = &rgb_config, .blink_lists = led_mode, .blink_list_num = BLINK_MAX, }; - led_handle = led_indicator_create(&config); + esp_err_t ret = led_indicator_new_rgb_device(&config, &rgb_config, &led_handle); + ESP_ERROR_CHECK(ret); assert(led_handle != NULL); #if CONFIG_EXAMPLE_ENABLE_CONSOLE_CONTROL diff --git a/examples/indicator/ws2812_strips/main/main.c b/examples/indicator/ws2812_strips/main/main.c index f44c49feea..061da6545c 100644 --- a/examples/indicator/ws2812_strips/main/main.c +++ b/examples/indicator/ws2812_strips/main/main.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -11,7 +11,7 @@ #include "cmd_led_indicator.h" #include "esp_idf_version.h" #include "esp_log.h" -#include "led_indicator.h" +#include "led_indicator_strips.h" #define WS2812_GPIO_NUM CONFIG_EXAMPLE_WS2812_GPIO_NUM #define WS2812_STRIPS_NUM CONFIG_EXAMPLE_WS2812_STRIPS_NUM @@ -212,13 +212,12 @@ void app_main(void) }; const led_indicator_config_t config = { - .mode = LED_STRIPS_MODE, - .led_indicator_strips_config = &strips_config, .blink_lists = led_mode, .blink_list_num = BLINK_MAX, }; - led_handle = led_indicator_create(&config); + esp_err_t ret = led_indicator_new_strips_device(&config, &strips_config, &led_handle); + ESP_ERROR_CHECK(ret); assert(led_handle != NULL); #if CONFIG_EXAMPLE_ENABLE_CONSOLE_CONTROL diff --git a/examples/usb/host/usb_cdc_4g_module/main/app_main.c b/examples/usb/host/usb_cdc_4g_module/main/app_main.c index 573aee9178..43ec1376b7 100644 --- a/examples/usb/host/usb_cdc_4g_module/main/app_main.c +++ b/examples/usb/host/usb_cdc_4g_module/main/app_main.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -18,7 +18,7 @@ #include "nvs_flash.h" #include "nvs.h" #include "esp_netif.h" -#include "led_indicator.h" +#include "led_indicator_gpio.h" #include "led_indicator_blink_default.h" #include "usbh_modem_board.h" #include "usbh_modem_wifi.h" @@ -114,25 +114,28 @@ static void _led_indicator_init() }; led_indicator_config_t led_config = { - .led_indicator_gpio_config = &led_indicator_gpio_config, - .mode = LED_GPIO_MODE, + .blink_lists = NULL, + .blink_list_num = 0, }; if (LED_RED_SYSTEM_GPIO) { led_indicator_gpio_config.gpio_num = LED_RED_SYSTEM_GPIO; - s_led_system_handle = led_indicator_create(&led_config); + esp_err_t ret = led_indicator_new_gpio_device(&led_config, &led_indicator_gpio_config, &s_led_system_handle); + ESP_ERROR_CHECK(ret); assert(s_led_system_handle != NULL); } if (LED_BLUE_WIFI_GPIO) { led_indicator_gpio_config.gpio_num = LED_BLUE_WIFI_GPIO; - s_led_wifi_handle = led_indicator_create(&led_config); + ret = led_indicator_new_gpio_device(&led_config, &led_indicator_gpio_config, &s_led_wifi_handle); + ESP_ERROR_CHECK(ret); assert(s_led_wifi_handle != NULL); led_indicator_stop(s_led_wifi_handle, BLINK_CONNECTED); led_indicator_start(s_led_wifi_handle, BLINK_CONNECTING); } if (LED_GREEN_4GMODEM_GPIO) { led_indicator_gpio_config.gpio_num = LED_GREEN_4GMODEM_GPIO; - s_led_4g_handle = led_indicator_create(&led_config); + ret = led_indicator_new_gpio_device(&led_config, &led_indicator_gpio_config, &s_led_4g_handle); + ESP_ERROR_CHECK(ret); assert(s_led_4g_handle != NULL); led_indicator_stop(s_led_4g_handle, BLINK_CONNECTED); led_indicator_start(s_led_4g_handle, BLINK_CONNECTING);