forked from macetech/RGBShades
-
Notifications
You must be signed in to change notification settings - Fork 0
/
effects.h
330 lines (258 loc) · 9 KB
/
effects.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Graphical effects to run on the RGB Shades LED array
// Each function should have the following components:
// * Must be declared void with no parameters or will break function pointer array
// * Check effectInit, if false then init any required settings and set effectInit true
// * Set effectDelay (the time in milliseconds until the next run of this effect)
// * All animation should be controlled with counters and effectDelay, no delay() or loops
// * Pixel data should be written using leds[XY(x,y)] to map coordinates to the RGB Shades layout
// Triple Sine Waves
void threeSine() {
static byte sineOffset = 0; // counter for current position of sine waves
// startup tasks
if (effectInit == false) {
effectInit = true;
effectDelay = 20;
}
// Draw one frame of the animation into the LED array
for (byte x = 0; x < kMatrixWidth; x++) {
for (int y = 0; y < kMatrixHeight; y++) {
// Calculate "sine" waves with varying periods
// sin8 is used for speed; cos8, quadwave8, or triwave8 would also work here
byte sinDistanceR = qmul8(abs(y * (255 / kMatrixHeight) - sin8(sineOffset * 9 + x * 16)), 2);
byte sinDistanceG = qmul8(abs(y * (255 / kMatrixHeight) - sin8(sineOffset * 10 + x * 16)), 2);
byte sinDistanceB = qmul8(abs(y * (255 / kMatrixHeight) - sin8(sineOffset * 11 + x * 16)), 2);
leds[XY(x, y)] = CRGB(255 - sinDistanceR, 255 - sinDistanceG, 255 - sinDistanceB);
}
}
sineOffset++; // byte will wrap from 255 to 0, matching sin8 0-255 cycle
}
// RGB Plasma
void plasma() {
static byte offset = 0; // counter for radial color wave motion
static int plasVector = 0; // counter for orbiting plasma center
// startup tasks
if (effectInit == false) {
effectInit = true;
effectDelay = 10;
}
// Calculate current center of plasma pattern (can be offscreen)
int xOffset = cos8(plasVector / 256);
int yOffset = sin8(plasVector / 256);
// Draw one frame of the animation into the LED array
for (int x = 0; x < kMatrixWidth; x++) {
for (int y = 0; y < kMatrixHeight; y++) {
byte color = sin8(sqrt(sq(((float)x - 7.5) * 10 + xOffset - 127) + sq(((float)y - 2) * 10 + yOffset - 127)) + offset);
leds[XY(x, y)] = CHSV(color, 255, 255);
}
}
offset++; // wraps at 255 for sin8
plasVector += 16; // using an int for slower orbit (wraps at 65536)
}
// Scanning pattern left/right, uses global hue cycle
void rider() {
static byte riderPos = 0;
// startup tasks
if (effectInit == false) {
effectInit = true;
effectDelay = 5;
riderPos = 0;
}
// Draw one frame of the animation into the LED array
for (byte x = 0; x < kMatrixWidth; x++) {
int brightness = abs(x * (256 / kMatrixWidth) - triwave8(riderPos) * 2 + 127) * 3;
if (brightness > 255) brightness = 255;
brightness = 255 - brightness;
CRGB riderColor = CHSV(cycleHue, 255, brightness);
for (byte y = 0; y < kMatrixHeight; y++) {
leds[XY(x, y)] = riderColor;
}
}
riderPos++; // byte wraps to 0 at 255, triwave8 is also 0-255 periodic
}
// Shimmering noise, uses global hue cycle
void glitter() {
// startup tasks
if (effectInit == false) {
effectInit = true;
effectDelay = 15;
}
// Draw one frame of the animation into the LED array
for (int x = 0; x < kMatrixWidth; x++) {
for (int y = 0; y < kMatrixHeight; y++) {
leds[XY(x, y)] = CHSV(cycleHue, 255, random8(5) * 63);
}
}
}
// Fills saturated colors into the array from alternating directions
void colorFill() {
static byte currentColor = 0;
static byte currentRow = 0;
static byte currentDirection = 0;
// startup tasks
if (effectInit == false) {
effectInit = true;
effectDelay = 45;
currentColor = 0;
currentRow = 0;
currentDirection = 0;
currentPalette = RainbowColors_p;
}
// test a bitmask to fill up or down when currentDirection is 0 or 2 (0b00 or 0b10)
if (!(currentDirection & 1)) {
effectDelay = 45; // slower since vertical has fewer pixels
for (byte x = 0; x < kMatrixWidth; x++) {
byte y = currentRow;
if (currentDirection == 2) y = kMatrixHeight - 1 - currentRow;
leds[XY(x, y)] = currentPalette[currentColor];
}
}
// test a bitmask to fill left or right when currentDirection is 1 or 3 (0b01 or 0b11)
if (currentDirection & 1) {
effectDelay = 20; // faster since horizontal has more pixels
for (byte y = 0; y < kMatrixHeight; y++) {
byte x = currentRow;
if (currentDirection == 3) x = kMatrixWidth - 1 - currentRow;
leds[XY(x, y)] = currentPalette[currentColor];
}
}
currentRow++;
// detect when a fill is complete, change color and direction
if ((!(currentDirection & 1) && currentRow >= kMatrixHeight) || ((currentDirection & 1) && currentRow >= kMatrixWidth)) {
currentRow = 0;
currentColor += random8(3, 6);
if (currentColor > 15) currentColor -= 16;
currentDirection++;
if (currentDirection > 3) currentDirection = 0;
effectDelay = 300; // wait a little bit longer after completing a fill
}
}
// Emulate 3D anaglyph glasses
void threeDee() {
// startup tasks
if (effectInit == false) {
effectInit = true;
effectDelay = 50;
}
for (byte x = 0; x < kMatrixWidth; x++) {
for (byte y = 0; y < kMatrixHeight; y++) {
if (x < 7) {
leds[XY(x, y)] = CRGB::Blue;
} else if (x > 8) {
leds[XY(x, y)] = CRGB::Red;
} else {
leds[XY(x, y)] = CRGB::Black;
}
}
}
leds[XY(6, 0)] = CRGB::Black;
leds[XY(9, 0)] = CRGB::Black;
}
// Random pixels scroll sideways, uses current hue
#define rainDir 0
void sideRain() {
// startup tasks
if (effectInit == false) {
effectInit = true;
effectDelay = 30;
}
scrollArray(rainDir);
byte randPixel = random8(kMatrixHeight);
for (byte y = 0; y < kMatrixHeight; y++) leds[XY((kMatrixWidth - 1) * rainDir, y)] = CRGB::Black;
leds[XY((kMatrixWidth - 1)*rainDir, randPixel)] = CHSV(cycleHue, 255, 255);
}
// Pixels with random locations and random colors selected from a palette
// Use with the fadeAll function to allow old pixels to decay
void confetti() {
// startup tasks
if (effectInit == false) {
effectInit = true;
effectDelay = 10;
selectRandomPalette();
}
// scatter random colored pixels at several random coordinates
for (byte i = 0; i < 4; i++) {
leds[XY(random16(kMatrixWidth), random16(kMatrixHeight))] = ColorFromPalette(currentPalette, random16(255), 255); //CHSV(random16(255), 255, 255);
random16_add_entropy(1);
}
}
// Draw slanting bars scrolling across the array, uses current hue
void slantBars() {
static byte slantPos = 0;
// startup tasks
if (effectInit == false) {
effectInit = true;
effectDelay = 5;
}
for (byte x = 0; x < kMatrixWidth; x++) {
for (byte y = 0; y < kMatrixHeight; y++) {
leds[XY(x, y)] = CHSV(cycleHue, 255, quadwave8(x * 32 + y * 32 + slantPos));
}
}
slantPos -= 4;
}
#define NORMAL 0
#define RAINBOW 1
#define charSpacing 2
// Scroll a text string
void scrollText(byte message, byte style, CRGB fgColor, CRGB bgColor) {
static byte currentMessageChar = 0;
static byte currentCharColumn = 0;
static byte paletteCycle = 0;
static CRGB currentColor;
static byte bitBuffer[16] = {0};
static byte bitBufferPointer = 0;
// startup tasks
if (effectInit == false) {
effectInit = true;
effectDelay = 35;
currentMessageChar = 0;
currentCharColumn = 0;
selectFlashString(message);
loadCharBuffer(loadStringChar(message, currentMessageChar));
currentPalette = RainbowColors_p;
for (byte i = 0; i < kMatrixWidth; i++) bitBuffer[i] = 0;
}
paletteCycle += 15;
if (currentCharColumn < 5) { // characters are 5 pixels wide
bitBuffer[(bitBufferPointer + kMatrixWidth - 1) % kMatrixWidth] = charBuffer[currentCharColumn]; // character
} else {
bitBuffer[(bitBufferPointer + kMatrixWidth - 1) % kMatrixWidth] = 0; // space
}
CRGB pixelColor;
for (byte x = 0; x < kMatrixWidth; x++) {
for (byte y = 0; y < 5; y++) { // characters are 5 pixels tall
if (bitRead(bitBuffer[(bitBufferPointer + x) % kMatrixWidth], y) == 1) {
if (style == RAINBOW) {
pixelColor = ColorFromPalette(currentPalette, paletteCycle+y*16, 255);
} else {
pixelColor = fgColor;
}
} else {
pixelColor = bgColor;
}
leds[XY(x, y)] = pixelColor;
}
}
currentCharColumn++;
if (currentCharColumn > (4 + charSpacing)) {
currentCharColumn = 0;
currentMessageChar++;
char nextChar = loadStringChar(message, currentMessageChar);
if (nextChar == 0) { // null character at end of strong
currentMessageChar = 0;
nextChar = loadStringChar(message, currentMessageChar);
}
loadCharBuffer(nextChar);
}
bitBufferPointer++;
if (bitBufferPointer > 15) bitBufferPointer = 0;
}
void scrollTextZero() {
scrollText(0, NORMAL, CRGB::Red, CRGB::Black);
}
void scrollTextOne() {
scrollText(1, RAINBOW, 0, CRGB::Black);
}
void scrollTextTwo() {
scrollText(2, NORMAL, CRGB::Green, CRGB(0,0,8));
}