A program that fades between a selection of colors.

Dependencies:   PixelArray WS2812 mbed

Committer:
theros
Date:
Sun Mar 12 22:23:08 2017 +0000
Revision:
2:381421ec57e1
Parent:
1:ad5c2cfb2002
Child:
3:b99f66d3e77e
Corrected assignment of colorFrom/colorTo values at end of each loop

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 2:381421ec57e1 7 #define NUM_STEPS 8 //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 //get starting RGB components for interpolation
theros 1:ad5c2cfb2002 46 std::size_t c1 = colorbuf[colorFrom];
theros 1:ad5c2cfb2002 47 std::size_t r1 = (c1 & 0xff0000) >> 16;
theros 1:ad5c2cfb2002 48 std::size_t g1 = (c1 & 0x00ff00) >> 8;
theros 1:ad5c2cfb2002 49 std::size_t b1 = (c1 & 0x0000ff);
theros 1:ad5c2cfb2002 50
theros 1:ad5c2cfb2002 51 printf("Starting colors (r,g,b): %d %d %d\n",r1,g1,b1);
theros 0:de636c7cdfda 52
theros 1:ad5c2cfb2002 53 //get ending RGB components for interpolation
theros 1:ad5c2cfb2002 54 std::size_t c2 = colorbuf[colorTo];
theros 1:ad5c2cfb2002 55 std::size_t r2 = (c2 & 0xff0000) >> 16;
theros 1:ad5c2cfb2002 56 std::size_t g2 = (c2 & 0x00ff00) >> 8;
theros 1:ad5c2cfb2002 57 std::size_t b2 = (c2 & 0x0000ff);
theros 1:ad5c2cfb2002 58
theros 1:ad5c2cfb2002 59 printf("Ending colors (r,g,b): %d %d %d\n",r2,g2,b2);
theros 0:de636c7cdfda 60
theros 1:ad5c2cfb2002 61 for (int i = 0; i <= NUM_STEPS; i++)
theros 0:de636c7cdfda 62 {
theros 1:ad5c2cfb2002 63 ir = interpolate(r1, r2, i, NUM_STEPS);
theros 1:ad5c2cfb2002 64 ig = interpolate(g1, g2, i, NUM_STEPS);
theros 1:ad5c2cfb2002 65 ib = interpolate(b1, b2, i, NUM_STEPS);
theros 1:ad5c2cfb2002 66
theros 1:ad5c2cfb2002 67 //write the color value for each pixel
theros 1:ad5c2cfb2002 68 px.SetAll(color_set(ir,ig,ib));
theros 1:ad5c2cfb2002 69
theros 1:ad5c2cfb2002 70 //write the II value for each pixel
theros 1:ad5c2cfb2002 71 px.SetAllI(32);
theros 1:ad5c2cfb2002 72
theros 1:ad5c2cfb2002 73 for (int i = WS2812_BUF; i >= 0; i--)
theros 1:ad5c2cfb2002 74 {
theros 1:ad5c2cfb2002 75 ws.write(px.getBuf());
theros 1:ad5c2cfb2002 76 }
theros 0:de636c7cdfda 77 }
theros 0:de636c7cdfda 78
theros 2:381421ec57e1 79 colorFrom = colorIdx;
theros 0:de636c7cdfda 80 colorIdx++;
theros 0:de636c7cdfda 81
theros 0:de636c7cdfda 82 if (colorIdx >= NUM_COLORS)
theros 0:de636c7cdfda 83 {
theros 0:de636c7cdfda 84 colorIdx = 0;
theros 0:de636c7cdfda 85 }
theros 1:ad5c2cfb2002 86
theros 2:381421ec57e1 87 colorTo = colorIdx;
theros 0:de636c7cdfda 88 }
theros 0:de636c7cdfda 89 }