From 66c9c0b1a6a36b85d27cdac0fb52098368de1a09 Mon Sep 17 00:00:00 2001 From: Davide Perini Date: Tue, 20 Aug 2024 00:29:13 +0200 Subject: [PATCH] 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 Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Co-authored-by: me-no-dev --- cores/esp32/esp32-hal-rgb-led.c | 54 ++++++++++++++++++++++++++- cores/esp32/esp32-hal-rgb-led.h | 21 ++++++++++- variants/lolin_s3_mini/pins_arduino.h | 3 ++ 3 files changed, 75 insertions(+), 3 deletions(-) diff --git a/cores/esp32/esp32-hal-rgb-led.c b/cores/esp32/esp32-hal-rgb-led.c index 99c95e194..5afec10f8 100644 --- a/cores/esp32/esp32-hal-rgb-led.c +++ b/cores/esp32/esp32-hal-rgb-led.c @@ -1,8 +1,26 @@ +// Copyright 2024 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + #include "soc/soc_caps.h" #include "esp32-hal-rgb-led.h" -void neopixelWrite(uint8_t pin, uint8_t red_val, uint8_t green_val, uint8_t blue_val) { +void rgbLedWrite(uint8_t pin, uint8_t red_val, uint8_t green_val, uint8_t blue_val) { + rgbLedWriteOrdered(pin, RGB_BUILTIN_LED_COLOR_ORDER, red_val, green_val, blue_val); +} + +void rgbLedWriteOrdered(uint8_t pin, rgb_led_color_order_t order, uint8_t red_val, uint8_t green_val, uint8_t blue_val) { #if SOC_RMT_SUPPORTED rmt_data_t led_data[24]; @@ -15,7 +33,39 @@ void neopixelWrite(uint8_t pin, uint8_t red_val, uint8_t green_val, uint8_t blue return; } - int color[] = {green_val, red_val, blue_val}; // Color coding is in order GREEN, RED, BLUE + // default WS2812B color order is G, R, B + int color[3] = {green_val, red_val, blue_val}; + + switch (order) { + case LED_COLOR_ORDER_RGB: + color[0] = red_val; + color[1] = green_val; + color[2] = blue_val; + break; + case LED_COLOR_ORDER_BGR: + color[0] = blue_val; + color[1] = green_val; + color[2] = red_val; + break; + case LED_COLOR_ORDER_BRG: + color[0] = blue_val; + color[1] = red_val; + color[2] = green_val; + break; + case LED_COLOR_ORDER_RBG: + color[0] = red_val; + color[1] = blue_val; + color[2] = green_val; + break; + case LED_COLOR_ORDER_GBR: + color[0] = green_val; + color[1] = blue_val; + color[2] = red_val; + break; + default: // GRB + break; + } + int i = 0; for (int col = 0; col < 3; col++) { for (int bit = 0; bit < 8; bit++) { diff --git a/cores/esp32/esp32-hal-rgb-led.h b/cores/esp32/esp32-hal-rgb-led.h index 33f37c849..84e5498ab 100644 --- a/cores/esp32/esp32-hal-rgb-led.h +++ b/cores/esp32/esp32-hal-rgb-led.h @@ -11,7 +11,26 @@ extern "C" { #define RGB_BRIGHTNESS 64 #endif -void neopixelWrite(uint8_t pin, uint8_t red_val, uint8_t green_val, uint8_t blue_val); +#ifndef RGB_BUILTIN_LED_COLOR_ORDER +#define RGB_BUILTIN_LED_COLOR_ORDER LED_COLOR_ORDER_GRB // default WS2812B color order +#endif + +typedef enum { + LED_COLOR_ORDER_RGB, + LED_COLOR_ORDER_BGR, + LED_COLOR_ORDER_BRG, + LED_COLOR_ORDER_RBG, + LED_COLOR_ORDER_GBR, + LED_COLOR_ORDER_GRB +} rgb_led_color_order_t; + +void rgbLedWriteOrdered(uint8_t pin, rgb_led_color_order_t order, uint8_t red_val, uint8_t green_val, uint8_t blue_val); + +// Will use RGB_BUILTIN_LED_COLOR_ORDER +void rgbLedWrite(uint8_t pin, uint8_t red_val, uint8_t green_val, uint8_t blue_val); + +// Backward compatibility +#define neopixelWrite(p, r, g, b) rgbLedWrite(p, r, g, b) #ifdef __cplusplus } diff --git a/variants/lolin_s3_mini/pins_arduino.h b/variants/lolin_s3_mini/pins_arduino.h index 346c7628b..361d32389 100644 --- a/variants/lolin_s3_mini/pins_arduino.h +++ b/variants/lolin_s3_mini/pins_arduino.h @@ -12,6 +12,9 @@ static const uint8_t LED_BUILTIN = 47 + SOC_GPIO_PIN_COUNT; #define LED_BUILTIN LED_BUILTIN // allow testing #ifdef LED_BUILTIN #define RGB_BUILTIN LED_BUILTIN #define RGB_BRIGHTNESS 64 +// This board has a builtin RGB LED that works with a different signal color order +// Other order options can be found in https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/esp32-hal-rgb-led.h +#define RGB_BUILTIN_LED_COLOR_ORDER LED_COLOR_ORDER_RGB static const uint8_t TX = 43; static const uint8_t RX = 44;