A program that fades between a selection of colors.

Dependencies:   PixelArray WS2812 mbed

Committer:
theros
Date:
Sun Mar 12 16:51:05 2017 +0000
Revision:
1:ad5c2cfb2002
Parent:
0:de636c7cdfda
Child:
2:381421ec57e1
Changed name to "WS2812_Fader" from "WS2812_Gradient"; working color cycle and fading between colors via interpolation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
theros 0:de636c7cdfda 1 #include "mbed.h"
theros 0:de636c7cdfda 2 #include "WS2812.h"
theros 0:de636c7cdfda 3 #include "PixelArray.h"
theros 0:de636c7cdfda 4
theros 0:de636c7cdfda 5 #define WS2812_BUF 77 //number of LEDs in the array
theros 0:de636c7cdfda 6 #define NUM_COLORS 6 //number of colors to store in the array
theros 1:ad5c2cfb2002 7 #define NUM_STEPS 32 //number of steps between colors
theros 0:de636c7cdfda 8
theros 0:de636c7cdfda 9 DigitalIn usrBtn(USER_BUTTON);
theros 0:de636c7cdfda 10 DigitalOut usrLed(LED1);
theros 0:de636c7cdfda 11 PixelArray px(WS2812_BUF);
theros 0:de636c7cdfda 12
theros 0:de636c7cdfda 13 // See the program page for information on the timing numbers
theros 0:de636c7cdfda 14 WS2812 ws(D9, WS2812_BUF, 6,17,9,14); //nucleo-f411re
theros 0:de636c7cdfda 15
theros 1:ad5c2cfb2002 16 int color_set(uint8_t red,uint8_t green, uint8_t blue)
theros 1:ad5c2cfb2002 17 {
theros 1:ad5c2cfb2002 18 return ((red<<16) + (green<<8) + blue);
theros 1:ad5c2cfb2002 19 }
theros 1:ad5c2cfb2002 20
theros 1:ad5c2cfb2002 21 // 0 <= stepNumber <= lastStepNumber
theros 1:ad5c2cfb2002 22 int interpolate(int startValue, int endValue, int stepNumber, int lastStepNumber)
theros 1:ad5c2cfb2002 23 {
theros 1:ad5c2cfb2002 24 return (endValue - startValue) * stepNumber / lastStepNumber + startValue;
theros 1:ad5c2cfb2002 25 }
theros 1:ad5c2cfb2002 26
theros 0:de636c7cdfda 27 int main()
theros 0:de636c7cdfda 28 {
theros 0:de636c7cdfda 29 int colorIdx = 0;
theros 1:ad5c2cfb2002 30 int colorTo = 0;
theros 1:ad5c2cfb2002 31 int colorFrom = 0;
theros 1:ad5c2cfb2002 32
theros 1:ad5c2cfb2002 33 uint8_t ir = 0;
theros 1:ad5c2cfb2002 34 uint8_t ig = 0;
theros 1:ad5c2cfb2002 35 uint8_t ib = 0;
theros 0:de636c7cdfda 36
theros 0:de636c7cdfda 37 ws.useII(WS2812::PER_PIXEL); // use per-pixel intensity scaling
theros 0:de636c7cdfda 38
theros 0:de636c7cdfda 39 // set up the colours we want to draw with
theros 0:de636c7cdfda 40 int colorbuf[NUM_COLORS] = {0x2f0000,0x2f2f00,0x002f00,0x002f2f,0x00002f,0x2f002f};
theros 0:de636c7cdfda 41
theros 0:de636c7cdfda 42 // Now the buffer is written, write it to the led array.
theros 0:de636c7cdfda 43 while (1)
theros 0:de636c7cdfda 44 {
theros 1:ad5c2cfb2002 45 if ((colorIdx + 1) > NUM_COLORS)
theros 1:ad5c2cfb2002 46 {
theros 1:ad5c2cfb2002 47 colorTo = 0;
theros 1:ad5c2cfb2002 48 }
theros 1:ad5c2cfb2002 49 else
theros 1:ad5c2cfb2002 50 {
theros 1:ad5c2cfb2002 51 colorTo = (colorIdx + 1);
theros 1:ad5c2cfb2002 52 }
theros 1:ad5c2cfb2002 53
theros 1:ad5c2cfb2002 54 //get starting RGB components for interpolation
theros 1:ad5c2cfb2002 55 std::size_t c1 = colorbuf[colorFrom];
theros 1:ad5c2cfb2002 56 std::size_t r1 = (c1 & 0xff0000) >> 16;
theros 1:ad5c2cfb2002 57 std::size_t g1 = (c1 & 0x00ff00) >> 8;
theros 1:ad5c2cfb2002 58 std::size_t b1 = (c1 & 0x0000ff);
theros 1:ad5c2cfb2002 59
theros 1:ad5c2cfb2002 60 printf("Starting colors (r,g,b): %d %d %d\n",r1,g1,b1);
theros 0:de636c7cdfda 61
theros 1:ad5c2cfb2002 62 //get ending RGB components for interpolation
theros 1:ad5c2cfb2002 63 std::size_t c2 = colorbuf[colorTo];
theros 1:ad5c2cfb2002 64 std::size_t r2 = (c2 & 0xff0000) >> 16;
theros 1:ad5c2cfb2002 65 std::size_t g2 = (c2 & 0x00ff00) >> 8;
theros 1:ad5c2cfb2002 66 std::size_t b2 = (c2 & 0x0000ff);
theros 1:ad5c2cfb2002 67
theros 1:ad5c2cfb2002 68 printf("Ending colors (r,g,b): %d %d %d\n",r2,g2,b2);
theros 0:de636c7cdfda 69
theros 1:ad5c2cfb2002 70 for (int i = 0; i <= NUM_STEPS; i++)
theros 0:de636c7cdfda 71 {
theros 1:ad5c2cfb2002 72 ir = interpolate(r1, r2, i, NUM_STEPS);
theros 1:ad5c2cfb2002 73 ig = interpolate(g1, g2, i, NUM_STEPS);
theros 1:ad5c2cfb2002 74 ib = interpolate(b1, b2, i, NUM_STEPS);
theros 1:ad5c2cfb2002 75
theros 1:ad5c2cfb2002 76 //write the color value for each pixel
theros 1:ad5c2cfb2002 77 px.SetAll(color_set(ir,ig,ib));
theros 1:ad5c2cfb2002 78
theros 1:ad5c2cfb2002 79 //write the II value for each pixel
theros 1:ad5c2cfb2002 80 px.SetAllI(32);
theros 1:ad5c2cfb2002 81
theros 1:ad5c2cfb2002 82 for (int i = WS2812_BUF; i >= 0; i--)
theros 1:ad5c2cfb2002 83 {
theros 1:ad5c2cfb2002 84 ws.write(px.getBuf());
theros 1:ad5c2cfb2002 85 }
theros 0:de636c7cdfda 86 }
theros 0:de636c7cdfda 87
theros 0:de636c7cdfda 88 colorIdx++;
theros 0:de636c7cdfda 89
theros 0:de636c7cdfda 90 if (colorIdx >= NUM_COLORS)
theros 0:de636c7cdfda 91 {
theros 0:de636c7cdfda 92 colorIdx = 0;
theros 0:de636c7cdfda 93 }
theros 1:ad5c2cfb2002 94
theros 1:ad5c2cfb2002 95 colorFrom = colorIdx;
theros 0:de636c7cdfda 96 }
theros 0:de636c7cdfda 97 }