Displays a gradient between two colors

Dependencies:   mbed WS2812 PixelArray

Committer:
radmir102
Date:
Mon Apr 19 12:08:45 2021 +0000
Revision:
1:3fa4e131f518
Parent:
0:3bb97062a703
Child:
2:c4a43cc93ca8
The program was created for the matrix 4x4 Olympiad for schoolchildren

Who changed what in which revision?

UserRevisionLine numberNew 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
radmir102 1:3fa4e131f518 5 #define WS2812_BUF 16 //number of LEDs in the array
radmir102 1:3fa4e131f518 6 #define NUM_COLORS 6 //number of colors to store in the array
radmir102 1:3fa4e131f518 7 #define PIN_NUM A2
radmir102 1:3fa4e131f518 8 #define LIGHT_INTENSITY 30
theros 0:3bb97062a703 9
radmir102 1:3fa4e131f518 10 using namespace std;
theros 0:3bb97062a703 11 DigitalOut usrLed(LED1);
theros 0:3bb97062a703 12 PixelArray px(WS2812_BUF);
radmir102 1:3fa4e131f518 13 DigitalIn usrBtn(USER_BUTTON);
radmir102 1:3fa4e131f518 14
theros 0:3bb97062a703 15
theros 0:3bb97062a703 16 // See the program page for information on the timing numbers
radmir102 1:3fa4e131f518 17 WS2812 ws(PIN_NUM, WS2812_BUF,6,17,9,14); //nucleo-f411re
theros 0:3bb97062a703 18
theros 0:3bb97062a703 19 int color_set(uint8_t red,uint8_t green, uint8_t blue)
theros 0:3bb97062a703 20 {
theros 0:3bb97062a703 21 return ((red<<16) + (green<<8) + blue);
theros 0:3bb97062a703 22 }
theros 0:3bb97062a703 23
theros 0:3bb97062a703 24 // 0 <= stepNumber <= lastStepNumber
theros 0:3bb97062a703 25 int interpolate(int startValue, int endValue, int stepNumber, int lastStepNumber)
theros 0:3bb97062a703 26 {
theros 0:3bb97062a703 27 return (endValue - startValue) * stepNumber / lastStepNumber + startValue;
theros 0:3bb97062a703 28 }
theros 0:3bb97062a703 29
theros 0:3bb97062a703 30 int main()
theros 0:3bb97062a703 31 {
theros 0:3bb97062a703 32 uint8_t ir = 0;
theros 0:3bb97062a703 33 uint8_t ig = 0;
theros 0:3bb97062a703 34 uint8_t ib = 0;
radmir102 1:3fa4e131f518 35 int btnState = 0;
theros 0:3bb97062a703 36
theros 0:3bb97062a703 37 ws.useII(WS2812::PER_PIXEL); // use per-pixel intensity scaling
theros 0:3bb97062a703 38
theros 0:3bb97062a703 39 // set up the colours we want to draw with
radmir102 1:3fa4e131f518 40 int colorbuf[NUM_COLORS] = {0xff0000,0x0000ff,0x0002f0,0x00002f,0x00002f,0x2f002f};
theros 0:3bb97062a703 41
theros 0:3bb97062a703 42 //get starting RGB components for interpolation
radmir102 1:3fa4e131f518 43 size_t c1 = colorbuf[0];
radmir102 1:3fa4e131f518 44 size_t r1 = (c1 & 0xff0000) >> 16;
radmir102 1:3fa4e131f518 45 size_t g1 = (c1 & 0x00ff00) >> 8;
radmir102 1:3fa4e131f518 46 size_t b1 = (c1 & 0x0000ff);
theros 0:3bb97062a703 47
theros 0:3bb97062a703 48 //get ending RGB components for interpolation
radmir102 1:3fa4e131f518 49 size_t c2 = colorbuf[1];
radmir102 1:3fa4e131f518 50 size_t r2 = (c2 & 0xff0000) >> 16;
radmir102 1:3fa4e131f518 51 size_t g2 = (c2 & 0x00ff00) >> 8;
radmir102 1:3fa4e131f518 52 size_t b2 = (c2 & 0x0000ff);
theros 0:3bb97062a703 53
theros 0:3bb97062a703 54 for (int i = 0; i <= WS2812_BUF; i++)
theros 0:3bb97062a703 55 {
theros 0:3bb97062a703 56 ir = interpolate(r1, r2, i, WS2812_BUF);
theros 0:3bb97062a703 57 ig = interpolate(g1, g2, i, WS2812_BUF);
theros 0:3bb97062a703 58 ib = interpolate(b1, b2, i, WS2812_BUF);
theros 0:3bb97062a703 59
theros 0:3bb97062a703 60 //write the color value for each pixel
theros 0:3bb97062a703 61 px.Set(i, color_set(ir,ig,ib));
theros 0:3bb97062a703 62
theros 0:3bb97062a703 63 //write the II value for each pixel
radmir102 1:3fa4e131f518 64 /*px.SetI(4, 30);
radmir102 1:3fa4e131f518 65 px.SetI(1, 30);
radmir102 1:3fa4e131f518 66 px.SetI(2, 30);
radmir102 1:3fa4e131f518 67 px.SetI(7, 30);
radmir102 1:3fa4e131f518 68 px.SetI(12, 30);
radmir102 1:3fa4e131f518 69 px.SetI(15, 30);*/// смайлик убийца
radmir102 1:3fa4e131f518 70
radmir102 1:3fa4e131f518 71 px.SetI(i, 10);
radmir102 1:3fa4e131f518 72
radmir102 1:3fa4e131f518 73 /*px.SetI(10, 15);
radmir102 1:3fa4e131f518 74
radmir102 1:3fa4e131f518 75 px.SetI(15, 20);
radmir102 1:3fa4e131f518 76
radmir102 1:3fa4e131f518 77 px.SetI(3, 25);
radmir102 1:3fa4e131f518 78
radmir102 1:3fa4e131f518 79 px.SetI(6, 30);
radmir102 1:3fa4e131f518 80
radmir102 1:3fa4e131f518 81 px.SetI(9, 15);
radmir102 1:3fa4e131f518 82
radmir102 1:3fa4e131f518 83 px.SetI(12, 5);*/
radmir102 1:3fa4e131f518 84
theros 0:3bb97062a703 85 }
radmir102 1:3fa4e131f518 86 /*
theros 0:3bb97062a703 87 for (int i = WS2812_BUF; i >= 0; i--)
theros 0:3bb97062a703 88 {
theros 0:3bb97062a703 89 ws.write(px.getBuf());
radmir102 1:3fa4e131f518 90 }*/
radmir102 1:3fa4e131f518 91
radmir102 1:3fa4e131f518 92 usrLed = 1;
radmir102 1:3fa4e131f518 93
radmir102 1:3fa4e131f518 94
radmir102 1:3fa4e131f518 95
theros 0:3bb97062a703 96
radmir102 1:3fa4e131f518 97 /*
radmir102 1:3fa4e131f518 98 while (1)
radmir102 1:3fa4e131f518 99 {
radmir102 1:3fa4e131f518 100 if (usrBtn == 0) //button is pressed
radmir102 1:3fa4e131f518 101 {
radmir102 1:3fa4e131f518 102 //write the color value for each pixel
radmir102 1:3fa4e131f518 103 px.SetAll(colorbuf[btnState]);
radmir102 1:3fa4e131f518 104 //write the II value for each pixel
radmir102 1:3fa4e131f518 105 px.SetAllI(64);
radmir102 1:3fa4e131f518 106
radmir102 1:3fa4e131f518 107 usrLed = 1;
radmir102 1:3fa4e131f518 108 btnState = btnState++;
radmir102 1:3fa4e131f518 109
radmir102 1:3fa4e131f518 110 if (btnState == NUM_COLORS) {
radmir102 1:3fa4e131f518 111 btnState = 0;
radmir102 1:3fa4e131f518 112 }
radmir102 1:3fa4e131f518 113 }
radmir102 1:3fa4e131f518 114 else
radmir102 1:3fa4e131f518 115 {
radmir102 1:3fa4e131f518 116 px.SetAll(colorbuf[btnState]);
radmir102 1:3fa4e131f518 117 px.SetI(0, LIGHT_INTENSITY);
radmir102 1:3fa4e131f518 118
radmir102 1:3fa4e131f518 119 px.SetI(5, LIGHT_INTENSITY);
radmir102 1:3fa4e131f518 120
radmir102 1:3fa4e131f518 121 px.SetI(10, LIGHT_INTENSITY);
radmir102 1:3fa4e131f518 122
radmir102 1:3fa4e131f518 123 px.SetI(15, LIGHT_INTENSITY);
radmir102 1:3fa4e131f518 124
radmir102 1:3fa4e131f518 125 px.SetI(3, LIGHT_INTENSITY);
radmir102 1:3fa4e131f518 126
radmir102 1:3fa4e131f518 127 px.SetI(6, LIGHT_INTENSITY);
radmir102 1:3fa4e131f518 128
radmir102 1:3fa4e131f518 129 px.SetI(9, LIGHT_INTENSITY);
radmir102 1:3fa4e131f518 130
radmir102 1:3fa4e131f518 131 px.SetI(12, LIGHT_INTENSITY);
radmir102 1:3fa4e131f518 132
radmir102 1:3fa4e131f518 133 usrLed = 0;
radmir102 1:3fa4e131f518 134 }
radmir102 1:3fa4e131f518 135
radmir102 1:3fa4e131f518 136
radmir102 1:3fa4e131f518 137
radmir102 1:3fa4e131f518 138
radmir102 1:3fa4e131f518 139 for (int i = WS2812_BUF; i >= 0; i--)
radmir102 1:3fa4e131f518 140 {
radmir102 1:3fa4e131f518 141 ws.write(px.getBuf());
radmir102 1:3fa4e131f518 142 }
radmir102 1:3fa4e131f518 143
radmir102 1:3fa4e131f518 144 }*/
radmir102 1:3fa4e131f518 145
theros 0:3bb97062a703 146 }