Displays a gradient between two colors
Dependencies: mbed WS2812 PixelArray
main.cpp@0:3bb97062a703, 2017-03-12 (annotated)
- Committer:
- theros
- Date:
- Sun Mar 12 23:02:59 2017 +0000
- Revision:
- 0:3bb97062a703
- Child:
- 1:3fa4e131f518
First Commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
theros | 0:3bb97062a703 | 1 | #include "mbed.h" |
theros | 0:3bb97062a703 | 2 | #include "WS2812.h" |
theros | 0:3bb97062a703 | 3 | #include "PixelArray.h" |
theros | 0:3bb97062a703 | 4 | |
theros | 0:3bb97062a703 | 5 | #define WS2812_BUF 77 //number of LEDs in the array |
theros | 0:3bb97062a703 | 6 | #define NUM_COLORS 2 //number of colors to store in the array |
theros | 0:3bb97062a703 | 7 | |
theros | 0:3bb97062a703 | 8 | DigitalOut usrLed(LED1); |
theros | 0:3bb97062a703 | 9 | PixelArray px(WS2812_BUF); |
theros | 0:3bb97062a703 | 10 | |
theros | 0:3bb97062a703 | 11 | // See the program page for information on the timing numbers |
theros | 0:3bb97062a703 | 12 | WS2812 ws(D9, WS2812_BUF, 6,17,9,14); //nucleo-f411re |
theros | 0:3bb97062a703 | 13 | |
theros | 0:3bb97062a703 | 14 | int color_set(uint8_t red,uint8_t green, uint8_t blue) |
theros | 0:3bb97062a703 | 15 | { |
theros | 0:3bb97062a703 | 16 | return ((red<<16) + (green<<8) + blue); |
theros | 0:3bb97062a703 | 17 | } |
theros | 0:3bb97062a703 | 18 | |
theros | 0:3bb97062a703 | 19 | // 0 <= stepNumber <= lastStepNumber |
theros | 0:3bb97062a703 | 20 | int interpolate(int startValue, int endValue, int stepNumber, int lastStepNumber) |
theros | 0:3bb97062a703 | 21 | { |
theros | 0:3bb97062a703 | 22 | return (endValue - startValue) * stepNumber / lastStepNumber + startValue; |
theros | 0:3bb97062a703 | 23 | } |
theros | 0:3bb97062a703 | 24 | |
theros | 0:3bb97062a703 | 25 | int main() |
theros | 0:3bb97062a703 | 26 | { |
theros | 0:3bb97062a703 | 27 | uint8_t ir = 0; |
theros | 0:3bb97062a703 | 28 | uint8_t ig = 0; |
theros | 0:3bb97062a703 | 29 | uint8_t ib = 0; |
theros | 0:3bb97062a703 | 30 | |
theros | 0:3bb97062a703 | 31 | ws.useII(WS2812::PER_PIXEL); // use per-pixel intensity scaling |
theros | 0:3bb97062a703 | 32 | |
theros | 0:3bb97062a703 | 33 | // set up the colours we want to draw with |
theros | 0:3bb97062a703 | 34 | int colorbuf[NUM_COLORS] = {0xff0000,0x0000ff}; |
theros | 0:3bb97062a703 | 35 | |
theros | 0:3bb97062a703 | 36 | //get starting RGB components for interpolation |
theros | 0:3bb97062a703 | 37 | std::size_t c1 = colorbuf[0]; |
theros | 0:3bb97062a703 | 38 | std::size_t r1 = (c1 & 0xff0000) >> 16; |
theros | 0:3bb97062a703 | 39 | std::size_t g1 = (c1 & 0x00ff00) >> 8; |
theros | 0:3bb97062a703 | 40 | std::size_t b1 = (c1 & 0x0000ff); |
theros | 0:3bb97062a703 | 41 | |
theros | 0:3bb97062a703 | 42 | //get ending RGB components for interpolation |
theros | 0:3bb97062a703 | 43 | std::size_t c2 = colorbuf[1]; |
theros | 0:3bb97062a703 | 44 | std::size_t r2 = (c2 & 0xff0000) >> 16; |
theros | 0:3bb97062a703 | 45 | std::size_t g2 = (c2 & 0x00ff00) >> 8; |
theros | 0:3bb97062a703 | 46 | std::size_t b2 = (c2 & 0x0000ff); |
theros | 0:3bb97062a703 | 47 | |
theros | 0:3bb97062a703 | 48 | for (int i = 0; i <= WS2812_BUF; i++) |
theros | 0:3bb97062a703 | 49 | { |
theros | 0:3bb97062a703 | 50 | ir = interpolate(r1, r2, i, WS2812_BUF); |
theros | 0:3bb97062a703 | 51 | ig = interpolate(g1, g2, i, WS2812_BUF); |
theros | 0:3bb97062a703 | 52 | ib = interpolate(b1, b2, i, WS2812_BUF); |
theros | 0:3bb97062a703 | 53 | |
theros | 0:3bb97062a703 | 54 | //write the color value for each pixel |
theros | 0:3bb97062a703 | 55 | px.Set(i, color_set(ir,ig,ib)); |
theros | 0:3bb97062a703 | 56 | |
theros | 0:3bb97062a703 | 57 | //write the II value for each pixel |
theros | 0:3bb97062a703 | 58 | px.SetI(i, 32); |
theros | 0:3bb97062a703 | 59 | } |
theros | 0:3bb97062a703 | 60 | |
theros | 0:3bb97062a703 | 61 | for (int i = WS2812_BUF; i >= 0; i--) |
theros | 0:3bb97062a703 | 62 | { |
theros | 0:3bb97062a703 | 63 | ws.write(px.getBuf()); |
theros | 0:3bb97062a703 | 64 | } |
theros | 0:3bb97062a703 | 65 | |
theros | 0:3bb97062a703 | 66 | usrLed = 1; |
theros | 0:3bb97062a703 | 67 | } |