ESP32 Addressable LED Library

lbruder
Posts: 14
Joined: Sat Nov 12, 2016 1:51 am

ESP32 Addressable LED Library

Postby lbruder » Sun Nov 27, 2016 6:02 pm

Just finished up work on a library for addressable LED strips located here: https://github.com/Lucas-Bruder/ESP32_LED_STRIP

Thanks for various members of the forum for help! It's still in its early stages so there may be some small bugs. Expect more example code coming in the next few days to highlight the full functionality of the library.

The library uses the RMT peripheral to generate waveforms needed to drive the LEDs. The library only supports single-wire LEDs like WS2812/B variant.

The logic has been added to support other single-wire LED chips like the WS2811 and SK6812, but I don't have any of those to test on so the functions to generate those waveforms hasn't been added yet.

Any feedback is much appreciated!

Lucas

bud.de
Posts: 2
Joined: Wed Nov 30, 2016 12:09 pm

Re: ESP32 Addressable LED Library

Postby bud.de » Wed Nov 30, 2016 1:57 pm

I just came to say thanks for the library. It looks nice and for me it was the best project to startup the ESP32.
Here just some edits to your example to have a nice rainbow effect. (GPIO changed to 14 for convenience). I hope the SK6812 I ordered arrive soon, so I can test them.

Code: Select all

/*  ----------------------------------------------------------------------------
    File: main.c
    Author(s):  Lucas Bruder <LBruder@me.com>
    Date Created: 11/23/2016
    Last modified: 11/26/2016
    ------------------------------------------------------------------------- */

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

#include "esp_system.h"
#include "nvs_flash.h"
#include "driver/gpio.h"
#include "led_strip/led_strip.h"
#include "esp_log.h"

#include <stdio.h>

#define LED_STRIP_LENGTH 4U
static struct led_color_t led_strip_buf_1[LED_STRIP_LENGTH];
static struct led_color_t led_strip_buf_2[LED_STRIP_LENGTH];

static const char *TAG = "rgb_led";

#define LED_STRIP_RMT_INTR_NUM 19

const uint8_t lights[360]={
  0,   0,   0,   0,   0,   1,   1,   2, 
  2,   3,   4,   5,   6,   7,   8,   9, 
 11,  12,  13,  15,  17,  18,  20,  22, 
 24,  26,  28,  30,  32,  35,  37,  39, 
 42,  44,  47,  49,  52,  55,  58,  60, 
 63,  66,  69,  72,  75,  78,  81,  85, 
 88,  91,  94,  97, 101, 104, 107, 111, 
114, 117, 121, 124, 127, 131, 134, 137, 
141, 144, 147, 150, 154, 157, 160, 163, 
167, 170, 173, 176, 179, 182, 185, 188, 
191, 194, 197, 200, 202, 205, 208, 210, 
213, 215, 217, 220, 222, 224, 226, 229, 
231, 232, 234, 236, 238, 239, 241, 242, 
244, 245, 246, 248, 249, 250, 251, 251, 
252, 253, 253, 254, 254, 255, 255, 255, 
255, 255, 255, 255, 254, 254, 253, 253, 
252, 251, 251, 250, 249, 248, 246, 245, 
244, 242, 241, 239, 238, 236, 234, 232, 
231, 229, 226, 224, 222, 220, 217, 215, 
213, 210, 208, 205, 202, 200, 197, 194, 
191, 188, 185, 182, 179, 176, 173, 170, 
167, 163, 160, 157, 154, 150, 147, 144, 
141, 137, 134, 131, 127, 124, 121, 117, 
114, 111, 107, 104, 101,  97,  94,  91, 
 88,  85,  81,  78,  75,  72,  69,  66, 
 63,  60,  58,  55,  52,  49,  47,  44, 
 42,  39,  37,  35,  32,  30,  28,  26, 
 24,  22,  20,  18,  17,  15,  13,  12, 
 11,   9,   8,   7,   6,   5,   4,   3, 
  2,   2,   1,   1,   0,   0,   0,   0, 
  0,   0,   0,   0,   0,   0,   0,   0, 
  0,   0,   0,   0,   0,   0,   0,   0, 
  0,   0,   0,   0,   0,   0,   0,   0, 
  0,   0,   0,   0,   0,   0,   0,   0, 
  0,   0,   0,   0,   0,   0,   0,   0, 
  0,   0,   0,   0,   0,   0,   0,   0, 
  0,   0,   0,   0,   0,   0,   0,   0, 
  0,   0,   0,   0,   0,   0,   0,   0, 
  0,   0,   0,   0,   0,   0,   0,   0, 
  0,   0,   0,   0,   0,   0,   0,   0, 
  0,   0,   0,   0,   0,   0,   0,   0, 
  0,   0,   0,   0,   0,   0,   0,   0, 
  0,   0,   0,   0,   0,   0,   0,   0, 
  0,   0,   0,   0,   0,   0,   0,   0, 
  0,   0,   0,   0,   0,   0,   0,   0};

