Skip to content

Commit 66c9c0b

Browse files
sblantipodiSuGliderpre-commit-ci-lite[bot]me-no-dev
authored
Add ability for boards to provide a custom pixel order in neopixelWrite() (#10128)
* fix(esp32): Fixed the hint for the builtin neopixleWrite() function * change(esp32): Added neopixelWriteOrdered() function * change(esp32): Added neopixelWriteOrdered() function * change(esp32): Added neopixelWriteOrdered() function * change(esp32): Added the possibility to specify LED color order * change(esp32): Added the possibility to specify LED color order * feat(rgbled): add license information * feat(rgbled): add color order enum * feat(rgbled): add color order feature * feat(rgbled): change color order for lolin_s3_mini * fix(rgbled): suffix * fix(rgbled): suffix * ci(pre-commit): Apply automatic fixes * fix(rgbled): it lacks GRB case Made GRB default + switch/case exceptions. * fix(rgbled): add guard for rgb_led_color_order_t If RGB_BUILTIN_LED_COLOR_ORDER is not defined, the type rgb_led_color_order_t won't be declared. * fix(rgb-led): Implement rgbLedWriteOrdered() * ci(pre-commit): Apply automatic fixes * Remove const to allow changing the order --------- Co-authored-by: Rodrigo Garcia <[email protected]> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Co-authored-by: me-no-dev <[email protected]>
1 parent def319a commit 66c9c0b

File tree

3 files changed

+75
-3
lines changed

3 files changed

+75
-3
lines changed

cores/esp32/esp32-hal-rgb-led.c

+52-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
1+
// Copyright 2024 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
115
#include "soc/soc_caps.h"
216

317
#include "esp32-hal-rgb-led.h"
418

5-
void neopixelWrite(uint8_t pin, uint8_t red_val, uint8_t green_val, uint8_t blue_val) {
19+
void rgbLedWrite(uint8_t pin, uint8_t red_val, uint8_t green_val, uint8_t blue_val) {
20+
rgbLedWriteOrdered(pin, RGB_BUILTIN_LED_COLOR_ORDER, red_val, green_val, blue_val);
21+
}
22+
23+
void rgbLedWriteOrdered(uint8_t pin, rgb_led_color_order_t order, uint8_t red_val, uint8_t green_val, uint8_t blue_val) {
624
#if SOC_RMT_SUPPORTED
725
rmt_data_t led_data[24];
826

@@ -15,7 +33,39 @@ void neopixelWrite(uint8_t pin, uint8_t red_val, uint8_t green_val, uint8_t blue
1533
return;
1634
}
1735

18-
int color[] = {green_val, red_val, blue_val}; // Color coding is in order GREEN, RED, BLUE
36+
// default WS2812B color order is G, R, B
37+
int color[3] = {green_val, red_val, blue_val};
38+
39+
switch (order) {
40+
case LED_COLOR_ORDER_RGB:
41+
color[0] = red_val;
42+
color[1] = green_val;
43+
color[2] = blue_val;
44+
break;
45+
case LED_COLOR_ORDER_BGR:
46+
color[0] = blue_val;
47+
color[1] = green_val;
48+
color[2] = red_val;
49+
break;
50+
case LED_COLOR_ORDER_BRG:
51+
color[0] = blue_val;
52+
color[1] = red_val;
53+
color[2] = green_val;
54+
break;
55+
case LED_COLOR_ORDER_RBG:
56+
color[0] = red_val;
57+
color[1] = blue_val;
58+
color[2] = green_val;
59+
break;
60+
case LED_COLOR_ORDER_GBR:
61+
color[0] = green_val;
62+
color[1] = blue_val;
63+
color[2] = red_val;
64+
break;
65+
default: // GRB
66+
break;
67+
}
68+
1969
int i = 0;
2070
for (int col = 0; col < 3; col++) {
2171
for (int bit = 0; bit < 8; bit++) {

cores/esp32/esp32-hal-rgb-led.h

+20-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,26 @@ extern "C" {
1111
#define RGB_BRIGHTNESS 64
1212
#endif
1313

14-
void neopixelWrite(uint8_t pin, uint8_t red_val, uint8_t green_val, uint8_t blue_val);
14+
#ifndef RGB_BUILTIN_LED_COLOR_ORDER
15+
#define RGB_BUILTIN_LED_COLOR_ORDER LED_COLOR_ORDER_GRB // default WS2812B color order
16+
#endif
17+
18+
typedef enum {
19+
LED_COLOR_ORDER_RGB,
20+
LED_COLOR_ORDER_BGR,
21+
LED_COLOR_ORDER_BRG,
22+
LED_COLOR_ORDER_RBG,
23+
LED_COLOR_ORDER_GBR,
24+
LED_COLOR_ORDER_GRB
25+
} rgb_led_color_order_t;
26+
27+
void rgbLedWriteOrdered(uint8_t pin, rgb_led_color_order_t order, uint8_t red_val, uint8_t green_val, uint8_t blue_val);
28+
29+
// Will use RGB_BUILTIN_LED_COLOR_ORDER
30+
void rgbLedWrite(uint8_t pin, uint8_t red_val, uint8_t green_val, uint8_t blue_val);
31+
32+
// Backward compatibility
33+
#define neopixelWrite(p, r, g, b) rgbLedWrite(p, r, g, b)
1534

1635
#ifdef __cplusplus
1736
}

variants/lolin_s3_mini/pins_arduino.h

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ static const uint8_t LED_BUILTIN = 47 + SOC_GPIO_PIN_COUNT;
1212
#define LED_BUILTIN LED_BUILTIN // allow testing #ifdef LED_BUILTIN
1313
#define RGB_BUILTIN LED_BUILTIN
1414
#define RGB_BRIGHTNESS 64
15+
// This board has a builtin RGB LED that works with a different signal color order
16+
// Other order options can be found in https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/esp32-hal-rgb-led.h
17+
#define RGB_BUILTIN_LED_COLOR_ORDER LED_COLOR_ORDER_RGB
1518

1619
static const uint8_t TX = 43;
1720
static const uint8_t RX = 44;

0 commit comments

Comments
 (0)