Code to control an individually addressable RGB strip.

Dependencies:   mbed WS2812 PixelArray

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 30
00006 #define NUM_COLORS 6
00007 #define NUM_LEDS_PER_COLOR 1
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, 3, 11, 10, 11);
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] = {0xfffff0,0xffff00,0xfff000,0xff0000,0xf00000,0xffffff};
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, 0x0f);
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 }