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 11:26:44 2014 +0000
Revision:
24:feb1dae0403a
Parent:
23:33df42ff2541
Child:
25:751c89f7e654
added R/G/B test at start; 50usec guardtime doesn't work for some reason.

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