Skip to content

Commit a1760b8

Browse files
committed
feat(led_indicator): Refactor the components into the Factory Pattern
1 parent 7af24e9 commit a1760b8

File tree

16 files changed

+426
-257
lines changed

16 files changed

+426
-257
lines changed

components/led/led_indicator/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ set(srcs "src/led_ledc.c"
66
"src/led_rgb.c"
77
"src/led_strips.c"
88
"src/led_convert.c"
9+
"src/led_custom.c"
910
)
1011

1112
idf_component_register( SRCS ${srcs}

components/led/led_indicator/include/led_convert.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -16,6 +16,29 @@ extern "C" {
1616
#define MAX_BRIGHTNESS 255
1717
#define MAX_INDEX 127
1818

19+
/**
20+
* @brief LED duty should be consistent with the physical resolution of the indicator.
21+
* eg. LED_GPIO_MODE should with LED_DUTY_1_BIT
22+
*
23+
*/
24+
typedef enum {
25+
LED_DUTY_1_BIT = 1, /*!< LED duty resolution of 1 bits */
26+
LED_DUTY_2_BIT, /*!< LED duty resolution of 2 bits */
27+
LED_DUTY_3_BIT, /*!< LED duty resolution of 3 bits */
28+
LED_DUTY_4_BIT, /*!< LED duty resolution of 4 bits */
29+
LED_DUTY_5_BIT, /*!< LED duty resolution of 5 bits */
30+
LED_DUTY_6_BIT, /*!< LED duty resolution of 6 bits */
31+
LED_DUTY_7_BIT, /*!< LED duty resolution of 7 bits */
32+
LED_DUTY_8_BIT, /*!< LED duty resolution of 8 bits */
33+
LED_DUTY_9_BIT, /*!< LED duty resolution of 9 bits */
34+
LED_DUTY_10_BIT, /*!< LED duty resolution of 10 bits */
35+
LED_DUTY_11_BIT, /*!< LED duty resolution of 11 bits */
36+
LED_DUTY_12_BIT, /*!< LED duty resolution of 12 bits */
37+
LED_DUTY_13_BIT, /*!< LED duty resolution of 13 bits */
38+
LED_DUTY_14_BIT, /*!< LED duty resolution of 14 bits */
39+
LED_DUTY_15_BIT, /*!< LED duty resolution of 15 bits */
40+
} led_indicator_duty_t;
41+
1942
#define SET_RGB(r, g, b) \
2043
((((r) & 0xFF) << 16) | (((g) & 0xFF) << 8) | ((b) & 0xFF))
2144

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -13,29 +13,7 @@ extern "C" {
1313
#include <stdbool.h>
1414
#include <stdint.h>
1515
#include "esp_err.h"
16-
17-
/**
18-
* @brief LED duty should be consistent with the physical resolution of the indicator.
19-
* eg. LED_GPIO_MODE should with LED_DUTY_1_BIT
20-
*
21-
*/
22-
typedef enum {
23-
LED_DUTY_1_BIT = 1, /*!< LED duty resolution of 1 bits */
24-
LED_DUTY_2_BIT, /*!< LED duty resolution of 2 bits */
25-
LED_DUTY_3_BIT, /*!< LED duty resolution of 3 bits */
26-
LED_DUTY_4_BIT, /*!< LED duty resolution of 4 bits */
27-
LED_DUTY_5_BIT, /*!< LED duty resolution of 5 bits */
28-
LED_DUTY_6_BIT, /*!< LED duty resolution of 6 bits */
29-
LED_DUTY_7_BIT, /*!< LED duty resolution of 7 bits */
30-
LED_DUTY_8_BIT, /*!< LED duty resolution of 8 bits */
31-
LED_DUTY_9_BIT, /*!< LED duty resolution of 9 bits */
32-
LED_DUTY_10_BIT, /*!< LED duty resolution of 10 bits */
33-
LED_DUTY_11_BIT, /*!< LED duty resolution of 11 bits */
34-
LED_DUTY_12_BIT, /*!< LED duty resolution of 12 bits */
35-
LED_DUTY_13_BIT, /*!< LED duty resolution of 13 bits */
36-
LED_DUTY_14_BIT, /*!< LED duty resolution of 14 bits */
37-
LED_DUTY_15_BIT, /*!< LED duty resolution of 15 bits */
38-
} led_indicator_duty_t;
16+
#include "led_indicator.h"
3917

4018
/**
4119
* @brief LED custom configuration
@@ -53,6 +31,21 @@ typedef struct {
5331
void *hardware_data; /*!< user hardware data*/
5432
} led_indicator_custom_config_t;
5533

34+
/**
35+
* @brief Create a new custom LED indicator device.
36+
*
37+
* This function initializes a new LED indicator device using the provided LED configuration
38+
* and custom configuration parameters.
39+
*
40+
* @param led_config Pointer to the LED configuration structure.
41+
* @param custom_cfg Pointer to the custom configuration structure for the LED indicator.
42+
* @param handle pointer to LED indicator handle
43+
* @return esp_err_t
44+
* - ESP_ERR_INVALID_ARG if parameter is invalid
45+
* - ESP_OK Success
46+
* - ESP_FAIL Delete fail
47+
*/
48+
led_indicator_handle_t iot_led_new_custom_device(const led_config_t *led_config, const led_indicator_custom_config_t *custom_cfg);
5649
#ifdef __cplusplus
5750
}
5851
#endif

components/led/led_indicator/include/led_gpio.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
/*
2-
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

77
#pragma once
88

99
#include "driver/gpio.h"
10+
#include "led_indicator.h"
1011

1112
#ifdef __cplusplus
1213
extern "C" {
@@ -54,6 +55,22 @@ esp_err_t led_indicator_gpio_deinit(void *handle);
5455
*/
5556
esp_err_t led_indicator_gpio_set_on_off(void *handle, bool on_off);
5657

58+
/**
59+
* @brief Create a new LED indicator device using a GPIO pin.
60+
*
61+
* This function initializes a new LED indicator device based on the provided LED configuration
62+
* and GPIO configuration. It returns a handle to the created LED indicator device, which can be
63+
* used for further control and management.
64+
*
65+
* @param led_config Pointer to the LED configuration structure.
66+
* @param gpio_cfg Pointer to the GPIO configuration structure for the LED.
67+
* @param handle pointer to LED indicator handle
68+
* @return esp_err_t
69+
* - ESP_ERR_INVALID_ARG if parameter is invalid
70+
* - ESP_OK Success
71+
* - ESP_FAIL Delete fail
72+
*/
73+
led_indicator_handle_t iot_led_new_gpio_device(const led_config_t *led_config, const led_indicator_gpio_config_t *gpio_cfg);
5774
#ifdef __cplusplus
5875
}
5976
#endif

