Example for WS2812 Library

Dependencies:   PixelArray WS2812 mbed

Dependents:   Button_Neopixel

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 150
00006 #define NUM_COLORS 6
00007 #define NUM_LEDS_PER_COLOR 10
00008 
00009 PixelArray px(WS2812_BUF);
00010 
00011 // See the program page for information on the timing numbers
00012 // The given numbers are for the K64F
00013 WS2812 ws(D9, WS2812_BUF, 0, 5, 5, 0);
00014 
00015 int main()
00016 {
00017 
00018     ws.useII(WS2812::PER_PIXEL); // use per-pixel intensity scaling
00019     
00020     // set up the colours we want to draw with
00021     int colorbuf[NUM_COLORS] = {0x2f0000,0x2f2f00,0x002f00,0x002f2f,0x00002f,0x2f002f};
00022 
00023     // for each of the colours (j) write out 10 of them
00024     // the pixels are written at the colour*10, plus the colour position
00025     // all modulus 60 so it wraps around
00026     for (int i = 0; i < WS2812_BUF; i++) {
00027         px.Set(i, colorbuf[(i / NUM_LEDS_PER_COLOR) % NUM_COLORS]);
00028     }
00029 
00030     // now all the colours are computed, add a fade effect using intensity scaling
00031     // compute and write the II value for each pixel
00032     for (int j=0; j<WS2812_BUF; j++) {
00033         // px.SetI(pixel position, II value)
00034         px.SetI(j%WS2812_BUF, 0xf+(0xf*(j%NUM_LEDS_PER_COLOR)));
00035     }
00036 
00037 
00038     // Now the buffer is written, rotate it
00039     // by writing it out with an increasing offset
00040     while (1) {
00041         for (int z=WS2812_BUF; z >= 0 ; z--) {
00042             ws.write_offsets(px.getBuf(),z,z,z);
00043             wait(0.075);
00044         }
00045     }
00046 
00047 }