Skip to content

Commit 6e7086c

Browse files
committed
Merge branch 'fix/fix_led_indicator_ledc_invert' into 'master'
fix(led_indicator): fix ledc default low output in activate-low mode See merge request ae_group/esp-iot-solution!1341
2 parents 9011c85 + ca0d15b commit 6e7086c

File tree

4 files changed

+13
-10
lines changed

4 files changed

+13
-10
lines changed

components/led/led_indicator/CHANGELOG.md

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

3+
## v2.0.1 - 2025-8-12
4+
5+
### Bugfix
6+
7+
* Fix: For LEDs driven by the LEDC driver with active-low enable, use the LEDC inversion function to avoid the LED turning on immediately after creation due to an initial low-level output.
8+
39
## v2.0.0 - 2025-7-30
410

511
### Improve:

components/led/led_indicator/idf_component.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: "2.0.0"
1+
version: "2.0.1"
22
description: LED indicator driver
33
url: https://github.com/espressif/esp-iot-solution/tree/master/components/led/led_indicator
44
dependencies:

components/led/led_indicator/src/led_indicator_ledc.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
#include <math.h>
8+
#include "driver/gpio.h"
89
#include "driver/ledc.h"
910
#include "esp_log.h"
1011
#include "led_indicator_ledc.h"
@@ -56,6 +57,7 @@ static esp_err_t led_indicator_ledc_init(void *param)
5657
ledc_channel_t ch = cfg->channel;
5758
LED_LEDC_CHECK(!s_ledc->ledc_channel[ch].is_init, "LEDC channel is already initialized!", goto EXIT);
5859
ledc_channel_config_t ledc_ch_cfg = LEDC_CHANNEL_CONFIG(cfg->timer_num, cfg->channel, cfg->gpio_num);
60+
ledc_ch_cfg.flags.output_invert = cfg->is_active_level_high ? false : true;
5961
ret = ledc_channel_config(&ledc_ch_cfg);
6062
LED_LEDC_CHECK(ESP_OK == ret, "ledc_channel_config fail!", goto EXIT);
6163
s_ledc->ledc_channel[ch].channel = ch;
@@ -93,9 +95,6 @@ static esp_err_t led_indicator_ledc_set_on_off(void *channel, bool on_off)
9395
uint32_t ch = (uint32_t)channel;
9496
LED_LEDC_CHECK(s_ledc->ledc_channel[ch].is_init, "LEDC channel doesn't init", return ESP_FAIL);
9597
uint32_t duty = on_off ? s_ledc->max_duty : 0;
96-
if (!s_ledc->ledc_channel[ch].is_active_level_high) {
97-
duty = s_ledc->max_duty - duty;
98-
}
9998

10099
ret = ledc_set_duty(LEDC_MODE, s_ledc->ledc_channel[ch].channel, duty);
101100
LED_LEDC_CHECK(ESP_OK == ret, "LEDC set duty error", return ret);
@@ -110,7 +109,6 @@ static esp_err_t led_indicator_ledc_set_brightness(void *channel, uint32_t brigh
110109
uint32_t ch = (uint32_t)channel;
111110
LED_LEDC_CHECK(s_ledc->ledc_channel[ch].is_init, "LEDC channel doesn't init", return ESP_FAIL);
112111
LED_LEDC_CHECK(brightness <= UINT8_MAX, "brightness can't be larger than UINT8_MAX", return ESP_FAIL);
113-
brightness = s_ledc->ledc_channel[ch].is_active_level_high ? brightness : (UINT8_MAX - brightness);
114112
ret = ledc_set_duty(LEDC_MODE, s_ledc->ledc_channel[ch].channel, brightness * s_ledc->max_duty / UINT8_MAX);
115113
LED_LEDC_CHECK(ESP_OK == ret, "LEDC set duty error", return ret);
116114
ret = ledc_update_duty(LEDC_MODE, s_ledc->ledc_channel[ch].channel);

components/led/led_indicator/src/led_indicator_rgb.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
#include <math.h>
8+
#include "driver/gpio.h"
89
#include "driver/ledc.h"
910
#include "esp_log.h"
1011
#include "led_indicator_rgb.h"
@@ -42,12 +43,15 @@ static esp_err_t led_indicator_rgb_init(void *param, void **ret_rgb)
4243
}
4344

4445
ledc_channel_config_t red_ch_cfg = LEDC_CHANNEL_CONFIG(cfg->timer_num, cfg->red_channel, cfg->red_gpio_num);
46+
red_ch_cfg.flags.output_invert = cfg->is_active_level_high ? false : true;
4547
ret = ledc_channel_config(&red_ch_cfg);
4648
LED_RGB_CHECK(ESP_OK == ret, "red ledc_channel_config fail!", goto EXIT);
4749
ledc_channel_config_t green_ch_cfg = LEDC_CHANNEL_CONFIG(cfg->timer_num, cfg->green_channel, cfg->green_gpio_num);
50+
green_ch_cfg.flags.output_invert = cfg->is_active_level_high ? false : true;
4851
ret = ledc_channel_config(&green_ch_cfg);
4952
LED_RGB_CHECK(ESP_OK == ret, "green ledc_channel_config fail!", goto EXIT);
5053
ledc_channel_config_t blue_ch_cfg = LEDC_CHANNEL_CONFIG(cfg->timer_num, cfg->blue_channel, cfg->blue_gpio_num);
54+
blue_ch_cfg.flags.output_invert = cfg->is_active_level_high ? false : true;
5155
ret = ledc_channel_config(&blue_ch_cfg);
5256
LED_RGB_CHECK(ESP_OK == ret, "blud ledc_channel_config fail!", goto EXIT);
5357
rgb->rgb_channel[0] = cfg->red_channel;
@@ -73,11 +77,6 @@ static esp_err_t led_indicator_rgb_deinit(void *rgb_handle)
7377
static esp_err_t led_indicator_rgb_set_duty(led_rgb_t *p_rgb, uint32_t rgb[])
7478
{
7579
esp_err_t ret;
76-
if (!p_rgb->is_active_level_high) {
77-
rgb[0] = p_rgb->max_duty - rgb[0];
78-
rgb[1] = p_rgb->max_duty - rgb[1];
79-
rgb[2] = p_rgb->max_duty - rgb[2];
80-
}
8180
for (int i = 0; i < 3; i++) {
8281
ret = ledc_set_duty(LEDC_MODE, p_rgb->rgb_channel[i], rgb[i]);
8382
LED_RGB_CHECK(ESP_OK == ret, "LEDC set duty error", return ret);

0 commit comments

Comments
 (0)