-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulti_pattern.ino
More file actions
419 lines (347 loc) · 9.65 KB
/
multi_pattern.ino
File metadata and controls
419 lines (347 loc) · 9.65 KB
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
#include <assert.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <util/atomic.h>
#include <FastLED.h>
#define DATA_PIN 6
#define WIDTH 32
#define HEIGHT 8
#define NUM_LEDS (WIDTH * HEIGHT)
#define SEQUENCE_TIME (60L * 1000L)
CRGB leds[NUM_LEDS];
// For efficient memory usage, these are globals shared among patterns.
int16_t xt = 0; // Used for horizontal scrolling effects
int16_t yt = 0; // Used for vertical scrolling effects
uint16_t ht = 0; // Used for color cycling
uint16_t tt = 0; // Frame count to feed into the above
// Shared buffer for patterns that need scratch arrays
uint16_t data[NUM_LEDS];
// Global utilities
inline uint16_t mapY(uint8_t x, uint8_t y) {
return x & 1 ? HEIGHT - 1 - y : y;
}
inline uint16_t mapXY(uint8_t x, uint8_t y) {
return x * HEIGHT + mapY(x, y);
}
void toXY(uint16_t index, uint8_t &x, uint8_t &y) {
x = index / HEIGHT;
y = mapY(x, index % HEIGHT);
}
// https://sites.google.com/site/astudyofentropy/project-definition/timer-jitter-entropy-sources/entropy-library/arduino-random-seed
volatile uint32_t seed;
volatile int8_t nrot;
void createTrulyRandomSeed() {
seed = 0;
nrot = 32; // Must be at least 4, but more increased the uniformity of the produced
// seeds entropy.
// The following five lines of code turn on the watch dog timer interrupt to create
// the seed value
cli();
MCUSR = 0;
_WD_CONTROL_REG |= (1<<_WD_CHANGE_BIT) | (1<<WDE);
_WD_CONTROL_REG = (1<<WDIE);
sei();
while (nrot > 0); // wait here until seed is created
// The following five lines turn off the watch dog timer interrupt
cli();
MCUSR = 0;
_WD_CONTROL_REG |= (1<<_WD_CHANGE_BIT) | (0<<WDE);
_WD_CONTROL_REG = (0<< WDIE);
sei();
randomSeed(seed);
}
ISR(WDT_vect)
{
nrot--;
seed = seed << 8;
seed = seed ^ TCNT1L;
}
// PATTERN: Sinus
unsigned long beforeSinus() {
xt = sin16(tt * 40) * 4;
ht += 15;
return 0;
}
void renderSinus(uint16_t index, uint16_t x, int16_t y) {
const uint16_t xScale = 65536 / WIDTH;
const uint16_t yScale = 16384 / HEIGHT;
x *= xScale;
y *= yScale;
// Squeeze vertically just a little bit
y = (y - 8192) * 1.4 + 9830;
int16_t w = (int16_t)(sin16((x + xt) * 3) >> 2) + 8192;
uint16_t v = constrain(16384 - (w > y ? w - y : y - w) * 2, 0, 16384);
uint16_t h = constrain(16384 - (w > y ? w - y : y - w) / 2, 0, 16384) + ht;
leds[index] = CHSV(h >> 6, 255, v >> 6);
}
// PATTERN: Wander
#define NUM_WANDERERS 50
const uint8_t kWanderSpeed = 100;
uint16_t wander(uint16_t p) {
uint8_t x, y;
toXY(p, x, y);
uint8_t r = random(kWanderSpeed);
if (r == 0) {
// Wraps around a cylinder.
x = (x + 1) % WIDTH;
}
else if (r == 1) {
x = x == 0 ? WIDTH - 1 : x - 1;
}
else if (r == 2) {
y = constrain(y + 1, 0, HEIGHT - 1);
}
else if (r == 3) {
y = constrain(y - 1, 0, HEIGHT - 1);
}
return mapXY(x, y);
}
uint16_t *wars = (uint16_t *)data;
static_assert(
sizeof(typeof(*wars)) * NUM_WANDERERS <=
sizeof(data), "out_of_memory");
void setupWander() {
for (uint8_t i = 0; i < NUM_WANDERERS; i++) wars[i] = random(NUM_LEDS);
}
unsigned long beforeWander() {
fill_solid(leds, NUM_LEDS, CRGB::Black);
uint8_t gHue = 0;
uint8_t gHueDelta = 256 * 1 / NUM_WANDERERS;
for (uint8_t i = 0; i < NUM_WANDERERS; i++) {
wars[i] = wander(wars[i]);
leds[wars[i]] = CHSV(gHue, 255, 255);
gHue += gHueDelta;
}
return 25;
}
// PATTERN: SlideUp
unsigned long beforeSlideUp() {
for (uint8_t x = 0; x < WIDTH; x++) {
// slide up
for (uint8_t y = 0; y < HEIGHT - 1; y++) {
leds[mapXY(x, y)] = leds[mapXY(x, y + 1)];
}
// new ones enter at the bottom
leds[mapXY(x, HEIGHT - 1)] = random(10) < 2 ? CHSV(random(256), 255, 255) : CHSV(0, 0, 0);
}
return 90;
}
// PATTERN: Bounce
int16_t cx = 0, cy = 0, cx2 = 0, cy2 = HEIGHT - 1;
int8_t dx = 1, dy = 1, dx2 = 1, dy2 = -1;
void setupBounce() {
cx = 0;
cy = 0;
cx2 = 0;
cy2 = HEIGHT - 1;
dx = 1;
dy = 1;
dx2 = 1;
dy2 = -1;
xt = 0;
}
unsigned long beforeBounce() {
if (++xt > 8) {
xt = 0;
leds[mapXY(cx, cy)] = CHSV(sin8(tt / 31), 255, 255);
cx += dx;
if (cx < 0) cx += WIDTH;
if (cx >= WIDTH) cx -= WIDTH;
if (random(100) < 2) dx = -dx;
cy += dy;
if (cy >= HEIGHT - 1 || cy <= 0) dy = -dy;
leds[mapXY(cx2, cy2)] = CHSV(sin8(tt / 47) + 99, 255, 255);
cx2 += dx2;
if (cx2 < 0) cx2 += WIDTH;
if (cx2 >= WIDTH) cx2 -= WIDTH;
if (random(100) < 2) dx2 = -dx2;
cy2 += dy2;
if (cy2 >= HEIGHT - 1 || cy2 <= 0) dy2 = -dy2;
}
else {
leds[mapXY(cx, cy)] = CRGB::Black;
leds[mapXY(cx2, cy2)] = CRGB::Black;
}
return 0;
}
// PATTERN: Rain
int16_t *cols = (int16_t *)data;
uint8_t *speed = (uint8_t *)(cols + WIDTH);
uint8_t *color = (uint8_t *)(speed + WIDTH);
static_assert(
sizeof(typeof(*cols)) * WIDTH +
sizeof(typeof(*speed)) * WIDTH +
sizeof(typeof(*color)) * WIDTH <=
sizeof(data), "out_of_memory");
void setupRain() {
for (uint8_t x = 0; x < WIDTH; x++) {
cols[x] = -1;
}
}
unsigned long beforeRain() {
for (uint8_t x = 0; x < WIDTH; x++)
if (cols[x] >= 0 && tt % speed[x] == 0)
if (++cols[x] >= HEIGHT)
cols[x] = -1;
if (random(40) < 4) {
uint8_t c = random(WIDTH);
if (cols[c] == -1) {
cols[c] = 0;
speed[c] = 5 + random(7);
color[c] = 130 + random(33);
}
}
return 0;
}
void renderRain(uint16_t index, uint16_t x, int16_t y) {
uint8_t h = color[x];
uint8_t s = 255;
uint8_t v = cols[x] == 0 ? 255 : 255 - 32 * (cols[x] - 1);
leds[index] = CHSV(h, s, cols[x] == y ? v : 0);
}
// PATTERN: BlinkFade
uint8_t *bfVals = (uint8_t *)data;
uint8_t *bfHues = (uint8_t *)(bfVals + NUM_LEDS);
static_assert(
sizeof(typeof(*bfVals)) * NUM_LEDS +
sizeof(typeof(*bfHues)) * NUM_LEDS <=
sizeof(data), "out_of_memory");
void setupBlinkFade() {
for (uint16_t i = 0; i < NUM_LEDS; i++) {
bfVals[i] = 0;
}
}
unsigned long beforeBlinkFade() {
ht += 1;
for (uint16_t i = 0; i < NUM_LEDS; i++) {
bfVals[i] = qsub8(bfVals[i], 1);
if (bfVals[i] == 0) {
bfVals[i] = random(256);
bfHues[i] = ht / 2 + i / 5;
}
}
return 0;
}
void renderBlinkFade(uint16_t index, uint16_t x, int16_t y) {
uint8_t v = bfVals[index];
leds[index] = CHSV(bfHues[index], 255, v);
}
// PATTERN: Shuffle
#define SHUFFLE_LEDS 40
uint16_t *shOrder = (uint16_t *)data;
uint8_t *shHues = (uint8_t *)(shOrder + SHUFFLE_LEDS);
static_assert(
sizeof(typeof(*shOrder)) * SHUFFLE_LEDS +
sizeof(typeof(*shHues)) * SHUFFLE_LEDS <=
sizeof(data), "out_of_memory");
void setupShuffle() {
// Use xt for pointer to end of pixel list
xt = 0;
for (uint16_t i = 0; i < SHUFFLE_LEDS; i++) {
shOrder[i] = i;
shHues[i] = 1 + i * 10; // XXX 0
}
}
unsigned long beforeShuffle() {
uint16_t tail = xt + 1;
if (tail >= SHUFFLE_LEDS) tail = 0;
leds[shOrder[tail]] = CRGB::Black;
leds[shOrder[xt]] = CHSV(shHues[xt], 150, 255);
xt = tail;
shOrder[xt] = random(NUM_LEDS);
shHues[xt] = random(256);
return 30;
}
// PATTERN: WavesInSpace
unsigned long beforeWavesInSpace() {
xt = sin8(tt / 5) + sin8(tt / 4) * 4 + sin8(tt / 3);
yt = sin8(tt / 3) / 2 + sin8(tt / 4) / 2 + sin8(tt / 5);
ht += 3;
return 0;
}
void renderWavesInSpace(uint16_t index, uint16_t x, int16_t y) {
const uint16_t xScale = 256 / WIDTH;
const uint16_t yScale = 256 / HEIGHT;
uint16_t v = (uint16_t)triwave8(x * 4 * xScale + xt) * (uint16_t)triwave8(y * yScale + yt);
if (v < 25000) v = 0;
leds[index] = CHSV(x * 6 + sin8(ht >> 6), 255, v >> 8);
}
// Pattern catalog
struct pattern {
void (*setup)(void);
unsigned long (*beforeRender)(void);
void (*renderXY)(uint16_t index, uint16_t x, int16_t y);
};
pattern patterns[] = {
{ setupBounce, beforeBounce, NULL },
{ NULL, beforeSinus, renderSinus },
{ setupWander, beforeWander, NULL },
{ NULL, beforeSlideUp, NULL },
{ setupRain, beforeRain, renderRain },
{ setupBlinkFade, beforeBlinkFade, renderBlinkFade },
{ setupShuffle, beforeShuffle, NULL },
{ NULL, beforeWavesInSpace, renderWavesInSpace },
};
const size_t NUM_PATTERNS = sizeof patterns / sizeof *patterns;
// Driver program
size_t pi;
unsigned long startTime;
void setup() {
#if DEBUG_ENABLE
Serial.begin(115200);
Serial.print(F("NUM_PATTERNS = "));
Serial.println(NUM_PATTERNS);
Serial.flush();
#endif
createTrulyRandomSeed();
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(30);
startTime = millis();
pi = random(NUM_PATTERNS);
}
void loop() {
if (tt == 0) {
// Start new pattern
#if DEBUG_ENABLE
Serial.print(F("Starting pattern "));
Serial.println(pi);
Serial.flush();
#endif
fill_solid(leds, NUM_LEDS, CRGB::Black);
if (*patterns[pi].setup) (*patterns[pi].setup)();
}
unsigned long delayms = 0;
if (*patterns[pi].beforeRender) delayms = (*patterns[pi].beforeRender)();
if (*patterns[pi].renderXY) {
// call the renderXY function for each pixel
uint16_t index = 0;
for (uint8_t x = 0; x < WIDTH; x++) {
for (uint8_t y = 0; y < HEIGHT; y++) {
(*patterns[pi].renderXY)(index, x, mapY(x, y));
index++;
}
}
}
FastLED.show();
delay(delayms);
tt += 1;
unsigned long m = millis();
if (m - startTime > SEQUENCE_TIME) {
#if DEBUG_ENABLE
Serial.print(F("End pattern "));
Serial.print(pi);
Serial.print(F(": Time "));
Serial.print(m);
Serial.print(F(" - "));
Serial.print(startTime);
Serial.print(F(" ("));
Serial.print(m - startTime);
Serial.print(F(") > "));
Serial.println(SEQUENCE_TIME);
Serial.flush();
#endif
if (++pi >= NUM_PATTERNS) pi = 0;
startTime = m;
tt = 0;
}
}