
A program that fades between a selection of colors.
Dependencies: PixelArray WS2812 mbed
main.cpp
- Committer:
- theros
- Date:
- 2017-03-12
- Revision:
- 2:381421ec57e1
- Parent:
- 1:ad5c2cfb2002
- Child:
- 3:b99f66d3e77e
File content as of revision 2:381421ec57e1:
#include "mbed.h" #include "WS2812.h" #include "PixelArray.h" #define WS2812_BUF 77 //number of LEDs in the array #define NUM_COLORS 6 //number of colors to store in the array #define NUM_STEPS 8 //number of steps between colors DigitalIn usrBtn(USER_BUTTON); DigitalOut usrLed(LED1); PixelArray px(WS2812_BUF); // See the program page for information on the timing numbers WS2812 ws(D9, WS2812_BUF, 6,17,9,14); //nucleo-f411re int color_set(uint8_t red,uint8_t green, uint8_t blue) { return ((red<<16) + (green<<8) + blue); } // 0 <= stepNumber <= lastStepNumber int interpolate(int startValue, int endValue, int stepNumber, int lastStepNumber) { return (endValue - startValue) * stepNumber / lastStepNumber + startValue; } int main() { int colorIdx = 0; int colorTo = 0; int colorFrom = 0; uint8_t ir = 0; uint8_t ig = 0; uint8_t ib = 0; ws.useII(WS2812::PER_PIXEL); // use per-pixel intensity scaling // set up the colours we want to draw with int colorbuf[NUM_COLORS] = {0x2f0000,0x2f2f00,0x002f00,0x002f2f,0x00002f,0x2f002f}; // Now the buffer is written, write it to the led array. while (1) { //get starting RGB components for interpolation std::size_t c1 = colorbuf[colorFrom]; std::size_t r1 = (c1 & 0xff0000) >> 16; std::size_t g1 = (c1 & 0x00ff00) >> 8; std::size_t b1 = (c1 & 0x0000ff); printf("Starting colors (r,g,b): %d %d %d\n",r1,g1,b1); //get ending RGB components for interpolation std::size_t c2 = colorbuf[colorTo]; std::size_t r2 = (c2 & 0xff0000) >> 16; std::size_t g2 = (c2 & 0x00ff00) >> 8; std::size_t b2 = (c2 & 0x0000ff); printf("Ending colors (r,g,b): %d %d %d\n",r2,g2,b2); for (int i = 0; i <= NUM_STEPS; i++) { ir = interpolate(r1, r2, i, NUM_STEPS); ig = interpolate(g1, g2, i, NUM_STEPS); ib = interpolate(b1, b2, i, NUM_STEPS); //write the color value for each pixel px.SetAll(color_set(ir,ig,ib)); //write the II value for each pixel px.SetAllI(32); for (int i = WS2812_BUF; i >= 0; i--) { ws.write(px.getBuf()); } } colorFrom = colorIdx; colorIdx++; if (colorIdx >= NUM_COLORS) { colorIdx = 0; } colorTo = colorIdx; } }