RGB LEDS of the neopixels
Dependencies: C12832_lcd PixelArray WS2812 mbed
Fork of WS2812_Example by
main.cpp@5:60a9e2389a9c, 2015-04-21 (annotated)
- Committer:
- cathal66
- Date:
- Tue Apr 21 13:26:10 2015 +0000
- Revision:
- 5:60a9e2389a9c
- Parent:
- 4:7a998a6934f1
function to control the leds;
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" |
cathal66 | 5:60a9e2389a9c | 4 | #include "C12832_lcd.h" |
bridadan | 0:12cb6f0c2788 | 5 | |
cathal66 | 3:d429d33844c9 | 6 | #define WS2812_BUF 3 |
cathal66 | 3:d429d33844c9 | 7 | #define NUM_COLORS 9 |
cathal66 | 3:d429d33844c9 | 8 | #define NUM_LEDS_PER_COLOR 3 |
bridadan | 0:12cb6f0c2788 | 9 | PixelArray px(WS2812_BUF); |
bridadan | 0:12cb6f0c2788 | 10 | |
bridadan | 2:cb82a3dc4031 | 11 | // See the program page for information on the timing numbers |
bridadan | 1:e04a0ecefa29 | 12 | // The given numbers are for the K64F |
cathal66 | 3:d429d33844c9 | 13 | WS2812 ws(p9, WS2812_BUF, 5, 10, 10, 15); |
bridadan | 0:12cb6f0c2788 | 14 | |
cathal66 | 5:60a9e2389a9c | 15 | //LCD Setup |
cathal66 | 5:60a9e2389a9c | 16 | C12832_LCD lcd; |
cathal66 | 5:60a9e2389a9c | 17 | |
cathal66 | 5:60a9e2389a9c | 18 | Serial pc(USBTX, USBRX); // tx, rx |
bridadan | 0:12cb6f0c2788 | 19 | |
cathal66 | 5:60a9e2389a9c | 20 | void LED_Colour(int red , int green ,int blue , int bright) |
cathal66 | 5:60a9e2389a9c | 21 | { |
cathal66 | 5:60a9e2389a9c | 22 | int colorbuf[NUM_COLORS] = {0x000000}; |
cathal66 | 5:60a9e2389a9c | 23 | //int colorbuf[NUM_COLORS] = {0x000000,0x00f0ff,0x00ff00,0x00ffff,0xffffff,0x00ff00,0x00ffff,0x0000ff,0xff00ff}; |
bridadan | 0:12cb6f0c2788 | 24 | |
cathal66 | 5:60a9e2389a9c | 25 | colorbuf[0] = red*0xffff + green*0xff + blue*0x00; |
cathal66 | 3:d429d33844c9 | 26 | |
cathal66 | 5:60a9e2389a9c | 27 | pc.printf("Colour: %x \n\r",colorbuf[0]); |
bridadan | 0:12cb6f0c2788 | 28 | // for each of the colours (j) write out 10 of them |
bridadan | 0:12cb6f0c2788 | 29 | // the pixels are written at the colour*10, plus the colour position |
bridadan | 0:12cb6f0c2788 | 30 | // all modulus 60 so it wraps around |
bridadan | 2:cb82a3dc4031 | 31 | for (int i = 0; i < WS2812_BUF; i++) { |
bridadan | 2:cb82a3dc4031 | 32 | px.Set(i, colorbuf[(i / NUM_LEDS_PER_COLOR) % NUM_COLORS]); |
bridadan | 0:12cb6f0c2788 | 33 | } |
bridadan | 0:12cb6f0c2788 | 34 | |
bridadan | 0:12cb6f0c2788 | 35 | // now all the colours are computed, add a fade effect using intensity scaling |
bridadan | 0:12cb6f0c2788 | 36 | // compute and write the II value for each pixel |
bridadan | 2:cb82a3dc4031 | 37 | for (int j=0; j<WS2812_BUF; j++) { |
bridadan | 0:12cb6f0c2788 | 38 | // px.SetI(pixel position, II value) |
cathal66 | 3:d429d33844c9 | 39 | //px.SetI(j%WS2812_BUF, 0xff+(0xf*(j%NUM_LEDS_PER_COLOR))); //full brightness |
cathal66 | 5:60a9e2389a9c | 40 | px.SetI(j%WS2812_BUF, 0xf +(0xf*(j%NUM_LEDS_PER_COLOR))); //Dim lighting |
cathal66 | 5:60a9e2389a9c | 41 | } |
cathal66 | 5:60a9e2389a9c | 42 | } |
cathal66 | 5:60a9e2389a9c | 43 | int main() |
cathal66 | 5:60a9e2389a9c | 44 | { |
bridadan | 0:12cb6f0c2788 | 45 | |
cathal66 | 5:60a9e2389a9c | 46 | ws.useII(WS2812::PER_PIXEL); // use per-pixel intensity scaling |
cathal66 | 5:60a9e2389a9c | 47 | |
cathal66 | 3:d429d33844c9 | 48 | while (1) { |
cathal66 | 5:60a9e2389a9c | 49 | |
cathal66 | 5:60a9e2389a9c | 50 | LED_Colour(125, 125 ,125 , 125); |
cathal66 | 5:60a9e2389a9c | 51 | wait(1); |
bridadan | 0:12cb6f0c2788 | 52 | } |
bridadan | 0:12cb6f0c2788 | 53 | } |