-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrack_assistant_helpers.h
More file actions
1540 lines (1405 loc) · 40.9 KB
/
Copy pathrack_assistant_helpers.h
File metadata and controls
1540 lines (1405 loc) · 40.9 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
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include "esphome/components/light/addressable_light.h"
#include "esphome/core/color.h"
#include "esphome/core/hal.h"
#include <algorithm>
#include <array>
#include <cctype>
#include <cmath>
#include <cstdint>
#include <string>
#include <vector>
#ifndef RACK_UNIT_COUNT
#define RACK_UNIT_COUNT 15
#endif
namespace rack_fx {
struct Segment {
int start;
int width;
};
struct RgbwChannels {
float red;
float green;
float blue;
float white;
};
constexpr int RACK_MODE_MANUAL = 0;
constexpr int RACK_MODE_ZONE_STATIC = 1;
constexpr int RACK_MODE_ZONE_BREATHING_SELECTED = 2;
constexpr int RACK_MODE_ZONE_BREATHING_BLUE = 3;
constexpr int RACK_MODE_ZONE_BREATHING_ORANGE = 4;
constexpr int RACK_MODE_ZONE_BREATHING_RED = 5;
constexpr int RACK_MODE_CALIBRATION_BLOCK = 6;
constexpr int RACK_MODE_DOOR_OPEN = 7;
constexpr int RACK_MODE_RESTORE_TRANSITION = 8;
constexpr int RACK_MODE_U_ALERTS = 9;
constexpr int RACK_MODE_ZONE_BREATHING_WHITE = 10;
constexpr int RACK_MODE_INTERNET_DOWN = 11;
constexpr int RACK_MODE_CENTER_BURST_ERROR = 12;
constexpr int RACK_MODE_CENTER_BURST_SUCCESS = 13;
constexpr int RACK_MODE_U_SCAN_RED = 14;
constexpr int RACK_MODE_U_SCAN_SELECTED = 15;
constexpr int BREATHING_CURVE_SMOOTH = 0;
constexpr int BREATHING_CURVE_DYNAMIC = 1;
constexpr int BREATHING_CURVE_NATURAL = 2;
constexpr int ALERT_NONE = 0;
constexpr int ALERT_SELECTED_STATIC = 1;
constexpr int ALERT_SELECTED_BREATHING = 2;
constexpr int ALERT_UNIFI_BREATHING = 3;
constexpr int ALERT_ORANGE_BREATHING = 4;
constexpr int ALERT_RED_BREATHING = 5;
constexpr int ALERT_WHITE_BREATHING = 6;
constexpr int ALERT_RED_BLINK = 7;
constexpr int ALERT_WHITE_BLINK = 8;
constexpr int ALERT_UNIFI_BLINK = 9;
constexpr int ALERT_ORANGE_BLINK = 10;
constexpr int ALERT_CUSTOM_STATIC = 11;
constexpr int ALERT_CUSTOM_BREATHING = 12;
constexpr int ALERT_CUSTOM_BLINK = 13;
constexpr int ALERT_WHITE_STATIC = 14;
constexpr int ALERT_SELECTED_BLINK = 15;
struct UAlertSlot {
bool active = false;
int first_u = 1;
int last_u = 1;
int mode = ALERT_NONE;
uint32_t start_ms = 0;
float red = 0.0f;
float green = 0.0f;
float blue = 0.0f;
float white = 0.0f;
};
inline float clamp01(float value) {
if (value < 0.0f) {
return 0.0f;
}
if (value > 1.0f) {
return 1.0f;
}
return value;
}
inline uint32_t breathing_cycle_ms(float seconds) {
auto cycle = uint32_t(seconds * 1000.0f);
return cycle < 1000 ? 1000 : cycle;
}
inline void clamp_brightness_range(float &minimum, float &maximum) {
minimum = clamp01(minimum);
if (maximum < minimum + 0.02f) {
maximum = minimum + 0.02f;
}
maximum = clamp01(maximum);
}
inline float smootherstep(float value) {
return value * value * value * (value * (value * 6.0f - 15.0f) + 10.0f);
}
inline float ease_in_out_sine(float value) {
value = clamp01(value);
return 0.5f - 0.5f * cosf(value * 3.14159265f);
}
inline int breathing_curve_mode(const std::string &option) {
if (option == "Dynamic") {
return BREATHING_CURVE_DYNAMIC;
}
if (option == "Natural") {
return BREATHING_CURVE_NATURAL;
}
return BREATHING_CURVE_SMOOTH;
}
inline float breathing_curve(uint32_t start_ms, uint32_t cycle_ms, int curve_mode) {
const float phase = ((millis() - start_ms) % cycle_ms) / float(cycle_ms);
float eased;
if (curve_mode == BREATHING_CURVE_NATURAL) {
constexpr float inhale_time = 0.40f;
constexpr float exhale_time = 1.0f - inhale_time;
if (phase < inhale_time) {
eased = ease_in_out_sine(phase / inhale_time);
} else {
const float exhale = (phase - inhale_time) / exhale_time;
eased = 1.0f - powf(clamp01(exhale), 1.15f);
}
} else if (curve_mode == BREATHING_CURVE_DYNAMIC) {
constexpr float bottom_hold = 0.015f;
constexpr float inhale_time = 0.39f;
constexpr float exhale_time = 1.0f - bottom_hold - inhale_time;
if (phase < bottom_hold) {
eased = 0.0f;
} else if (phase < bottom_hold + inhale_time) {
const float inhale = (phase - bottom_hold) / inhale_time;
eased = ease_in_out_sine(inhale);
} else {
const float exhale = (phase - bottom_hold - inhale_time) / exhale_time;
eased = 1.0f - powf(clamp01(exhale), 1.35f);
}
} else {
const float wave = 0.5f - 0.5f * cosf(phase * 6.2831853f);
eased = wave;
}
return clamp01(eased);
}
inline float breathing_level(uint32_t start_ms, uint32_t cycle_ms, float minimum, float maximum, int curve_mode) {
const float eased = breathing_curve(start_ms, cycle_ms, curve_mode);
return minimum + (maximum - minimum) * eased;
}
inline float transition_progress(uint32_t start_ms, float duration_ms) {
const float progress = clamp01((millis() - start_ms) / duration_ms);
return smootherstep(progress);
}
inline uint8_t scaled_channel(float value) {
return uint8_t(roundf(clamp01(value) * 255.0f));
}
inline uint8_t dithered_channel(float value, int index, uint8_t salt);
inline void clamp_segment(int &start, int &width, int total) {
if (width <= 0 || total <= 0) {
width = 0;
return;
}
if (start < 0) {
start = 0;
}
if (start >= total) {
start = total - 1;
}
if (start + width > total) {
width = total - start;
}
}
inline int clamp_int(int value, int minimum, int maximum) {
if (value < minimum) {
return minimum;
}
if (value > maximum) {
return maximum;
}
return value;
}
inline float clamp_channel(float value) {
if (value < 0.0f) {
return 0.0f;
}
if (value > 255.0f) {
return 255.0f;
}
return value;
}
inline void separate_rgbw_unit(float &red, float &green, float &blue, float &white) {
red = clamp01(red);
green = clamp01(green);
blue = clamp01(blue);
white = clamp01(white);
if (red > 0.0f || green > 0.0f || blue > 0.0f) {
white = 0.0f;
} else {
red = 0.0f;
green = 0.0f;
blue = 0.0f;
}
}
inline void separate_rgbw_channels(float &red, float &green, float &blue, float &white) {
red = clamp_channel(red);
green = clamp_channel(green);
blue = clamp_channel(blue);
white = clamp_channel(white);
if (red > 0.0f || green > 0.0f || blue > 0.0f) {
white = 0.0f;
} else {
red = 0.0f;
green = 0.0f;
blue = 0.0f;
}
}
inline RgbwChannels separated_rgbw_unit(float red, float green, float blue, float white) {
separate_rgbw_unit(red, green, blue, white);
return RgbwChannels{red, green, blue, white};
}
inline RgbwChannels separated_rgbw_channels(float red, float green, float blue, float white) {
separate_rgbw_channels(red, green, blue, white);
return RgbwChannels{red, green, blue, white};
}
inline bool rack_mode_is_zone_breathing(int mode) {
return mode == RACK_MODE_ZONE_BREATHING_SELECTED ||
mode == RACK_MODE_ZONE_BREATHING_BLUE ||
mode == RACK_MODE_ZONE_BREATHING_ORANGE ||
mode == RACK_MODE_ZONE_BREATHING_RED ||
mode == RACK_MODE_ZONE_BREATHING_WHITE ||
mode == RACK_MODE_INTERNET_DOWN;
}
inline RgbwChannels rack_mode_breathing_channels(
int mode,
const esphome::Color &selected_color,
float level
) {
switch (mode) {
case RACK_MODE_ZONE_BREATHING_WHITE:
return RgbwChannels{0.0f, 0.0f, 0.0f, level * 255.0f};
case RACK_MODE_ZONE_BREATHING_BLUE:
return RgbwChannels{0.0f, level * 111.0f, level * 255.0f, 0.0f};
case RACK_MODE_ZONE_BREATHING_ORANGE:
return RgbwChannels{level * 255.0f, level * 82.0f, 0.0f, 0.0f};
case RACK_MODE_INTERNET_DOWN:
case RACK_MODE_ZONE_BREATHING_RED:
return RgbwChannels{level * 255.0f, 0.0f, 0.0f, 0.0f};
case RACK_MODE_ZONE_BREATHING_SELECTED:
default:
return RgbwChannels{
selected_color.r * level,
selected_color.g * level,
selected_color.b * level,
0.0f
};
}
}
inline std::string normalize_name(std::string value) {
for (char &ch : value) {
if (ch == ' ' || ch == '-') {
ch = '_';
} else {
ch = char(std::tolower(static_cast<unsigned char>(ch)));
}
}
return value;
}
inline bool contains(const std::string &text, const char *needle) {
return text.find(needle) != std::string::npos;
}
inline bool is_clear_alert_option(const std::string &effect) {
const std::string name = normalize_name(effect);
return name == "no_alert" || name == "none" || name == "off";
}
inline std::string canonical_alert_option(const std::string &effect) {
if (is_clear_alert_option(effect)) {
return "No alert";
}
const std::string name = normalize_name(effect);
const bool selected = contains(name, "selected");
const bool red = contains(name, "red");
const bool orange = contains(name, "orange");
const bool white = contains(name, "white");
const bool blue = contains(name, "blue");
if (selected) {
return "Selected color blink";
}
if (white) {
return "White blink";
}
if (orange) {
return "Orange blink";
}
if (red) {
return "Red blink";
}
if (blue) {
return "Blue blink";
}
return "Red blink";
}
inline std::string canonical_mode_name(const std::string &effect) {
if (effect == "Manual / no effect" || effect == "None") {
return "Manual";
}
if (effect == "Selected color breathing") {
return "Selected color breathing";
}
if (effect == "Blue breathing" || effect == "Blue breathing") {
return "Blue breathing";
}
if (effect == "U alert") {
return "U alerts";
}
if (effect == "Selected color U scan") {
return "Selected color U scan";
}
if (effect == "Rainbow") {
return "Rainbow";
}
if (effect == "Success" || effect == "Success") {
return "Success";
}
if (effect == "Error") {
return "Error";
}
return effect;
}
inline bool is_manual_mode_option(const std::string &effect) {
return canonical_mode_name(effect) == "Manual";
}
inline bool is_alert_layer_mode_option(const std::string &effect) {
return canonical_mode_name(effect) == "U alerts";
}
inline bool is_mode_select_effect(const std::string &effect) {
const std::string name = canonical_mode_name(effect);
return name == "White breathing" ||
name == "Selected color breathing" ||
name == "Blue breathing" ||
name == "Orange breathing" ||
name == "Red breathing" ||
name == "Internet down" ||
name == "Red U scan" ||
name == "Selected color U scan" ||
name == "White wipe" ||
name == "Red wipe" ||
name == "White scan" ||
name == "Rainbow" ||
name == "Error" ||
name == "Success";
}
inline int internal_mode_for_select_option(const std::string &effect) {
const std::string name = canonical_mode_name(effect);
if (name == "White breathing") {
return RACK_MODE_ZONE_BREATHING_WHITE;
}
if (name == "Selected color breathing") {
return RACK_MODE_ZONE_BREATHING_SELECTED;
}
if (name == "Blue breathing") {
return RACK_MODE_ZONE_BREATHING_BLUE;
}
if (name == "Orange breathing") {
return RACK_MODE_ZONE_BREATHING_ORANGE;
}
if (name == "Red breathing") {
return RACK_MODE_ZONE_BREATHING_RED;
}
if (name == "Internet down") {
return RACK_MODE_INTERNET_DOWN;
}
if (name == "Error") {
return RACK_MODE_CENTER_BURST_ERROR;
}
if (name == "Success") {
return RACK_MODE_CENTER_BURST_SUCCESS;
}
if (name == "Red U scan") {
return RACK_MODE_U_SCAN_RED;
}
if (name == "Selected color U scan") {
return RACK_MODE_U_SCAN_SELECTED;
}
return RACK_MODE_MANUAL;
}
inline std::string select_option_for_internal_mode(int mode) {
switch (mode) {
case RACK_MODE_U_ALERTS:
return "U alerts";
case RACK_MODE_ZONE_BREATHING_WHITE:
return "White breathing";
case RACK_MODE_ZONE_BREATHING_SELECTED:
return "Selected color breathing";
case RACK_MODE_ZONE_BREATHING_BLUE:
return "Blue breathing";
case RACK_MODE_ZONE_BREATHING_ORANGE:
return "Orange breathing";
case RACK_MODE_ZONE_BREATHING_RED:
return "Red breathing";
case RACK_MODE_INTERNET_DOWN:
return "Internet down";
case RACK_MODE_CENTER_BURST_ERROR:
return "Error";
case RACK_MODE_CENTER_BURST_SUCCESS:
return "Success";
case RACK_MODE_U_SCAN_RED:
return "Red U scan";
case RACK_MODE_U_SCAN_SELECTED:
return "Selected color U scan";
default:
return "";
}
}
inline int alert_mode_from_name(const std::string &effect, bool custom_color) {
if (is_clear_alert_option(effect)) {
return ALERT_NONE;
}
const std::string name = normalize_name(effect);
const bool blink = contains(name, "blink");
const bool fixed = contains(name, "static");
const bool selected = contains(name, "selected");
const bool red = contains(name, "red");
const bool orange = contains(name, "orange");
const bool white = contains(name, "white");
const bool blue = contains(name, "blue");
if (custom_color) {
if (blink) {
return ALERT_CUSTOM_BLINK;
}
if (fixed) {
return ALERT_CUSTOM_STATIC;
}
return ALERT_CUSTOM_BREATHING;
}
if (selected) {
return ALERT_SELECTED_BLINK;
}
if (white) {
return ALERT_WHITE_BLINK;
}
if (blue) {
return ALERT_UNIFI_BLINK;
}
if (orange) {
return ALERT_ORANGE_BLINK;
}
if (red) {
return ALERT_RED_BLINK;
}
return ALERT_RED_BLINK;
}
inline Segment rack_u_segment(int u, int side_start, int side_len, bool reversed_side) {
u = clamp_int(u, 1, RACK_UNIT_COUNT);
const int start_offset = int(roundf((u - 1) * side_len / float(RACK_UNIT_COUNT)));
const int end_offset = int(roundf(u * side_len / float(RACK_UNIT_COUNT)));
const int width = std::max(1, end_offset - start_offset);
if (!reversed_side) {
return Segment{side_start + start_offset, width};
}
const int side_end = side_start + side_len - 1;
return Segment{side_end - (end_offset - 1), width};
}
inline bool is_in_segment(int index, int start, int width) {
return width > 0 && index >= start && index < start + width;
}
inline bool is_in_visible_rack(
int index,
int bottom_start,
int bottom_len,
int left_start,
int side_len,
int top_start,
int top_len,
int right_start
) {
return is_in_segment(index, bottom_start, bottom_len) ||
is_in_segment(index, left_start, side_len) ||
is_in_segment(index, top_start, top_len) ||
is_in_segment(index, right_start, side_len);
}
inline std::string canonical_zone_option(const std::string &option) {
if (option == "Laterales" || option == "Side rails") {
return "Side rails";
}
if (option == "Arriba" || option == "Top only") {
return "Top only";
}
if (option == "Abajo" || option == "Bottom only") {
return "Bottom only";
}
return "Whole rack";
}
inline bool is_in_u_range(int index, int first_u, int last_u, int left_start, int side_len, int right_start) {
first_u = clamp_int(first_u, 1, RACK_UNIT_COUNT);
last_u = clamp_int(last_u, 1, RACK_UNIT_COUNT);
if (first_u > last_u) {
std::swap(first_u, last_u);
}
for (int u = first_u; u <= last_u; u++) {
const Segment left = rack_u_segment(u, left_start, side_len, false);
const Segment right = rack_u_segment(u, right_start, side_len, true);
if (is_in_segment(index, left.start, left.width) || is_in_segment(index, right.start, right.width)) {
return true;
}
}
return false;
}
inline bool is_in_zone(
int index,
const std::string &zone_mode,
int first_u,
int last_u,
int bottom_start,
int bottom_len,
int left_start,
int side_len,
int top_start,
int top_len,
int right_start
) {
if (zone_mode == "Whole rack") {
return is_in_visible_rack(index, bottom_start, bottom_len, left_start, side_len, top_start, top_len, right_start);
}
if (zone_mode == "Side rails") {
return is_in_segment(index, left_start, side_len) || is_in_segment(index, right_start, side_len);
}
if (zone_mode == "Top only") {
return is_in_segment(index, top_start, top_len);
}
if (zone_mode == "Bottom only") {
return is_in_segment(index, bottom_start, bottom_len);
}
if (zone_mode == "Selected U") {
last_u = first_u;
}
return is_in_u_range(index, first_u, last_u, left_start, side_len, right_start);
}
inline void set_u_zone(std::string &zone_mode, int &first_u, int &last_u, int requested_first_u, int requested_last_u) {
first_u = clamp_int(requested_first_u, 1, RACK_UNIT_COUNT);
last_u = clamp_int(requested_last_u, 1, RACK_UNIT_COUNT);
if (first_u > last_u) {
std::swap(first_u, last_u);
}
if (first_u == 1 && last_u == RACK_UNIT_COUNT) {
zone_mode = "Whole rack";
} else if (first_u == last_u) {
zone_mode = "Selected U";
} else {
zone_mode = "U range";
}
}
template<size_t N>
inline bool has_active_alerts(const std::array<bool, N> &active, const std::array<int, N> &modes) {
for (size_t i = 0; i < N; i++) {
if (active[i] && modes[i] != ALERT_NONE) {
return true;
}
}
return false;
}
template<size_t N>
inline void set_alert_slot(
std::array<bool, N> &active,
std::array<int, N> &first_u,
std::array<int, N> &last_u,
std::array<int, N> &modes,
std::array<uint32_t, N> &starts,
std::array<float, N> &reds,
std::array<float, N> &greens,
std::array<float, N> &blues,
std::array<float, N> &whites,
int requested_slot,
int requested_first_u,
int requested_last_u,
int mode,
float red,
float green,
float blue,
float white
) {
if (N == 0) {
return;
}
const int slot_index = clamp_int(requested_slot, 0, int(N) - 1);
first_u[slot_index] = clamp_int(requested_first_u, 1, RACK_UNIT_COUNT);
last_u[slot_index] = clamp_int(requested_last_u, 1, RACK_UNIT_COUNT);
if (first_u[slot_index] > last_u[slot_index]) {
std::swap(first_u[slot_index], last_u[slot_index]);
}
modes[slot_index] = mode;
active[slot_index] = mode != ALERT_NONE;
starts[slot_index] = millis();
separate_rgbw_channels(red, green, blue, white);
reds[slot_index] = red;
greens[slot_index] = green;
blues[slot_index] = blue;
whites[slot_index] = white;
}
template<size_t N>
inline void set_alert_slot_preset(
std::array<bool, N> &active,
std::array<int, N> &first_u,
std::array<int, N> &last_u,
std::array<int, N> &modes,
std::array<uint32_t, N> &starts,
std::array<float, N> &reds,
std::array<float, N> &greens,
std::array<float, N> &blues,
std::array<float, N> &whites,
int requested_slot,
int requested_first_u,
int requested_last_u,
const std::string &effect
) {
set_alert_slot(
active,
first_u,
last_u,
modes,
starts,
reds,
greens,
blues,
whites,
requested_slot,
requested_first_u,
requested_last_u,
alert_mode_from_name(effect, false),
0.0f,
0.0f,
0.0f,
0.0f
);
}
template<size_t N>
inline void set_alert_slot_custom(
std::array<bool, N> &active,
std::array<int, N> &first_u,
std::array<int, N> &last_u,
std::array<int, N> &modes,
std::array<uint32_t, N> &starts,
std::array<float, N> &reds,
std::array<float, N> &greens,
std::array<float, N> &blues,
std::array<float, N> &whites,
int requested_slot,
int requested_first_u,
int requested_last_u,
const std::string &effect,
float red,
float green,
float blue,
float white
) {
set_alert_slot(
active,
first_u,
last_u,
modes,
starts,
reds,
greens,
blues,
whites,
requested_slot,
requested_first_u,
requested_last_u,
alert_mode_from_name(effect, true),
red,
green,
blue,
white
);
}
template<size_t N>
inline void clear_alert_slot(
std::array<bool, N> &active,
std::array<int, N> &modes,
int requested_slot
) {
if (N == 0) {
return;
}
const int slot_index = clamp_int(requested_slot, 0, int(N) - 1);
active[slot_index] = false;
modes[slot_index] = ALERT_NONE;
}
template<size_t N>
inline void clear_all_alert_slots(std::array<bool, N> &active, std::array<int, N> &modes) {
for (size_t i = 0; i < N; i++) {
active[i] = false;
modes[i] = ALERT_NONE;
}
}
inline bool uses_firmware_brightness(const std::string &effect_name) {
const std::string name = canonical_mode_name(effect_name);
return name == "White breathing" ||
name == "Selected color breathing" ||
name == "Blue breathing" ||
name == "Orange breathing" ||
name == "Red breathing" ||
name == "Internet down" ||
name == "Error" ||
name == "Success" ||
name == "Rack internal" ||
name == "Red U scan" ||
name == "Selected color U scan";
}
inline bool alert_mode_is_breathing(int mode) {
return mode == ALERT_SELECTED_BREATHING ||
mode == ALERT_UNIFI_BREATHING ||
mode == ALERT_ORANGE_BREATHING ||
mode == ALERT_RED_BREATHING ||
mode == ALERT_WHITE_BREATHING ||
mode == ALERT_CUSTOM_BREATHING;
}
inline bool alert_mode_is_blink(int mode) {
return mode == ALERT_RED_BLINK ||
mode == ALERT_WHITE_BLINK ||
mode == ALERT_UNIFI_BLINK ||
mode == ALERT_ORANGE_BLINK ||
mode == ALERT_SELECTED_BLINK ||
mode == ALERT_CUSTOM_BLINK;
}
inline float soft_blink_level(uint32_t start_ms, uint32_t cycle_ms, float maximum) {
if (cycle_ms < 250) {
cycle_ms = 250;
}
const float phase = ((millis() - start_ms) % cycle_ms) / float(cycle_ms);
float level = 0.0f;
if (phase < 0.14f) {
level = smootherstep(phase / 0.14f);
} else if (phase < 0.54f) {
level = 1.0f;
} else if (phase < 0.72f) {
level = 1.0f - smootherstep((phase - 0.54f) / 0.18f);
}
return clamp01(maximum) * level;
}
inline int ping_pong_u(uint32_t step_index, int total_u = RACK_UNIT_COUNT) {
if (total_u <= 1) {
return 1;
}
const uint32_t period = uint32_t(total_u * 2 - 2);
const uint32_t position = step_index % period;
if (position < uint32_t(total_u)) {
return int(position) + 1;
}
return int(period - position) + 1;
}
inline float scan_step_fade(uint32_t elapsed_ms, uint32_t step_ms) {
if (step_ms < 1) {
return 1.0f;
}
const float phase = (elapsed_ms % step_ms) / float(step_ms);
if (phase < 0.5f) {
return smootherstep(phase * 2.0f);
}
return 1.0f - smootherstep((phase - 0.5f) * 2.0f);
}
inline esphome::Color rgbw_color(float red, float green, float blue, float white) {
const RgbwChannels channels = separated_rgbw_unit(red, green, blue, white);
return esphome::Color(
scaled_channel(channels.red),
scaled_channel(channels.green),
scaled_channel(channels.blue),
scaled_channel(channels.white)
);
}
inline uint8_t blend_channel(uint8_t source, uint8_t target, float progress) {
return uint8_t(roundf(source + (int(target) - int(source)) * progress));
}
inline esphome::Color blend_color(const esphome::Color &source, const esphome::Color &target, float progress) {
progress = clamp01(progress);
return esphome::Color(
blend_channel(source.r, target.r, progress),
blend_channel(source.g, target.g, progress),
blend_channel(source.b, target.b, progress),
blend_channel(source.w, target.w, progress)
);
}
inline void capture_pixels(esphome::light::AddressableLight &it, std::vector<esphome::Color> &pixels) {
pixels.clear();
pixels.reserve(it.size());
for (int i = 0; i < it.size(); i++) {
pixels.push_back(it[i].get());
}
}
inline void render_transition(
esphome::light::AddressableLight &it,
const std::vector<esphome::Color> &start,
const esphome::Color &target,
float progress
) {
if (start.size() != size_t(it.size())) {
for (int i = 0; i < it.size(); i++) {
it[i] = target;
}
return;
}
for (int i = 0; i < it.size(); i++) {
it[i] = blend_color(start[i], target, progress);
}
}
inline esphome::Color dithered_rgbw_color_unchecked(float red, float green, float blue, float white, int index) {
return esphome::Color(
dithered_channel(red, index, 17),
dithered_channel(green, index, 79),
dithered_channel(blue, index, 131),
dithered_channel(white, index, 211)
);
}
inline esphome::Color dithered_rgbw_color(float red, float green, float blue, float white, int index) {
const RgbwChannels channels = separated_rgbw_channels(red, green, blue, white);
return dithered_rgbw_color_unchecked(channels.red, channels.green, channels.blue, channels.white, index);
}
inline esphome::Color alert_slot_color(
const UAlertSlot &slot,
int pixel_index,
const esphome::Color &selected_color,
float min_brightness,
float max_brightness,
uint32_t breathing_cycle,
int curve_mode,
uint32_t blink_cycle,
float fade_ms
) {
clamp_brightness_range(min_brightness, max_brightness);
float level = max_brightness;
if (alert_mode_is_breathing(slot.mode)) {
level = breathing_level(slot.start_ms, breathing_cycle, min_brightness, max_brightness, curve_mode);
} else if (alert_mode_is_blink(slot.mode)) {
level = soft_blink_level(slot.start_ms, blink_cycle, max_brightness);
}
if (fade_ms > 0.0f) {
level *= transition_progress(slot.start_ms, fade_ms);
}
float red = 0.0f;
float green = 0.0f;
float blue = 0.0f;
float white = 0.0f;
switch (slot.mode) {
case ALERT_SELECTED_STATIC:
case ALERT_SELECTED_BREATHING:
case ALERT_SELECTED_BLINK:
red = selected_color.r * level;
green = selected_color.g * level;
blue = selected_color.b * level;
white = 0.0f;
break;
case ALERT_UNIFI_BREATHING:
case ALERT_UNIFI_BLINK:
green = level * 111.0f;
blue = level * 255.0f;
break;
case ALERT_ORANGE_BREATHING:
case ALERT_ORANGE_BLINK:
red = level * 255.0f;
green = level * 82.0f;
break;
case ALERT_RED_BREATHING:
case ALERT_RED_BLINK:
red = level * 255.0f;
break;
case ALERT_WHITE_BREATHING:
case ALERT_WHITE_BLINK:
case ALERT_WHITE_STATIC:
white = level * 255.0f;
break;
case ALERT_CUSTOM_STATIC:
case ALERT_CUSTOM_BREATHING:
case ALERT_CUSTOM_BLINK:
red = slot.red * level;
green = slot.green * level;
blue = slot.blue * level;
white = slot.white * level;
break;
default:
break;
}
return dithered_rgbw_color(red, green, blue, white, pixel_index);
}
template<size_t N>
inline esphome::Color alert_target_for_pixel(
int pixel_index,
const std::array<bool, N> &active,
const std::array<int, N> &first_u,
const std::array<int, N> &last_u,
const std::array<int, N> &modes,
const std::array<uint32_t, N> &starts,
const std::array<float, N> &reds,
const std::array<float, N> &greens,
const std::array<float, N> &blues,
const std::array<float, N> &whites,
const esphome::Color &selected_color,
float min_brightness,
float max_brightness,
uint32_t breathing_cycle,
int curve_mode,
uint32_t blink_cycle,
float fade_ms,
int left_start,