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 Jun 11 15:33:47 2015 +0000
Revision:
33:43b504e417e3
Parent:
32:115032de785f
Child:
34:cd56c00ed910
changed abs() to fabs() to fix offline compilation.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bikeNomad 22:abfed71656bd 1 #include "mbed.h"
bikeNomad 22:abfed71656bd 2 #include "Colors.h"
bikeNomad 24:feb1dae0403a 3 #include "TSISensor.h"
bikeNomad 27:88c2abdf5eb9 4 #include "MMA8451Q.h"
bikeNomad 27:88c2abdf5eb9 5
bikeNomad 27:88c2abdf5eb9 6 #define MMA8451_I2C_ADDRESS (0x1d<<1)
bikeNomad 22:abfed71656bd 7
bikeNomad 32:115032de785f 8 #define INSTANTIATE_TEMPLATES 1
bikeNomad 32:115032de785f 9 #include "WS2811.h"
bikeNomad 32:115032de785f 10
bikeNomad 32:115032de785f 11 // I/O pin usage
bikeNomad 32:115032de785f 12 // PTD0 TPM0 CH0 monitor
bikeNomad 32:115032de785f 13 // PTD1 TPM0 CH1 monitor
bikeNomad 32:115032de785f 14 // PTD2 data output for strip# 1
bikeNomad 32:115032de785f 15 // PTD3 data output for strip# 2
bikeNomad 32:115032de785f 16 // PTD4 blinking eyes output (HI = ON)
bikeNomad 32:115032de785f 17
bikeNomad 32:115032de785f 18 unsigned const DATA_OUT_PIN1 = 2; // PTD2
bikeNomad 32:115032de785f 19 unsigned const DATA_OUT_PIN2 = 3; // PTD3
bikeNomad 32:115032de785f 20
bikeNomad 32:115032de785f 21 const unsigned MAX_LEDS_PER_STRIP = 32;
bikeNomad 32:115032de785f 22
bikeNomad 23:33df42ff2541 23 // per LED: 3 * 20 mA = 60mA max
bikeNomad 23:33df42ff2541 24 // 60 LEDs: 60 * 60mA = 3600 mA max
bikeNomad 25:751c89f7e654 25 // 120 LEDs: 7200 mA max
bikeNomad 22:abfed71656bd 26 unsigned const nLEDs = MAX_LEDS_PER_STRIP;
bikeNomad 22:abfed71656bd 27
bikeNomad 32:115032de785f 28 template class WS2811<MAX_LEDS_PER_STRIP>;
bikeNomad 22:abfed71656bd 29
bikeNomad 32:115032de785f 30 typedef WS2811<MAX_LEDS_PER_STRIP> MyWS2811;
bikeNomad 32:115032de785f 31
bikeNomad 32:115032de785f 32 MyWS2811 lightStrip1(nLEDs, DATA_OUT_PIN1);
bikeNomad 32:115032de785f 33 MyWS2811 lightStrip2(nLEDs, DATA_OUT_PIN2);
bikeNomad 22:abfed71656bd 34
bikeNomad 22:abfed71656bd 35 Serial pc(USBTX, USBRX);
bikeNomad 25:751c89f7e654 36 Timer timeRunning;
bikeNomad 22:abfed71656bd 37
bikeNomad 24:feb1dae0403a 38 TSISensor touchSensor;
bikeNomad 27:88c2abdf5eb9 39 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
bikeNomad 29:a76075c853ee 40 PwmOut rled(LED_RED);
bikeNomad 29:a76075c853ee 41 PwmOut gled(LED_GREEN);
bikeNomad 30:52e9205a8059 42 // LED_BLUE is on PTD1
bikeNomad 27:88c2abdf5eb9 43
bikeNomad 24:feb1dae0403a 44 float touchPercentage;
bikeNomad 25:751c89f7e654 45 unsigned frames;
bikeNomad 25:751c89f7e654 46
bikeNomad 25:751c89f7e654 47 float const minBrite = 0.2;
bikeNomad 25:751c89f7e654 48 float const maxBrite = 0.5;
bikeNomad 24:feb1dae0403a 49 float brite;
bikeNomad 24:feb1dae0403a 50
bikeNomad 24:feb1dae0403a 51 void readTouchSensor()
bikeNomad 24:feb1dae0403a 52 {
bikeNomad 24:feb1dae0403a 53 touchPercentage *= 0.9;
bikeNomad 24:feb1dae0403a 54 touchPercentage += touchSensor.readPercentage() * 0.1;
bikeNomad 25:751c89f7e654 55 brite = minBrite + (maxBrite - minBrite) * touchPercentage;
bikeNomad 24:feb1dae0403a 56 }
bikeNomad 24:feb1dae0403a 57
bikeNomad 22:abfed71656bd 58 // @brief sets different colors in each of the LEDs of a strip
bikeNomad 22:abfed71656bd 59 // @param strip the light strip
bikeNomad 22:abfed71656bd 60 // @param sat saturation, 0.0 - 1.0
bikeNomad 22:abfed71656bd 61 // @param brite brightness, 0.0 - 1.0
bikeNomad 22:abfed71656bd 62 // @param hueShift shift, 0.0 - 1.0 is equivalent to 0 - 360 degrees
bikeNomad 32:115032de785f 63 static void showRainbow(MyWS2811 &strip, float sat, float brite, float hueShift)
bikeNomad 22:abfed71656bd 64 {
bikeNomad 22:abfed71656bd 65 unsigned nLEDs = strip.numPixels();
bikeNomad 22:abfed71656bd 66 for (unsigned i = 0; i < nLEDs; i++) {
bikeNomad 22:abfed71656bd 67 uint8_t r, g, b;
bikeNomad 22:abfed71656bd 68 float hue = ((float)i / (float)nLEDs) + hueShift;
bikeNomad 22:abfed71656bd 69 HSBtoRGB(hue, sat, brite, &r, &g, &b);
bikeNomad 28:dbe551a3dd64 70 strip.setPixelColor(i, r, g, b);
bikeNomad 22:abfed71656bd 71 }
bikeNomad 22:abfed71656bd 72 strip.show();
bikeNomad 22:abfed71656bd 73 }
bikeNomad 20:b9d76e567637 74
bikeNomad 32:115032de785f 75 static void showSolidColor(MyWS2811 &strip, uint8_t r, uint8_t g, uint8_t b)
bikeNomad 24:feb1dae0403a 76 {
bikeNomad 24:feb1dae0403a 77 unsigned nLEDs = strip.numPixels();
bikeNomad 24:feb1dae0403a 78 for (unsigned i = 0; i < nLEDs; i++) {
bikeNomad 28:dbe551a3dd64 79 strip.setPixelColor(i, r, g, b);
bikeNomad 24:feb1dae0403a 80 }
bikeNomad 24:feb1dae0403a 81 strip.show();
bikeNomad 24:feb1dae0403a 82 }
bikeNomad 24:feb1dae0403a 83
bikeNomad 22:abfed71656bd 84 int main(void)
bikeNomad 22:abfed71656bd 85 {
bikeNomad 22:abfed71656bd 86 pc.baud(115200);
bikeNomad 32:115032de785f 87
bikeNomad 22:abfed71656bd 88
bikeNomad 25:751c89f7e654 89 lightStrip1.begin();
bikeNomad 25:751c89f7e654 90 lightStrip2.begin();
bikeNomad 29:a76075c853ee 91 rled = 1.0;
bikeNomad 29:a76075c853ee 92 gled = 1.0;
bikeNomad 22:abfed71656bd 93
bikeNomad 23:33df42ff2541 94 float sat = 1.0;
bikeNomad 22:abfed71656bd 95
bikeNomad 23:33df42ff2541 96 timeRunning.start();
bikeNomad 22:abfed71656bd 97
bikeNomad 24:feb1dae0403a 98 uint8_t r =0;
bikeNomad 24:feb1dae0403a 99 uint8_t g =0;
bikeNomad 24:feb1dae0403a 100 uint8_t b =0;
bikeNomad 24:feb1dae0403a 101 for (;;) {
bikeNomad 25:751c89f7e654 102 if (r < 40)
bikeNomad 24:feb1dae0403a 103 r++;
bikeNomad 25:751c89f7e654 104 else if (g < 40)
bikeNomad 24:feb1dae0403a 105 g++;
bikeNomad 25:751c89f7e654 106 else if (b < 40)
bikeNomad 24:feb1dae0403a 107 b++;
bikeNomad 24:feb1dae0403a 108 else {
bikeNomad 24:feb1dae0403a 109 unsigned running = timeRunning.read_us();
bikeNomad 24:feb1dae0403a 110 pc.printf("%u frames in %u usec = %u frames / sec\r\n", frames, running, frames * 1000000 / running);
bikeNomad 24:feb1dae0403a 111 break;
bikeNomad 24:feb1dae0403a 112 }
bikeNomad 24:feb1dae0403a 113
bikeNomad 25:751c89f7e654 114 showSolidColor(lightStrip1, r, g, b);
bikeNomad 25:751c89f7e654 115 showSolidColor(lightStrip2, r, g, b);
bikeNomad 32:115032de785f 116 MyWS2811::startDMA();
bikeNomad 25:751c89f7e654 117
bikeNomad 24:feb1dae0403a 118 frames++;
bikeNomad 24:feb1dae0403a 119 }
bikeNomad 24:feb1dae0403a 120
bikeNomad 24:feb1dae0403a 121 timeRunning.reset();
bikeNomad 24:feb1dae0403a 122 frames = 0;
bikeNomad 24:feb1dae0403a 123
bikeNomad 29:a76075c853ee 124 float xyz[3];
bikeNomad 29:a76075c853ee 125 acc.getAccAllAxis(xyz);
bikeNomad 29:a76075c853ee 126 pc.printf("x: %f y: %f z: %f\r\n", xyz[0], xyz[1], xyz[2]);
bikeNomad 29:a76075c853ee 127
bikeNomad 22:abfed71656bd 128 for (;;) {
bikeNomad 27:88c2abdf5eb9 129 acc.getAccAllAxis(xyz);
bikeNomad 33:43b504e417e3 130 rled = 1.0 - fabs(xyz[0]);
bikeNomad 33:43b504e417e3 131 gled = 1.0 - fabs(xyz[1]);
bikeNomad 29:a76075c853ee 132 readTouchSensor();
bikeNomad 33:43b504e417e3 133 showRainbow(lightStrip1, sat, brite, fabs(xyz[0]));
bikeNomad 33:43b504e417e3 134 showRainbow(lightStrip2, sat, brite, fabs(xyz[1]));
bikeNomad 32:115032de785f 135 MyWS2811::startDMA();
bikeNomad 25:751c89f7e654 136
bikeNomad 23:33df42ff2541 137 frames ++;
bikeNomad 23:33df42ff2541 138 }
bikeNomad 22:abfed71656bd 139 }
bikeNomad 22:abfed71656bd 140