Example for WS2812 Library

Dependencies:   PixelArray WS2812 mbed

Dependents:   Button_Neopixel

This is a demo for the WS2812 LED driver library.

The the timings of the bit banging operation are very sensitive. Depending on the platform, you will need to change the timing values in the WS2812 constructor. The default values in this demo are for the K64F. Here are a few other values for other platforms:

K64F, KL46Z: 0, 5, 5, 0

LPC1768: 5, 10, 10, 15

NUCLEO F411RE: 7, 15, 10, 15

UPDATE 11-29-2016

Thanks to the user Proff for the new measurements!

LPC1768: 3, 11, 10, 11

NUCLEO_F401RE: 3, 12, 9, 12

UPDATE 3-14-2017

Thanks to the user SashaK for the new measurements

NUCELO_F746ZG: 32, 105, 70, 123

If you are curious how to determine these values, see the WS2812 library page below.

Import libraryWS2812

Library for the WS2812 LED Driver. Uses bit banging and nops for precise timing. Number of nops executed are configurable at run time.

.

Files at this revision

API Documentation at this revision

Comitter:
bridadan
Date:
Thu Feb 12 19:20:00 2015 +0000
Child:
1:e04a0ecefa29
Commit message:
Example for WS2812 Library

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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PixelArray.lib	Thu Feb 12 19:20:00 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/chris/code/PixelArray/#b45a70faaa83
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WS2812.lib	Thu Feb 12 19:20:00 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/bridadan/code/WS2812/#0b79cafcb387
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Feb 12 19:20:00 2015 +0000
@@ -0,0 +1,46 @@
+#include "mbed.h"
+#include "WS2812.h"
+#include "PixelArray.h"
+
+#define WS2812_BUF 60
+
+WS2812 ws(D9,WS2812_BUF);
+PixelArray px(WS2812_BUF);
+
+DigitalOut led(LED1);
+
+int main()
+{
+
+    ws.useII(2); // use per-pixel intensity scaling
+    
+    // set up the colours we want to draw with
+    int colorbuf[6] = {0x2f0000,0x2f2f00,0x002f00,0x002f2f,0x00002f,0x2f002f};
+
+    // for each of the colours (j) write out 10 of them
+    // the pixels are written at the colour*10, plus the colour position
+    // all modulus 60 so it wraps around
+    for (int i =0; i<6; i++) {
+        for (int j=0; j<10; j++) {
+            px.Set(((i*10)+j)%60,colorbuf[i]);
+        }
+    }
+
+    // now all the colours are computed, add a fade effect using intensity scaling
+    // compute and write the II value for each pixel
+    for (int j=0; j<60; j++) {
+        // px.SetI(pixel position, II value)
+        px.SetI(j%60, 0xf+(0xf*(j%10)));
+    }
+
+
+    // Now the buffer is written, rotate it
+    // by writing it out with an increasing offset
+    while (1) {
+        for (int z=59; z >= 0 ; z--) {
+            ws.write_offsets(px.getBuf(),z,z,z);
+            wait(0.075);
+        }
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Feb 12 19:20:00 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/e188a91d3eaa
\ No newline at end of file