Repository navigation
Expand file tree
/
Copy pathAnimationsModule.cpp
More file actions
3421 lines (2866 loc) · 133 KB
/
Copy pathAnimationsModule.cpp
File metadata and controls
3421 lines (2866 loc) · 133 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
#include "AnimationsModule.hpp"
#include "webconfig.hpp"
#include "MultiLogger.hpp"
#include "PsramUtils.hpp"
#include "TimeUtilities.hpp"
#include <time.h>
// Hilfsfunktion für RGB565
uint16_t AnimationsModule::rgb565(uint8_t r, uint8_t g, uint8_t b) {
return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
}
// Hilfsfunktion um Hex-Farbe zu RGB565 zu konvertieren
uint16_t AnimationsModule::hexToRgb565(const char* hex) {
if (!hex || hex[0] != '#' || strlen(hex) < 7) {
return rgb565(255, 255, 255);
}
long r = strtol(PsramString(hex + 1, 2).c_str(), NULL, 16);
long g = strtol(PsramString(hex + 3, 2).c_str(), NULL, 16);
long b = strtol(PsramString(hex + 5, 2).c_str(), NULL, 16);
return rgb565(r, g, b);
}
// Hilfsfunktion zum Mischen zweier RGB565-Farben
uint16_t AnimationsModule::blendRGB565(uint16_t color1, uint16_t color2, int blend) {
// Extract RGB components from RGB565
uint8_t r1 = (color1 >> 11) & 0x1F;
uint8_t g1 = (color1 >> 5) & 0x3F;
uint8_t b1 = color1 & 0x1F;
uint8_t r2 = (color2 >> 11) & 0x1F;
uint8_t g2 = (color2 >> 5) & 0x3F;
uint8_t b2 = color2 & 0x1F;
// Linear interpolation (blend is 0-15)
uint8_t r = r1 + ((r2 - r1) * blend) / 16;
uint8_t g = g1 + ((g2 - g1) * blend) / 16;
uint8_t b = b1 + ((b2 - b1) * blend) / 16;
// Repack into RGB565
return (r << 11) | (g << 5) | b;
}
// Einfacher Pseudo-Zufallsgenerator
uint32_t AnimationsModule::simpleRandom(uint32_t seed) {
seed = seed * 1103515245 + 12345;
return (seed / 65536) % 32768;
}
AnimationsModule::AnimationsModule(U8G2_FOR_ADAFRUIT_GFX& u8g2, GFXcanvas16& canvas,
GeneralTimeConverter& timeConverter, DeviceConfig* config)
: u8g2(u8g2), canvas(canvas), timeConverter(timeConverter), config(config) {
// Allokiere Arrays in PSRAM
_snowflakes = (Snowflake*)ps_malloc(sizeof(Snowflake) * MAX_SNOWFLAKES);
_flowers = (Flower*)ps_malloc(sizeof(Flower) * MAX_FLOWERS);
_butterflies = (Butterfly*)ps_malloc(sizeof(Butterfly) * MAX_BUTTERFLIES);
_leaves = (Leaf*)ps_malloc(sizeof(Leaf) * MAX_LEAVES);
_birds = (Bird*)ps_malloc(sizeof(Bird) * MAX_BIRDS);
// Log allocation status
if (!_snowflakes || !_flowers || !_butterflies || !_leaves || !_birds) {
Log.println("[AnimationsModule] WARNUNG: PSRAM-Allokation fehlgeschlagen!");
if (!_snowflakes) Log.println(" - _snowflakes Allokation fehlgeschlagen");
if (!_flowers) Log.println(" - _flowers Allokation fehlgeschlagen");
if (!_butterflies) Log.println(" - _butterflies Allokation fehlgeschlagen");
if (!_leaves) Log.println(" - _leaves Allokation fehlgeschlagen");
if (!_birds) Log.println(" - _birds Allokation fehlgeschlagen");
}
// Initialisiere mit 0
if (_snowflakes) memset(_snowflakes, 0, sizeof(Snowflake) * MAX_SNOWFLAKES);
if (_flowers) memset(_flowers, 0, sizeof(Flower) * MAX_FLOWERS);
if (_butterflies) memset(_butterflies, 0, sizeof(Butterfly) * MAX_BUTTERFLIES);
if (_leaves) memset(_leaves, 0, sizeof(Leaf) * MAX_LEAVES);
if (_birds) memset(_birds, 0, sizeof(Bird) * MAX_BIRDS);
}
AnimationsModule::~AnimationsModule() {
// Gebe PSRAM-Speicher frei
if (_snowflakes) { free(_snowflakes); _snowflakes = nullptr; }
if (_flowers) { free(_flowers); _flowers = nullptr; }
if (_butterflies) { free(_butterflies); _butterflies = nullptr; }
if (_leaves) { free(_leaves); _leaves = nullptr; }
if (_birds) { free(_birds); _birds = nullptr; }
}
void AnimationsModule::begin() {
time_t now = time(nullptr);
time_t local_time = timeConverter.toLocal(now);
struct tm tm_now;
localtime_r(&local_time, &tm_now);
_lastCheckedDay = tm_now.tm_mday;
setConfig();
shuffleCandleOrder();
Log.println("[AnimationsModule] Modul initialisiert");
}
void AnimationsModule::setConfig() {
if (config) {
_displayDurationMs = config->adventWreathDisplaySec * 1000UL;
_repeatIntervalMs = config->adventWreathRepeatMin * 60UL * 1000UL;
_flameAnimationMs = config->adventWreathFlameSpeedMs;
}
}
void AnimationsModule::onUpdate(std::function<void()> callback) {
_updateCallback = callback;
}
void AnimationsModule::shuffleCandleOrder() {
time_t now_utc;
time(&now_utc);
uint32_t seed = (uint32_t)now_utc + _displayCounter;
for (int i = 3; i > 0; i--) {
seed = simpleRandom(seed);
int j = seed % (i + 1);
int temp = _candleOrder[i];
_candleOrder[i] = _candleOrder[j];
_candleOrder[j] = temp;
}
_lastOrderSeed = seed;
}
void AnimationsModule::shuffleTreeElements() {
time_t now_utc;
time(&now_utc);
uint32_t seed = (uint32_t)now_utc + millis();
int canvasH = _currentCanvas->height();
int canvasW = _currentCanvas->width();
int centerX = canvasW / 2;
float scale = canvasH / 66.0f;
int baseY = canvasH - 4;
int trunkHeight = (int)(10 * scale);
int treeHeight = (int)(54 * scale);
int treeBaseY = baseY - trunkHeight + 2;
// Tree layer dimensions (used by both ornaments and lights)
int layer1Height = (int)(18 * scale);
int layer2Height = (int)(18 * scale);
int layer3Height = (int)(18 * scale);
int layer1Width = (int)(28 * scale);
int layer2Width = (int)(22 * scale);
int layer3Width = (int)(16 * scale);
// Hole konfigurierte Anzahl oder verwende Standard
int ornamentCount = config ? config->christmasTreeOrnamentCount : 12;
if (ornamentCount < 8) ornamentCount = 8;
if (ornamentCount > 20) ornamentCount = 20;
if (scale > 1.2) ornamentCount += 4; // Mehr bei Fullscreen
// Shuffle ornament positions
for (int i = 0; i < ornamentCount && i < 30; i++) {
seed = simpleRandom(seed + i * 137);
// Y position (height on tree)
float t = (float)i / ornamentCount;
float yFraction = 0.4f * t + 0.6f * (t * t);
int yOffset = 3 + (int)(yFraction * (treeHeight - 10));
yOffset += (seed % 8) - 4;
if (yOffset < 4) yOffset = 4;
if (yOffset > treeHeight - 6) yOffset = treeHeight - 6;
_shuffledOrnamentY[i] = yOffset;
// X position (left/right) - constrain within tree boundaries
int maxWidth;
int ornamentRadius = 2 + (seed / 17) % 3; // Radius for boundary calculation
if (yOffset < layer1Height) {
float progress = (float)yOffset / layer1Height;
maxWidth = (int)(layer1Width * (1.0f - progress * 0.5f) * 0.85f);
} else if (yOffset < layer1Height + layer2Height - (int)(4 * scale)) {
float progress = (float)(yOffset - layer1Height + (int)(4 * scale)) / layer2Height;
maxWidth = (int)(layer2Width * (1.0f - progress * 0.5f) * 0.85f);
} else {
float progress = (float)(yOffset - layer1Height - layer2Height + (int)(14 * scale)) / (layer3Width + 4);
maxWidth = (int)(layer3Width * (1.0f - progress * 0.6f) * 0.8f);
}
// Reduce maxWidth by ornament radius to keep ornament fully inside tree
maxWidth -= ornamentRadius;
if (maxWidth < 2) maxWidth = 2;
int xRange = maxWidth * 2;
int xPos = seed % xRange;
if (i % 2 == 0) {
_shuffledOrnamentX[i] = -maxWidth + (xPos / 2);
} else {
_shuffledOrnamentX[i] = -maxWidth + xRange/2 + (xPos / 2);
}
// Random color index (0-7)
_shuffledOrnamentColors[i] = seed % 8;
// Random size - same as calculated radius above
_shuffledOrnamentSizes[i] = ornamentRadius;
}
// Shuffle light positions
int lightCount = config ? config->christmasTreeLightCount : 18;
if (lightCount < 5) lightCount = 5;
if (lightCount > 30) lightCount = 30;
for (int i = 0; i < lightCount && i < 30; i++) {
seed = simpleRandom(seed + i * 211);
// Random Y position throughout tree
int lightY = (seed % treeHeight);
if (lightY < 4) lightY = 4;
_shuffledLightY[i] = lightY;
// Calculate proper max width based on tree layer (matching drawNaturalTree)
int maxLightWidth;
int layer2Top = layer1Height + layer2Height - (int)(4 * scale);
if (lightY < layer1Height) {
// Layer 1 (bottom): tapers with 0.8 factor
float progress = (float)lightY / layer1Height;
maxLightWidth = (int)(layer1Width - progress * layer1Width * 0.8f);
} else if (lightY < layer2Top) {
// Layer 2 (middle): tapers with 0.9 factor
float progress = (float)(lightY - layer1Height) / layer2Height;
maxLightWidth = (int)(layer2Width - progress * layer2Width * 0.9f);
} else {
// Layer 3 (top): tapers with 0.85 factor
float progress = (float)(lightY - layer2Top) / layer3Height;
maxLightWidth = (int)(layer3Width - progress * layer3Width * 0.85f);
}
// Reduce width by light size (2px) to keep light center inside tree
maxLightWidth -= 2;
if (maxLightWidth < 2) maxLightWidth = 2;
int lightX = ((seed / 7) % (maxLightWidth * 2)) - maxLightWidth;
_shuffledLightX[i] = lightX;
}
}
bool AnimationsModule::isAdventSeason() {
time_t now_utc;
time(&now_utc);
time_t local_now = timeConverter.toLocal(now_utc);
struct tm tm_now;
localtime_r(&local_now, &tm_now);
int month = tm_now.tm_mon + 1;
int day = tm_now.tm_mday;
int year = tm_now.tm_year + 1900;
// Option: Erst ab dem 1. Advent zeigen
if (config && config->adventWreathOnlyFromFirstAdvent) {
// Berechne 1. Advent (4. Advent - 21 Tage)
time_t fourthAdvent = calculateFourthAdvent(year);
time_t firstAdvent = fourthAdvent - (21 * 24 * 60 * 60);
// Prüfe ob wir vor dem 1. Advent sind
if (local_now < firstAdvent) {
return false;
}
} else {
// Berechne Start- und Enddatum basierend auf "Tage vor dem 24."
int daysBefore = config ? config->adventWreathDaysBefore24 : 30;
if (daysBefore > 30) daysBefore = 30;
if (daysBefore < 0) daysBefore = 0;
// Startdatum: 24. Dezember minus daysBefore
int startDay = 24 - daysBefore;
int startMonth = 12;
if (startDay <= 0) {
startDay += 30; // November hat 30 Tage
startMonth = 11;
}
// Prüfe ob wir vor dem Startdatum sind
if (startMonth == 11 && month == 11 && day < startDay) {
return false;
}
if (month < 11) {
return false;
}
}
// Adventskranz wird bis zum 24.12. gezeigt
if (month == 12 && day <= 24) {
return true;
} else if (month == 11) {
return true;
}
return false;
}
bool AnimationsModule::isChristmasSeason() {
time_t now_utc;
time(&now_utc);
time_t local_now = timeConverter.toLocal(now_utc);
struct tm tm_now;
localtime_r(&local_now, &tm_now);
int month = tm_now.tm_mon + 1;
int day = tm_now.tm_mday;
// Berechne Start- und Enddatum basierend auf "Tage vor/nach dem 24."
int daysBefore = config ? config->christmasTreeDaysBefore24 : 23;
int daysAfter = config ? config->christmasTreeDaysAfter24 : 7;
if (daysBefore > 30) daysBefore = 30;
if (daysBefore < 0) daysBefore = 0;
if (daysAfter > 30) daysAfter = 30;
if (daysAfter < 0) daysAfter = 0;
// Startdatum: 24. Dezember minus daysBefore
int startDay = 24 - daysBefore;
int startMonth = 12;
if (startDay <= 0) {
startDay += 30;
startMonth = 11;
}
// Enddatum: 24. Dezember plus daysAfter
int endDay = 24 + daysAfter;
int endMonth = 12;
if (endDay > 31) {
endDay -= 31;
endMonth = 1; // Januar nächstes Jahr
}
// Prüfe ob aktuelles Datum im Bereich liegt
if (endMonth == 1) {
// Bereich geht über Jahreswechsel
if (month == 12 && day >= startDay) return true;
if (month == 1 && day <= endDay) return true;
} else if (startMonth == 11) {
// Bereich startet im November
if (month == 11 && day >= startDay) return true;
if (month == 12 && day <= endDay) return true;
} else {
// Alles im Dezember
if (month == 12 && day >= startDay && day <= endDay) return true;
}
return false;
}
bool AnimationsModule::isHolidaySeason() {
return isAdventSeason() || isChristmasSeason() || isFireplaceSeason();
}
bool AnimationsModule::isFireplaceSeason() {
// Prüfe nur ob das Kamin-Feature aktiviert ist
if (!config || !config->fireplaceEnabled) return false;
// Kamin ist nur im Winter "in Season" (Dezember, Januar, Februar)
time_t now_utc;
time(&now_utc);
time_t local_now = timeConverter.toLocal(now_utc);
struct tm tm_now;
localtime_r(&local_now, &tm_now);
int month = tm_now.tm_mon + 1; // 1-12
// Kamin nur in Wintermonaten aktiv
return (month == 12 || month == 1 || month == 2);
}
ChristmasDisplayMode AnimationsModule::getCurrentDisplayMode() {
time_t now_utc;
time(&now_utc);
time_t local_now = timeConverter.toLocal(now_utc);
struct tm tm_now;
localtime_r(&local_now, &tm_now);
int month = tm_now.tm_mon + 1;
int day = tm_now.tm_mday;
bool treeEnabled = config ? config->christmasTreeEnabled : true;
bool wreathEnabled = config ? config->adventWreathEnabled : true;
bool fireplaceEnabled = config ? config->fireplaceEnabled : true;
// Zähle aktive Modi
int activeCount = 0;
if (wreathEnabled && isAdventSeason()) activeCount++;
if (treeEnabled && isChristmasSeason()) activeCount++;
if (fireplaceEnabled && isFireplaceSeason()) activeCount++;
// Adventskranz bis zum 24.12., danach nur noch Baum/Kamin
if (month == 12 && day > 24) {
if (treeEnabled && fireplaceEnabled) return ChristmasDisplayMode::Alternate;
if (treeEnabled) return ChristmasDisplayMode::Tree;
if (fireplaceEnabled) return ChristmasDisplayMode::Fireplace;
return ChristmasDisplayMode::Tree;
} else if (month == 1) {
if (treeEnabled && fireplaceEnabled) return ChristmasDisplayMode::Alternate;
if (treeEnabled) return ChristmasDisplayMode::Tree;
if (fireplaceEnabled) return ChristmasDisplayMode::Fireplace;
return ChristmasDisplayMode::Tree;
}
// Mehrere aktiv = Wechsel
if (activeCount > 1) {
return ChristmasDisplayMode::Alternate;
}
// Nur einer aktiv
if (treeEnabled && isChristmasSeason()) return ChristmasDisplayMode::Tree;
if (fireplaceEnabled && isFireplaceSeason()) return ChristmasDisplayMode::Fireplace;
return ChristmasDisplayMode::Wreath;
}
int AnimationsModule::calculateCurrentAdvent() {
time_t now_utc;
time(&now_utc);
time_t local_now = timeConverter.toLocal(now_utc);
struct tm tm_now;
localtime_r(&local_now, &tm_now);
int year = tm_now.tm_year + 1900;
int month = tm_now.tm_mon + 1;
if (month < 11 || month > 12) {
return 0;
}
time_t fourthAdvent = calculateFourthAdvent(year);
struct tm tm_fourth;
localtime_r(&fourthAdvent, &tm_fourth);
struct tm tm_today = tm_now;
tm_today.tm_hour = 12;
tm_today.tm_min = 0;
tm_today.tm_sec = 0;
time_t today = mktime(&tm_today);
int diffDays = (int)((fourthAdvent - today) / 86400);
if (diffDays == 0) return 4;
if (diffDays == 7) return 3;
if (diffDays == 14) return 2;
if (diffDays == 21) return 1;
if (diffDays < 0) return 4;
if (diffDays < 7) return 3;
if (diffDays < 14) return 2;
if (diffDays < 21) return 1;
return 0;
}
time_t AnimationsModule::calculateFourthAdvent(int year) {
struct tm tm_christmas;
memset(&tm_christmas, 0, sizeof(tm_christmas));
tm_christmas.tm_year = year - 1900;
tm_christmas.tm_mon = 11;
tm_christmas.tm_mday = 24;
tm_christmas.tm_hour = 12;
mktime(&tm_christmas);
int daysToSubtract = tm_christmas.tm_wday;
if (daysToSubtract == 0) {
daysToSubtract = 0;
}
tm_christmas.tm_mday -= daysToSubtract;
return mktime(&tm_christmas);
}
void AnimationsModule::periodicTick() {
if (!config) return;
bool hasHolidayAnimations = config->adventWreathEnabled || config->christmasTreeEnabled || config->fireplaceEnabled;
bool hasSeasonalAnimations = config->seasonalAnimationsEnabled;
if (!hasHolidayAnimations && !hasSeasonalAnimations) return;
unsigned long now = millis();
if (now - _lastPeriodicCheck < 1000) return;
_lastPeriodicCheck = now;
bool inHolidaySeason = isHolidaySeason();
// Wenn ein Request noch aussteht (noch nicht aktiviert), nichts tun
if (_requestPending) {
return;
}
// Use appropriate interval for seasonal vs holiday animations
unsigned long repeatInterval = _repeatIntervalMs; // Standard interval for all animations
unsigned long displayDuration = _displayDurationMs;
unsigned long minInterval = (_lastAdventDisplayTime == 0) ? 0 : repeatInterval;
if (!_isAdventViewActive && (now - _lastAdventDisplayTime > minInterval)) {
// Berechne welche Animationen grundsätzlich aktiv sind
bool wreathActive = config->adventWreathEnabled && isAdventSeason();
bool treeActive = config->christmasTreeEnabled && isChristmasSeason();
// Kamin ist IMMER eine separate Animation (nicht nur in Holiday-Season)
bool fireplaceActive = config->fireplaceEnabled && isFireplaceSeason();
// Prüfe Nachtmodus für Kamin
if (fireplaceActive && config->fireplaceNightModeOnly && !TimeUtilities::isNightTime()) {
fireplaceActive = false;
}
// Seasonal animation ist IMMER aktiv wenn enabled (unabhängig von Holiday-Season)
bool seasonalActive = hasSeasonalAnimations;
if (seasonalActive && inHolidaySeason) {
// Im Winter: Check ob Winter-Seasonal bei Advent/Weihnachten erlaubt ist
TimeUtilities::Season currentSeason = config->seasonalAnimationsTestMode ?
(TimeUtilities::Season)_testModeSeasonIndex : TimeUtilities::getCurrentSeason();
if (currentSeason == TimeUtilities::Season::WINTER && !config->seasonalWinterWithHolidays) {
seasonalActive = false;
}
}
// Prüfe ob überhaupt etwas anzuzeigen ist
bool hasAnythingToShow = wreathActive || treeActive || fireplaceActive || seasonalActive;
if (!hasAnythingToShow) {
Log.println("[AnimationsModule] Nichts anzuzeigen - kein Request");
return;
}
// Entscheide WAS angezeigt wird
shuffleCandleOrder();
ChristmasDisplayMode mode = getCurrentDisplayMode();
// Setze Anzeige-Flags
_showTree = false;
_showFireplace = false;
_showSeasonalAnimation = false;
if (mode == ChristmasDisplayMode::Alternate) {
// Rotiere durch ALLE aktiven Animationen
// Im Winter z.B.: Adventskranz > Weihnachtsbaum > Winteranimation > Kamin
// Im Frühling z.B.: Frühlingsanimation > Kamin (wenn Kamin abends aktiv)
int activeCount = (wreathActive ? 1 : 0) + (treeActive ? 1 : 0) +
(fireplaceActive ? 1 : 0) + (seasonalActive ? 1 : 0);
if (activeCount > 0) {
int modeIndex = _displayCounter % activeCount;
int current = 0;
if (wreathActive) {
if (current == modeIndex) { /* Kranz - default */ }
current++;
}
if (treeActive && current <= modeIndex) {
if (current == modeIndex) { _showTree = true; }
current++;
}
if (seasonalActive && current <= modeIndex) {
if (current == modeIndex) {
_showSeasonalAnimation = true;
_showTree = false;
}
current++;
}
if (fireplaceActive && current <= modeIndex) {
if (current == modeIndex) {
_showFireplace = true;
_showTree = false;
_showSeasonalAnimation = false;
}
}
}
} else if (mode == ChristmasDisplayMode::Tree) {
_showTree = treeActive;
} else if (mode == ChristmasDisplayMode::Fireplace) {
_showFireplace = fireplaceActive;
} else if (mode == ChristmasDisplayMode::Wreath) {
// Kranz-Modus: alle Flags bleiben false (Kranz ist der Default).
// Ausnahme: Wenn der Adventskranz nicht aktiv ist (z.B. außerhalb der Adventszeit),
// aber eine Seasonal-Animation aktiv ist, zeige stattdessen die Seasonal-Animation.
if (!wreathActive && seasonalActive) {
_showSeasonalAnimation = true;
}
}
// Alle Animationen verwenden die gleiche Display-Duration aus den globalen Einstellungen
// (seasonalAnimationsDisplaySec wird nicht mehr verwendet)
// Feste UID für diese Anzeige-Session (nicht vom Advent-Woche abhängig)
// Verwende eine einfache UID basierend auf dem Display-Counter
_currentAdventUID = ADVENT_WREATH_UID_BASE + (_displayCounter % 100);
unsigned long safeDuration = displayDuration + 5000UL;
Priority prio = config->adventWreathInterrupt ? Priority::Low : Priority::PlayNext;
_requestPending = true; // Markiere Request als ausstehend
bool success = requestPriorityEx(prio, _currentAdventUID, safeDuration);
if (success) {
const char* modeName;
if (_showSeasonalAnimation) {
modeName = TimeUtilities::getSeasonName(
config->seasonalAnimationsTestMode ?
(TimeUtilities::Season)_testModeSeasonIndex :
TimeUtilities::getCurrentSeason()
);
} else if (_showFireplace) {
modeName = "Kamin";
} else if (_showTree) {
modeName = "Weihnachtsbaum";
} else {
modeName = "Adventskranz";
}
Log.printf("[AnimationsModule] %s %s angefordert (UID=%lu, Counter=%d)\n",
modeName,
config->adventWreathInterrupt ? "Interrupt" : "PlayNext",
_currentAdventUID, _displayCounter);
_displayCounter++; // Nur erhöhen wenn Request erfolgreich
} else {
Log.println("[AnimationsModule] Request abgelehnt!");
_requestPending = false; // Request fehlgeschlagen, zurücksetzen
}
}
else if (_isAdventViewActive && (now - _adventViewStartTime > displayDuration)) {
releasePriorityEx(_currentAdventUID);
_isAdventViewActive = false;
_requestPending = false;
_lastAdventDisplayTime = now;
}
}
void AnimationsModule::tick() {
unsigned long now = millis();
bool needsUpdate = false;
// Flammen-Animation (Kerzen)
if (now - _lastFlameUpdate > _flameAnimationMs) {
_lastFlameUpdate = now;
_flamePhase = (_flamePhase + 1) % 32;
needsUpdate = true;
}
// Baum-Lichter-Animation (konfigurierbare Geschwindigkeit)
unsigned long treeLightSpeed = config ? (unsigned long)config->christmasTreeLightSpeedMs : 80;
if (now - _lastTreeLightUpdate > treeLightSpeed) {
_lastTreeLightUpdate = now;
_treeLightPhase = (_treeLightPhase + 1) % 24;
needsUpdate = true;
}
// LED-Rahmen-Animation - simple phase rotation (no blending to avoid pulsing effect)
unsigned long ledBorderSpeed = config ? (unsigned long)config->ledBorderSpeedMs : 100;
if (now - _lastLedBorderUpdate > ledBorderSpeed) {
_lastLedBorderUpdate = now;
_ledBorderPhase = (_ledBorderPhase + 1) % 4; // Simple 4-phase rotation
needsUpdate = true;
}
// Kaminfeuer-Animation
unsigned long fireplaceSpeed = config ? (unsigned long)config->fireplaceFlameSpeedMs : 40;
static unsigned long _lastFireplaceUpdate = 0;
if (now - _lastFireplaceUpdate > fireplaceSpeed) {
_lastFireplaceUpdate = now;
_fireplaceFlamePhase = (_fireplaceFlamePhase + 1) % 24;
needsUpdate = true;
}
// Seasonal animation phase (for general seasonal animation counter)
if (now - _lastSeasonAnimUpdate > 50) {
_lastSeasonAnimUpdate = now;
_seasonAnimationPhase = (_seasonAnimationPhase + 1) % 360;
needsUpdate = true;
}
if (needsUpdate && _updateCallback) {
_updateCallback();
}
}
void AnimationsModule::logicTick() {
}
bool AnimationsModule::wantsFullscreen() const {
// Always use fullscreen mode
return _fullscreenCanvas != nullptr;
}
void AnimationsModule::draw() {
// Wähle den richtigen Canvas basierend auf Fullscreen-Modus
_currentCanvas = wantsFullscreen() ? _fullscreenCanvas : &canvas;
// Hintergrundfarbe basierend auf Modus
uint16_t bgColor = 0;
if (_showFireplace && config && config->fireplaceBgColor.length() > 0) {
bgColor = hexToRgb565(config->fireplaceBgColor.c_str());
} else if (_showTree && config && config->christmasTreeBgColor.length() > 0) {
bgColor = hexToRgb565(config->christmasTreeBgColor.c_str());
} else if (!_showFireplace && !_showTree && !_showSeasonalAnimation && config && config->adventWreathBgColor.length() > 0) {
bgColor = hexToRgb565(config->adventWreathBgColor.c_str());
}
_currentCanvas->fillScreen(bgColor);
u8g2.begin(*_currentCanvas);
// Show EITHER holiday animations OR seasonal animations, NEVER both
if (_showSeasonalAnimation) {
// Show seasonal animation ONLY (not mixed with holiday animations)
drawSeasonalAnimations();
} else {
// Show holiday animations (Adventskranz, Weihnachtsbaum, or Kamin)
if (_showFireplace) {
drawFireplace();
} else if (_showTree) {
// Mische Kugeln und Lichter nur EINMAL bei neuer Anzeige, nicht bei jedem Tick
unsigned long now = millis();
if (_lastTreeDisplay == 0 || (now - _lastTreeDisplay) > 60000) { // Neue Anzeige oder >60s her
shuffleTreeElements();
_lastTreeDisplay = now;
}
drawChristmasTree();
drawSnowflakes();
// LED-Lauflicht-Rahmen
drawLedBorder();
if (config && config->showNewYearCountdown) {
drawNewYearCountdown();
}
} else {
// Adventskranz ohne Beschriftung - mehr Platz für die Grafik
drawGreenery();
drawWreath();
drawBerries();
// LED-Lauflicht-Rahmen
drawLedBorder();
if (config && config->showNewYearCountdown) {
drawNewYearCountdown();
}
}
}
}
void AnimationsModule::drawChristmasTree() {
int canvasW = _currentCanvas->width();
int canvasH = _currentCanvas->height();
int centerX = canvasW / 2;
// Dynamische Skalierung basierend auf Canvas-Höhe
// Nutze scaleY um den gesamten verfügbaren Platz in der Höhe zu nutzen
float scale = canvasH / 66.0f; // 66 ist die Referenz-Höhe
int baseY = canvasH - 4; // Basis immer am unteren Rand
// Skalierte Baumhöhe
int treeHeight = (int)(54 * scale);
int trunkHeight = (int)(10 * scale);
int trunkWidth = (int)(8 * scale);
// Zeichne zuerst den Baumstamm
uint16_t trunkColor = rgb565(139, 69, 19);
uint16_t trunkDark = rgb565(100, 50, 15);
_currentCanvas->fillRect(centerX - trunkWidth/2, baseY - trunkHeight, trunkWidth, trunkHeight + 2, trunkColor);
_currentCanvas->drawLine(centerX - trunkWidth/2, baseY - trunkHeight, centerX - trunkWidth/2, baseY + 2, trunkDark);
// Natürlicherer Baum mit mehreren Grüntönen und organischer Form
drawNaturalTree(centerX, baseY - trunkHeight + 2, scale);
// Stern an der Spitze
uint16_t starColor = rgb565(255, 255, 0);
uint16_t starGlow = rgb565(255, 230, 100);
int starY = baseY - trunkHeight - treeHeight + (int)(6 * scale);
int starSize = max(2, (int)(3 * scale));
_currentCanvas->fillCircle(centerX, starY, starSize, starColor);
// Strahlen
int rayLen = (int)(5 * scale);
_currentCanvas->drawLine(centerX, starY - rayLen, centerX, starY + rayLen, starGlow);
_currentCanvas->drawLine(centerX - rayLen, starY, centerX + rayLen, starY, starGlow);
_currentCanvas->drawLine(centerX - rayLen/2, starY - rayLen/2, centerX + rayLen/2, starY + rayLen/2, starGlow);
_currentCanvas->drawLine(centerX - rayLen/2, starY + rayLen/2, centerX + rayLen/2, starY - rayLen/2, starGlow);
// Kugeln am Baum
drawTreeOrnaments(centerX, baseY - trunkHeight + 2, scale);
// Blinkende Lichter
drawTreeLights();
// Geschenke unter dem Baum
drawGifts(centerX, baseY, scale);
}
void AnimationsModule::drawNaturalTree(int centerX, int baseY, float scale) {
// Grüntöne für natürlicheren Look
uint16_t greens[] = {
rgb565(0, 80, 0), // Dunkelgrün
rgb565(0, 100, 20), // Mittelgrün
rgb565(20, 120, 30), // Hellgrün
rgb565(0, 90, 10), // Dunkel-Mittel
rgb565(34, 100, 34) // Forest Green
};
int numGreens = sizeof(greens) / sizeof(greens[0]);
// Skalierte Werte
int layer1Height = (int)(18 * scale);
int layer2Height = (int)(18 * scale);
int layer3Height = (int)(18 * scale);
int layer1Width = (int)(28 * scale);
int layer2Width = (int)(22 * scale);
int layer3Width = (int)(16 * scale);
// Unterste Schicht (breiteste)
int layer1Top = baseY;
int layer1Bottom = baseY - layer1Height;
for (int y = layer1Top; y >= layer1Bottom; y--) {
int progress = layer1Top - y;
int maxWidth = layer1Width - (int)(progress * 0.8);
for (int x = -maxWidth; x <= maxWidth; x++) {
uint32_t seed = simpleRandom((y * 47 + x * 13) ^ 0xDEAD);
int edgeVar = (seed % 3) - 1;
if (abs(x) <= maxWidth + edgeVar) {
int colorIdx = seed % numGreens;
_currentCanvas->drawPixel(centerX + x, y, greens[colorIdx]);
}
}
}
// Mittlere Schicht
int layer2Top = baseY - (int)(14 * scale);
int layer2Bottom = layer2Top - layer2Height;
for (int y = layer2Top; y >= layer2Bottom; y--) {
int progress = layer2Top - y;
int maxWidth = layer2Width - (int)(progress * 0.9);
for (int x = -maxWidth; x <= maxWidth; x++) {
uint32_t seed = simpleRandom((y * 53 + x * 17) ^ 0xBEEF);
int edgeVar = (seed % 3) - 1;
if (abs(x) <= maxWidth + edgeVar) {
int colorIdx = seed % numGreens;
_currentCanvas->drawPixel(centerX + x, y, greens[colorIdx]);
}
}
}
// Oberste Schicht (Spitze)
int layer3Top = baseY - (int)(28 * scale);
int layer3Bottom = layer3Top - layer3Height;
for (int y = layer3Top; y >= layer3Bottom; y--) {
int progress = layer3Top - y;
int maxWidth = layer3Width - (int)(progress * 0.85);
if (maxWidth < 1) maxWidth = 1;
for (int x = -maxWidth; x <= maxWidth; x++) {
uint32_t seed = simpleRandom((y * 59 + x * 19) ^ 0xCAFE);
int edgeVar = (seed % 2);
if (abs(x) <= maxWidth + edgeVar) {
int colorIdx = seed % numGreens;
_currentCanvas->drawPixel(centerX + x, y, greens[colorIdx]);
}
}
}
// Extra Schicht bei Fullscreen (4. Ebene)
if (scale > 1.2) {
int layer4Top = baseY - (int)(42 * scale);
int layer4Bottom = layer4Top - (int)(14 * scale);
int layer4Width = (int)(10 * scale);
for (int y = layer4Top; y >= layer4Bottom; y--) {
int progress = layer4Top - y;
int maxWidth = layer4Width - (int)(progress * 0.9);
if (maxWidth < 1) maxWidth = 1;
for (int x = -maxWidth; x <= maxWidth; x++) {
uint32_t seed = simpleRandom((y * 61 + x * 23) ^ 0xFACE);
int edgeVar = (seed % 2);
if (abs(x) <= maxWidth + edgeVar) {
int colorIdx = seed % numGreens;
_currentCanvas->drawPixel(centerX + x, y, greens[colorIdx]);
}
}
}
}
}
void AnimationsModule::drawTreeOrnaments(int centerX, int baseY, float scale) {
uint16_t ornamentColors[] = {
rgb565(255, 0, 0), // Rot
rgb565(255, 215, 0), // Gold
rgb565(0, 100, 200), // Blau
rgb565(255, 0, 255), // Magenta
rgb565(200, 50, 50), // Dunkelrot
rgb565(255, 140, 0), // Orange
rgb565(100, 200, 255), // Hellblau
rgb565(220, 220, 220) // Silber
};
// Hole konfigurierte Anzahl oder verwende Standard
int numOrnaments = config ? config->christmasTreeOrnamentCount : 12;
if (numOrnaments < 8) numOrnaments = 8;
if (numOrnaments > 20) numOrnaments = 20;
if (scale > 1.2) numOrnaments += 4; // Mehr bei Fullscreen
// Verwende gemischte Positionen aus shuffleTreeElements()
for (int i = 0; i < numOrnaments && i < 30; i++) {
int ox = centerX + _shuffledOrnamentX[i];
int oy = baseY - _shuffledOrnamentY[i];
int radius = _shuffledOrnamentSizes[i];
uint16_t color = ornamentColors[_shuffledOrnamentColors[i] % 8];
drawOrnament(ox, oy, radius, color);
}
}
void AnimationsModule::drawTreeLights() {
int canvasW = _currentCanvas->width();
int canvasH = _currentCanvas->height();
int centerX = canvasW / 2;
// Dynamische Skalierung - GLEICH wie im Baum
float scale = canvasH / 66.0f;
// baseY MUSS exakt mit drawChristmasTree übereinstimmen
int baseY = canvasH - 4; // Basis am unteren Rand
int trunkHeight = (int)(10 * scale);
int treeBaseY = baseY - trunkHeight + 2; // Basis des Baumes (Grüns)
// Konfigurierbare Anzahl und Geschwindigkeit
int lightCount = config ? config->christmasTreeLightCount : 18;
if (lightCount < 5) lightCount = 5;
if (lightCount > 30) lightCount = 30;
int lightMode = config ? config->christmasTreeLightMode : 0;
uint16_t fixedColor = rgb565(255, 215, 0);
if (lightMode == 1 && config) {
fixedColor = hexToRgb565(config->christmasTreeLightColor.c_str());
}
// Verwende gemischte Positionen aus shuffleTreeElements()
uint16_t lightColors[] = {
rgb565(255, 255, 100), // Gelb
rgb565(255, 100, 100), // Rot
rgb565(100, 255, 100), // Grün
rgb565(100, 100, 255), // Blau
rgb565(255, 150, 255), // Rosa
rgb565(255, 200, 100) // Warmweiß
};
int numColors = 6;
// Baumschichten (müssen mit drawNaturalTree übereinstimmen)
int layer1Height = (int)(18 * scale);
int layer2Height = (int)(18 * scale);
int layer3Height = (int)(18 * scale);
int layer1Width = (int)(28 * scale);
int layer2Width = (int)(22 * scale);
int layer3Width = (int)(16 * scale);
int treeHeight = layer1Height + layer2Height - (int)(4 * scale) + layer3Height - (int)(10 * scale);
// Verwende gemischte Positionen aus shuffleTreeElements()
// Lichter über den Baum verteilt
for (int i = 0; i < lightCount && i < 30; i++) {
int lightY = treeBaseY - _shuffledLightY[i];
int lightX = centerX + _shuffledLightX[i];
// Blinken basierend auf Phase
uint32_t seed = simpleRandom(i * 11 + _shuffledLightX[i]);
bool isOn = ((i + _treeLightPhase + (seed % 3)) % 4) < 2;
if (isOn) {
uint16_t color;
if (lightMode == 1) {
// Feste Farbe
color = fixedColor;
} else {
// Zufällige Farbe
color = lightColors[(seed / 7) % numColors];
}
_currentCanvas->fillCircle(lightX, lightY, 1, color);
// Kleiner Glow-Effekt
_currentCanvas->drawPixel(lightX, lightY - 1, color);
}
}
}
void AnimationsModule::drawGifts(int centerX, int baseY, float scale) {
// Konfigurierbare Anzahl der Geschenke (0-10)
int giftCount = config ? config->christmasTreeGiftCount : 5;
if (giftCount < 0) giftCount = 0;
if (giftCount > 10) giftCount = 10;
if (giftCount == 0) return; // Keine Geschenke zeichnen
// Geschenkfarben (Hauptfarbe, Bandfarbe)
uint16_t giftColors[][2] = {
{rgb565(200, 0, 0), rgb565(255, 215, 0)}, // Rot mit Gold
{rgb565(0, 100, 200), rgb565(255, 255, 255)}, // Blau mit Weiß