ws2812b

Dependencies:   PixelArray mbed

Fork of mbed_ws2812b by Yoshitaka Kuwata

Committer:
AamirNiaz
Date:
Tue Nov 01 04:36:49 2016 +0000
Revision:
1:d9c6619240a3
Parent:
0:afb6ebe20c1f
ws2813b sample firmware

Who changed what in which revision?

UserRevisionLine numberNew 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
AamirNiaz 1:d9c6619240a3 30
morecat_lab 0:afb6ebe20c1f 31 void generate(neopixel::Pixel * out, uint32_t index, uintptr_t val) {
morecat_lab 0:afb6ebe20c1f 32 #ifdef ONE_COLOR
morecat_lab 0:afb6ebe20c1f 33 uint32_t pat1 = val / 256;
morecat_lab 0:afb6ebe20c1f 34 #else
morecat_lab 0:afb6ebe20c1f 35 uint32_t pat1 = val / 256 + index;
morecat_lab 0:afb6ebe20c1f 36 #endif
morecat_lab 0:afb6ebe20c1f 37 uint32_t pat2;
morecat_lab 0:afb6ebe20c1f 38 uint32_t seq = val % 256;
morecat_lab 0:afb6ebe20c1f 39
morecat_lab 0:afb6ebe20c1f 40 if (pat1 >= MAX_PATTERN) {
morecat_lab 0:afb6ebe20c1f 41 pat1 = 0;
morecat_lab 0:afb6ebe20c1f 42 }
morecat_lab 0:afb6ebe20c1f 43 pat2 = pat1 + 1;
morecat_lab 0:afb6ebe20c1f 44 if (pat2 >= MAX_PATTERN) {
morecat_lab 0:afb6ebe20c1f 45 pat2 = 0;
morecat_lab 0:afb6ebe20c1f 46 }
morecat_lab 0:afb6ebe20c1f 47 out->red = (pattern[pat1].red * (256-seq) + pattern[pat2].red * seq) /256;
morecat_lab 0:afb6ebe20c1f 48 out->green = (pattern[pat1].green * (256-seq) + pattern[pat2].green * seq) /256;
morecat_lab 0:afb6ebe20c1f 49 out->blue = (pattern[pat1].blue * (256-seq) + pattern[pat2].blue * seq) /256;
morecat_lab 0:afb6ebe20c1f 50 }
morecat_lab 0:afb6ebe20c1f 51
morecat_lab 0:afb6ebe20c1f 52
morecat_lab 0:afb6ebe20c1f 53 int main() {
AamirNiaz 1:d9c6619240a3 54
AamirNiaz 1:d9c6619240a3 55
AamirNiaz 1:d9c6619240a3 56
AamirNiaz 1:d9c6619240a3 57 DigitalIn(P1_22, PullDown);
AamirNiaz 1:d9c6619240a3 58 neopixel::PixelArray array(P1_22);
AamirNiaz 1:d9c6619240a3 59
AamirNiaz 1:d9c6619240a3 60
morecat_lab 0:afb6ebe20c1f 61 uint32_t val = 0;
morecat_lab 0:afb6ebe20c1f 62 while (1) {
morecat_lab 0:afb6ebe20c1f 63 if ((++val) >= MAX_INDEX) {
morecat_lab 0:afb6ebe20c1f 64 val = 0;
morecat_lab 0:afb6ebe20c1f 65 }
morecat_lab 0:afb6ebe20c1f 66 array.update(generate, NLED, val);
morecat_lab 0:afb6ebe20c1f 67 wait_ms(10);
AamirNiaz 1:d9c6619240a3 68
morecat_lab 0:afb6ebe20c1f 69 }
morecat_lab 0:afb6ebe20c1f 70 }