components/led/led_indicator/include/led_indicator.h

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -12,14 +12,21 @@ extern "C" {
1212

1313
#include <stdint.h>
1414
#include <stdbool.h>
15+
#include "freertos/FreeRTOS.h"
16+
#include "freertos/timers.h"
17+
#include "freertos/semphr.h"
1518
#include "esp_err.h"
16-
#include "led_gpio.h"
17-
#include "led_ledc.h"
18-
#include "led_rgb.h"
19-
#include "led_strips.h"
2019
#include "led_convert.h"
21-
#include "led_custom.h"
2220

21+
#define LED_INDICATOR_CHECK(a, str, action) if(!(a)) { \
22+
ESP_LOGE(TAG,"%s:%d (%s):%s", __FILE__, __LINE__, __FUNCTION__, str); \
23+
action; \
24+
}
25+
26+
#define LED_INDICATOR_CHECK_WARNING(a, str, action) if(!(a)) { \
27+
ESP_LOGW(TAG,"%s:%d (%s):%s", __FILE__, __LINE__, __FUNCTION__, str); \
28+
action; \
29+
}
2330
/**
2431
* @brief LED state: 0-100, only hardware that supports to set brightness can adjust brightness.
2532
*
@@ -70,32 +77,58 @@ typedef enum {
7077
LED_CUSTOM_MODE, /*!< blink with custom driver */
7178
} led_indicator_mode_t;
7279

