-
Notifications
You must be signed in to change notification settings - Fork 0
/
bbq_hut.ino
493 lines (399 loc) · 10.8 KB
/
bbq_hut.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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
// https://www.selecolor.com/en/hsv-color-picker/
// but it goes to 255 in CHSV for S and V
#include <FastLED.h>
#define BUTTON_PIN 2
#define LED_PIN 6
#define NUM_LEDS 1007 + 1 // actual number + 1 -> extra is used for out of bounds indexed setting
#define MAX_BRIGHTNESS 255
typedef CHSV (*FunctionPointerHSV)(CHSV, int);
typedef CRGB (*FunctionPointerRGB)(CRGB, int);
enum IndexingType {
NORMAL,
REVERSE,
MIDDLE,
MIDDLE_TOP,
};
enum Side {
LEFT,
TOP,
RIGHT,
BOTTOM,
};
enum Direction {
FORWARDS,
BACKWARDS,
};
CRGB leds[NUM_LEDS];
int nLeds[6] = {
168,
168,
168,
168,
167,
168,
};
int sideLengths[4] = {
54, 10, 54, 50,
};
int modeIdx = 0;
int nModes = 8;
void setup(void) {
pinMode(BUTTON_PIN, INPUT_PULLUP);
FastLED.addLeds<WS2812B, 6, GRB>(leds, NUM_LEDS);
setAllBlack();
delay(20);
}
void loop(void) {
// Check for keypress
if (!digitalRead(BUTTON_PIN)) {
delay(100);
// wait for it to turn off
while (!digitalRead(BUTTON_PIN)) {
// spin loop
delay(1);
}
modeIdx += 1;
modeIdx %= nModes;
}
switch (modeIdx) {
case 0:
setAllBlack();
break;
case 1:
messageSenderHSV(hueChange, 1, MIDDLE, CHSV(235, 255, 255));
break;
case 2:
messageSenderHSV(hueChangeIndexed, 5, MIDDLE, CHSV(0, 0, 0));
break;
case 3:
messageSenderHSV(hueLerp, 1, MIDDLE, CHSV(0, 0, 0));
break;
case 4:
messageSenderRGB(twoColorLerpLCH, 1, MIDDLE, CRGB(0, 0, 0));
break;
case 5:
messageSenderRGB(hueChangeIndexedRGB, 1, MIDDLE, CRGB(0, 0, 0));
break;
case 6:
circlingDot();
break;
case 7:
circlingPulse();
break;
}
}
CHSV hueLerp(CHSV oldColor, int changeIdx) {
int fromHue = 120;
int toHue = 155;
int speed = 10;
int fract = (changeIdx * speed) % 511 - 255;
if (fract < 0) {
fract *= -1;
}
int hue = lerp8by8(fromHue, toHue, fract);
return CHSV(hue, 255, 255);
}
CHSV hueChangeIndexed(CHSV oldColor, int changeIdx) {
int speed = 20;
int hue = changeIdx * speed;
hue %= 255;
return CHSV(hue, 255, 255);
}
CHSV hueChange(CHSV oldColor, int changeIdx) {
int hue = oldColor.hue;
hue += 20;
hue %= 255;
CHSV newColor = oldColor;
newColor.hue = hue;
return newColor;
}
// message delay should be 1 or more. 1 means message is sent every step
void messageSenderHSV(FunctionPointerHSV changeFunction, int messageDelay, IndexingType indexing, CHSV startColor) {
#define N_MESSAGES 85
CHSV colors[N_MESSAGES];
for (int i = 0; i <= N_MESSAGES - 1; i++) {
colors[i] = startColor;
}
int changeIdx = 0;
while (true) {
if (isTryingToSwitchMode()) {
break;
}
for (int i = N_MESSAGES; i > 0; i--) {
colors[i] = colors[(i + N_MESSAGES - 1) % N_MESSAGES];
}
colors[0] = changeFunction(colors[messageDelay], changeIdx / messageDelay);
for (int i = 0; i <= N_MESSAGES - 1; i++) {
for (int t = 0; t <= 6 - 1; t++) {
leds[index(t, i, indexing)] = colors[i];
leds[index(t, 170-i, indexing)] = colors[i];
}
}
FastLED.show();
delay(40);
changeIdx += 1;
}
}
CRGB twoColorLerpLCH(CRGB oldColor, int changeIdx) {
double f_l = 53.62576010848221;
double f_c = 58.118145058923176;
double f_h = 51.33352103353704;
double t_l = 43.18878705912335;
double t_c = 60.471578319098455;
double t_h = 284.33499171833785;
int speed = 10;
int fract = (changeIdx * speed) % 511 - 255;
if (fract < 0) {
fract *= -1;
}
double l = lerpDouble(f_l, t_l, fract);
double c = lerpDouble(f_c, t_c, fract);
double h = lerpDouble(f_h, t_h, fract);
return LCHtoRGB(l, c, h);
}
CRGB hueChangeIndexedRGB(CRGB oldColor, int changeIdx) {
int speed = 20;
int fract = (changeIdx * speed) % 511 - 255;
if (fract < 0) {
fract *= -1;
}
double normalized = (double)fract / 255.0;
double transformed = pow(normalized, 4.0) * 255.0;
return CRGB((int)transformed, 0, 0);
}
double lerpDouble(double from, double to, int fract) {
return from + (( to - from ) * (double)fract) / 256;
}
// message delay should be 1 or more. 1 means message is sent every step
void messageSenderRGB(FunctionPointerRGB changeFunction, int messageDelay, IndexingType indexing, CRGB startColor) {
#define N_MESSAGES 85
CRGB colors[N_MESSAGES];
for (int i = 0; i <= N_MESSAGES - 1; i++) {
colors[i] = startColor;
}
int changeIdx = 0;
while (true) {
if (isTryingToSwitchMode()) {
break;
}
for (int i = N_MESSAGES; i > 0; i--) {
colors[i] = colors[(i + N_MESSAGES - 1) % N_MESSAGES];
}
colors[0] = changeFunction(colors[messageDelay], changeIdx / messageDelay);
for (int i = 0; i <= N_MESSAGES - 1; i++) {
for (int t = 0; t <= 6 - 1; t++) {
leds[index(t, i, indexing)] = colors[i];
leds[index(t, 170-i, indexing)] = colors[i];
}
}
FastLED.show();
delay(40);
changeIdx += 1;
}
}
void setTapestryBlack(int tapestryIdx) {
setTapestry(tapestryIdx, CRGB(0, 0, 0));
}
void setTapestry(int tapestryIdx, CRGB color) {
for (int i = 0; i <= 170 - 1; i++) {
leds[index(tapestryIdx, i, NORMAL)] = color;
}
FastLED.show();
}
void setAllBlack() {
for (int i = 0; i <= 6 - 1; i++) {
setTapestryBlack(i);
}
}
void setAll(CRGB color) {
for (int i = 0; i <= 6 - 1; i++) {
setTapestry(i, color);
}
}
int offsetForBottomMiddle[6] = {
143,
143,
143,
143,
143,
143,
};
int offsetForTopMiddle[6] = {
70,
70,
70,
70,
70,
70,
};
// returns dummy led idx if out of bounds for the tapestry
int index(int tapestryIdx, int idx, IndexingType indexing) {
int padding = 0;
for (int i = 0; i <= tapestryIdx - 1; i++) {
padding += nLeds[i];
}
// return dummy led if out of bounds
if (idx >= nLeds[tapestryIdx]) {
return NUM_LEDS;
}
switch (indexing) {
case NORMAL:
return padding + idx;
case REVERSE:
return padding + (nLeds[tapestryIdx] - idx);
case MIDDLE:
return padding + (idx + offsetForBottomMiddle[tapestryIdx]) % nLeds[tapestryIdx];
case MIDDLE_TOP:
return padding + (idx + offsetForTopMiddle[tapestryIdx]) % nLeds[tapestryIdx];
default:
return padding + idx; // same as normal indexing
}
}
int sideIndex(int tapestryIdx, int idx, Side side, Direction direction) {
int tapestryPadding = 0;
for (int i = 0; i <= tapestryIdx - 1; i++) {
tapestryPadding += nLeds[i];
}
int sidePadding = 0;
for (int i = 0; i <= (int)side - 1; i++) {
sidePadding += sideLengths[i];
}
// return dummy led if out of bounds
if (idx >= sideLengths[(int)side]) {
return NUM_LEDS;
}
if (direction == FORWARDS) {
return tapestryPadding + sidePadding + idx;
} else {
return tapestryPadding + sidePadding + (sideLengths[(int)side] - idx);
}
}
int circularIndex(int idx, Side side) {
int tapestry_idx = idx / sideLengths[(int)side];
int side_idx = idx % sideLengths[(int)side];
int transformed_idx = sideIndex(tapestry_idx, side_idx, side, FORWARDS);
return transformed_idx;
}
// pulse(i % 10, 5)
// min value will be 0 max will be 5
int pulse(int n, int max_val) {
if (n > max_val) {
n -= max_val * 2;
n *= -1;
}
return n;
}
void circlingDot() {
int changeIdx = 0;
while (true) {
if (isTryingToSwitchMode()) {
break;
}
setAllBlack();
// 0 to 1
double pattern_progress = (double)(changeIdx % 100) / 100.0;
double tapestry_progress = fmod(pattern_progress * 6.0, 1.0);
int tapestry_idx = (int)(pattern_progress * 6);
{
int side = 1;
int side_idx = (int)(tapestry_progress * sideLengths[side]);
int transformed_idx = sideIndex(tapestry_idx, side_idx, side, 0);
leds[transformed_idx] = CRGB(255, 0, 0);
}
{
int side = 3;
int side_idx = (int)(tapestry_progress * sideLengths[side]);
int transformed_idx = sideIndex(tapestry_idx, side_idx, side, 0);
leds[transformed_idx] = CRGB(255, 0, 0);
}
changeIdx += 1;
}
}
void circlingPulse() {
int changeIdx = 0;
while (true) {
if (isTryingToSwitchMode()) {
break;
}
setAllBlack();
int side = LEFT;
int maximum = sideLengths[(int)side] * 6;
int n_dots = 20;
for (int t = 0; t <= n_dots - 1; t++) {
int i = (changeIdx + t) % maximum;
int transformed_idx = circularIndex(i, side);
double intensity = (double)pulse(t, n_dots / 2) / ((double)n_dots / 2.0);
int color = (int)(255.0 * powf(intensity, 1.3));
leds[transformed_idx] = CRGB(color, 0, 0);
}
changeIdx += 1;
}
}
bool isTryingToSwitchMode() {
return !digitalRead(BUTTON_PIN);
}
//////////////////
/// LCH TO RGB ///
//////////////////
void _LCHtoLab(double L, double C, double H, double *l, double *a, double *b) {
*l = L;
*a = cos(H * M_PI / 180.0) * C;
*b = sin(H * M_PI / 180.0) * C;
}
// Function to convert Lab to XYZ
void _LabToXYZ(double L, double A, double B, double *X, double *Y, double *Z) {
double epsilon = 0.008856; // Intent for use in a function or algorithm
double kappa = 903.3; // Intent for use in a function or algorithm
double Xn = 0.95047; // Assuming D65 illuminant
double Yn = 1.00000; // Assuming D65 illuminant
double Zn = 1.08883; // Assuming D65 illuminant
*Y = L > (kappa * epsilon) ? pow((L + 16) / 116, 3) : L / kappa;
double fY = L > (kappa * epsilon) ? pow(*Y, 1.0/3.0) : (kappa * *Y + 16) / 116;
double fX = A / 500 + fY;
double fZ = fY - B / 200;
*X = pow(fX, 3) > epsilon ? pow(fX, 3) : (116 * fX - 16) / kappa;
*Z = pow(fZ, 3) > epsilon ? pow(fZ, 3) : (116 * fZ - 16) / kappa;
*X *= Xn;
*Y *= Yn;
*Z *= Zn;
}
// Function to clamp RGB values
double _clamp(double value, double min, double max) {
if (value < min) return min;
if (value > max) return max;
return value;
}
// Function to apply gamma correction
double _gammaCorrect(double value) {
if (value <= 0.0031308) {
return 12.92 * value;
} else {
return 1.055 * pow(value, 1.0 / 2.4) - 0.055;
}
}
// Function to convert XYZ to RGB
CRGB _XYZtoRGB(double X, double Y, double Z) {
// Assuming sRGB color space and D65 white point
double R = 3.2406 * X - 1.5372 * Y - 0.4986 * Z;
double G = -0.9689 * X + 1.8758 * Y + 0.0415 * Z;
double B = 0.0557 * X - 0.2040 * Y + 1.0570 * Z;
// Clamp values to the 0-1 range
R = _clamp(R, 0.0, 1.0);
G = _clamp(G, 0.0, 1.0);
B = _clamp(B, 0.0, 1.0);
// Apply gamma correction
R = _gammaCorrect(R);
G = _gammaCorrect(G);
B = _gammaCorrect(B);
return CRGB((int)(R * 255), (int)(G * 255), (int)(B * 255));
}
// L darkness: black 0-100 white
// C intensity: ?
// H hue: 0-360
CRGB LCHtoRGB(double L, double C, double H) {
double l, a, b;
double X, Y, Z;
_LCHtoLab(L, C, H, &l, &a, &b);
_LabToXYZ(l, a, b, &X, &Y, &Z);
return _XYZtoRGB(X, Y, Z);
}