A program that fades between a selection of colors.

Dependencies:   PixelArray WS2812 mbed

Revision:
1:ad5c2cfb2002
Parent:
0:de636c7cdfda
Child:
2:381421ec57e1
diff -r de636c7cdfda -r ad5c2cfb2002 main.cpp
--- a/main.cpp	Sun Mar 12 14:56:52 2017 +0000
+++ b/main.cpp	Sun Mar 12 16:51:05 2017 +0000
@@ -4,6 +4,7 @@
 
 #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 32    //number of steps between colors
 
 DigitalIn usrBtn(USER_BUTTON);
 DigitalOut usrLed(LED1);
@@ -12,9 +13,26 @@
 // 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
     
@@ -24,15 +42,47 @@
     // Now the buffer is written, write it to the led array.
     while (1) 
     {
-        //write the color value for each pixel
-        px.SetAll(colorbuf[colorIdx]);
+        if ((colorIdx + 1) > NUM_COLORS)
+        {
+            colorTo = 0;
+        }
+        else
+        {
+            colorTo = (colorIdx + 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);
         
-        //write the II value for each pixel
-        px.SetAllI(64);
+        //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 = WS2812_BUF; i >= 0; i--) 
+        for (int i = 0; i <= NUM_STEPS; i++)
         {
-            ws.write(px.getBuf());
+            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());
+            }
         }
         
         colorIdx++;
@@ -41,5 +91,7 @@
         {
             colorIdx = 0;
         }
+        
+        colorFrom = colorIdx;
     }
 }