A program that fades between a selection of colors.

Dependencies:   PixelArray WS2812 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "WS2812.h"
00003 #include "PixelArray.h"
00004 
00005 #define WS2812_BUF 77   //number of LEDs in the array
00006 #define NUM_COLORS 6    //number of colors to store in the array
00007 #define NUM_STEPS 8    //number of steps between colors
00008 
00009 DigitalOut usrLed(LED1);
00010 PixelArray px(WS2812_BUF);
00011 
00012 // See the program page for information on the timing numbers
00013 WS2812 ws(D9, WS2812_BUF, 6,17,9,14);   //nucleo-f411re
00014 
00015 int color_set(uint8_t red,uint8_t green, uint8_t blue)
00016 {
00017   return ((red<<16) + (green<<8) + blue);   
00018 }
00019 
00020 // 0 <= stepNumber <= lastStepNumber
00021 int interpolate(int startValue, int endValue, int stepNumber, int lastStepNumber)
00022 {
00023     return (endValue - startValue) * stepNumber / lastStepNumber + startValue;
00024 }
00025 
00026 int main()
00027 {
00028     int colorIdx = 0;
00029     int colorTo = 0;
00030     int colorFrom = 0;
00031     
00032     uint8_t ir = 0;
00033     uint8_t ig = 0;
00034     uint8_t ib = 0;
00035     
00036     ws.useII(WS2812::PER_PIXEL); // use per-pixel intensity scaling
00037     
00038     // set up the colours we want to draw with
00039     int colorbuf[NUM_COLORS] = {0x2f0000,0x2f2f00,0x002f00,0x002f2f,0x00002f,0x2f002f};
00040     
00041     // Now the buffer is written, write it to the led array.
00042     while (1) 
00043     {
00044         //get starting RGB components for interpolation
00045         std::size_t c1 = colorbuf[colorFrom];
00046         std::size_t r1 = (c1 & 0xff0000) >> 16;
00047         std::size_t g1 = (c1 & 0x00ff00) >> 8;
00048         std::size_t b1 = (c1 & 0x0000ff);
00049         
00050         //get ending RGB components for interpolation
00051         std::size_t c2 = colorbuf[colorTo];
00052         std::size_t r2 = (c2 & 0xff0000) >> 16;
00053         std::size_t g2 = (c2 & 0x00ff00) >> 8;
00054         std::size_t b2 = (c2 & 0x0000ff);
00055         
00056         for (int i = 0; i <= NUM_STEPS; i++)
00057         {
00058             ir = interpolate(r1, r2, i, NUM_STEPS);
00059             ig = interpolate(g1, g2, i, NUM_STEPS);
00060             ib = interpolate(b1, b2, i, NUM_STEPS);
00061             
00062             //write the color value for each pixel
00063             px.SetAll(color_set(ir,ig,ib));
00064             
00065             //write the II value for each pixel
00066             px.SetAllI(32);
00067             
00068             for (int i = WS2812_BUF; i >= 0; i--) 
00069             {
00070                 ws.write(px.getBuf());
00071             }
00072         }
00073         
00074         colorFrom = colorIdx;
00075         colorIdx++;
00076         
00077         if (colorIdx >= NUM_COLORS)
00078         {
00079             colorIdx = 0;
00080         }
00081         
00082         colorTo = colorIdx;
00083     }
00084 }