Cycles through colors set in a buffer when the user button is pressed.

Dependencies:   PixelArray WS2812 mbed

Committer:
theros
Date:
Sat Mar 11 22:07:13 2017 +0000
Revision:
0:d258c72c1ae2
First version.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
theros 0:d258c72c1ae2 1 #include "mbed.h"
theros 0:d258c72c1ae2 2 #include "WS2812.h"
theros 0:d258c72c1ae2 3 #include "PixelArray.h"
theros 0:d258c72c1ae2 4
theros 0:d258c72c1ae2 5 #define WS2812_BUF 77 //number of LEDs in the array
theros 0:d258c72c1ae2 6 #define NUM_COLORS 6 //number of colors to store in the array
theros 0:d258c72c1ae2 7
theros 0:d258c72c1ae2 8 DigitalIn usrBtn(USER_BUTTON);
theros 0:d258c72c1ae2 9 DigitalOut usrLed(LED1);
theros 0:d258c72c1ae2 10 PixelArray px(WS2812_BUF);
theros 0:d258c72c1ae2 11
theros 0:d258c72c1ae2 12 // See the program page for information on the timing numbers
theros 0:d258c72c1ae2 13 WS2812 ws(D9, WS2812_BUF, 6,17,9,14); //nucleo-f411re
theros 0:d258c72c1ae2 14
theros 0:d258c72c1ae2 15 int main()
theros 0:d258c72c1ae2 16 {
theros 0:d258c72c1ae2 17 int btnState = 0;
theros 0:d258c72c1ae2 18
theros 0:d258c72c1ae2 19 ws.useII(WS2812::PER_PIXEL); // use per-pixel intensity scaling
theros 0:d258c72c1ae2 20
theros 0:d258c72c1ae2 21 // set up the colours we want to draw with
theros 0:d258c72c1ae2 22 int colorbuf[NUM_COLORS] = {0x2f0000,0x2f2f00,0x002f00,0x002f2f,0x00002f,0x2f002f};
theros 0:d258c72c1ae2 23
theros 0:d258c72c1ae2 24 // Now the buffer is written, write it to the led array.
theros 0:d258c72c1ae2 25 while (1)
theros 0:d258c72c1ae2 26 {
theros 0:d258c72c1ae2 27 if (usrBtn == 0) //button is pressed
theros 0:d258c72c1ae2 28 {
theros 0:d258c72c1ae2 29 usrLed = 1;
theros 0:d258c72c1ae2 30 btnState = btnState++;
theros 0:d258c72c1ae2 31
theros 0:d258c72c1ae2 32 if (btnState == NUM_COLORS) {
theros 0:d258c72c1ae2 33 btnState = 0;
theros 0:d258c72c1ae2 34 }
theros 0:d258c72c1ae2 35 }
theros 0:d258c72c1ae2 36 else
theros 0:d258c72c1ae2 37 {
theros 0:d258c72c1ae2 38 usrLed = 0;
theros 0:d258c72c1ae2 39 }
theros 0:d258c72c1ae2 40
theros 0:d258c72c1ae2 41 //write the color value for each pixel
theros 0:d258c72c1ae2 42 px.SetAll(colorbuf[btnState]);
theros 0:d258c72c1ae2 43
theros 0:d258c72c1ae2 44 //write the II value for each pixel
theros 0:d258c72c1ae2 45 px.SetAllI(64);
theros 0:d258c72c1ae2 46
theros 0:d258c72c1ae2 47 for (int i = WS2812_BUF; i >= 0; i--)
theros 0:d258c72c1ae2 48 {
theros 0:d258c72c1ae2 49 ws.write(px.getBuf());
theros 0:d258c72c1ae2 50 }
theros 0:d258c72c1ae2 51 }
theros 0:d258c72c1ae2 52 }