Robert Bui / Multi_WS2811

Fork of Multi_WS2811 by Richard Thompson

Embed: (wiki syntax)

« Back to documentation index

WS2811 Class Reference

WS2811/WS2812/WS2812B LED Strip controller
For FRDM-KL25Z and FRDM-KL46Z. More...

#include <WS2811.h>

Inherits LedStrip.

Public Member Functions

 WS2811 (uint16_t pixelCount, uint8_t pinNumber)
 Set up the LED strip.
virtual void begin ()
 Initialise the LED strip.
virtual void show ()
 Apply the new LED strip values.
virtual void blank ()
 Blank the LED strip.
uint16_t numPixels ()
 Number of RGB pixels.
uint16_t numPixelBytes ()
 Number of bytes used for pixel colour data.
uint32_t total_luminance ()
 Total brightness of all diodes
Use to check power budget.
void setPixelB (uint16_t pixNum, uint8_t blue)
 Set Blue level of pixel.
void setPixelG (uint16_t pixNum, uint8_t green)
 Set Green level of pixel.
void setPixelR (uint16_t pixNum, uint8_t red)
 Set Red level of pixel.
void setPixelColor (uint16_t pixNum, uint32_t color)
 Set color of pixel.
void setPixelColor (uint16_t pixNum, uint8_t red, uint8_t green, uint8_t blue)
 Set color of pixel.
void setPackedPixels (uint8_t *buffer, uint32_t count)
 Set color of all pixels.

Static Public Member Functions

static void startDMA ()
 Send a level update to all the WS2811 LED strips
All updates happen in parallel, ensure all (max 8) strips have complete data before calling this.
static uint32_t Color (uint8_t red, uint8_t green, uint8_t blue)
 Pack RGB Color data.

Detailed Description

WS2811/WS2812/WS2812B LED Strip controller
For FRDM-KL25Z and FRDM-KL46Z.

