A program that fades between a selection of colors.
Dependencies: PixelArray WS2812 mbed
main.cpp@3:b99f66d3e77e, 2017-03-12 (annotated)
- Committer:
- theros
- Date:
- Sun Mar 12 23:04:00 2017 +0000
- Revision:
- 3:b99f66d3e77e
- Parent:
- 2:381421ec57e1
Debugging cleanup
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
theros | 0:de636c7cdfda | 1 | #include "mbed.h" |
theros | 0:de636c7cdfda | 2 | #include "WS2812.h" |
theros | 0:de636c7cdfda | 3 | #include "PixelArray.h" |
theros | 0:de636c7cdfda | 4 | |
theros | 0:de636c7cdfda | 5 | #define WS2812_BUF 77 //number of LEDs in the array |
theros | 0:de636c7cdfda | 6 | #define NUM_COLORS 6 //number of colors to store in the array |
theros | 2:381421ec57e1 | 7 | #define NUM_STEPS 8 //number of steps between colors |
theros | 0:de636c7cdfda | 8 | |
theros | 0:de636c7cdfda | 9 | DigitalOut usrLed(LED1); |
theros | 0:de636c7cdfda | 10 | PixelArray px(WS2812_BUF); |
theros | 0:de636c7cdfda | 11 | |
theros | 0:de636c7cdfda | 12 | // See the program page for information on the timing numbers |
theros | 0:de636c7cdfda | 13 | WS2812 ws(D9, WS2812_BUF, 6,17,9,14); //nucleo-f411re |
theros | 0:de636c7cdfda | 14 | |
theros | 1:ad5c2cfb2002 | 15 | int color_set(uint8_t red,uint8_t green, uint8_t blue) |
theros | 1:ad5c2cfb2002 | 16 | { |
theros | 1:ad5c2cfb2002 | 17 | return ((red<<16) + (green<<8) + blue); |
theros | 1:ad5c2cfb2002 | 18 | } |
theros | 1:ad5c2cfb2002 | 19 | |
theros | 1:ad5c2cfb2002 | 20 | // 0 <= stepNumber <= lastStepNumber |
theros | 1:ad5c2cfb2002 | 21 | int interpolate(int startValue, int endValue, int stepNumber, int lastStepNumber) |
theros | 1:ad5c2cfb2002 | 22 | { |
theros | 1:ad5c2cfb2002 | 23 | return (endValue - startValue) * stepNumber / lastStepNumber + startValue; |
theros | 1:ad5c2cfb2002 | 24 | } |
theros | 1:ad5c2cfb2002 | 25 | |
theros | 0:de636c7cdfda | 26 | int main() |
theros | 0:de636c7cdfda | 27 | { |
theros | 0:de636c7cdfda | 28 | int colorIdx = 0; |
theros | 1:ad5c2cfb2002 | 29 | int colorTo = 0; |
theros | 1:ad5c2cfb2002 | 30 | int colorFrom = 0; |
theros | 1:ad5c2cfb2002 | 31 | |
theros | 1:ad5c2cfb2002 | 32 | uint8_t ir = 0; |
theros | 1:ad5c2cfb2002 | 33 | uint8_t ig = 0; |
theros | 1:ad5c2cfb2002 | 34 | uint8_t ib = 0; |
theros | 0:de636c7cdfda | 35 | |
theros | 0:de636c7cdfda | 36 | ws.useII(WS2812::PER_PIXEL); // use per-pixel intensity scaling |
theros | 0:de636c7cdfda | 37 | |
theros | 0:de636c7cdfda | 38 | // set up the colours we want to draw with |
theros | 0:de636c7cdfda | 39 | int colorbuf[NUM_COLORS] = {0x2f0000,0x2f2f00,0x002f00,0x002f2f,0x00002f,0x2f002f}; |
theros | 0:de636c7cdfda | 40 | |
theros | 0:de636c7cdfda | 41 | // Now the buffer is written, write it to the led array. |
theros | 0:de636c7cdfda | 42 | while (1) |
theros | 0:de636c7cdfda | 43 | { |
theros | 1:ad5c2cfb2002 | 44 | //get starting RGB components for interpolation |
theros | 1:ad5c2cfb2002 | 45 | std::size_t c1 = colorbuf[colorFrom]; |
theros | 1:ad5c2cfb2002 | 46 | std::size_t r1 = (c1 & 0xff0000) >> 16; |
theros | 1:ad5c2cfb2002 | 47 | std::size_t g1 = (c1 & 0x00ff00) >> 8; |
theros | 1:ad5c2cfb2002 | 48 | std::size_t b1 = (c1 & 0x0000ff); |
theros | 1:ad5c2cfb2002 | 49 | |
theros | 1:ad5c2cfb2002 | 50 | //get ending RGB components for interpolation |
theros | 1:ad5c2cfb2002 | 51 | std::size_t c2 = colorbuf[colorTo]; |
theros | 1:ad5c2cfb2002 | 52 | std::size_t r2 = (c2 & 0xff0000) >> 16; |
theros | 1:ad5c2cfb2002 | 53 | std::size_t g2 = (c2 & 0x00ff00) >> 8; |
theros | 1:ad5c2cfb2002 | 54 | std::size_t b2 = (c2 & 0x0000ff); |
theros | 1:ad5c2cfb2002 | 55 | |
theros | 1:ad5c2cfb2002 | 56 | for (int i = 0; i <= NUM_STEPS; i++) |
theros | 0:de636c7cdfda | 57 | { |
theros | 1:ad5c2cfb2002 | 58 | ir = interpolate(r1, r2, i, NUM_STEPS); |
theros | 1:ad5c2cfb2002 | 59 | ig = interpolate(g1, g2, i, NUM_STEPS); |
theros | 1:ad5c2cfb2002 | 60 | ib = interpolate(b1, b2, i, NUM_STEPS); |
theros | 1:ad5c2cfb2002 | 61 | |
theros | 1:ad5c2cfb2002 | 62 | //write the color value for each pixel |
theros | 1:ad5c2cfb2002 | 63 | px.SetAll(color_set(ir,ig,ib)); |
theros | 1:ad5c2cfb2002 | 64 | |
theros | 1:ad5c2cfb2002 | 65 | //write the II value for each pixel |
theros | 1:ad5c2cfb2002 | 66 | px.SetAllI(32); |
theros | 1:ad5c2cfb2002 | 67 | |
theros | 1:ad5c2cfb2002 | 68 | for (int i = WS2812_BUF; i >= 0; i--) |
theros | 1:ad5c2cfb2002 | 69 | { |
theros | 1:ad5c2cfb2002 | 70 | ws.write(px.getBuf()); |
theros | 1:ad5c2cfb2002 | 71 | } |
theros | 0:de636c7cdfda | 72 | } |
theros | 0:de636c7cdfda | 73 | |
theros | 2:381421ec57e1 | 74 | colorFrom = colorIdx; |
theros | 0:de636c7cdfda | 75 | colorIdx++; |
theros | 0:de636c7cdfda | 76 | |
theros | 0:de636c7cdfda | 77 | if (colorIdx >= NUM_COLORS) |
theros | 0:de636c7cdfda | 78 | { |
theros | 0:de636c7cdfda | 79 | colorIdx = 0; |
theros | 0:de636c7cdfda | 80 | } |
theros | 1:ad5c2cfb2002 | 81 | |
theros | 2:381421ec57e1 | 82 | colorTo = colorIdx; |
theros | 0:de636c7cdfda | 83 | } |
theros | 0:de636c7cdfda | 84 | } |