Skip to content

Commit de7cac1

Browse files
authoredNov 9, 2023
Neopixel fix (#8845)
* improve neopixel definition for all boards * Improve NeoPixel definition for all boards * neopixelWrite uses Peirpheral Manager now The function can now be used for many GPIOs in the same sketch by using Peripheral Manager attach/detach, not being necesary to worry about initialization. * improve error message * check if pin is RGB_BUILTIN * fixes boards that don't RGB_BUILTIN
1 parent 2ac1c06 commit de7cac1

File tree

3 files changed

+18
-32
lines changed

3 files changed

+18
-32
lines changed
 

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

+10-17
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,27 @@
33

44
void neopixelWrite(uint8_t pin, uint8_t red_val, uint8_t green_val, uint8_t blue_val){
55
rmt_data_t led_data[24];
6-
static bool initialized = false;
76

8-
uint8_t _pin = pin;
7+
// Verify if the pin used is RGB_BUILTIN and fix GPIO number
98
#ifdef RGB_BUILTIN
10-
if(pin == RGB_BUILTIN){
11-
_pin = RGB_BUILTIN - SOC_GPIO_PIN_COUNT;
12-
}
9+
pin = pin == RGB_BUILTIN ? pin - SOC_GPIO_PIN_COUNT : pin;
1310
#endif
14-
15-
if(!initialized){
16-
if (!rmtInit(_pin, RMT_TX_MODE, RMT_MEM_NUM_BLOCKS_1, 10000000)){
17-
log_e("RGB LED driver initialization failed!");
18-
return;
19-
}
20-
initialized = true;
11+
if (!rmtInit(pin, RMT_TX_MODE, RMT_MEM_NUM_BLOCKS_1, 10000000)) {
12+
log_e("RGB LED driver initialization failed for GPIO%d!", pin);
13+
return;
2114
}
2215

2316
int color[] = {green_val, red_val, blue_val}; // Color coding is in order GREEN, RED, BLUE
2417
int i = 0;
25-
for(int col=0; col<3; col++ ){
26-
for(int bit=0; bit<8; bit++){
27-
if((color[col] & (1<<(7-bit)))){
18+
for (int col = 0; col < 3; col++ ) {
19+
for (int bit = 0; bit < 8; bit++) {
20+
if ((color[col] & (1 << (7 - bit)))) {
2821
// HIGH bit
2922
led_data[i].level0 = 1; // T1H
3023
led_data[i].duration0 = 8; // 0.8us
3124
led_data[i].level1 = 0; // T1L
3225
led_data[i].duration1 = 4; // 0.4us
33-
}else{
26+
} else {
3427
// LOW bit
3528
led_data[i].level0 = 1; // T0H
3629
led_data[i].duration0 = 4; // 0.4us
@@ -40,5 +33,5 @@ void neopixelWrite(uint8_t pin, uint8_t red_val, uint8_t green_val, uint8_t blue
4033
i++;
4134
}
4235
}
43-
rmtWrite(_pin, led_data, RMT_SYMBOLS_OF(led_data), RMT_WAIT_FOR_EVER);
36+
rmtWrite(pin, led_data, RMT_SYMBOLS_OF(led_data), RMT_WAIT_FOR_EVER);
4437
}

‎libraries/ESP32/examples/RMT/RMTWriteNeoPixel/RMTWriteNeoPixel.ino

+4-8
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,11 @@
1919
* Parameters can be changed by the user. In a single LED circuit, it will just blink.
2020
*/
2121

22-
// The effect seen in ESP32C3, ESP32S2 and ESP32S3 is like a Blink of RGB LED
23-
#if CONFIG_IDF_TARGET_ESP32S2
24-
#define BUILTIN_RGBLED_PIN 18
25-
#elif CONFIG_IDF_TARGET_ESP32S3
26-
#define BUILTIN_RGBLED_PIN 48 // 48 or 38
27-
#elif CONFIG_IDF_TARGET_ESP32C3
28-
#define BUILTIN_RGBLED_PIN 8
22+
// The effect seen in (Espressif devkits) ESP32C6, ESP32H2, ESP32C3, ESP32S2 and ESP32S3 is like a Blink of RGB LED
23+
#ifdef PIN_NEOPIXEL
24+
#define BUILTIN_RGBLED_PIN PIN_NEOPIXEL
2925
#else
30-
#define BUILTIN_RGBLED_PIN 21 // ESP32 has no builtin RGB LED
26+
#define BUILTIN_RGBLED_PIN 21 // ESP32 has no builtin RGB LED (PIN_NEOPIXEL)
3127
#endif
3228

3329
#define NR_OF_LEDS 8*4

‎libraries/ESP32/examples/RMT/RMT_CPUFreq_Test/RMT_CPUFreq_Test.ino

+4-7
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,11 @@
2626

2727

2828
// Default DevKit RGB LED GPIOs:
29-
#if CONFIG_IDF_TARGET_ESP32S2
30-
#define MY_LED_GPIO 18
31-
#elif CONFIG_IDF_TARGET_ESP32S3
32-
#define MY_LED_GPIO 48 // 48 or 38
33-
#elif CONFIG_IDF_TARGET_ESP32C3
34-
#define MY_LED_GPIO 8
29+
// The effect seen in (Espressif devkits) ESP32C6, ESP32H2, ESP32C3, ESP32S2 and ESP32S3 is like a Blink of RGB LED
30+
#ifdef PIN_NEOPIXEL
31+
#define MY_LED_GPIO PIN_NEOPIXEL
3532
#else
36-
#define MY_LED_GPIO 21 // Any, ESP32 has no RGB LED - depends on circuit setup
33+
#define MY_LED_GPIO 21 // ESP32 has no builtin RGB LED (PIN_NEOPIXEL)
3734
#endif
3835

3936
// Set the correct GPIO to any necessary by changing RGB_LED_GPIO value

0 commit comments

Comments
 (0)
Please sign in to comment.