-
Notifications
You must be signed in to change notification settings - Fork 1
/
patterns.h
441 lines (381 loc) · 11.5 KB
/
patterns.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#pragma once
#include "PROGMEM_readAnything.h"
#include <FastLED.h>
#include "palettes.h"
#include "tuba_bell.h"
namespace Patterns {
#define AS_PATTERN(A) {A::getName, A::getPalette, A::render}
typedef void Render(long now, CRGBPalette16 ¤tPalette);
typedef void GetPalette(CRGBPalette16 ¤tPalette);
typedef const char* GetName(void);
typedef void PerPixel(long now, CRGBPalette16 ¤tPalette, CRGB &led, PositionInfo currentPositionInfo);
class PatternClass
{
public:
static void setup(CRGB *leds, const PositionInfo *ledInfo, int numLeds);
protected:
static CRGB *m_leds;
static int m_nLeds;
static const PositionInfo *m_ledInfo;
static PositionInfo getPositionInfo(uint8_t pos);
static void eachPixel(PerPixel perPixel, long now, CRGBPalette16 currentPalette);
};
CRGB *PatternClass::m_leds;
int PatternClass::m_nLeds;
const PositionInfo *PatternClass::m_ledInfo;
void PatternClass::setup(CRGB *leds, const PositionInfo *ledInfo, int numLeds)
{
PatternClass::m_leds = leds;
PatternClass::m_ledInfo = ledInfo;
PatternClass::m_nLeds = numLeds;
}
void PatternClass::eachPixel(PerPixel perPixel, long now, CRGBPalette16 currentPalette)
{
for (int i = 0; i < PatternClass::m_nLeds; i++)
{
perPixel(now, currentPalette, PatternClass::m_leds[i], getPositionInfo(i));
}
}
PositionInfo PatternClass::getPositionInfo(uint8_t pos)
{
return PROGMEM_getAnything(&PatternClass::m_ledInfo[pos]);
}
class CycledPattern : public PatternClass
{
protected:
static const long cycleTime = 2500;
};
class SpinPattern : public CycledPattern
{
public:
static const char* getName()
{
return "SPIN";
}
static void getPalette(CRGBPalette16 ¤tPalette)
{
Palettes::RainbowPalette::getPalette(currentPalette);
}
static void render(long now, CRGBPalette16 ¤tPalette)
{
eachPixel(perPixel, now, currentPalette);
}
protected:
static void perPixel(long now, CRGBPalette16 ¤tPalette, CRGB &led, PositionInfo currentPositionInfo)
{
led = ColorFromPalette(currentPalette, currentPositionInfo.theta + map(now % cycleTime, 0, cycleTime, 0, 255), 255, LINEARBLEND);
}
};
class SpiralPattern : public CycledPattern
{
public:
static const char* getName()
{
return "SPRL";
}
static void getPalette(CRGBPalette16 ¤tPalette)
{
Palettes::StripesPalette::getPalette(currentPalette);
}
static void render(long now, CRGBPalette16 ¤tPalette)
{
eachPixel(perPixel, now, currentPalette);
}
protected:
static void perPixel(long now, CRGBPalette16 ¤tPalette, CRGB &led, PositionInfo currentPositionInfo)
{
led = ColorFromPalette(currentPalette, currentPositionInfo.theta + map(now % cycleTime, 0, cycleTime, 0, 255) - (currentPositionInfo.r8 / 2), 255, LINEARBLEND);
}
};
class FadePattern : public CycledPattern
{
public:
static const char* getName()
{
return "FADE";
}
static void getPalette(CRGBPalette16 ¤tPalette)
{
Palettes::RainbowPalette::getPalette(currentPalette);
}
static void render(long now, CRGBPalette16 ¤tPalette)
{
fill_solid(PatternClass::m_leds, PatternClass::m_nLeds, ColorFromPalette(currentPalette, map(now % cycleTime, 0, cycleTime, 0, 255), 255, LINEARBLEND));
}
};
class ScanPattern : public CycledPattern
{
public:
static const char* getName()
{
return "SCAN";
}
static void getPalette(CRGBPalette16 ¤tPalette)
{
Palettes::RedPalette::getPalette(currentPalette);
}
static void render(long now, CRGBPalette16 ¤tPalette)
{
beat = quadwave8((uint8_t)map(now % beatTime, 0, beatTime, 0, 255));
eachPixel(perPixel, now, currentPalette);
}
protected:
static const long beatTime = 1500;
static uint8_t beat;
static void perPixel(long now, CRGBPalette16 ¤tPalette, CRGB &led, PositionInfo currentPositionInfo)
{
uint8_t normpos = currentPositionInfo.y8 + 127;
uint8_t distance = (normpos > beat ? (normpos - beat) : (beat - normpos));
uint16_t dsquared = (uint16_t)distance * 3;
uint8_t brightness = dsquared >= 255 ? 0 : 255 - dsquared;
led = ColorFromPalette(currentPalette, map(now % cycleTime, 0, cycleTime, 0, 255), brightness, LINEARBLEND);
}
};
uint8_t ScanPattern::beat;
class LavaPattern : public PatternClass
{
public:
static const char* getName()
{
return "LAVA";
}
static void getPalette(CRGBPalette16 ¤tPalette)
{
Palettes::HeatPalette::getPalette(currentPalette);
}
static void render(long now, CRGBPalette16 ¤tPalette)
{
lava_z = (uint16_t)(now / 2);
eachPixel(perPixel, now, currentPalette);
}
protected:
static const int scale = 5;
static uint16_t lava_z;
static void perPixel(long now, CRGBPalette16 ¤tPalette, CRGB &led, PositionInfo currentPositionInfo)
{
uint8_t data = inoise8((uint16_t)currentPositionInfo.x8 * scale + (lava_z / 8), (uint16_t)currentPositionInfo.y8 * scale + (lava_z / 16), lava_z);
data = qsub8(data, 16);
data = qadd8(data, scale8(data, 39));
led = ColorFromPalette(currentPalette, data, 255, LINEARBLEND);
}
};
uint16_t LavaPattern::lava_z;
class VerticalPattern : public CycledPattern
{
public:
static const char* getName()
{
return "VERT";
}
static void getPalette(CRGBPalette16 ¤tPalette)
{
Palettes::RainbowPalette::getPalette(currentPalette);
}
static void render(long now, CRGBPalette16 ¤tPalette)
{
eachPixel(perPixel, now, currentPalette);
}
protected:
static void perPixel(long now, CRGBPalette16 ¤tPalette, CRGB &led, PositionInfo currentPositionInfo)
{
led = ColorFromPalette(currentPalette, (255 - currentPositionInfo.y8) - map(now % cycleTime, 0, cycleTime, 0, 255), 255, LINEARBLEND);
}
};
class WavePattern : public CycledPattern
{
public:
static const char* getName()
{
return "WAVE";
}
static void getPalette(CRGBPalette16 ¤tPalette)
{
Palettes::RainbowPalette::getPalette(currentPalette);
}
static void render(long now, CRGBPalette16 ¤tPalette)
{
pos = map(now % cycleTime * 2, 0, cycleTime * 2, 0, 255);
eachPixel(perPixel, now, currentPalette);
}
protected:
static uint8_t pos;
static void perPixel(long now, CRGBPalette16 ¤tPalette, CRGB &led, PositionInfo currentPositionInfo)
{
auto w = quadwave8(pos + (currentPositionInfo.y8));
led = ColorFromPalette(currentPalette, currentPositionInfo.x8 + w, 255, LINEARBLEND);
}
};
uint8_t WavePattern::pos = 0;
class RadialInPattern : public CycledPattern
{
public:
static const char* getName()
{
return "RIN";
}
static void getPalette(CRGBPalette16 ¤tPalette)
{
Palettes::RainbowPalette::getPalette(currentPalette);
}
static void render(long now, CRGBPalette16 ¤tPalette)
{
eachPixel(perPixel, now, currentPalette);
}
protected:
static void perPixel(long now, CRGBPalette16 ¤tPalette, CRGB &led, PositionInfo currentPositionInfo)
{
led = ColorFromPalette(currentPalette, (255 - currentPositionInfo.r8) - map(now % cycleTime, 0, cycleTime, 0, 255), 255, LINEARBLEND);
}
};
class RadialOutPattern : public CycledPattern
{
public:
static const char* getName()
{
return "ROUT";
}
static void getPalette(CRGBPalette16 ¤tPalette)
{
Palettes::RainbowPalette::getPalette(currentPalette);
}
static void render(long now, CRGBPalette16 ¤tPalette)
{
eachPixel(perPixel, now, currentPalette);
}
protected:
static void perPixel(long now, CRGBPalette16 ¤tPalette, CRGB &led, PositionInfo currentPositionInfo)
{
led = ColorFromPalette(currentPalette, currentPositionInfo.r8 - map(now % cycleTime, 0, cycleTime, 0, 255), 255, LINEARBLEND);
}
};
class CloudsPattern : public PatternClass
{
public:
static const char* getName()
{
return "CLDS";
}
static void getPalette(CRGBPalette16 ¤tPalette)
{
Palettes::SkyPalette::getPalette(currentPalette);
}
static void render(long now, CRGBPalette16 ¤tPalette)
{
eachPixel(perPixel, now, currentPalette);
}
protected:
static const int scale = 5;
static void perPixel(long now, CRGBPalette16 ¤tPalette, CRGB &led, PositionInfo currentPositionInfo)
{
uint8_t data = inoise8((uint16_t)currentPositionInfo.x8 * scale, (uint16_t)currentPositionInfo.y8 + (now / 12));
data = qsub8(data, 16);
data = qadd8(data, scale8(data, 39));
data = scale8(data, 25) + scale8(data, 256 - 25);
nblend(led, ColorFromPalette(currentPalette, data, 255, LINEARBLEND), 192);
}
};
class FirePattern : public PatternClass
{
public:
static const char* getName()
{
return "FIRE";
}
static void getPalette(CRGBPalette16 ¤tPalette)
{
currentPalette = HeatColors_p;
}
static void render(long now, CRGBPalette16 ¤tPalette)
{
eachPixel(perPixel, now, currentPalette);
}
protected:
static const int scale = 5;
static void perPixel(long now, CRGBPalette16 ¤tPalette, CRGB &led, PositionInfo currentPositionInfo)
{
uint8_t data = inoise8((uint16_t)currentPositionInfo.theta * scale, ((uint16_t)currentPositionInfo.r8 + (now/3)) * scale);
data = qsub8(data, 16);
data = qadd8(data, scale8(data, 39));
data = scale8(data, 25) + scale8(data, 255 - 25);
data = qsub8(data, 254 - currentPositionInfo.r8 * 2);
if (data > 253)
data = 253; // >= 254 shows up as the wrong color...bug in ColorFromPalette?
led = ColorFromPalette(currentPalette, data, 255, LINEARBLEND);
}
};
class AudioBurstPattern : public PatternClass
{
public:
static void setup(uint8_t micPin);
static const char* getName()
{
return "AUDB";
}
static void getPalette(CRGBPalette16 ¤tPalette)
{
Palettes::RainbowPalette::getPalette(currentPalette);
}
static void render(long now, CRGBPalette16 ¤tPalette)
{
int n = analogRead(micPin);
n = abs(n - 512);
n = (n <= noise) ? 0 : (n - noise);
lvl = ((lvl * 7) + n) >> 3;
height = top * lvl / 150;
if (height <= 1 && fixColor)
fixColor = false;
if (height > 1 && !fixColor)
{
fixColor = true;
burstColor += 64 + random8(64);
}
eachPixel(perPixel, now, currentPalette);
}
protected:
static uint8_t micPin;
static const int noise = 50;
static const int top = 9; // TODO: use radius instead of ring count
static int lvl;
static int height;
static uint8_t burstColor;
static bool fixColor;
static void perPixel(long now, CRGBPalette16 ¤tPalette, CRGB &led, PositionInfo currentPositionInfo)
{
led = currentPositionInfo.ring < height ? ColorFromPalette(currentPalette, burstColor) : CRGB::Black;
}
};
int AudioBurstPattern::lvl = 10;
int AudioBurstPattern::height = 0;
uint8_t AudioBurstPattern::burstColor = 0;
uint8_t AudioBurstPattern::micPin;
bool AudioBurstPattern::fixColor = false;
void AudioBurstPattern::setup(uint8_t micPin) {
AudioBurstPattern::micPin = micPin;
}
void Setup(CRGB* leds, const PositionInfo* ledInfo, int numLeds) {
PatternClass::setup(leds, ledInfo, numLeds);
}
void Setup(CRGB* leds, const PositionInfo* ledInfo, int numLeds, uint8_t micPin) {
PatternClass::setup(leds, ledInfo, numLeds);
AudioBurstPattern::setup(micPin);
}
struct Pattern {
GetName *getName;
GetPalette *getPalette;
Render *render;
};
static const Pattern PatternList[]{
AS_PATTERN(SpinPattern),
AS_PATTERN(VerticalPattern),
AS_PATTERN(SpiralPattern),
AS_PATTERN(RadialOutPattern),
AS_PATTERN(RadialInPattern),
AS_PATTERN(WavePattern),
AS_PATTERN(LavaPattern),
AS_PATTERN(FirePattern),
AS_PATTERN(AudioBurstPattern),
AS_PATTERN(ScanPattern),
AS_PATTERN(FadePattern),
AS_PATTERN(CloudsPattern),
};
}