diff --git a/cores/esp32/esp32-hal-rgb-led.c b/cores/esp32/esp32-hal-rgb-led.c index 99c95e1943e..5afec10f89b 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 33f37c849b6..84e5498abd7 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 346c7628b23..361d3238960 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;