![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
ws2812b sample
Dependencies: PixelArray mbed
Fork of mbed_ws2812b by
main.cpp@0:afb6ebe20c1f, 2014-12-13 (annotated)
- Committer:
- morecat_lab
- Date:
- Sat Dec 13 06:58:47 2014 +0000
- Revision:
- 0:afb6ebe20c1f
- Child:
- 1:d9c6619240a3
1st release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
morecat_lab | 0:afb6ebe20c1f | 1 | /* |
morecat_lab | 0:afb6ebe20c1f | 2 | |
morecat_lab | 0:afb6ebe20c1f | 3 | Neopixel LED controller for mbed LPC1114 (RAMdaccha) |
morecat_lab | 0:afb6ebe20c1f | 4 | |
morecat_lab | 0:afb6ebe20c1f | 5 | 2014/12/13 by morecat_lab |
morecat_lab | 0:afb6ebe20c1f | 6 | |
morecat_lab | 0:afb6ebe20c1f | 7 | This program depends on neopixel library. |
morecat_lab | 0:afb6ebe20c1f | 8 | http://developer.mbed.org/users/JacobBramley/code/PixelArray/ |
morecat_lab | 0:afb6ebe20c1f | 9 | |
morecat_lab | 0:afb6ebe20c1f | 10 | */ |
morecat_lab | 0:afb6ebe20c1f | 11 | #include "mbed.h" |
morecat_lab | 0:afb6ebe20c1f | 12 | #include "neopixel.h" |
morecat_lab | 0:afb6ebe20c1f | 13 | #include "LAMd.h" |
morecat_lab | 0:afb6ebe20c1f | 14 | |
morecat_lab | 0:afb6ebe20c1f | 15 | neopixel::Pixel pattern[] = { |
morecat_lab | 0:afb6ebe20c1f | 16 | {0x55, 0, 0xff}, // magenta |
morecat_lab | 0:afb6ebe20c1f | 17 | {0, 0, 0xff}, // blue |
morecat_lab | 0:afb6ebe20c1f | 18 | {0xaf, 0xaf, 0xaf}, // white |
morecat_lab | 0:afb6ebe20c1f | 19 | {0, 0xff, 0xff}, // cyan |
morecat_lab | 0:afb6ebe20c1f | 20 | {0xaf, 0xaf, 0}, // yellow |
morecat_lab | 0:afb6ebe20c1f | 21 | {0, 0xff, 0}, // greem |
morecat_lab | 0:afb6ebe20c1f | 22 | {0x2f, 0, 0}, // red |
morecat_lab | 0:afb6ebe20c1f | 23 | }; |
morecat_lab | 0:afb6ebe20c1f | 24 | |
morecat_lab | 0:afb6ebe20c1f | 25 | #define MAX_PATTERN (sizeof pattern / sizeof (struct neopixel::Pixel)) |
morecat_lab | 0:afb6ebe20c1f | 26 | #define MAX_INDEX (MAX_PATTERN *256) |
morecat_lab | 0:afb6ebe20c1f | 27 | #define NLED (4) |
morecat_lab | 0:afb6ebe20c1f | 28 | #define ONE_COLOR |
morecat_lab | 0:afb6ebe20c1f | 29 | |
morecat_lab | 0:afb6ebe20c1f | 30 | void generate(neopixel::Pixel * out, uint32_t index, uintptr_t val) { |
morecat_lab | 0:afb6ebe20c1f | 31 | #ifdef ONE_COLOR |
morecat_lab | 0:afb6ebe20c1f | 32 | uint32_t pat1 = val / 256; |
morecat_lab | 0:afb6ebe20c1f | 33 | #else |
morecat_lab | 0:afb6ebe20c1f | 34 | uint32_t pat1 = val / 256 + index; |
morecat_lab | 0:afb6ebe20c1f | 35 | #endif |
morecat_lab | 0:afb6ebe20c1f | 36 | uint32_t pat2; |
morecat_lab | 0:afb6ebe20c1f | 37 | uint32_t seq = val % 256; |
morecat_lab | 0:afb6ebe20c1f | 38 | |
morecat_lab | 0:afb6ebe20c1f | 39 | if (pat1 >= MAX_PATTERN) { |
morecat_lab | 0:afb6ebe20c1f | 40 | pat1 = 0; |
morecat_lab | 0:afb6ebe20c1f | 41 | } |
morecat_lab | 0:afb6ebe20c1f | 42 | pat2 = pat1 + 1; |
morecat_lab | 0:afb6ebe20c1f | 43 | if (pat2 >= MAX_PATTERN) { |
morecat_lab | 0:afb6ebe20c1f | 44 | pat2 = 0; |
morecat_lab | 0:afb6ebe20c1f | 45 | } |
morecat_lab | 0:afb6ebe20c1f | 46 | out->red = (pattern[pat1].red * (256-seq) + pattern[pat2].red * seq) /256; |
morecat_lab | 0:afb6ebe20c1f | 47 | out->green = (pattern[pat1].green * (256-seq) + pattern[pat2].green * seq) /256; |
morecat_lab | 0:afb6ebe20c1f | 48 | out->blue = (pattern[pat1].blue * (256-seq) + pattern[pat2].blue * seq) /256; |
morecat_lab | 0:afb6ebe20c1f | 49 | } |
morecat_lab | 0:afb6ebe20c1f | 50 | |
morecat_lab | 0:afb6ebe20c1f | 51 | |
morecat_lab | 0:afb6ebe20c1f | 52 | int main() { |
morecat_lab | 0:afb6ebe20c1f | 53 | DigitalIn(D3, PullDown); |
morecat_lab | 0:afb6ebe20c1f | 54 | neopixel::PixelArray array(D3); |
morecat_lab | 0:afb6ebe20c1f | 55 | |
morecat_lab | 0:afb6ebe20c1f | 56 | uint32_t val = 0; |
morecat_lab | 0:afb6ebe20c1f | 57 | while (1) { |
morecat_lab | 0:afb6ebe20c1f | 58 | if ((++val) >= MAX_INDEX) { |
morecat_lab | 0:afb6ebe20c1f | 59 | val = 0; |
morecat_lab | 0:afb6ebe20c1f | 60 | } |
morecat_lab | 0:afb6ebe20c1f | 61 | array.update(generate, NLED, val); |
morecat_lab | 0:afb6ebe20c1f | 62 | wait_ms(10); |
morecat_lab | 0:afb6ebe20c1f | 63 | } |
morecat_lab | 0:afb6ebe20c1f | 64 | } |