Maximilian Erlbeck
/
WS2812-Example
WS2812 example
main.cpp@0:52571581ff7e, 2015-11-18 (annotated)
- Committer:
- fastestoffer
- Date:
- Wed Nov 18 18:11:40 2015 +0000
- Revision:
- 0:52571581ff7e
Initial commit of example;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fastestoffer | 0:52571581ff7e | 1 | #include "mbed.h" |
fastestoffer | 0:52571581ff7e | 2 | #include "WS2812.h" |
fastestoffer | 0:52571581ff7e | 3 | |
fastestoffer | 0:52571581ff7e | 4 | LowPowerTicker toggleTicker; |
fastestoffer | 0:52571581ff7e | 5 | |
fastestoffer | 0:52571581ff7e | 6 | Color color_red = {255, 0, 0}; |
fastestoffer | 0:52571581ff7e | 7 | Color color_green = {0, 255, 0}; |
fastestoffer | 0:52571581ff7e | 8 | Color color_blue = {0, 0, 255}; |
fastestoffer | 0:52571581ff7e | 9 | Color color_black = {0,0,0}; |
fastestoffer | 0:52571581ff7e | 10 | |
fastestoffer | 0:52571581ff7e | 11 | bool update_led_flag; |
fastestoffer | 0:52571581ff7e | 12 | bool ledstrip_busy; |
fastestoffer | 0:52571581ff7e | 13 | int state = 0; |
fastestoffer | 0:52571581ff7e | 14 | |
fastestoffer | 0:52571581ff7e | 15 | void update(void) { |
fastestoffer | 0:52571581ff7e | 16 | update_led_flag = true; |
fastestoffer | 0:52571581ff7e | 17 | } |
fastestoffer | 0:52571581ff7e | 18 | |
fastestoffer | 0:52571581ff7e | 19 | void send_complete(int event) { |
fastestoffer | 0:52571581ff7e | 20 | (void)event; |
fastestoffer | 0:52571581ff7e | 21 | ledstrip_busy = false; |
fastestoffer | 0:52571581ff7e | 22 | } |
fastestoffer | 0:52571581ff7e | 23 | |
fastestoffer | 0:52571581ff7e | 24 | event_callback_t callback; |
fastestoffer | 0:52571581ff7e | 25 | |
fastestoffer | 0:52571581ff7e | 26 | int main(void) { |
fastestoffer | 0:52571581ff7e | 27 | |
fastestoffer | 0:52571581ff7e | 28 | SPI spi(PE10, NC, PE12); |
fastestoffer | 0:52571581ff7e | 29 | |
fastestoffer | 0:52571581ff7e | 30 | callback.attach(send_complete); |
fastestoffer | 0:52571581ff7e | 31 | |
fastestoffer | 0:52571581ff7e | 32 | WS2812 ledstrip(spi, callback); |
fastestoffer | 0:52571581ff7e | 33 | |
fastestoffer | 0:52571581ff7e | 34 | toggleTicker.attach(&update, 1.0f); |
fastestoffer | 0:52571581ff7e | 35 | |
fastestoffer | 0:52571581ff7e | 36 | while(1) { |
fastestoffer | 0:52571581ff7e | 37 | sleep(); |
fastestoffer | 0:52571581ff7e | 38 | if(update_led_flag) |
fastestoffer | 0:52571581ff7e | 39 | { |
fastestoffer | 0:52571581ff7e | 40 | update_led_flag=false; |
fastestoffer | 0:52571581ff7e | 41 | switch(state){ |
fastestoffer | 0:52571581ff7e | 42 | case 0: |
fastestoffer | 0:52571581ff7e | 43 | ledstrip.set_all(&color_green); |
fastestoffer | 0:52571581ff7e | 44 | state++; |
fastestoffer | 0:52571581ff7e | 45 | break; |
fastestoffer | 0:52571581ff7e | 46 | case 1: |
fastestoffer | 0:52571581ff7e | 47 | ledstrip.set_all(&color_blue); |
fastestoffer | 0:52571581ff7e | 48 | state++; |
fastestoffer | 0:52571581ff7e | 49 | break; |
fastestoffer | 0:52571581ff7e | 50 | case 2: |
fastestoffer | 0:52571581ff7e | 51 | ledstrip.set_all(&color_red); |
fastestoffer | 0:52571581ff7e | 52 | state=0; |
fastestoffer | 0:52571581ff7e | 53 | break; |
fastestoffer | 0:52571581ff7e | 54 | } |
fastestoffer | 0:52571581ff7e | 55 | ledstrip_busy = true; |
fastestoffer | 0:52571581ff7e | 56 | ledstrip.update_strip(); |
fastestoffer | 0:52571581ff7e | 57 | } |
fastestoffer | 0:52571581ff7e | 58 | } |
fastestoffer | 0:52571581ff7e | 59 | } |