73-
/**
74-
* @brief LED indicator specified configurations, as a arg when create a new indicator
75-
*
76-
*/
7780
typedef struct {
78-
led_indicator_mode_t mode; /*!< LED work mode, eg. GPIO or pwm mode */
79-
union {
80-
led_indicator_gpio_config_t *led_indicator_gpio_config; /*!< LED GPIO configuration */
81-
led_indicator_ledc_config_t *led_indicator_ledc_config; /*!< LED LEDC configuration */
82-
led_indicator_rgb_config_t *led_indicator_rgb_config; /*!< LED RGB configuration */
83-
led_indicator_strips_config_t *led_indicator_strips_config; /*!< LED LEDC rgb configuration */
84-
led_indicator_custom_config_t *led_indicator_custom_config; /*!< LED custom configuration */
85-
}; /**< LED configuration */
81+
esp_err_t (*hal_indicator_set_on_off)(void *hardware_data, bool on_off); /*!< Pointer function for setting on or off */
82+
esp_err_t (*hal_indicator_deinit)(void *hardware_data); /*!< Pointer function for Deinitialization */
83+
esp_err_t (*hal_indicator_set_brightness)(void *hardware_data, uint32_t brightness); /*!< Pointer function for setting brightness, must be supported by hardware */
84+
esp_err_t (*hal_indicator_set_rgb)(void *hardware, uint32_t rgb_value); /*!< Pointer function for setting rgb, must be supported by hardware */
85+
esp_err_t (*hal_indicator_set_hsv)(void *hardware, uint32_t hsv_value); /*!< Pointer function for setting hsv, must be supported by hardware */
86+
void *hardware_data; /*!< Hardware data of the LED indicator */
87+
led_indicator_mode_t mode; /*!< LED work mode, eg. GPIO or pwm mode */
88+
int active_blink; /*!< Active blink list*/
89+
int preempt_blink; /*!< Highest priority blink list*/
90+
int *p_blink_steps; /*!< Stage of each blink list */
91+
led_indicator_ihsv_t current_fade_value; /*!< Current fade value */
92+
led_indicator_ihsv_t last_fade_value; /*!< Save the last value. */
93+
uint16_t fade_value_count; /*!< Count the number of fade */
94+
uint16_t fade_step; /*!< Step of fade */
95+
uint16_t fade_total_step; /*!< Total step of fade */
96+
uint32_t max_duty; /*!< Max duty cycle from duty_resolution : 2^duty_resolution -1 */
97+
SemaphoreHandle_t mutex; /*!< Mutex to achieve thread-safe */
98+
TimerHandle_t h_timer; /*!< LED timer handle, invalid if works in pwm mode */
99+
blink_step_t const **blink_lists; /*!< User defined LED blink lists */
100+
uint16_t blink_list_num; /*!< Number of blink lists */
101+
} _led_indicator_t;
102+
103+
typedef struct _led_indicator_com_config {
104+
esp_err_t (*hal_indicator_set_on_off)(void *hardware_data, bool on_off); /*!< Pointer function for setting on or off */
105+
esp_err_t (*hal_indicator_deinit)(void *hardware_data); /*!< Pointer function for Deinitialization */
106+
esp_err_t (*hal_indicator_set_brightness)(void *hardware_data, uint32_t brightness); /*!< Pointer function for setting brightness, must be supported by hardware */
107+
esp_err_t (*hal_indicator_set_rgb)(void *hardware, uint32_t rgb_value); /*!< Pointer function for setting rgb, must be supported by hardware */
108+
esp_err_t (*hal_indicator_set_hsv)(void *hardware, uint32_t hsv_value); /*!< Pointer function for setting hsv, must be supported by hardware */
109+
void *hardware_data; /*!< GPIO number of the LED indicator */
110+
blink_step_t const **blink_lists; /*!< User defined LED blink lists */
111+
uint16_t blink_list_num; /*!< Number of blink lists */
112+
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. */
113+
} _led_indicator_com_config_t;
114+
115+
typedef struct {
86116
blink_step_t const **blink_lists; /*!< user defined LED blink lists */
87117
uint16_t blink_list_num; /*!< number of blink lists */
88-
} led_indicator_config_t;
118+
} led_config_t;
89119

90120
typedef void *led_indicator_handle_t; /*!< LED indicator operation handle */
91121

122+
_led_indicator_t *_led_indicator_create_com(_led_indicator_com_config_t *cfg);
123+
124+
esp_err_t _led_indicator_add_node(_led_indicator_t *p_led_indicator);
125+
92126
/**
93127
* @brief create a LED indicator instance with GPIO number and configuration
94128
*
95129
* @param config configuration of the LED, eg. GPIO level when LED off
96130
* @return led_indicator_handle_t handle of the LED indicator, NULL if create failed.
97131
*/
98-
led_indicator_handle_t led_indicator_create(const led_indicator_config_t *config);
99132