RAM usage:
Per individual LED: 3 bytes (malloc'd) for RGB data

Strip length per LED:
24 bytes (static) for bit data
+ 24 bytes (static) for ones data
= 48 bytes

240 LEDs max per string = 11,520 bytes static

One string:
240 LEDs : 11520 + 240*3 = 12,240 bytes

Eight strings:
240*8 LEDs: 11520 + (240*3) * 8 = 17,280 bytes

Example usage:

#include "mbed.h"
#include "WS2811.h"
#include "Colors.h"

static void showRainbow(WS2811 &strip, float startHue, float sat, float brite, float hueShift)
{
    unsigned nLEDs = strip.numPixels();
    float hue = startHue;
    for (unsigned i = 0; i < nLEDs; i++) {
        uint8_t r, g, b;
        Colors::HSBtoRGB(hue, sat, brite, &r, &g, &b);
        strip.setPixelColor(i, r, g, b);
        hue += hueShift;
        if (hue > 1.0) hue = 0.0;
    }
    strip.show();
}

static void showSolidColor(WS2811 &strip, uint8_t r, uint8_t g, uint8_t b)
{
    unsigned nLEDs = strip.numPixels();
    for (unsigned i = 0; i < nLEDs; i++) {
        strip.setPixelColor(i, r, g, b);
    }
    strip.show();
}

int main(void)
{
    WS2811 lightStrip1(nLEDs, 2);
    WS2811 lightStrip2(nLEDs, 3);
    WS2811 lightStrip3(nLEDs, 4);

    lightStrip1.begin();
    lightStrip2.begin();
    lightStrip3.begin();

    uint8_t r =0;
    uint8_t g =0;
    uint8_t b =0;

    bool fadeUp = true;

    float startHue = 0.0;
    for (;;) {
        startHue += 0.01;
        if (startHue > 1.0) startHue = 0.0;
        if (fadeUp) {
            if (r == 255) fadeUp = false;
            else {
                ++r;
                ++g;
                ++b;
            }
        } else {
            if (r == 0) fadeUp = true;
            else {
                --r;
                --g;
                --b;
            }
        }

        // Solid fading white
        showSolidColor(lightStrip1, r, g, b);
        // Maximum saturation rainbow
        showRainbow(lightStrip2, startHue, 1.0, 1.0, 1.0/nLEDs);
        // Pastel rainbow
        showRainbow(lightStrip3, startHue, 0.5, 1.0, 1.0/nLEDs);

        WS2811::startDMA();
        wait_ms(25);
        frames++;
    }
}

Definition at line 132 of file WS2811.h.


Constructor & Destructor Documentation

WS2811 ( uint16_t  pixelCount,
uint8_t  pinNumber 
)

Set up the LED strip.

Parameters:
pixelCountNumber of RGB LEDs on the strip. No more than MAX_LEDS_PER_STRIP (240)
pinNumberPin number on PORTD. 0-7.

Definition at line 215 of file WS2811.cpp.


Member Function Documentation

void begin (  ) [virtual]

Initialise the LED strip.

Implements LedStrip.

Definition at line 322 of file WS2811.cpp.

void blank (  ) [virtual]

Blank the LED strip.

Implements LedStrip.

Definition at line 328 of file WS2811.cpp.

uint32_t Color ( uint8_t  red,
uint8_t  green,
uint8_t  blue 
) [static, inherited]

Pack RGB Color data.

Parameters:
redAmount of Red
greenAmount of Green
blueAmount of Blue
Returns:
Packed RGB color data for one pixel

Definition at line 28 of file LedStrip.cpp.

uint16_t numPixelBytes (  ) [inherited]

Number of bytes used for pixel colour data.

Definition at line 48 of file LedStrip.h.

uint16_t numPixels (  ) [inherited]

Number of RGB pixels.

Definition at line 44 of file LedStrip.h.

void setPackedPixels ( uint8_t *  buffer,
uint32_t  count 
) [inherited]

Set color of all pixels.

Parameters:
*bufferPacked pixel data
countNumber of pixels

Definition at line 64 of file LedStrip.cpp.

void setPixelB ( uint16_t  pixNum,
uint8_t  blue 
) [inherited]

Set Blue level of pixel.

Parameters:
pixNumPixel Number
blueAmount of Blue

Definition at line 57 of file LedStrip.cpp.

void setPixelColor ( uint16_t  pixNum,
uint8_t  red,
uint8_t  green,
uint8_t  blue 
) [inherited]

Set color of pixel.

Parameters:
pixNumPixel Number
redAmount of Red
greenAmount of Green
blueAmount of Blue

Definition at line 34 of file LedStrip.cpp.

void setPixelColor ( uint16_t  pixNum,
uint32_t  color 
) [inherited]

Set color of pixel.

Parameters:
pixNumPixel Number
colorPacked RGB color data

Definition at line 70 of file LedStrip.cpp.

void setPixelG ( uint16_t  pixNum,
uint8_t  green 
) [inherited]

Set Green level of pixel.

Parameters:
pixNumPixel Number
greenAmount of Green

Definition at line 50 of file LedStrip.cpp.

void setPixelR ( uint16_t  pixNum,
uint8_t  red 
) [inherited]

Set Red level of pixel.

Parameters:
pixNumPixel Number
redAmount of Red

Definition at line 43 of file LedStrip.cpp.

void show (  ) [virtual]

Apply the new LED strip values.

Implements LedStrip.

Definition at line 340 of file WS2811.cpp.

void startDMA (  ) [static]

Send a level update to all the WS2811 LED strips
All updates happen in parallel, ensure all (max 8) strips have complete data before calling this.

Definition at line 224 of file WS2811.cpp.

uint32_t total_luminance (  ) [inherited]

Total brightness of all diodes
Use to check power budget.

Returns:
Sum total of all diodes (red + green + blue)

Definition at line 18 of file LedStrip.cpp.