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
This commit is contained in:
Rodrigo Garcia 2023-11-09 18:15:10 -03:00 committed by GitHub
parent 2ac1c06809
commit de7cac11ae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 32 deletions

View file

@ -3,34 +3,27 @@
void neopixelWrite(uint8_t pin, uint8_t red_val, uint8_t green_val, uint8_t blue_val){
rmt_data_t led_data[24];
static bool initialized = false;
uint8_t _pin = pin;
// Verify if the pin used is RGB_BUILTIN and fix GPIO number
#ifdef RGB_BUILTIN
if(pin == RGB_BUILTIN){
_pin = RGB_BUILTIN - SOC_GPIO_PIN_COUNT;
}
pin = pin == RGB_BUILTIN ? pin - SOC_GPIO_PIN_COUNT : pin;
#endif
if(!initialized){
if (!rmtInit(_pin, RMT_TX_MODE, RMT_MEM_NUM_BLOCKS_1, 10000000)){
log_e("RGB LED driver initialization failed!");
return;
}
initialized = true;
if (!rmtInit(pin, RMT_TX_MODE, RMT_MEM_NUM_BLOCKS_1, 10000000)) {
log_e("RGB LED driver initialization failed for GPIO%d!", pin);
return;
}
int color[] = {green_val, red_val, blue_val}; // Color coding is in order GREEN, RED, BLUE
int i = 0;
for(int col=0; col<3; col++ ){
for(int bit=0; bit<8; bit++){
if((color[col] & (1<<(7-bit)))){
for (int col = 0; col < 3; col++ ) {
for (int bit = 0; bit < 8; bit++) {
if ((color[col] & (1 << (7 - bit)))) {
// HIGH bit
led_data[i].level0 = 1; // T1H
led_data[i].duration0 = 8; // 0.8us
led_data[i].level1 = 0; // T1L
led_data[i].duration1 = 4; // 0.4us
}else{
} else {
// LOW bit
led_data[i].level0 = 1; // T0H
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
i++;
}
}
rmtWrite(_pin, led_data, RMT_SYMBOLS_OF(led_data), RMT_WAIT_FOR_EVER);
rmtWrite(pin, led_data, RMT_SYMBOLS_OF(led_data), RMT_WAIT_FOR_EVER);
}

View file

@ -19,15 +19,11 @@
* Parameters can be changed by the user. In a single LED circuit, it will just blink.
*/
// The effect seen in ESP32C3, ESP32S2 and ESP32S3 is like a Blink of RGB LED
#if CONFIG_IDF_TARGET_ESP32S2
#define BUILTIN_RGBLED_PIN 18
#elif CONFIG_IDF_TARGET_ESP32S3
#define BUILTIN_RGBLED_PIN 48 // 48 or 38
#elif CONFIG_IDF_TARGET_ESP32C3
#define BUILTIN_RGBLED_PIN 8
// The effect seen in (Espressif devkits) ESP32C6, ESP32H2, ESP32C3, ESP32S2 and ESP32S3 is like a Blink of RGB LED
#ifdef PIN_NEOPIXEL
#define BUILTIN_RGBLED_PIN PIN_NEOPIXEL
#else
#define BUILTIN_RGBLED_PIN 21 // ESP32 has no builtin RGB LED
#define BUILTIN_RGBLED_PIN 21 // ESP32 has no builtin RGB LED (PIN_NEOPIXEL)
#endif
#define NR_OF_LEDS 8*4

View file

@ -26,14 +26,11 @@
// Default DevKit RGB LED GPIOs:
#if CONFIG_IDF_TARGET_ESP32S2
#define MY_LED_GPIO 18
#elif CONFIG_IDF_TARGET_ESP32S3
#define MY_LED_GPIO 48 // 48 or 38
#elif CONFIG_IDF_TARGET_ESP32C3
#define MY_LED_GPIO 8
// The effect seen in (Espressif devkits) ESP32C6, ESP32H2, ESP32C3, ESP32S2 and ESP32S3 is like a Blink of RGB LED
#ifdef PIN_NEOPIXEL
#define MY_LED_GPIO PIN_NEOPIXEL
#else
#define MY_LED_GPIO 21 // Any, ESP32 has no RGB LED - depends on circuit setup
#define MY_LED_GPIO 21 // ESP32 has no builtin RGB LED (PIN_NEOPIXEL)
#endif
// Set the correct GPIO to any necessary by changing RGB_LED_GPIO value