Skip to content

Commit 73eb7bb

Browse files
Alicipykartben
authored andcommitted
drivers: led_strip: ws2812_gpio: fix overwriting in loop
Due to the alias of ptr on pixels, one iterates over the same area one reads from to set values. Therefore, if the strip is not 'RGB' but GBR, one would overwrite the pixels 'r' value with the 'g' value in the first loop, leading to a wrong read afterwards. By writing to a temporary variable, we have a clean, unmodified copy of the original values. Additionally, this removes the dependency on 'LED_STRIP_RGB_SCRATCH', as the scratch part (that masked most error cases) is not necessary anymore. Signed-off-by: Stefan Kraus <[email protected]>
1 parent 22eb051 commit 73eb7bb

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

drivers/led_strip/Kconfig.ws2812

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ config WS2812_STRIP_GPIO
4646
default y
4747
depends on DT_HAS_WORLDSEMI_WS2812_GPIO_ENABLED
4848
depends on (SOC_SERIES_NRF91X || SOC_SERIES_NRF51X || SOC_SERIES_NRF52X || SOC_SERIES_NRF53X)
49-
select LED_STRIP_RGB_SCRATCH
5049
help
5150
Enable driver for WS2812 (and compatibles) LED strip directly
5251
controlling with GPIO. The GPIO driver does bit-banging with inline

drivers/led_strip/ws2812_gpio.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ static int ws2812_gpio_update_rgb(const struct device *dev,
140140
/* Convert from RGB to on-wire format (e.g. GRB, GRBW, RGB, etc) */
141141
for (i = 0; i < num_pixels; i++) {
142142
uint8_t j;
143+
const struct led_rgb current_pixel = pixels[i];
143144

144145
for (j = 0; j < config->num_colors; j++) {
145146
switch (config->color_mapping[j]) {
@@ -148,13 +149,13 @@ static int ws2812_gpio_update_rgb(const struct device *dev,
148149
*ptr++ = 0;
149150
break;
150151
case LED_COLOR_ID_RED:
151-
*ptr++ = pixels[i].r;
152+
*ptr++ = current_pixel.r;
152153
break;
153154
case LED_COLOR_ID_GREEN:
154-
*ptr++ = pixels[i].g;
155+
*ptr++ = current_pixel.g;
155156
break;
156157
case LED_COLOR_ID_BLUE:
157-
*ptr++ = pixels[i].b;
158+
*ptr++ = current_pixel.b;
158159
break;
159160
default:
160161
return -EINVAL;

0 commit comments

Comments
 (0)