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:
Fri Dec 06 06:58:12 2013 +0000
Revision:
19:600deef36348
added HSB/RGB conversions; removed unnecessary virtuals

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bikeNomad 19:600deef36348 1 #include <math.h>
bikeNomad 19:600deef36348 2 #include <mbed.h>
bikeNomad 19:600deef36348 3 #include "Colors.h"
bikeNomad 19:600deef36348 4
bikeNomad 19:600deef36348 5 void HSBtoRGB(float hue, float saturation, float brightness, uint8_t *pr, uint8_t *pg, uint8_t *pb)
bikeNomad 19:600deef36348 6 {
bikeNomad 19:600deef36348 7 uint8_t r = 0, g = 0, b = 0;
bikeNomad 19:600deef36348 8 if (saturation == 0) {
bikeNomad 19:600deef36348 9 r = g = b = (uint8_t) (brightness * 255.0f + 0.5f);
bikeNomad 19:600deef36348 10 } else {
bikeNomad 19:600deef36348 11 float h = (hue - (float)floor(hue)) * 6.0f;
bikeNomad 19:600deef36348 12 float f = h - (float)floor(h);
bikeNomad 19:600deef36348 13 float p = brightness * (1.0f - saturation);
bikeNomad 19:600deef36348 14 float q = brightness * (1.0f - saturation * f);
bikeNomad 19:600deef36348 15 float t = brightness * (1.0f - (saturation * (1.0f - f)));
bikeNomad 19:600deef36348 16 switch ((int) h) {
bikeNomad 19:600deef36348 17 case 0:
bikeNomad 19:600deef36348 18 r = (int) (brightness * 255.0f + 0.5f);
bikeNomad 19:600deef36348 19 g = (int) (t * 255.0f + 0.5f);
bikeNomad 19:600deef36348 20 b = (int) (p * 255.0f + 0.5f);
bikeNomad 19:600deef36348 21 break;
bikeNomad 19:600deef36348 22 case 1:
bikeNomad 19:600deef36348 23 r = (int) (q * 255.0f + 0.5f);
bikeNomad 19:600deef36348 24 g = (int) (brightness * 255.0f + 0.5f);
bikeNomad 19:600deef36348 25 b = (int) (p * 255.0f + 0.5f);
bikeNomad 19:600deef36348 26 break;
bikeNomad 19:600deef36348 27 case 2:
bikeNomad 19:600deef36348 28 r = (int) (p * 255.0f + 0.5f);
bikeNomad 19:600deef36348 29 g = (int) (brightness * 255.0f + 0.5f);
bikeNomad 19:600deef36348 30 b = (int) (t * 255.0f + 0.5f);
bikeNomad 19:600deef36348 31 break;
bikeNomad 19:600deef36348 32 case 3:
bikeNomad 19:600deef36348 33 r = (int) (p * 255.0f + 0.5f);
bikeNomad 19:600deef36348 34 g = (int) (q * 255.0f + 0.5f);
bikeNomad 19:600deef36348 35 b = (int) (brightness * 255.0f + 0.5f);
bikeNomad 19:600deef36348 36 break;
bikeNomad 19:600deef36348 37 case 4:
bikeNomad 19:600deef36348 38 r = (int) (t * 255.0f + 0.5f);
bikeNomad 19:600deef36348 39 g = (int) (p * 255.0f + 0.5f);
bikeNomad 19:600deef36348 40 b = (int) (brightness * 255.0f + 0.5f);
bikeNomad 19:600deef36348 41 break;
bikeNomad 19:600deef36348 42 case 5:
bikeNomad 19:600deef36348 43 r = (int) (brightness * 255.0f + 0.5f);
bikeNomad 19:600deef36348 44 g = (int) (p * 255.0f + 0.5f);
bikeNomad 19:600deef36348 45 b = (int) (q * 255.0f + 0.5f);
bikeNomad 19:600deef36348 46 break;
bikeNomad 19:600deef36348 47 }
bikeNomad 19:600deef36348 48 }
bikeNomad 19:600deef36348 49 *pr = r;
bikeNomad 19:600deef36348 50 *pg = g;
bikeNomad 19:600deef36348 51 *pb = b;
bikeNomad 19:600deef36348 52 }
bikeNomad 19:600deef36348 53
bikeNomad 19:600deef36348 54 float* RGBtoHSB(uint8_t r, uint8_t g, uint8_t b, float* hsbvals)
bikeNomad 19:600deef36348 55 {
bikeNomad 19:600deef36348 56 float hue, saturation, brightness;
bikeNomad 19:600deef36348 57 if (!hsbvals) {
bikeNomad 19:600deef36348 58 hsbvals = new float[3];
bikeNomad 19:600deef36348 59 }
bikeNomad 19:600deef36348 60 uint8_t cmax = (r > g) ? r : g;
bikeNomad 19:600deef36348 61 if (b > cmax) cmax = b;
bikeNomad 19:600deef36348 62 uint8_t cmin = (r < g) ? r : g;
bikeNomad 19:600deef36348 63 if (b < cmin) cmin = b;
bikeNomad 19:600deef36348 64
bikeNomad 19:600deef36348 65 brightness = ((float) cmax) / 255.0f;
bikeNomad 19:600deef36348 66 if (cmax != 0)
bikeNomad 19:600deef36348 67 saturation = ((float) (cmax - cmin)) / ((float) cmax);
bikeNomad 19:600deef36348 68 else
bikeNomad 19:600deef36348 69 saturation = 0;
bikeNomad 19:600deef36348 70 if (saturation == 0)
bikeNomad 19:600deef36348 71 hue = 0;
bikeNomad 19:600deef36348 72 else {
bikeNomad 19:600deef36348 73 float redc = ((float) (cmax - r)) / ((float) (cmax - cmin));
bikeNomad 19:600deef36348 74 float greenc = ((float) (cmax - g)) / ((float) (cmax - cmin));
bikeNomad 19:600deef36348 75 float bluec = ((float) (cmax - b)) / ((float) (cmax - cmin));
bikeNomad 19:600deef36348 76 if (r == cmax)
bikeNomad 19:600deef36348 77 hue = bluec - greenc;
bikeNomad 19:600deef36348 78 else if (g == cmax)
bikeNomad 19:600deef36348 79 hue = 2.0f + redc - bluec;
bikeNomad 19:600deef36348 80 else
bikeNomad 19:600deef36348 81 hue = 4.0f + greenc - redc;
bikeNomad 19:600deef36348 82 hue = hue / 6.0f;
bikeNomad 19:600deef36348 83 if (hue < 0)
bikeNomad 19:600deef36348 84 hue = hue + 1.0f;
bikeNomad 19:600deef36348 85 }
bikeNomad 19:600deef36348 86 hsbvals[0] = hue;
bikeNomad 19:600deef36348 87 hsbvals[1] = saturation;
bikeNomad 19:600deef36348 88 hsbvals[2] = brightness;
bikeNomad 19:600deef36348 89 return hsbvals;
bikeNomad 19:600deef36348 90 }