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