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.
LPD8806.cpp@4:0b75eb84a6d2, 2012-10-05 (annotated)
- Committer:
- heroic
- Date:
- Fri Oct 05 05:41:39 2012 +0000
- Revision:
- 4:0b75eb84a6d2
- Parent:
- 3:a415f73507c9
- Child:
- 5:70ded6500cd3
Change to use virtual base class
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| ehbmbed2 | 0:12e734116fea | 1 | // Mbed library to control LPD8806-based RGB LED Strips |
| ehbmbed2 | 0:12e734116fea | 2 | // (c) 2011 Jelmer Tiete |
| ehbmbed2 | 0:12e734116fea | 3 | // This library is ported from the Arduino implementation of Adafruit Industries |
| ehbmbed2 | 0:12e734116fea | 4 | // found at: http://github.com/adafruit/LPD8806 |
| ehbmbed2 | 0:12e734116fea | 5 | // and their strips: http://www.adafruit.com/products/306 |
| ehbmbed2 | 0:12e734116fea | 6 | // Released under the MIT License: http://mbed.org/license/mit |
| ehbmbed2 | 1:6ebd3ac910b6 | 7 | // |
| heroic | 2:af5af64e114d | 8 | // Parameterized and modified to use soft SPI. |
| heroic | 2:af5af64e114d | 9 | // Jas Strong <jasmine@electronpusher.org> |
| ehbmbed2 | 0:12e734116fea | 10 | /*****************************************************************************/ |
| ehbmbed2 | 0:12e734116fea | 11 | |
| heroic | 4:0b75eb84a6d2 | 12 | #include "LedStrip.h" |
| ehbmbed2 | 0:12e734116fea | 13 | #include "LPD8806.h" |
| ehbmbed2 | 0:12e734116fea | 14 | |
| heroic | 2:af5af64e114d | 15 | LPD8806::LPD8806(PinName dataPin, PinName clockPin, int n) : |
| heroic | 2:af5af64e114d | 16 | dat(dataPin), |
| heroic | 2:af5af64e114d | 17 | clk(clockPin) { |
| ehbmbed2 | 0:12e734116fea | 18 | // Allocate 3 bytes per pixel: |
| heroic | 2:af5af64e114d | 19 | numLEDs = n; |
| heroic | 3:a415f73507c9 | 20 | if ((pixels = (uint8_t *)malloc(numLEDs * 3))) { |
| ehbmbed2 | 0:12e734116fea | 21 | memset(pixels, 0x80, numLEDs * 3); // Init to RGB 'off' state |
| ehbmbed2 | 0:12e734116fea | 22 | } |
| ehbmbed2 | 0:12e734116fea | 23 | } |
| ehbmbed2 | 0:12e734116fea | 24 | |
| heroic | 2:af5af64e114d | 25 | /* |
| heroic | 2:af5af64e114d | 26 | * Soft SPI clock-out implementation (CPOL = 0, CPHA = 0). |
| heroic | 2:af5af64e114d | 27 | * Certainly not the fastest in the world but it'll do. |
| heroic | 2:af5af64e114d | 28 | * Gets about 3.6 MHz; could get several times as much |
| heroic | 2:af5af64e114d | 29 | * using the bitbands directly - jas. |
| heroic | 2:af5af64e114d | 30 | */ |
| heroic | 2:af5af64e114d | 31 | |
| heroic | 2:af5af64e114d | 32 | void LPD8806::write(uint8_t byte) { |
| heroic | 2:af5af64e114d | 33 | for (int i=0; i<8; i++) { |
| heroic | 2:af5af64e114d | 34 | clk = 0; |
| heroic | 2:af5af64e114d | 35 | dat = (byte & 0x80); |
| heroic | 2:af5af64e114d | 36 | clk = 1; |
| heroic | 2:af5af64e114d | 37 | byte <<= 1; |
| heroic | 2:af5af64e114d | 38 | } |
| heroic | 2:af5af64e114d | 39 | clk = 0; |
| heroic | 2:af5af64e114d | 40 | } |
| ehbmbed2 | 0:12e734116fea | 41 | |
| heroic | 2:af5af64e114d | 42 | void LPD8806::begin(void) { |
| ehbmbed2 | 0:12e734116fea | 43 | |
| ehbmbed2 | 0:12e734116fea | 44 | // Issue initial latch to 'wake up' strip (latch length varies w/numLEDs) |
| ehbmbed2 | 0:12e734116fea | 45 | writezeros(3 * ((numLEDs + 63) / 64)); |
| ehbmbed2 | 0:12e734116fea | 46 | } |
| ehbmbed2 | 0:12e734116fea | 47 | |
| ehbmbed2 | 0:12e734116fea | 48 | uint16_t LPD8806::numPixels(void) { |
| ehbmbed2 | 0:12e734116fea | 49 | return numLEDs; |
| ehbmbed2 | 0:12e734116fea | 50 | } |
| ehbmbed2 | 0:12e734116fea | 51 | |
| ehbmbed2 | 0:12e734116fea | 52 | void LPD8806::writezeros(uint16_t n) { |
| heroic | 2:af5af64e114d | 53 | while (n--) write(0x00); |
| heroic | 2:af5af64e114d | 54 | } |
| heroic | 2:af5af64e114d | 55 | |
| heroic | 2:af5af64e114d | 56 | void LPD8806::blank(void) { |
| heroic | 2:af5af64e114d | 57 | memset(pixels, 0x80, numLEDs * 3); |
| ehbmbed2 | 0:12e734116fea | 58 | } |
| ehbmbed2 | 0:12e734116fea | 59 | |
| ehbmbed2 | 0:12e734116fea | 60 | // This is how data is pushed to the strip. Unfortunately, the company |
| ehbmbed2 | 0:12e734116fea | 61 | // that makes the chip didnt release the protocol document or you need |
| ehbmbed2 | 0:12e734116fea | 62 | // to sign an NDA or something stupid like that, but we reverse engineered |
| ehbmbed2 | 0:12e734116fea | 63 | // this from a strip controller and it seems to work very nicely! |
| ehbmbed2 | 0:12e734116fea | 64 | void LPD8806::show(void) { |
| ehbmbed2 | 0:12e734116fea | 65 | uint16_t i, nl3 = numLEDs * 3; // 3 bytes per LED |
| ehbmbed2 | 0:12e734116fea | 66 | |
| ehbmbed2 | 0:12e734116fea | 67 | for (i=0; i<nl3; i++ ) { |
| heroic | 2:af5af64e114d | 68 | write(pixels[i]); |
| ehbmbed2 | 0:12e734116fea | 69 | } |
| ehbmbed2 | 0:12e734116fea | 70 | |
| ehbmbed2 | 0:12e734116fea | 71 | // Write latch at end of data; latch length varies with number of LEDs |
| ehbmbed2 | 0:12e734116fea | 72 | writezeros(3 * ((numLEDs + 63) / 64)); |
| ehbmbed2 | 0:12e734116fea | 73 | } |
| ehbmbed2 | 0:12e734116fea | 74 | |
| ehbmbed2 | 0:12e734116fea | 75 | // Convert R,G,B to combined 32-bit color |
| ehbmbed2 | 0:12e734116fea | 76 | uint32_t LPD8806::Color(uint8_t r, uint8_t g, uint8_t b) { |
| ehbmbed2 | 0:12e734116fea | 77 | // Take the lowest 7 bits of each value and append them end to end |
| ehbmbed2 | 0:12e734116fea | 78 | // We have the top bit set high (its a 'parity-like' bit in the protocol |
| ehbmbed2 | 0:12e734116fea | 79 | // and must be set!) |
| ehbmbed2 | 0:12e734116fea | 80 | return 0x808080 | ((uint32_t)g << 16) | ((uint32_t)r << 8) | (uint32_t)b; |
| ehbmbed2 | 0:12e734116fea | 81 | } |
| ehbmbed2 | 0:12e734116fea | 82 | |
| ehbmbed2 | 0:12e734116fea | 83 | // store the rgb component in our array |
| ehbmbed2 | 0:12e734116fea | 84 | void LPD8806::setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b) { |
| ehbmbed2 | 0:12e734116fea | 85 | if (n >= numLEDs) return; // '>=' because arrays are 0-indexed |
| ehbmbed2 | 0:12e734116fea | 86 | |
| ehbmbed2 | 0:12e734116fea | 87 | pixels[n*3 ] = g | 0x80; |
| ehbmbed2 | 0:12e734116fea | 88 | pixels[n*3+1] = r | 0x80; |
| ehbmbed2 | 0:12e734116fea | 89 | pixels[n*3+2] = b | 0x80; |
| ehbmbed2 | 0:12e734116fea | 90 | } |
| ehbmbed2 | 0:12e734116fea | 91 | |
| heroic | 2:af5af64e114d | 92 | void LPD8806::setPixelR(uint16_t n, uint8_t r) { |
| heroic | 2:af5af64e114d | 93 | if (n >= numLEDs) return; // '>=' because arrays are 0-indexed |
| heroic | 2:af5af64e114d | 94 | |
| heroic | 2:af5af64e114d | 95 | pixels[n*3+1] = r | 0x80; |
| heroic | 2:af5af64e114d | 96 | } |
| heroic | 2:af5af64e114d | 97 | |
| heroic | 2:af5af64e114d | 98 | void LPD8806::setPixelG(uint16_t n, uint8_t g) { |
| heroic | 2:af5af64e114d | 99 | if (n >= numLEDs) return; // '>=' because arrays are 0-indexed |
| heroic | 2:af5af64e114d | 100 | |
| heroic | 2:af5af64e114d | 101 | pixels[n*3] = g | 0x80; |
| heroic | 2:af5af64e114d | 102 | } |
| heroic | 2:af5af64e114d | 103 | |
| heroic | 2:af5af64e114d | 104 | void LPD8806::setPixelB(uint16_t n, uint8_t b) { |
| heroic | 2:af5af64e114d | 105 | if (n >= numLEDs) return; // '>=' because arrays are 0-indexed |
| heroic | 2:af5af64e114d | 106 | |
| heroic | 2:af5af64e114d | 107 | pixels[n*3+2] = b | 0x80; |
| heroic | 2:af5af64e114d | 108 | } |
| heroic | 2:af5af64e114d | 109 | |
| ehbmbed2 | 0:12e734116fea | 110 | void LPD8806::setPixelColor(uint16_t n, uint32_t c) { |
| ehbmbed2 | 0:12e734116fea | 111 | if (n >= numLEDs) return; // '>=' because arrays are 0-indexed |
| ehbmbed2 | 0:12e734116fea | 112 | |
| ehbmbed2 | 0:12e734116fea | 113 | pixels[n*3 ] = (c >> 16) | 0x80; |
| ehbmbed2 | 0:12e734116fea | 114 | pixels[n*3+1] = (c >> 8) | 0x80; |
| ehbmbed2 | 0:12e734116fea | 115 | pixels[n*3+2] = c | 0x80; |
| ehbmbed2 | 0:12e734116fea | 116 | } |
Ned Konz


Generic WS2811/WS2812