Code to control an individually addressable RGB strip.
Dependencies: mbed WS2812 PixelArray
main.cpp@0:b47973ee96a9, 2019-12-14 (annotated)
- Committer:
- ahdyer
- Date:
- Sat Dec 14 16:57:40 2019 +0000
- Revision:
- 0:b47973ee96a9
Code to control individually addressable RGB strips
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ahdyer | 0:b47973ee96a9 | 1 | #include "mbed.h" |
ahdyer | 0:b47973ee96a9 | 2 | #include "WS2812.h" |
ahdyer | 0:b47973ee96a9 | 3 | #include "PixelArray.h" |
ahdyer | 0:b47973ee96a9 | 4 | |
ahdyer | 0:b47973ee96a9 | 5 | #define WS2812_BUF 30 |
ahdyer | 0:b47973ee96a9 | 6 | #define NUM_COLORS 6 |
ahdyer | 0:b47973ee96a9 | 7 | #define NUM_LEDS_PER_COLOR 1 |
ahdyer | 0:b47973ee96a9 | 8 | |
ahdyer | 0:b47973ee96a9 | 9 | PixelArray px(WS2812_BUF); |
ahdyer | 0:b47973ee96a9 | 10 | |
ahdyer | 0:b47973ee96a9 | 11 | // See the program page for information on the timing numbers |
ahdyer | 0:b47973ee96a9 | 12 | // The given numbers are for the K64F |
ahdyer | 0:b47973ee96a9 | 13 | WS2812 ws(D9, WS2812_BUF, 3, 11, 10, 11); |
ahdyer | 0:b47973ee96a9 | 14 | |
ahdyer | 0:b47973ee96a9 | 15 | int main() |
ahdyer | 0:b47973ee96a9 | 16 | { |
ahdyer | 0:b47973ee96a9 | 17 | |
ahdyer | 0:b47973ee96a9 | 18 | ws.useII(WS2812::PER_PIXEL); // use per-pixel intensity scaling |
ahdyer | 0:b47973ee96a9 | 19 | |
ahdyer | 0:b47973ee96a9 | 20 | // set up the colours we want to draw with |
ahdyer | 0:b47973ee96a9 | 21 | int colorbuf[NUM_COLORS] = {0xfffff0,0xffff00,0xfff000,0xff0000,0xf00000,0xffffff}; |
ahdyer | 0:b47973ee96a9 | 22 | |
ahdyer | 0:b47973ee96a9 | 23 | // for each of the colours (j) write out 10 of them |
ahdyer | 0:b47973ee96a9 | 24 | // the pixels are written at the colour*10, plus the colour position |
ahdyer | 0:b47973ee96a9 | 25 | // all modulus 60 so it wraps around |
ahdyer | 0:b47973ee96a9 | 26 | for (int i = 0; i < WS2812_BUF; i++) { |
ahdyer | 0:b47973ee96a9 | 27 | px.Set(i, colorbuf[(i / NUM_LEDS_PER_COLOR) % NUM_COLORS]); |
ahdyer | 0:b47973ee96a9 | 28 | } |
ahdyer | 0:b47973ee96a9 | 29 | |
ahdyer | 0:b47973ee96a9 | 30 | // now all the colours are computed, add a fade effect using intensity scaling |
ahdyer | 0:b47973ee96a9 | 31 | // compute and write the II value for each pixel |
ahdyer | 0:b47973ee96a9 | 32 | for (int j=0; j<WS2812_BUF; j++) { |
ahdyer | 0:b47973ee96a9 | 33 | // px.SetI(pixel position, II value) |
ahdyer | 0:b47973ee96a9 | 34 | px.SetI(j%WS2812_BUF, 0x0f); |
ahdyer | 0:b47973ee96a9 | 35 | } |
ahdyer | 0:b47973ee96a9 | 36 | |
ahdyer | 0:b47973ee96a9 | 37 | |
ahdyer | 0:b47973ee96a9 | 38 | // Now the buffer is written, rotate it |
ahdyer | 0:b47973ee96a9 | 39 | // by writing it out with an increasing offset |
ahdyer | 0:b47973ee96a9 | 40 | while (1) { |
ahdyer | 0:b47973ee96a9 | 41 | for (int z=WS2812_BUF; z >= 0 ; z--) { |
ahdyer | 0:b47973ee96a9 | 42 | ws.write_offsets(px.getBuf(),z,z,z); |
ahdyer | 0:b47973ee96a9 | 43 | wait(0.075); |
ahdyer | 0:b47973ee96a9 | 44 | } |
ahdyer | 0:b47973ee96a9 | 45 | } |
ahdyer | 0:b47973ee96a9 | 46 | |
ahdyer | 0:b47973ee96a9 | 47 | } |