forked from slawa19/IKEA-LED-Table
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rainbowAnimation.ino
103 lines (87 loc) · 2.62 KB
/
rainbowAnimation.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* LedTable
*
* Written by: Ing. David Hrbaty
*
* Rainbow animation for the LED table. Code is based on the OctoWS2812 Rainbow example
*/
#define RAINBOWSPEED 250
int rainbowColors[180];
boolean rainBowRunning = true;
void initRainbow(){
for (int i=0; i<180; i++) {
int hue = i * 2;
int saturation = 100;
int lightness = 50;
// pre-compute the 180 rainbow colors
rainbowColors[i] = makeColor(hue, saturation, lightness);
}
}
void runRainbow(){
initRainbow();
rainBowRunning = true;
while (rainBowRunning){
rainbow(10, 5000);
}
fadeOut();
}
// phaseShift is the shift between each row. phaseShift=0
// causes all rows to show the same colors moving together.
// phaseShift=180 causes each row to be the opposite colors
// as the previous.
//
// cycleTime is the number of milliseconds to shift through
// the entire 360 degrees of the color wheel:
// Red -> Orange -> Yellow -> Green -> Blue -> Violet -> Red
//
void rainbow(int phaseShift, int cycleTime){
int color, x, y, offset, wait;
wait = cycleTime * 1000 / NUM_PIXELS;
for (color=0; color < 180; color++) {
for (x=0; x < FIELD_WIDTH; x++) {
for (y=0; y < FIELD_HEIGHT; y++) {
int index = (color + x + y*phaseShift/2) % 180;
setTablePixel(x, y, rainbowColors[index]);
}
}
showPixels();
Serial.println("OK");
//Read buttons
readInput();
if (curControl == BTN_START){
Serial.println("START");
rainBowRunning = false;
break;
}
delayMicroseconds(wait);
}
}
int makeColor(unsigned int hue, unsigned int saturation, unsigned int lightness){
unsigned int red, green, blue;
unsigned int var1, var2;
if (hue > 359) hue = hue % 360;
if (saturation > 100) saturation = 100;
if (lightness > 100) lightness = 100;
// algorithm from: http://www.easyrgb.com/index.php?X=MATH&H=19#text19
if (saturation == 0) {
red = green = blue = lightness * 255 / 100;
}
else {
if (lightness < 50) {
var2 = lightness * (100 + saturation);
}
else {
var2 = ((lightness + saturation) * 100) - (saturation * lightness);
}
var1 = lightness * 200 - var2;
red = h2rgb(var1, var2, (hue < 240) ? hue + 120 : hue - 240) * 255 / 600000;
green = h2rgb(var1, var2, hue) * 255 / 600000;
blue = h2rgb(var1, var2, (hue >= 120) ? hue - 120 : hue + 240) * 255 / 600000;
}
return (red << 16) | (green << 8) | blue;
}
unsigned int h2rgb(unsigned int v1, unsigned int v2, unsigned int hue){
if (hue < 60) return v1 * 60 + (v2 - v1) * hue;
if (hue < 180) return v2 * 60;
if (hue < 240) return v1 * 60 + (v2 - v1) * (240 - hue);
return v1 * 60;
}