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.

.

Committer:
bridadan
Date:
Thu Feb 12 20:20:54 2015 +0000
Revision:
1:e04a0ecefa29
Parent:
0:12cb6f0c2788
Child:
2:cb82a3dc4031
Removed platform dependent code from library. Cleaned up code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bridadan 0:12cb6f0c2788 1 #include "mbed.h"
bridadan 0:12cb6f0c2788 2 #include "WS2812.h"
bridadan 0:12cb6f0c2788 3 #include "PixelArray.h"
bridadan 0:12cb6f0c2788 4
bridadan 0:12cb6f0c2788 5 #define WS2812_BUF 60
bridadan 0:12cb6f0c2788 6
bridadan 0:12cb6f0c2788 7 PixelArray px(WS2812_BUF);
bridadan 0:12cb6f0c2788 8
bridadan 1:e04a0ecefa29 9 // See the program page for information on the timing numbers (eg: 0, 5, 5, 0)
bridadan 1:e04a0ecefa29 10 // The given numbers are for the K64F
bridadan 1:e04a0ecefa29 11 WS2812 ws(D9,WS2812_BUF, 0, 5, 5, 0);
bridadan 1:e04a0ecefa29 12
bridadan 0:12cb6f0c2788 13 DigitalOut led(LED1);
bridadan 0:12cb6f0c2788 14
bridadan 0:12cb6f0c2788 15 int main()
bridadan 0:12cb6f0c2788 16 {
bridadan 0:12cb6f0c2788 17
bridadan 1:e04a0ecefa29 18 ws.useII(WS2812::PER_PIXEL); // use per-pixel intensity scaling
bridadan 0:12cb6f0c2788 19
bridadan 0:12cb6f0c2788 20 // set up the colours we want to draw with
bridadan 0:12cb6f0c2788 21 int colorbuf[6] = {0x2f0000,0x2f2f00,0x002f00,0x002f2f,0x00002f,0x2f002f};
bridadan 0:12cb6f0c2788 22
bridadan 0:12cb6f0c2788 23 // for each of the colours (j) write out 10 of them
bridadan 0:12cb6f0c2788 24 // the pixels are written at the colour*10, plus the colour position
bridadan 0:12cb6f0c2788 25 // all modulus 60 so it wraps around
bridadan 0:12cb6f0c2788 26 for (int i =0; i<6; i++) {
bridadan 0:12cb6f0c2788 27 for (int j=0; j<10; j++) {
bridadan 0:12cb6f0c2788 28 px.Set(((i*10)+j)%60,colorbuf[i]);
bridadan 0:12cb6f0c2788 29 }
bridadan 0:12cb6f0c2788 30 }
bridadan 0:12cb6f0c2788 31
bridadan 0:12cb6f0c2788 32 // now all the colours are computed, add a fade effect using intensity scaling
bridadan 0:12cb6f0c2788 33 // compute and write the II value for each pixel
bridadan 0:12cb6f0c2788 34 for (int j=0; j<60; j++) {
bridadan 0:12cb6f0c2788 35 // px.SetI(pixel position, II value)
bridadan 0:12cb6f0c2788 36 px.SetI(j%60, 0xf+(0xf*(j%10)));
bridadan 0:12cb6f0c2788 37 }
bridadan 0:12cb6f0c2788 38
bridadan 0:12cb6f0c2788 39
bridadan 0:12cb6f0c2788 40 // Now the buffer is written, rotate it
bridadan 0:12cb6f0c2788 41 // by writing it out with an increasing offset
bridadan 0:12cb6f0c2788 42 while (1) {
bridadan 0:12cb6f0c2788 43 for (int z=59; z >= 0 ; z--) {
bridadan 0:12cb6f0c2788 44 ws.write_offsets(px.getBuf(),z,z,z);
bridadan 0:12cb6f0c2788 45 wait(0.075);
bridadan 0:12cb6f0c2788 46 }
bridadan 0:12cb6f0c2788 47 }
bridadan 0:12cb6f0c2788 48
bridadan 0:12cb6f0c2788 49 }