-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLightShow.h
156 lines (133 loc) · 3.48 KB
/
LightShow.h
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#ifndef LightShow_h
#define LightShow_h
#include "Arduino.h"
#include "Adafruit_NeoPixel.h"
class Color {
private:
int16_t r;
int16_t g;
int16_t b;
public:
Color operator-(const Color& rhs) const;
Color operator+(const Color& rhs) const;
Color operator*(const float& rhs) const;
Color operator*(const Color& rhs) const;
Color();
Color(uint8_t r, uint8_t g, uint8_t b);
Color(int16_t r, int16_t g, int16_t b);
Color(const Color& color);
void put(Adafruit_NeoPixel& pixels, uint8_t index) const;
void putAll(Adafruit_NeoPixel& pixels, uint8_t count) const;
void dump() const;
uint8_t getR();
uint8_t getG();
uint8_t getB();
};
template<uint8_t COUNT>
void put(Color colors[COUNT], Adafruit_NeoPixel& pixels) {
for (uint8_t i = 0; i < COUNT; i++) {
colors[i].put(pixels, i);
}
}
struct Colors {
static const Color black;
static const Color white;
static const Color red;
static const Color green;
static const Color blue;
};
#define PIXEL_FPS 60
class ColorFade {
private:
Color from;
Color to;
Color amount;
Color current;
uint32_t duration;
public:
unsigned long iteration;
uint32_t wait;
//duration: in millis
ColorFade();
ColorFade(Color from, uint32_t duration, Color to);
void reset();
Color nextColor();
Color prevColor();
void play(Adafruit_NeoPixel& pixels, bool backAndForth, uint8_t index);
void dump();
};
#define FADE(from, duration, to, pixels, index, back) { ColorFade(from, duration, to).play(pixels, back, index); }
template<uint8_t COUNT>
class MultiColorFade
{
private:
ColorFade fades[COUNT];
uint32_t duration;
public:
MultiColorFade(Color froms[COUNT], uint32_t duration, Color tos[COUNT]);
void reset();
void forwardPut(Adafruit_NeoPixel& pixels);
void backwardPut(Adafruit_NeoPixel& pixels);
unsigned long iteration();
uint32_t wait();
void play(Adafruit_NeoPixel& pixels, bool backAndForth);
void dump();
};
#define MULTIFADE(count, froms, duration, tos, pixels, back) { MultiColorFade<count>(froms, duration, tos).play(pixels, back); }
template<uint8_t COUNT>
MultiColorFade<COUNT>::MultiColorFade(Color froms[COUNT], uint32_t duration, Color tos[COUNT]) {
for (uint8_t i = 0; i < COUNT; i++) {
this->fades[i] = ColorFade(froms[i], duration, tos[i]);
}
this->duration = duration;
}
template<uint8_t COUNT>
void MultiColorFade<COUNT>::reset() {
for (uint8_t i = 0; i < COUNT; i++) {
this->fades[i].reset();
}
}
template<uint8_t COUNT>
void MultiColorFade<COUNT>::forwardPut(Adafruit_NeoPixel& pixels) {
for (uint8_t i = 0; i < COUNT; i++) {
this->fades[i].nextColor().put(pixels, i);
}
}
template<uint8_t COUNT>
void MultiColorFade<COUNT>::backwardPut(Adafruit_NeoPixel& pixels) {
for (uint8_t i = 0; i < COUNT; i++) {
this->fades[i].prevColor().put(pixels, i);
}
}
template<uint8_t COUNT>
unsigned long MultiColorFade<COUNT>::iteration() {
return this->fades[0].iteration;
}
template<uint8_t COUNT>
uint32_t MultiColorFade<COUNT>::wait() {
return this->fades[0].wait;
}
template<uint8_t COUNT>
void MultiColorFade<COUNT>::play(Adafruit_NeoPixel& pixels, bool backAndForth) {
this->reset();
for (uint32_t i = 0; i < this->iteration(); i++) {
this->forwardPut(pixels);
pixels.show();
delay(this->wait());
}
if (backAndForth) {
for (uint32_t i = 0; i < this->iteration(); i++) {
this->backwardPut(pixels);
pixels.show();
delay(this->wait());
}
}
}
template<uint8_t COUNT>
void MultiColorFade<COUNT>::dump() {
for (uint8_t i = 0; i < COUNT; i++) {
this->fades[i].dump();
Serial.println(F("--------------"));
}
}
#endif