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 Heroic Robotics

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

/media/uploads/bikeNomad/img_0482.jpg

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.

Committer:
heroic
Date:
Wed Nov 14 05:06:29 2012 +0000
Revision:
9:dd524af149e6
Parent:
8:e3249c2b7607
Child:
10:62368b801d16
Mask interrupts during critical section.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
heroic 8:e3249c2b7607 1 // WS2811 driver, kinda.
heroic 8:e3249c2b7607 2 //
heroic 8:e3249c2b7607 3 // Parameterized and modified to use soft SPI.
heroic 8:e3249c2b7607 4 // Jas Strong <jasmine@electronpusher.org>
heroic 8:e3249c2b7607 5 /*****************************************************************************/
heroic 8:e3249c2b7607 6
heroic 8:e3249c2b7607 7 #include "LedStrip.h"
heroic 8:e3249c2b7607 8 #include "WS2811.h"
heroic 8:e3249c2b7607 9
heroic 8:e3249c2b7607 10 WS2811::WS2811(PinName dataPin, PinName clockPin, int n) :
heroic 8:e3249c2b7607 11 dat(dataPin),
heroic 8:e3249c2b7607 12 clk(clockPin) {
heroic 8:e3249c2b7607 13 // Allocate 3 bytes per pixel:
heroic 8:e3249c2b7607 14 numLEDs = n;
heroic 8:e3249c2b7607 15 if ((pixels = (uint8_t *)malloc(numLEDs * 3))) {
heroic 8:e3249c2b7607 16 memset(pixels, 0x00, numLEDs * 3); // Init to RGB 'off' state
heroic 8:e3249c2b7607 17 }
heroic 8:e3249c2b7607 18 // calibrate delay loops for NRZ
heroic 8:e3249c2b7607 19 int i;
heroic 8:e3249c2b7607 20 guardtime.start();
heroic 8:e3249c2b7607 21 for (i=0; i<1000; i++)
heroic 8:e3249c2b7607 22 /* do nothing */;
heroic 8:e3249c2b7607 23 i=guardtime.read_us();
heroic 8:e3249c2b7607 24 printf("ws2811: 1000 iters took %d usec.\n", i);
heroic 8:e3249c2b7607 25 bogocal = (1000 / (i*2.4)); // iterations per bitcell (417 nsec)
heroic 8:e3249c2b7607 26 printf("ws2811: calibrating to %d bogojiffies.\n", bogocal);
heroic 8:e3249c2b7607 27 }
heroic 8:e3249c2b7607 28
heroic 8:e3249c2b7607 29 /*
heroic 8:e3249c2b7607 30 * These chips use a one-wire protocol based on a sort of NRZ signalling- jas.
heroic 8:e3249c2b7607 31 */
heroic 8:e3249c2b7607 32
heroic 8:e3249c2b7607 33 void WS2811::write(uint8_t byte) {
heroic 9:dd524af149e6 34 __disable_irq();
heroic 8:e3249c2b7607 35 for (int i=0; i<8; i++) {
heroic 8:e3249c2b7607 36 if (byte & 0x80)
heroic 8:e3249c2b7607 37 writebit(1);
heroic 8:e3249c2b7607 38 else
heroic 8:e3249c2b7607 39 writebit(0);
heroic 8:e3249c2b7607 40 byte <<= 1;
heroic 8:e3249c2b7607 41 }
heroic 9:dd524af149e6 42 __enable_irq();
heroic 8:e3249c2b7607 43 }
heroic 8:e3249c2b7607 44
heroic 8:e3249c2b7607 45 inline void WS2811::celldelay(void) {
heroic 8:e3249c2b7607 46 for (int i = 0; i<bogocal; i++)
heroic 8:e3249c2b7607 47 /* do nothing */ ;
heroic 8:e3249c2b7607 48 }
heroic 8:e3249c2b7607 49
heroic 8:e3249c2b7607 50 void WS2811::writebit(bool bit) {
heroic 8:e3249c2b7607 51 // first cell is always 1
heroic 8:e3249c2b7607 52 dat = 1;
heroic 8:e3249c2b7607 53 celldelay();
heroic 8:e3249c2b7607 54 if (bit) {
heroic 8:e3249c2b7607 55 celldelay();
heroic 8:e3249c2b7607 56 } else {
heroic 8:e3249c2b7607 57 dat=0;
heroic 8:e3249c2b7607 58 celldelay();
heroic 8:e3249c2b7607 59 }
heroic 8:e3249c2b7607 60 // last cell is always 0
heroic 8:e3249c2b7607 61 dat=0;
heroic 8:e3249c2b7607 62 celldelay();
heroic 8:e3249c2b7607 63 }
heroic 8:e3249c2b7607 64
heroic 8:e3249c2b7607 65 void WS2811::begin(void) {
heroic 8:e3249c2b7607 66 blank();
heroic 8:e3249c2b7607 67 show();
heroic 8:e3249c2b7607 68 }
heroic 8:e3249c2b7607 69
heroic 8:e3249c2b7607 70 uint16_t WS2811::numPixels(void) {
heroic 8:e3249c2b7607 71 return numLEDs;
heroic 8:e3249c2b7607 72 }
heroic 8:e3249c2b7607 73
heroic 8:e3249c2b7607 74 void WS2811::blank(void) {
heroic 8:e3249c2b7607 75 memset(pixels, 0x00, numLEDs * 3);
heroic 8:e3249c2b7607 76 }
heroic 8:e3249c2b7607 77
heroic 8:e3249c2b7607 78 void WS2811::show(void) {
heroic 8:e3249c2b7607 79 uint16_t i, nl3 = numLEDs * 3; // 3 bytes per LED
heroic 8:e3249c2b7607 80 while (guardtime.read_us() < 50)
heroic 8:e3249c2b7607 81 /* spin */;
heroic 8:e3249c2b7607 82 for (i=0; i<nl3; i++ ) {
heroic 8:e3249c2b7607 83 write(pixels[i]);
heroic 8:e3249c2b7607 84 }
heroic 8:e3249c2b7607 85
heroic 8:e3249c2b7607 86 guardtime.reset();
heroic 8:e3249c2b7607 87 }
heroic 8:e3249c2b7607 88
heroic 8:e3249c2b7607 89
heroic 8:e3249c2b7607 90 uint32_t WS2811::total_luminance(void) {
heroic 8:e3249c2b7607 91 uint32_t running_total;
heroic 8:e3249c2b7607 92 running_total = 0;
heroic 8:e3249c2b7607 93 for (int i=0; i<numLEDs*3; i++)
heroic 8:e3249c2b7607 94 running_total += pixels[i];
heroic 8:e3249c2b7607 95 return running_total;
heroic 8:e3249c2b7607 96 }
heroic 8:e3249c2b7607 97
heroic 8:e3249c2b7607 98 // Convert R,G,B to combined 32-bit color
heroic 8:e3249c2b7607 99 uint32_t WS2811::Color(uint8_t r, uint8_t g, uint8_t b) {
heroic 8:e3249c2b7607 100 // Take the lowest 7 bits of each value and append them end to end
heroic 8:e3249c2b7607 101 // We have the top bit set high (its a 'parity-like' bit in the protocol
heroic 8:e3249c2b7607 102 // and must be set!)
heroic 8:e3249c2b7607 103 return ((uint32_t)g << 16) | ((uint32_t)r << 8) | (uint32_t)b;
heroic 8:e3249c2b7607 104 }
heroic 8:e3249c2b7607 105
heroic 8:e3249c2b7607 106 // store the rgb component in our array
heroic 8:e3249c2b7607 107 void WS2811::setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b) {
heroic 8:e3249c2b7607 108 if (n >= numLEDs) return; // '>=' because arrays are 0-indexed
heroic 8:e3249c2b7607 109
heroic 8:e3249c2b7607 110 pixels[n*3 ] = g;
heroic 8:e3249c2b7607 111 pixels[n*3+1] = r;
heroic 8:e3249c2b7607 112 pixels[n*3+2] = b;
heroic 8:e3249c2b7607 113 }
heroic 8:e3249c2b7607 114
heroic 8:e3249c2b7607 115 void WS2811::setPixelR(uint16_t n, uint8_t r) {
heroic 8:e3249c2b7607 116 if (n >= numLEDs) return; // '>=' because arrays are 0-indexed
heroic 8:e3249c2b7607 117
heroic 8:e3249c2b7607 118 pixels[n*3+1] = r;
heroic 8:e3249c2b7607 119 }
heroic 8:e3249c2b7607 120
heroic 8:e3249c2b7607 121 void WS2811::setPixelG(uint16_t n, uint8_t g) {
heroic 8:e3249c2b7607 122 if (n >= numLEDs) return; // '>=' because arrays are 0-indexed
heroic 8:e3249c2b7607 123
heroic 8:e3249c2b7607 124 pixels[n*3] = g;
heroic 8:e3249c2b7607 125 }
heroic 8:e3249c2b7607 126
heroic 8:e3249c2b7607 127 void WS2811::setPixelB(uint16_t n, uint8_t b) {
heroic 8:e3249c2b7607 128 if (n >= numLEDs) return; // '>=' because arrays are 0-indexed
heroic 8:e3249c2b7607 129
heroic 8:e3249c2b7607 130 pixels[n*3+2] = b;
heroic 8:e3249c2b7607 131 }
heroic 8:e3249c2b7607 132
heroic 8:e3249c2b7607 133 void WS2811::setPixelColor(uint16_t n, uint32_t c) {
heroic 8:e3249c2b7607 134 if (n >= numLEDs) return; // '>=' because arrays are 0-indexed
heroic 8:e3249c2b7607 135
heroic 8:e3249c2b7607 136 pixels[n*3 ] = (c >> 16);
heroic 8:e3249c2b7607 137 pixels[n*3+1] = (c >> 8);
heroic 8:e3249c2b7607 138 pixels[n*3+2] = c;
heroic 8:e3249c2b7607 139 }