int app_main(void)
{
    nvs_flash_init();

    int rgb_red=0;
    int rgb_green=120;
    int rgb_blue=240;

    struct led_strip_t led_strip = {
        .rgb_led_type = RGB_LED_TYPE_WS2812,
        .rmt_channel = RMT_CHANNEL_1,
        .rmt_interrupt_num = LED_STRIP_RMT_INTR_NUM,
        .gpio = GPIO_NUM_14,
        .led_strip_buf_1 = led_strip_buf_1,
        .led_strip_buf_2 = led_strip_buf_2,
        .led_strip_length = LED_STRIP_LENGTH
    };
    led_strip.access_semaphore = xSemaphoreCreateBinary();

    bool led_init_ok = led_strip_init(&led_strip);
    assert(led_init_ok);

    struct led_color_t led_color = {
        .red = 5,
        .green = 0,
        .blue = 0,
    };

    while (true) {
        for (uint32_t index = 0; index < LED_STRIP_LENGTH; index++) {
            led_strip_set_pixel_color(&led_strip, index, &led_color);
        }
        led_strip_show(&led_strip);

        led_color.red   = lights[rgb_red];
        led_color.green = lights[rgb_green];
        led_color.blue  = lights[rgb_blue];

        rgb_red += 1;
        rgb_green += 1;
        rgb_blue += 1;

        if (rgb_red >= 360) rgb_red=0;
        if (rgb_green >= 360) rgb_green=0;
        if (rgb_blue >= 360) rgb_blue=0;
    
        vTaskDelay(60 / portTICK_PERIOD_MS);
    }
    return 0;
}


Lookup table source:
http://www.instructables.com/id/How-to- ... ors-With-/

lbruder
Posts: 14
Joined: Sat Nov 12, 2016 1:51 am

Re: ESP32 Addressable LED Library

Postby lbruder » Tue Dec 06, 2016 5:25 pm

Let me know if you want help getting those SK6812 up and running!

rinripper
Posts: 8
Joined: Wed Jun 28, 2017 3:52 am

Re: ESP32 Addressable LED Library

Postby rinripper » Mon Jul 24, 2017 4:43 pm

Hi lbruder, i´m doing a doing a project with the sk6812rgbw led strip. I´m interest in use your library but I need the white channel. I hope you can help me with that if you can. Thanks in advance.

hybryd
Posts: 2
Joined: Mon Aug 07, 2017 7:41 pm

Re: ESP32 Addressable LED Library

Postby hybryd » Mon Aug 07, 2017 7:45 pm

rinripper wrote:Hi lbruder, i´m doing a doing a project with the sk6812rgbw led strip. I´m interest in use your library but I need the white channel. I hope you can help me with that if you can. Thanks in advance.
Hi rinripper,
You should be able to make the following changes to lbruder's library and be able to use the white channel. I have this working with a strip of rgbw leds and all is good.

Code: Select all

diff --git a/components/led_strip/inc/led_strip/led_strip.h b/components/led_strip/inc/led_strip/led_strip.h
index 3728cbc..e7f5dac 100644
--- a/components/led_strip/inc/led_strip/led_strip.h
+++ b/components/led_strip/inc/led_strip/led_strip.h
@@ -36,6 +36,7 @@ struct led_color_t {
     uint8_t red;
     uint8_t green;
     uint8_t blue;
+    uint8_t white;
 };
 
 struct led_strip_t {
diff --git a/components/led_strip/led_strip.c b/components/led_strip/led_strip.c
index 7273aa8..39ebae1 100644
--- a/components/led_strip/led_strip.c
+++ b/components/led_strip/led_strip.c
@@ -25,7 +25,7 @@
 
 #define LED_STRIP_REFRESH_PERIOD_MS     (30U) // TODO: add as parameter to led_strip_init
 
-#define LED_STRIP_NUM_RMT_ITEMS_PER_LED (24U) // Assumes 24 bit color for each led
+#define LED_STRIP_NUM_RMT_ITEMS_PER_LED (32U) // Assumes 24 bit color for each led
 
 // RMT Clock source is @ 80 MHz. Dividing it by 8 gives us 10 MHz frequency, or 100ns period.
 #define LED_STRIP_RMT_CLK_DIV (8)
@@ -108,6 +108,15 @@ static void led_strip_fill_rmt_items_sk6812(struct led_color_t *led_strip_buf, r
             }
             rmt_items_index++;
         }
+        for (uint8_t bit = 8; bit != 0; bit--) {
+            uint8_t bit_set = (led_color.white >> (bit - 1)) & 1;
+            if(bit_set) {
+                led_strip_rmt_bit_1_sk6812(&(rmt_items[rmt_items_index]));
+            } else {
+                led_strip_rmt_bit_0_sk6812(&(rmt_items[rmt_items_index]));
+            }
+            rmt_items_index++;
+        }
     }
 }
 
 

Who is online

Users browsing this forum: No registered users and 44 guests