![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
RGB LEDS of the neopixels
Dependencies: C12832_lcd PixelArray WS2812 mbed
Fork of WS2812_Example by
main.cpp@3:d429d33844c9, 2015-04-15 (annotated)
- Committer:
- cathal66
- Date:
- Wed Apr 15 17:58:55 2015 +0000
- Revision:
- 3:d429d33844c9
- Parent:
- 2:cb82a3dc4031
- Child:
- 4:7a998a6934f1
changing between RGB colour;
Who changed what in which revision?
User | Revision | Line number | New 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 | |
cathal66 | 3:d429d33844c9 | 5 | #define WS2812_BUF 3 |
cathal66 | 3:d429d33844c9 | 6 | #define NUM_COLORS 9 |
cathal66 | 3:d429d33844c9 | 7 | #define NUM_LEDS_PER_COLOR 3 |
bridadan | 0:12cb6f0c2788 | 8 | PixelArray px(WS2812_BUF); |
bridadan | 0:12cb6f0c2788 | 9 | |
bridadan | 2:cb82a3dc4031 | 10 | // See the program page for information on the timing numbers |
bridadan | 1:e04a0ecefa29 | 11 | // The given numbers are for the K64F |
cathal66 | 3:d429d33844c9 | 12 | WS2812 ws(p9, WS2812_BUF, 5, 10, 10, 15); |
bridadan | 0:12cb6f0c2788 | 13 | |
bridadan | 0:12cb6f0c2788 | 14 | int main() |
bridadan | 0:12cb6f0c2788 | 15 | { |
bridadan | 0:12cb6f0c2788 | 16 | |
bridadan | 1:e04a0ecefa29 | 17 | ws.useII(WS2812::PER_PIXEL); // use per-pixel intensity scaling |
bridadan | 0:12cb6f0c2788 | 18 | |
bridadan | 0:12cb6f0c2788 | 19 | // set up the colours we want to draw with |
cathal66 | 3:d429d33844c9 | 20 | int colorbuf[NUM_COLORS] = {0x000000};//,0x00f0ff,0x00ff00,0x00ffff,0xffffff,0x00ff00,0x00ffff,0x0000ff,0xff00ff}; |
cathal66 | 3:d429d33844c9 | 21 | |
cathal66 | 3:d429d33844c9 | 22 | |
cathal66 | 3:d429d33844c9 | 23 | |
bridadan | 0:12cb6f0c2788 | 24 | // for each of the colours (j) write out 10 of them |
bridadan | 0:12cb6f0c2788 | 25 | // the pixels are written at the colour*10, plus the colour position |
bridadan | 0:12cb6f0c2788 | 26 | // all modulus 60 so it wraps around |
bridadan | 2:cb82a3dc4031 | 27 | for (int i = 0; i < WS2812_BUF; i++) { |
bridadan | 2:cb82a3dc4031 | 28 | px.Set(i, colorbuf[(i / NUM_LEDS_PER_COLOR) % NUM_COLORS]); |
bridadan | 0:12cb6f0c2788 | 29 | } |
bridadan | 0:12cb6f0c2788 | 30 | |
bridadan | 0:12cb6f0c2788 | 31 | // now all the colours are computed, add a fade effect using intensity scaling |
bridadan | 0:12cb6f0c2788 | 32 | // compute and write the II value for each pixel |
bridadan | 2:cb82a3dc4031 | 33 | for (int j=0; j<WS2812_BUF; j++) { |
bridadan | 0:12cb6f0c2788 | 34 | // px.SetI(pixel position, II value) |
cathal66 | 3:d429d33844c9 | 35 | //px.SetI(j%WS2812_BUF, 0xff+(0xf*(j%NUM_LEDS_PER_COLOR))); //full brightness |
cathal66 | 3:d429d33844c9 | 36 | px.SetI(j%WS2812_BUF, 0xf+(0xf*(j%NUM_LEDS_PER_COLOR))); //Dim lighting |
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 |
cathal66 | 3:d429d33844c9 | 42 | while (1) { |
cathal66 | 3:d429d33844c9 | 43 | if(colorbuf[0]>=0xfffffe) |
cathal66 | 3:d429d33844c9 | 44 | { |
cathal66 | 3:d429d33844c9 | 45 | colorbuf[0]=0x000000; |
cathal66 | 3:d429d33844c9 | 46 | } |
cathal66 | 3:d429d33844c9 | 47 | |
cathal66 | 3:d429d33844c9 | 48 | if(colorbuf[0]<=0x0000ff) |
cathal66 | 3:d429d33844c9 | 49 | { |
cathal66 | 3:d429d33844c9 | 50 | colorbuf[0]=colorbuf[0]+0x00000f; |
cathal66 | 3:d429d33844c9 | 51 | //colorbuf[0] = 0x000000; |
cathal66 | 3:d429d33844c9 | 52 | } |
cathal66 | 3:d429d33844c9 | 53 | else if(colorbuf[0]<=0x00ffff) |
cathal66 | 3:d429d33844c9 | 54 | { |
cathal66 | 3:d429d33844c9 | 55 | colorbuf[0]=colorbuf[0]+0x000f00; |
cathal66 | 3:d429d33844c9 | 56 | } |
cathal66 | 3:d429d33844c9 | 57 | else if(colorbuf[0]<=0xfffffe) |
cathal66 | 3:d429d33844c9 | 58 | { |
cathal66 | 3:d429d33844c9 | 59 | colorbuf[0]=colorbuf[0]+0x0f0000; |
cathal66 | 3:d429d33844c9 | 60 | } |
cathal66 | 3:d429d33844c9 | 61 | |
cathal66 | 3:d429d33844c9 | 62 | |
cathal66 | 3:d429d33844c9 | 63 | |
cathal66 | 3:d429d33844c9 | 64 | |
cathal66 | 3:d429d33844c9 | 65 | for (int i = 0; i < WS2812_BUF; i++) { |
cathal66 | 3:d429d33844c9 | 66 | px.Set(i, colorbuf[(i / NUM_LEDS_PER_COLOR) % NUM_COLORS]); |
cathal66 | 3:d429d33844c9 | 67 | px.SetI(i%WS2812_BUF, 0xf+(0xf*(i%NUM_LEDS_PER_COLOR))); //Dim lighting |
cathal66 | 3:d429d33844c9 | 68 | } |
bridadan | 2:cb82a3dc4031 | 69 | for (int z=WS2812_BUF; z >= 0 ; z--) { |
bridadan | 0:12cb6f0c2788 | 70 | ws.write_offsets(px.getBuf(),z,z,z); |
cathal66 | 3:d429d33844c9 | 71 | //wait(0.5); |
cathal66 | 3:d429d33844c9 | 72 | |
cathal66 | 3:d429d33844c9 | 73 | } |
cathal66 | 3:d429d33844c9 | 74 | wait(0.1); |
bridadan | 0:12cb6f0c2788 | 75 | } |
bridadan | 0:12cb6f0c2788 | 76 | } |