Displays a gradient between two colors

Dependencies:   PixelArray WS2812 mbed

Files at this revision

API Documentation at this revision

Comitter:
theros
Date:
Sun Mar 12 23:02:59 2017 +0000
Commit message:
First Commit

Changed in this revision

PixelArray.lib Show annotated file Show diff for this revision Revisions of this file
WS2812.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 3bb97062a703 PixelArray.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PixelArray.lib	Sun Mar 12 23:02:59 2017 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/chris/code/PixelArray/#b45a70faaa83
diff -r 000000000000 -r 3bb97062a703 WS2812.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WS2812.lib	Sun Mar 12 23:02:59 2017 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/bridadan/code/WS2812/#6e647820f587
diff -r 000000000000 -r 3bb97062a703 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun Mar 12 23:02:59 2017 +0000
@@ -0,0 +1,67 @@
+#include "mbed.h"
+#include "WS2812.h"
+#include "PixelArray.h"
+
+#define WS2812_BUF 77   //number of LEDs in the array
+#define NUM_COLORS 2    //number of colors to store in the array
+
+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()
+{    
+    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] = {0xff0000,0x0000ff};
+    
+    //get starting RGB components for interpolation
+    std::size_t c1 = colorbuf[0];
+    std::size_t r1 = (c1 & 0xff0000) >> 16;
+    std::size_t g1 = (c1 & 0x00ff00) >> 8;
+    std::size_t b1 = (c1 & 0x0000ff);
+    
+    //get ending RGB components for interpolation
+    std::size_t c2 = colorbuf[1];
+    std::size_t r2 = (c2 & 0xff0000) >> 16;
+    std::size_t g2 = (c2 & 0x00ff00) >> 8;
+    std::size_t b2 = (c2 & 0x0000ff);
+    
+    for (int i = 0; i <= WS2812_BUF; i++)
+    {
+        ir = interpolate(r1, r2, i, WS2812_BUF);
+        ig = interpolate(g1, g2, i, WS2812_BUF);
+        ib = interpolate(b1, b2, i, WS2812_BUF);
+        
+        //write the color value for each pixel
+        px.Set(i, color_set(ir,ig,ib));
+        
+        //write the II value for each pixel
+        px.SetI(i, 32);
+    }
+    
+    for (int i = WS2812_BUF; i >= 0; i--) 
+    {
+        ws.write(px.getBuf());
+    }
+    
+    usrLed = 1;
+}
diff -r 000000000000 -r 3bb97062a703 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sun Mar 12 23:02:59 2017 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/e1686b8d5b90
\ No newline at end of file