Test program for my Multi_WS2811 library that started out as a fork of heroic/WS2811. My library uses hardware DMA on the FRDM-KL25Z to drive up to 16 strings of WS2811 or WS2812 LEDs in parallel.
Dependencies: Multi_WS2811 mbed MMA8451Q
Fork of WS2811 by
NOTE: I have accidentally pushed changes for another fork of this program that I used in the recent Georgetown Carnival Power Tool Races. When I get some time, I will restore the test program to its original glory.
You can see my power tool racer (Nevermore's Revenge) here

This tests my FRDM-KL25Z multi-string WS2811/WS2812 library. It uses the accelerometer to change the rainbow phase on two strings of LEDs as well as the touch sense to change brightness.
A video of this program in operation is here.
Here is the library that I developed to run the LEDs:
Import libraryMulti_WS2811
Library allowing up to 16 strings of 60 WS2811 or WS2812 LEDs to be driven from a single FRDM-KL25Z board. Uses hardware DMA to do a full 800 KHz rate without much CPU burden.
LedStrip.cpp@17:b4e9d8f4baa9, 2013-12-05 (annotated)
- Committer:
- bikeNomad
- Date:
- Thu Dec 05 15:25:16 2013 +0000
- Revision:
- 17:b4e9d8f4baa9
renamed ws2812.* to ws2811.*
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| bikeNomad | 17:b4e9d8f4baa9 | 1 | #include "LedStrip.h" |
| bikeNomad | 17:b4e9d8f4baa9 | 2 | |
| bikeNomad | 17:b4e9d8f4baa9 | 3 | LedStrip::LedStrip(int n) |
| bikeNomad | 17:b4e9d8f4baa9 | 4 | { |
| bikeNomad | 17:b4e9d8f4baa9 | 5 | // Allocate 3 bytes per pixel: |
| bikeNomad | 17:b4e9d8f4baa9 | 6 | numLEDs = n; |
| bikeNomad | 17:b4e9d8f4baa9 | 7 | pixels = (uint8_t *)malloc(numPixelBytes()); |
| bikeNomad | 17:b4e9d8f4baa9 | 8 | if (pixels) { |
| bikeNomad | 17:b4e9d8f4baa9 | 9 | memset(pixels, 0x00, numPixelBytes()); // Init to RGB 'off' state |
| bikeNomad | 17:b4e9d8f4baa9 | 10 | } |
| bikeNomad | 17:b4e9d8f4baa9 | 11 | } |
| bikeNomad | 17:b4e9d8f4baa9 | 12 | |
| bikeNomad | 17:b4e9d8f4baa9 | 13 | LedStrip::~LedStrip() |
| bikeNomad | 17:b4e9d8f4baa9 | 14 | { |
| bikeNomad | 17:b4e9d8f4baa9 | 15 | free(pixels); |
| bikeNomad | 17:b4e9d8f4baa9 | 16 | } |
| bikeNomad | 17:b4e9d8f4baa9 | 17 | |
| bikeNomad | 17:b4e9d8f4baa9 | 18 | uint32_t LedStrip::total_luminance(void) |
| bikeNomad | 17:b4e9d8f4baa9 | 19 | { |
| bikeNomad | 17:b4e9d8f4baa9 | 20 | uint32_t running_total; |
| bikeNomad | 17:b4e9d8f4baa9 | 21 | running_total = 0; |
| bikeNomad | 17:b4e9d8f4baa9 | 22 | for (int i=0; i< numPixelBytes(); i++) |
| bikeNomad | 17:b4e9d8f4baa9 | 23 | running_total += pixels[i]; |
| bikeNomad | 17:b4e9d8f4baa9 | 24 | return running_total; |
| bikeNomad | 17:b4e9d8f4baa9 | 25 | } |
| bikeNomad | 17:b4e9d8f4baa9 | 26 | |
| bikeNomad | 17:b4e9d8f4baa9 | 27 | // Convert R,G,B to combined 32-bit color |
| bikeNomad | 17:b4e9d8f4baa9 | 28 | uint32_t LedStrip::Color(uint8_t r, uint8_t g, uint8_t b) |
| bikeNomad | 17:b4e9d8f4baa9 | 29 | { |
| bikeNomad | 17:b4e9d8f4baa9 | 30 | // Take the lowest 7 bits of each value and append them end to end |
| bikeNomad | 17:b4e9d8f4baa9 | 31 | // We have the top bit set high (its a 'parity-like' bit in the protocol |
| bikeNomad | 17:b4e9d8f4baa9 | 32 | // and must be set!) |
| bikeNomad | 17:b4e9d8f4baa9 | 33 | return ((uint32_t)g << 16) | ((uint32_t)r << 8) | (uint32_t)b; |
| bikeNomad | 17:b4e9d8f4baa9 | 34 | } |
| bikeNomad | 17:b4e9d8f4baa9 | 35 | |
| bikeNomad | 17:b4e9d8f4baa9 | 36 | // store the rgb component in our array |
| bikeNomad | 17:b4e9d8f4baa9 | 37 | void LedStrip::setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b) |
| bikeNomad | 17:b4e9d8f4baa9 | 38 | { |
| bikeNomad | 17:b4e9d8f4baa9 | 39 | if (n >= numLEDs) return; // '>=' because arrays are 0-indexed |
| bikeNomad | 17:b4e9d8f4baa9 | 40 | |
| bikeNomad | 17:b4e9d8f4baa9 | 41 | pixels[n*3 ] = g; |
| bikeNomad | 17:b4e9d8f4baa9 | 42 | pixels[n*3+1] = r; |
| bikeNomad | 17:b4e9d8f4baa9 | 43 | pixels[n*3+2] = b; |
| bikeNomad | 17:b4e9d8f4baa9 | 44 | } |
| bikeNomad | 17:b4e9d8f4baa9 | 45 | |
| bikeNomad | 17:b4e9d8f4baa9 | 46 | void LedStrip::setPixelR(uint16_t n, uint8_t r) |
| bikeNomad | 17:b4e9d8f4baa9 | 47 | { |
| bikeNomad | 17:b4e9d8f4baa9 | 48 | if (n >= numLEDs) return; // '>=' because arrays are 0-indexed |
| bikeNomad | 17:b4e9d8f4baa9 | 49 | |
| bikeNomad | 17:b4e9d8f4baa9 | 50 | pixels[n*3+1] = r; |
| bikeNomad | 17:b4e9d8f4baa9 | 51 | } |
| bikeNomad | 17:b4e9d8f4baa9 | 52 | |
| bikeNomad | 17:b4e9d8f4baa9 | 53 | void LedStrip::setPixelG(uint16_t n, uint8_t g) |
| bikeNomad | 17:b4e9d8f4baa9 | 54 | { |
| bikeNomad | 17:b4e9d8f4baa9 | 55 | if (n >= numLEDs) return; // '>=' because arrays are 0-indexed |
| bikeNomad | 17:b4e9d8f4baa9 | 56 | |
| bikeNomad | 17:b4e9d8f4baa9 | 57 | pixels[n*3] = g; |
| bikeNomad | 17:b4e9d8f4baa9 | 58 | } |
| bikeNomad | 17:b4e9d8f4baa9 | 59 | |
| bikeNomad | 17:b4e9d8f4baa9 | 60 | void LedStrip::setPixelB(uint16_t n, uint8_t b) |
| bikeNomad | 17:b4e9d8f4baa9 | 61 | { |
| bikeNomad | 17:b4e9d8f4baa9 | 62 | if (n >= numLEDs) return; // '>=' because arrays are 0-indexed |
| bikeNomad | 17:b4e9d8f4baa9 | 63 | |
| bikeNomad | 17:b4e9d8f4baa9 | 64 | pixels[n*3+2] = b; |
| bikeNomad | 17:b4e9d8f4baa9 | 65 | } |
| bikeNomad | 17:b4e9d8f4baa9 | 66 | |
| bikeNomad | 17:b4e9d8f4baa9 | 67 | void LedStrip::setPackedPixels(uint8_t * buffer, uint32_t n) |
| bikeNomad | 17:b4e9d8f4baa9 | 68 | { |
| bikeNomad | 17:b4e9d8f4baa9 | 69 | if (n >= numLEDs) return; |
| bikeNomad | 17:b4e9d8f4baa9 | 70 | memcpy(pixels, buffer, (size_t) (n*3)); |
| bikeNomad | 17:b4e9d8f4baa9 | 71 | } |
| bikeNomad | 17:b4e9d8f4baa9 | 72 | |
| bikeNomad | 17:b4e9d8f4baa9 | 73 | void LedStrip::setPixelColor(uint16_t n, uint32_t c) |
| bikeNomad | 17:b4e9d8f4baa9 | 74 | { |
| bikeNomad | 17:b4e9d8f4baa9 | 75 | if (n >= numLEDs) return; // '>=' because arrays are 0-indexed |
| bikeNomad | 17:b4e9d8f4baa9 | 76 | |
| bikeNomad | 17:b4e9d8f4baa9 | 77 | pixels[n*3 ] = (c >> 16); |
| bikeNomad | 17:b4e9d8f4baa9 | 78 | pixels[n*3+1] = (c >> 8); |
| bikeNomad | 17:b4e9d8f4baa9 | 79 | pixels[n*3+2] = c; |
| bikeNomad | 17:b4e9d8f4baa9 | 80 | } |
Ned Konz


Generic WS2811/WS2812