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:
bikeNomad
Date:
Thu Jan 02 19:42:14 2014 +0000
Revision:
25:751c89f7e654
Parent:
24:feb1dae0403a
Child:
27:88c2abdf5eb9
added second light strip to demo.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bikeNomad 22:abfed71656bd 1 #include "mbed.h"
bikeNomad 22:abfed71656bd 2 #include "WS2811.h"
bikeNomad 22:abfed71656bd 3 #include "Colors.h"
bikeNomad 24:feb1dae0403a 4 #include "TSISensor.h"
bikeNomad 22:abfed71656bd 5
bikeNomad 23:33df42ff2541 6 // per LED: 3 * 20 mA = 60mA max
bikeNomad 23:33df42ff2541 7 // 60 LEDs: 60 * 60mA = 3600 mA max
bikeNomad 25:751c89f7e654 8 // 120 LEDs: 7200 mA max
bikeNomad 22:abfed71656bd 9 unsigned const nLEDs = MAX_LEDS_PER_STRIP;
bikeNomad 22:abfed71656bd 10
bikeNomad 22:abfed71656bd 11 // I/O pin usage
bikeNomad 22:abfed71656bd 12 // PTD0 TPM0 CH0 monitor
bikeNomad 22:abfed71656bd 13 // PTD1 TPM0 CH1 monitor
bikeNomad 22:abfed71656bd 14 // PTD2 data output
bikeNomad 22:abfed71656bd 15 // PTD3 debug output
bikeNomad 22:abfed71656bd 16
bikeNomad 25:751c89f7e654 17 unsigned const DATA_OUT_PIN1 = 2; // PTD2
bikeNomad 25:751c89f7e654 18 unsigned const DATA_OUT_PIN2 = 3; // PTD3
bikeNomad 22:abfed71656bd 19
bikeNomad 22:abfed71656bd 20 Serial pc(USBTX, USBRX);
bikeNomad 25:751c89f7e654 21 Timer timeRunning;
bikeNomad 22:abfed71656bd 22
bikeNomad 24:feb1dae0403a 23 TSISensor touchSensor;
bikeNomad 24:feb1dae0403a 24 Ticker touchTicker;
bikeNomad 24:feb1dae0403a 25 float touchPercentage;
bikeNomad 25:751c89f7e654 26 unsigned frames;
bikeNomad 25:751c89f7e654 27
bikeNomad 25:751c89f7e654 28 float const minBrite = 0.2;
bikeNomad 25:751c89f7e654 29 float const maxBrite = 0.5;
bikeNomad 24:feb1dae0403a 30 float brite;
bikeNomad 24:feb1dae0403a 31
bikeNomad 24:feb1dae0403a 32 void readTouchSensor()
bikeNomad 24:feb1dae0403a 33 {
bikeNomad 24:feb1dae0403a 34 touchPercentage *= 0.9;
bikeNomad 24:feb1dae0403a 35 touchPercentage += touchSensor.readPercentage() * 0.1;
bikeNomad 25:751c89f7e654 36 brite = minBrite + (maxBrite - minBrite) * touchPercentage;
bikeNomad 24:feb1dae0403a 37 }
bikeNomad 24:feb1dae0403a 38
bikeNomad 22:abfed71656bd 39 // @brief sets different colors in each of the LEDs of a strip
bikeNomad 22:abfed71656bd 40 // @param strip the light strip
bikeNomad 22:abfed71656bd 41 // @param sat saturation, 0.0 - 1.0
bikeNomad 22:abfed71656bd 42 // @param brite brightness, 0.0 - 1.0
bikeNomad 22:abfed71656bd 43 // @param hueShift shift, 0.0 - 1.0 is equivalent to 0 - 360 degrees
bikeNomad 22:abfed71656bd 44 static void showRainbow(WS2811 &strip, float sat, float brite, float hueShift)
bikeNomad 22:abfed71656bd 45 {
bikeNomad 22:abfed71656bd 46 unsigned nLEDs = strip.numPixels();
bikeNomad 22:abfed71656bd 47 for (unsigned i = 0; i < nLEDs; i++) {
bikeNomad 22:abfed71656bd 48 uint8_t r, g, b;
bikeNomad 22:abfed71656bd 49 float hue = ((float)i / (float)nLEDs) + hueShift;
bikeNomad 22:abfed71656bd 50 HSBtoRGB(hue, sat, brite, &r, &g, &b);
bikeNomad 22:abfed71656bd 51 strip.setPixelColor(i, LedStrip::Color(r, g, b));
bikeNomad 22:abfed71656bd 52 }
bikeNomad 22:abfed71656bd 53 strip.show();
bikeNomad 22:abfed71656bd 54 }
bikeNomad 20:b9d76e567637 55
bikeNomad 24:feb1dae0403a 56 static void showSolidColor(WS2811 &strip, uint8_t r, uint8_t g, uint8_t b)
bikeNomad 24:feb1dae0403a 57 {
bikeNomad 24:feb1dae0403a 58 unsigned nLEDs = strip.numPixels();
bikeNomad 24:feb1dae0403a 59 for (unsigned i = 0; i < nLEDs; i++) {
bikeNomad 24:feb1dae0403a 60 strip.setPixelColor(i, LedStrip::Color(r, g, b));
bikeNomad 24:feb1dae0403a 61 }
bikeNomad 24:feb1dae0403a 62 strip.show();
bikeNomad 24:feb1dae0403a 63 }
bikeNomad 24:feb1dae0403a 64
bikeNomad 22:abfed71656bd 65 int main(void)
bikeNomad 22:abfed71656bd 66 {
bikeNomad 22:abfed71656bd 67 pc.baud(115200);
bikeNomad 25:751c89f7e654 68 WS2811 lightStrip1(nLEDs, DATA_OUT_PIN1);
bikeNomad 25:751c89f7e654 69 WS2811 lightStrip2(nLEDs, DATA_OUT_PIN2);
bikeNomad 22:abfed71656bd 70
bikeNomad 24:feb1dae0403a 71 touchTicker.attach(&readTouchSensor, 0.1);
bikeNomad 25:751c89f7e654 72 lightStrip1.begin();
bikeNomad 25:751c89f7e654 73 lightStrip2.begin();
bikeNomad 22:abfed71656bd 74
bikeNomad 23:33df42ff2541 75 float rainbowPeriod = 1 * 1.0e6; // usec
bikeNomad 23:33df42ff2541 76 float sat = 1.0;
bikeNomad 22:abfed71656bd 77
bikeNomad 23:33df42ff2541 78 timeRunning.start();
bikeNomad 23:33df42ff2541 79 bool printed = false;
bikeNomad 22:abfed71656bd 80
bikeNomad 24:feb1dae0403a 81 uint8_t r =0;
bikeNomad 24:feb1dae0403a 82 uint8_t g =0;
bikeNomad 24:feb1dae0403a 83 uint8_t b =0;
bikeNomad 24:feb1dae0403a 84 for (;;) {
bikeNomad 25:751c89f7e654 85 if (r < 40)
bikeNomad 24:feb1dae0403a 86 r++;
bikeNomad 25:751c89f7e654 87 else if (g < 40)
bikeNomad 24:feb1dae0403a 88 g++;
bikeNomad 25:751c89f7e654 89 else if (b < 40)
bikeNomad 24:feb1dae0403a 90 b++;
bikeNomad 24:feb1dae0403a 91 else {
bikeNomad 24:feb1dae0403a 92 unsigned running = timeRunning.read_us();
bikeNomad 24:feb1dae0403a 93 pc.printf("%u frames in %u usec = %u frames / sec\r\n", frames, running, frames * 1000000 / running);
bikeNomad 24:feb1dae0403a 94 break;
bikeNomad 24:feb1dae0403a 95 }
bikeNomad 24:feb1dae0403a 96
bikeNomad 24:feb1dae0403a 97 wait(0.1);
bikeNomad 25:751c89f7e654 98 showSolidColor(lightStrip1, r, g, b);
bikeNomad 25:751c89f7e654 99 showSolidColor(lightStrip2, r, g, b);
bikeNomad 25:751c89f7e654 100 WS2811::startDMA();
bikeNomad 25:751c89f7e654 101
bikeNomad 24:feb1dae0403a 102 frames++;
bikeNomad 24:feb1dae0403a 103 }
bikeNomad 24:feb1dae0403a 104
bikeNomad 24:feb1dae0403a 105 timeRunning.reset();
bikeNomad 24:feb1dae0403a 106 frames = 0;
bikeNomad 24:feb1dae0403a 107
bikeNomad 22:abfed71656bd 108 for (;;) {
bikeNomad 23:33df42ff2541 109 unsigned running = timeRunning.read_us();
bikeNomad 23:33df42ff2541 110 float hueShift = running / rainbowPeriod;
bikeNomad 23:33df42ff2541 111 if (!printed && running >= 10000000U) {
bikeNomad 23:33df42ff2541 112 pc.printf("%u frames in %u usec = %u frames / sec\r\n", frames, running, frames * 1000000 / running);
bikeNomad 23:33df42ff2541 113 printed = true;
bikeNomad 23:33df42ff2541 114 }
bikeNomad 25:751c89f7e654 115 showRainbow(lightStrip1, sat, brite, hueShift);
bikeNomad 25:751c89f7e654 116 showRainbow(lightStrip2, sat, brite, hueShift + 0.5);
bikeNomad 25:751c89f7e654 117 WS2811::startDMA();
bikeNomad 25:751c89f7e654 118
bikeNomad 23:33df42ff2541 119 frames ++;
bikeNomad 23:33df42ff2541 120 }
bikeNomad 22:abfed71656bd 121 }
bikeNomad 22:abfed71656bd 122