100133
/**
101134
* @brief delete the LED indicator and release resource

components/led/led_indicator/include/led_indicator_blink_default.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
#pragma once
8-
8+
#include "led_indicator.h"
99
#ifdef __cplusplus
1010
extern "C" {
1111
#endif

components/led/led_indicator/include/led_ledc.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
/*
2-
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

77
#pragma once
88

99
#include "driver/ledc.h"
10+
#include "led_indicator.h"
1011

1112
#ifdef __cplusplus
1213
extern "C" {
@@ -65,6 +66,22 @@ esp_err_t led_indicator_ledc_set_on_off(void *ledc_handle, bool on_off);
6566
*/
6667
esp_err_t led_indicator_ledc_set_brightness(void *ledc_handle, uint32_t brightness);
6768

69+
/**
70+
* @brief Create a new LED indicator device using the LEDC (LED Controller) peripheral.
71+
*
72+
* This function initializes a new LED indicator device with the specified configuration,
73+
* utilizing the ESP32's LEDC hardware for PWM control.
74+
*
75+
* @param led_config Pointer to the general LED configuration structure.
76+
* @param ledc_cfg Pointer to the LEDC-specific configuration structure.
77+
* @param handle pointer to LED indicator handle
78+
* @return esp_err_t
79+
* - ESP_ERR_INVALID_ARG if parameter is invalid
80+
* - ESP_OK Success
81+
* - ESP_FAIL Delete fail
82+
*/
83+
led_indicator_handle_t iot_led_new_ledc_device(const led_config_t *led_config, const led_indicator_ledc_config_t *ledc_cfg);
84+
6885
#ifdef __cplusplus
6986
}
7087
#endif

components/led/led_indicator/include/led_rgb.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/*
2-
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66
#pragma once
77

88
#include "driver/ledc.h"
9+
#include "led_indicator.h"
910

1011
#ifdef __cplusplus
1112
extern "C" {
@@ -97,6 +98,22 @@ esp_err_t led_indicator_rgb_set_hsv(void *rgb_handle, uint32_t hsv_value);
9798
*/
9899
esp_err_t led_indicator_rgb_set_brightness(void *rgb_handle, uint32_t brightness);
99100

101+
/**
102+
* @brief Create a new RGB LED device instance.
103+
*
104+
* This function initializes a new RGB LED device using the specified LED configuration
105+
* and RGB-specific configuration parameters.
106+
*
107+
* @param led_config Pointer to the general LED configuration structure.
108+
* @param rgb_cfg Pointer to the RGB-specific configuration structure.
109+
* @param handle pointer to LED indicator handle
110+
* @return esp_err_t
111+
* - ESP_ERR_INVALID_ARG if parameter is invalid
112+
* - ESP_OK Success
113+
* - ESP_FAIL Delete fail
114+
*/
115+
led_indicator_handle_t iot_led_new_rgb_device(const led_config_t *led_config, const led_indicator_rgb_config_t *rgb_cfg);
116+
100117
#ifdef __cplusplus
101118
}
102119
#endif

components/led/led_indicator/include/led_strips.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -9,6 +9,7 @@
99
#include "led_strip.h"
1010
#include "led_strip_types.h"
1111
#include "esp_idf_version.h"
12+
#include "led_indicator.h"
1213

1314
#ifdef __cplusplus
1415
extern "C" {
@@ -107,6 +108,21 @@ esp_err_t led_indicator_strips_set_hsv(void *strips, uint32_t ihsv_value);
107108
*/
108109
esp_err_t led_indicator_strips_set_brightness(void *strips, uint32_t ibrightness);
109110

111+
/**
112+
* @brief Create a new LED indicator device using LED strips.
113+
*
114+
* This function initializes and returns a handle to a new LED indicator device
115+
* based on the provided LED configuration and LED strips configuration.
116+
*
117+
* @param led_config Pointer to the LED configuration structure.
118+
* @param strips_cfg Pointer to the LED strips specific configuration structure.
119+
* @param handle pointer to LED indicator handle
120+
* @return esp_err_t
121+
* - ESP_ERR_INVALID_ARG if parameter is invalid
122+
* - ESP_OK Success
123+
* - ESP_FAIL Delete fail
124+
*/
125+
led_indicator_handle_t iot_led_new_strips_device(const led_config_t *led_config, const led_indicator_strips_config_t *strips_cfg);
110126
#ifdef __cplusplus
111127
}
112128
#endif

0 commit comments

Comments
 (0)