-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClockRadioScreen.cpp
More file actions
858 lines (766 loc) · 32.5 KB
/
Copy pathClockRadioScreen.cpp
File metadata and controls
858 lines (766 loc) · 32.5 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
#include "ClockRadioScreen.h"
#include "ClockDigitImages.h"
#include <esp_heap_caps.h>
#include <math.h>
namespace {
constexpr int DISPLAY_W = 480;
constexpr int DISPLAY_H = 222;
constexpr int DIGIT_EDGE = 16;
constexpr int DIGIT_COLON_W = 20;
constexpr int DIGIT_INNER_GAP = 4;
constexpr int DIGIT_OUTER_GAP = 2;
constexpr int DIGIT_W = (DISPLAY_W - (2 * DIGIT_EDGE) - DIGIT_COLON_W - (2 * DIGIT_INNER_GAP) - (2 * DIGIT_OUTER_GAP)) / 4;
constexpr int DIGIT_IMAGE_W = 74;
constexpr int DIGIT_IMAGE_H = 104;
constexpr int DIGIT_H = 170;
constexpr int HALF_H = 85;
constexpr int DIGIT_IMAGE_X = (DIGIT_W - DIGIT_IMAGE_W) / 2;
constexpr int DIGIT_IMAGE_TOP_Y = 38;
constexpr int DIGIT_IMAGE_BOTTOM_Y = DIGIT_IMAGE_TOP_Y - HALF_H;
constexpr int LEAF_CANVAS_PAD = 10;
constexpr int LEAF_CANVAS_W = DIGIT_W + (2 * LEAF_CANVAS_PAD);
constexpr bool ENABLE_FLIP_ANIMATION = true;
constexpr float FLIP_PERSPECTIVE = 0.16f;
constexpr float FLIP_MAX_DARKENING = 0.45f;
constexpr uint8_t FLIP_FRAME_COUNT = 16;
constexpr uint16_t DIGIT_STAGGER_MS = 40;
constexpr float DEG_TO_RAD_F = 0.01745329252f;
static const float flipAngles[FLIP_FRAME_COUNT] = {
0.0f, 14.0f, 28.0f, 42.0f,
55.0f, 66.0f, 76.0f, 84.0f,
89.0f, 91.0f, 97.0f, 107.0f,
120.0f, 136.0f, 154.0f, 180.0f
};
static const uint16_t flipFrameDurationMs[FLIP_FRAME_COUNT] = {
14, 14, 16, 18,
20, 22, 24, 26,
24, 24, 22, 20,
20, 22, 26, 34
};
constexpr uint32_t COL_BG = 0x000000;
constexpr uint32_t COL_CASE = 0x000000;
constexpr uint32_t COL_CASE_DARK = 0x332A21;
constexpr uint32_t COL_CASE_LIGHT = 0xEADCC0;
constexpr uint32_t COL_CARD = 0x050505;
constexpr uint32_t COL_CARD_DIM = 0x020202;
constexpr uint32_t COL_DIGIT = 0xCDB58B;
constexpr uint32_t COL_DIGIT_DIM = 0x7F6B4F;
constexpr uint32_t COL_SPLIT = 0x050505;
constexpr uint32_t COL_TEXT = 0xB4A78F;
constexpr uint32_t COL_ACCENT = 0xC4472D;
constexpr int SETTINGS_STATE_X = 20;
constexpr int SETTINGS_ROW_Y = 38;
constexpr int SETTINGS_STATE_W = 72;
constexpr int SETTINGS_VALUE_Y = 34;
constexpr int SETTINGS_VALUE_H = 50;
constexpr int SETTINGS_SAVE_X = 75;
constexpr int SETTINGS_SAVE_Y = 99;
constexpr int SETTINGS_SAVE_W = 142;
constexpr int SETTINGS_SAVE_H = 28;
struct FlipDigit
{
lv_obj_t *container;
lv_obj_t *staticTop;
lv_obj_t *staticBottom;
lv_obj_t *baseLabel;
lv_obj_t *staticTopLabel;
lv_obj_t *staticBottomLabel;
lv_obj_t *leafCanvas;
uint8_t *leafBuffer;
int digitScreenX;
int digitScreenY;
int currentValue;
int pendingValue;
int oldValue;
uint8_t frameIndex;
uint32_t nextFrameAtMs;
bool isAnimating;
};
lv_obj_t *screen = nullptr;
lv_obj_t *caseBox = nullptr;
lv_obj_t *flipOverlay = nullptr;
lv_obj_t *wifiLabel = nullptr;
lv_obj_t *brandLabel = nullptr;
lv_obj_t *batteryLabel = nullptr;
lv_obj_t *headerStatusLabel = nullptr;
lv_obj_t *wifiBars[4] = {};
lv_obj_t *colonTop = nullptr;
lv_obj_t *colonBottom = nullptr;
lv_obj_t *secondsLabel = nullptr;
lv_obj_t *dateLabel = nullptr;
lv_obj_t *stationLabel = nullptr;
lv_obj_t *titleLabel = nullptr;
lv_obj_t *volumeOverlay = nullptr;
lv_obj_t *volumeOverlayLabel = nullptr;
lv_obj_t *settingsOverlay = nullptr;
lv_obj_t *settingsTitleLabel = nullptr;
lv_obj_t *settingsStateLabel = nullptr;
lv_obj_t *settingsValue1Label = nullptr;
lv_obj_t *settingsColonLabel = nullptr;
lv_obj_t *settingsValue2Label = nullptr;
lv_obj_t *settingsSaveLabel = nullptr;
lv_obj_t *messageLabel = nullptr;
lv_timer_t *flipTimer = nullptr;
FlipDigit digits[4] = {};
String stationText = "RADIO OFF";
String titleText = "";
String batteryStatus = "Akku --";
bool wifiOk = false;
bool streamOk = false;
bool clockValid = false;
int wifiRssi = -127;
int displayedDigits[4] = {-1, -1, -1, -1};
uint8_t lastMinute = 255;
uint32_t volumeOverlayUntil = 0;
uint32_t demoStarted = 0;
uint32_t groundhogMessageUntil = 0;
static void setHidden(lv_obj_t *obj, bool hidden)
{
if (!obj) return;
if (hidden) lv_obj_add_flag(obj, LV_OBJ_FLAG_HIDDEN);
else lv_obj_clear_flag(obj, LV_OBJ_FLAG_HIDDEN);
}
static String safeText(const String &text, uint16_t maxLen)
{
if (text.length() <= maxLen) return text;
return text.substring(0, maxLen - 3) + "...";
}
static lv_obj_t *makeObj(lv_obj_t *parent, int x, int y, int w, int h, uint32_t bg, uint8_t radius)
{
lv_obj_t *obj = lv_obj_create(parent);
lv_obj_set_size(obj, w, h);
lv_obj_align(obj, LV_ALIGN_TOP_LEFT, x, y);
lv_obj_set_style_bg_color(obj, lv_color_hex(bg), 0);
lv_obj_set_style_bg_opa(obj, LV_OPA_COVER, 0);
lv_obj_set_style_border_width(obj, 0, 0);
lv_obj_set_style_radius(obj, radius, 0);
lv_obj_set_style_pad_all(obj, 0, 0);
lv_obj_clear_flag(obj, LV_OBJ_FLAG_SCROLLABLE);
return obj;
}
static lv_obj_t *makeLabel(lv_obj_t *parent, int x, int y, int w, const lv_font_t *font, uint32_t color, lv_text_align_t align = LV_TEXT_ALIGN_LEFT)
{
lv_obj_t *label = lv_label_create(parent);
lv_obj_set_width(label, w);
lv_obj_align(label, LV_ALIGN_TOP_LEFT, x, y);
lv_obj_set_style_text_font(label, font, 0);
lv_obj_set_style_text_color(label, lv_color_hex(color), 0);
lv_obj_set_style_text_align(label, align, 0);
lv_label_set_long_mode(label, LV_LABEL_LONG_CLIP);
return label;
}
static void styleSettingField(lv_obj_t *label, bool active)
{
if (!label) return;
lv_obj_set_style_bg_color(label, lv_color_hex(active ? COL_ACCENT : 0x241D16), 0);
lv_obj_set_style_bg_opa(label, LV_OPA_COVER, 0);
lv_obj_set_style_border_width(label, active ? 1 : 0, 0);
lv_obj_set_style_border_color(label, lv_color_hex(active ? 0xF0A080 : 0x241D16), 0);
lv_obj_set_style_radius(label, 5, 0);
lv_obj_set_style_text_color(label, lv_color_hex(active ? 0xFFF3DC : COL_DIGIT), 0);
}
static void styleSettingState(lv_obj_t *label, bool enabled, bool active)
{
if (!label) return;
const uint32_t background = enabled ? 0x244632 : 0x4A2828;
const uint32_t border = enabled ? 0x7FCB96 : 0xD28A85;
const uint32_t text = enabled ? 0xC9F2D4 : 0xF3C7C3;
lv_obj_set_style_bg_color(label, lv_color_hex(background), 0);
lv_obj_set_style_bg_opa(label, LV_OPA_COVER, 0);
lv_obj_set_style_border_width(label, 1, 0);
lv_obj_set_style_border_color(label, lv_color_hex(border), 0);
lv_obj_set_style_radius(label, 5, 0);
lv_obj_set_style_text_color(label, lv_color_hex(text), 0);
lv_obj_set_style_shadow_width(label, active ? 8 : 0, 0);
lv_obj_set_style_shadow_opa(label, active ? 70 : 0, 0);
lv_obj_set_style_shadow_color(label, lv_color_hex(border), 0);
}
static void placeSettingLabel(lv_obj_t *label, int x, int y, int w, int h, int padTop)
{
lv_obj_set_pos(label, x, y);
lv_obj_set_size(label, w, h);
lv_obj_set_style_pad_top(label, padTop, 0);
}
static void styleDigitHalf(lv_obj_t *half, uint32_t bg)
{
lv_obj_set_style_bg_color(half, lv_color_hex(bg), 0);
lv_obj_set_style_bg_opa(half, LV_OPA_COVER, 0);
lv_obj_set_style_border_width(half, 0, 0);
lv_obj_set_style_radius(half, 4, 0);
lv_obj_set_style_pad_all(half, 0, 0);
lv_obj_clear_flag(half, LV_OBJ_FLAG_SCROLLABLE);
}
static void createSplitLines(lv_obj_t *parent)
{
makeObj(parent, 0, HALF_H - 2, DIGIT_W, 1, 0x2A251D, 0);
makeObj(parent, 0, HALF_H - 1, DIGIT_W, 2, COL_SPLIT, 0);
makeObj(parent, 0, HALF_H + 1, DIGIT_W, 1, 0x332B22, 0);
}
static lv_obj_t *makeDigitImage(lv_obj_t *parent, int y, uint32_t color)
{
lv_obj_t *image = lv_image_create(parent);
lv_obj_align(image, LV_ALIGN_TOP_LEFT, (DIGIT_W - DIGIT_IMAGE_W) / 2, y);
lv_obj_set_style_image_recolor(image, lv_color_hex(color), 0);
lv_obj_set_style_image_recolor_opa(image, LV_OPA_COVER, 0);
return image;
}
static void setHalfDigit(lv_obj_t *image, int value)
{
lv_image_set_src(image, clockDigitImages[value]);
}
static void setDigitText(lv_obj_t *image, int value)
{
lv_image_set_src(image, clockDigitImages[value]);
}
static uint16_t color565(uint32_t rgb)
{
uint8_t r = (rgb >> 16) & 0xFF;
uint8_t g = (rgb >> 8) & 0xFF;
uint8_t b = rgb & 0xFF;
return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
}
static uint8_t *allocateLeafBuffer()
{
size_t bytes = LEAF_CANVAS_W * HALF_H * 4;
void *buffer = heap_caps_malloc(bytes, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
if (!buffer) buffer = heap_caps_malloc(bytes, MALLOC_CAP_8BIT);
return static_cast<uint8_t *>(buffer);
}
static uint32_t argb8888(uint8_t alpha, uint32_t rgb)
{
return (static_cast<uint32_t>(alpha) << 24) | (rgb & 0x00FFFFFF);
}
static uint32_t shadeArgb(uint32_t color, float shade)
{
uint8_t a = (color >> 24) & 0xFF;
uint8_t r = (color >> 16) & 0xFF;
uint8_t g = (color >> 8) & 0xFF;
uint8_t b = color & 0xFF;
r = static_cast<uint8_t>(constrain(static_cast<int>(r * shade), 0, 255));
g = static_cast<uint8_t>(constrain(static_cast<int>(g * shade), 0, 255));
b = static_cast<uint8_t>(constrain(static_cast<int>(b * shade), 0, 255));
return (static_cast<uint32_t>(a) << 24) | (static_cast<uint32_t>(r) << 16) |
(static_cast<uint32_t>(g) << 8) | b;
}
static uint32_t blendArgb(uint32_t bg, uint32_t fg, uint8_t alpha)
{
uint8_t br = (bg >> 16) & 0xFF;
uint8_t bgc = (bg >> 8) & 0xFF;
uint8_t bb = bg & 0xFF;
uint8_t fr = (fg >> 16) & 0xFF;
uint8_t fgc = (fg >> 8) & 0xFF;
uint8_t fb = fg & 0xFF;
uint8_t r = (fr * alpha + br * (255 - alpha)) / 255;
uint8_t g = (fgc * alpha + bgc * (255 - alpha)) / 255;
uint8_t b = (fb * alpha + bb * (255 - alpha)) / 255;
return 0xFF000000 | (static_cast<uint32_t>(r) << 16) | (static_cast<uint32_t>(g) << 8) | b;
}
static uint16_t shade565(uint16_t color, float shade)
{
uint8_t r = ((color >> 11) & 0x1F) * 255 / 31;
uint8_t g = ((color >> 5) & 0x3F) * 255 / 63;
uint8_t b = (color & 0x1F) * 255 / 31;
r = static_cast<uint8_t>(constrain(static_cast<int>(r * shade), 0, 255));
g = static_cast<uint8_t>(constrain(static_cast<int>(g * shade), 0, 255));
b = static_cast<uint8_t>(constrain(static_cast<int>(b * shade), 0, 255));
return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
}
static uint16_t blend565(uint16_t bg, uint16_t fg, uint8_t alpha)
{
if (alpha == 0) return bg;
if (alpha == 255) return fg;
uint8_t br = ((bg >> 11) & 0x1F) * 255 / 31;
uint8_t bgc = ((bg >> 5) & 0x3F) * 255 / 63;
uint8_t bb = (bg & 0x1F) * 255 / 31;
uint8_t fr = ((fg >> 11) & 0x1F) * 255 / 31;
uint8_t fgc = ((fg >> 5) & 0x3F) * 255 / 63;
uint8_t fb = (fg & 0x1F) * 255 / 31;
uint8_t r = (fr * alpha + br * (255 - alpha)) / 255;
uint8_t g = (fgc * alpha + bgc * (255 - alpha)) / 255;
uint8_t b = (fb * alpha + bb * (255 - alpha)) / 255;
return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
}
static uint8_t digitMaskPixel(int digit, bool bottomHalf, int halfX, int halfY)
{
if (digit < 0 || digit > 9) return 0;
int imageX = halfX - DIGIT_IMAGE_X;
int imageY = bottomHalf ? halfY - DIGIT_IMAGE_BOTTOM_Y : halfY - DIGIT_IMAGE_TOP_Y;
if (imageX < 0 || imageX >= DIGIT_IMAGE_W || imageY < 0 || imageY >= DIGIT_IMAGE_H) return 0;
const lv_image_dsc_t *image = clockDigitImages[digit];
return image->data[imageY * image->header.stride + imageX];
}
static void drawLeafEdge(uint8_t *buffer, int y, uint32_t color)
{
if (!buffer || y < 0 || y >= HALF_H) return;
uint32_t *row = reinterpret_cast<uint32_t *>(buffer) + y * LEAF_CANVAS_W;
for (int x = LEAF_CANVAS_PAD; x < LEAF_CANVAS_PAD + DIGIT_W; ++x) row[x] = color;
}
static void renderFlipLeaf(FlipDigit &digit, float angleDegrees)
{
if (!digit.leafCanvas || !digit.leafBuffer) return;
float rad = angleDegrees * DEG_TO_RAD_F;
float projection = fabsf(cosf(rad));
int visibleHeight = max(1, static_cast<int>(HALF_H * projection));
bool backSide = angleDegrees >= 90.0f;
int sourceDigit = backSide ? digit.pendingValue : digit.oldValue;
uint32_t card = argb8888(255, backSide ? 0x070707 : 0x10100F);
uint32_t text = argb8888(255, backSide ? COL_DIGIT : COL_DIGIT_DIM);
uint32_t edge = argb8888(255, 0x010101);
float shade = 1.0f - FLIP_MAX_DARKENING * sinf(rad);
uint32_t *leafPixels = reinterpret_cast<uint32_t *>(digit.leafBuffer);
for (int i = 0; i < LEAF_CANVAS_W * HALF_H; ++i) leafPixels[i] = 0x00000000;
int hingeWidth = DIGIT_W;
int freeEdgeWidth = static_cast<int>(DIGIT_W * (1.0f + FLIP_PERSPECTIVE * sinf(rad)));
freeEdgeWidth = constrain(freeEdgeWidth, DIGIT_W, LEAF_CANVAS_W);
for (int y = 0; y < visibleHeight; ++y) {
float t = visibleHeight <= 1 ? 0.0f : static_cast<float>(y) / static_cast<float>(visibleHeight - 1);
int rowWidth;
if (backSide) rowWidth = hingeWidth - static_cast<int>((hingeWidth - freeEdgeWidth) * t);
else rowWidth = freeEdgeWidth + static_cast<int>((hingeWidth - freeEdgeWidth) * t);
rowWidth = constrain(rowWidth, 1, LEAF_CANVAS_W);
int x0 = (LEAF_CANVAS_W - rowWidth) / 2;
int x1 = x0 + rowWidth;
int sourceHalfY = static_cast<int>(t * static_cast<float>(HALF_H - 1));
float rowLight = backSide ? (0.82f + 0.18f * t) : (0.96f - 0.18f * t);
float hingeShadow = backSide ? (0.88f + 0.12f * sinf(t * 3.14159f)) : (1.0f - 0.18f * sinf(t * 3.14159f));
float finalShade = shade * rowLight * hingeShadow;
uint32_t *row = leafPixels + y * LEAF_CANVAS_W;
for (int x = x0; x < x1; ++x) {
float sxNorm = rowWidth <= 1 ? 0.0f : static_cast<float>(x - x0) / static_cast<float>(rowWidth - 1);
int sourceX = static_cast<int>(sxNorm * static_cast<float>(DIGIT_W - 1));
uint8_t alpha = digitMaskPixel(sourceDigit, backSide, sourceX, sourceHalfY);
uint32_t px = blendArgb(card, text, alpha);
row[x] = shadeArgb(px, finalShade);
}
}
if (angleDegrees > 80.0f && angleDegrees < 102.0f) {
drawLeafEdge(digit.leafBuffer, backSide ? 0 : visibleHeight - 1, edge);
if (visibleHeight > 2) drawLeafEdge(digit.leafBuffer, backSide ? 1 : visibleHeight - 2, edge);
} else {
drawLeafEdge(digit.leafBuffer, backSide ? 0 : visibleHeight - 1, edge);
}
int leafLocalY = backSide ? HALF_H : HALF_H - visibleHeight;
int perspectiveDrop = static_cast<int>(3.0f * sinf(rad));
if (angleDegrees >= 176.0f) leafLocalY += 1;
int canvasX = digit.digitScreenX - LEAF_CANVAS_PAD;
int canvasY = digit.digitScreenY + leafLocalY + perspectiveDrop;
lv_obj_set_pos(digit.leafCanvas, canvasX, canvasY);
lv_obj_set_size(digit.leafCanvas, LEAF_CANVAS_W, visibleHeight);
lv_obj_invalidate(digit.leafCanvas);
}
static void finishDigitFlip(FlipDigit &digit)
{
setHalfDigit(digit.staticTopLabel, digit.pendingValue);
setHalfDigit(digit.staticBottomLabel, digit.pendingValue);
setDigitText(digit.baseLabel, digit.pendingValue);
setHidden(digit.leafCanvas, true);
digit.currentValue = digit.pendingValue;
digit.oldValue = -1;
digit.pendingValue = -1;
digit.frameIndex = 0;
digit.isAnimating = false;
}
static void advanceFlipAnimations(lv_timer_t *)
{
uint32_t now = millis();
for (int i = 0; i < 4; ++i) {
FlipDigit &digit = digits[i];
if (!digit.isAnimating || (int32_t)(now - digit.nextFrameAtMs) < 0) continue;
if (digit.frameIndex >= FLIP_FRAME_COUNT) {
finishDigitFlip(digit);
continue;
}
renderFlipLeaf(digit, flipAngles[digit.frameIndex]);
setHidden(digit.leafCanvas, false);
uint8_t durationIndex = digit.frameIndex;
digit.frameIndex++;
digit.nextFrameAtMs = now + flipFrameDurationMs[durationIndex];
}
}
static void setupDigit(FlipDigit &digit, uint8_t index, int x, int y)
{
digit.digitScreenX = x;
digit.digitScreenY = y;
digit.container = makeObj(screen, x, y, DIGIT_W, DIGIT_H, 0x111719, 9);
lv_obj_set_style_bg_opa(digit.container, LV_OPA_COVER, 0);
lv_obj_set_style_border_width(digit.container, 0, 0);
lv_obj_set_style_shadow_width(digit.container, 8, 0);
lv_obj_set_style_shadow_offset_y(digit.container, 4, 0);
lv_obj_set_style_shadow_opa(digit.container, 140, 0);
lv_obj_set_style_shadow_color(digit.container, lv_color_hex(0x000000), 0);
digit.staticTop = makeObj(digit.container, 0, 0, DIGIT_W, HALF_H, COL_CARD, 0);
digit.staticBottom = makeObj(digit.container, 0, HALF_H, DIGIT_W, HALF_H, COL_CARD, 0);
lv_obj_set_style_bg_opa(digit.staticTop, LV_OPA_COVER, 0);
lv_obj_set_style_bg_opa(digit.staticBottom, LV_OPA_COVER, 0);
digit.baseLabel = makeDigitImage(digit.container, 38, COL_DIGIT);
setHidden(digit.baseLabel, true);
digit.staticTopLabel = makeDigitImage(digit.staticTop, 38, COL_DIGIT);
digit.staticBottomLabel = makeDigitImage(digit.staticBottom, 38 - HALF_H, COL_DIGIT);
(void)index;
digit.leafBuffer = allocateLeafBuffer();
digit.leafCanvas = flipOverlay ? lv_canvas_create(flipOverlay) : nullptr;
if (digit.leafBuffer && digit.leafCanvas) {
lv_canvas_set_buffer(digit.leafCanvas, digit.leafBuffer, LEAF_CANVAS_W, HALF_H, LV_COLOR_FORMAT_ARGB8888);
}
if (digit.leafCanvas) {
lv_obj_set_pos(digit.leafCanvas, 0, 0);
lv_obj_set_size(digit.leafCanvas, LEAF_CANVAS_W, HALF_H);
lv_obj_clear_flag(digit.leafCanvas, LV_OBJ_FLAG_SCROLLABLE);
setHidden(digit.leafCanvas, true);
}
createSplitLines(digit.container);
digit.currentValue = -1;
digit.pendingValue = -1;
digit.oldValue = -1;
digit.frameIndex = 0;
digit.nextFrameAtMs = 0;
digit.isAnimating = false;
}
static void startDigitFlip(FlipDigit &digit, int newValue, uint32_t delayMs)
{
if (newValue == digit.currentValue && digit.currentValue >= 0) return;
if (digit.currentValue < 0) {
setHalfDigit(digit.staticTopLabel, newValue);
setHalfDigit(digit.staticBottomLabel, newValue);
setDigitText(digit.baseLabel, newValue);
digit.currentValue = newValue;
return;
}
if (digit.isAnimating) return;
if (!ENABLE_FLIP_ANIMATION || !digit.leafBuffer || !digit.leafCanvas) {
setHalfDigit(digit.staticTopLabel, newValue);
setHalfDigit(digit.staticBottomLabel, newValue);
setDigitText(digit.baseLabel, newValue);
digit.currentValue = newValue;
return;
}
digit.oldValue = digit.currentValue;
digit.pendingValue = newValue;
digit.isAnimating = true;
digit.frameIndex = 1;
setHalfDigit(digit.staticTopLabel, newValue);
setHalfDigit(digit.staticBottomLabel, digit.oldValue);
setDigitText(digit.baseLabel, digit.oldValue);
renderFlipLeaf(digit, flipAngles[0]);
lv_obj_move_foreground(digit.leafCanvas);
setHidden(digit.leafCanvas, false);
uint32_t now = millis();
digit.nextFrameAtMs = now + max<uint32_t>(delayMs, flipFrameDurationMs[0]);
}
static void colonOpacity(void *var, int32_t value)
{
if (!var) return;
lv_opa_t opa = (lv_opa_t)value;
lv_obj_set_style_bg_opa(static_cast<lv_obj_t *>(var), opa, 0);
}
static void startColonPulse()
{
if (!colonTop || !colonBottom) return;
lv_anim_t a1;
lv_anim_init(&a1);
lv_anim_set_var(&a1, colonTop);
lv_anim_set_values(&a1, LV_OPA_COVER, 45);
lv_anim_set_duration(&a1, 500);
lv_anim_set_playback_duration(&a1, 500);
lv_anim_set_repeat_count(&a1, LV_ANIM_REPEAT_INFINITE);
lv_anim_set_exec_cb(&a1, colonOpacity);
lv_anim_start(&a1);
lv_anim_t a2;
lv_anim_init(&a2);
lv_anim_set_var(&a2, colonBottom);
lv_anim_set_values(&a2, LV_OPA_COVER, 45);
lv_anim_set_duration(&a2, 500);
lv_anim_set_playback_duration(&a2, 500);
lv_anim_set_repeat_count(&a2, LV_ANIM_REPEAT_INFINITE);
lv_anim_set_exec_cb(&a2, colonOpacity);
lv_anim_start(&a2);
}
static int wifiBarsForRssi()
{
if (!wifiOk) return 0;
if (wifiRssi > -58) return 4;
if (wifiRssi > -68) return 3;
if (wifiRssi > -78) return 2;
return 1;
}
static const char *weekdayName(int wday)
{
static const char *names[] = {"Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"};
return (wday >= 0 && wday < 7) ? names[wday] : "";
}
static const char *monthName(int mon)
{
static const char *names[] = {"Januar", "Februar", "Maerz", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"};
return (mon >= 0 && mon < 12) ? names[mon] : "";
}
static void updateStatusVisuals()
{
int bars = wifiBarsForRssi();
lv_label_set_text(wifiLabel, wifiOk ? "WLAN" : "WLAN --");
for (int i = 0; i < 4; ++i) {
lv_obj_set_style_bg_color(wifiBars[i], lv_color_hex(i < bars ? 0x3A3025 : 0x151515), 0);
}
lv_label_set_text(batteryLabel, batteryStatus.c_str());
}
static void updateRadioText()
{
if (!streamOk) {
lv_label_set_text(stationLabel, "RADIO OFF");
lv_label_set_text(titleLabel, "");
return;
}
String line = stationText;
line += " | ";
line += titleText.length() ? titleText : "Live Stream";
lv_label_set_text(stationLabel, safeText(line, 57).c_str());
lv_label_set_text(titleLabel, "");
}
static bool getDemoTime(struct tm &timeinfo)
{
if (!CLOCK_DEMO_MODE) return false;
if (!demoStarted) demoStarted = millis();
uint32_t elapsedMinutes = (millis() - demoStarted) / 1000;
int totalMinutes = CLOCK_DEMO_0558_RUN ? (5 * 60 + 58 + elapsedMinutes) : elapsedMinutes;
totalMinutes %= 24 * 60;
memset(&timeinfo, 0, sizeof(timeinfo));
timeinfo.tm_hour = totalMinutes / 60;
timeinfo.tm_min = totalMinutes % 60;
timeinfo.tm_mday = 5;
timeinfo.tm_mon = 5;
timeinfo.tm_year = 126;
timeinfo.tm_wday = 5;
return true;
}
static void applyTime(const struct tm &timeinfo, bool valid)
{
if (!valid) {
lv_label_set_text(dateLabel, "TIME NOT SET");
int blankDigits[4] = {0, 0, 0, 0};
for (int i = 0; i < 4; ++i) startDigitFlip(digits[i], blankDigits[i], 0);
return;
}
int nextDigits[4] = {
timeinfo.tm_hour / 10,
timeinfo.tm_hour % 10,
timeinfo.tm_min / 10,
timeinfo.tm_min % 10,
};
if (timeinfo.tm_min != lastMinute) {
startDigitFlip(digits[3], nextDigits[3], 0);
startDigitFlip(digits[2], nextDigits[2], DIGIT_STAGGER_MS);
startDigitFlip(digits[1], nextDigits[1], DIGIT_STAGGER_MS * 2);
startDigitFlip(digits[0], nextDigits[0], DIGIT_STAGGER_MS * 3);
lastMinute = timeinfo.tm_min;
if (ENABLE_GROUNDHOG_0600_EFFECT && timeinfo.tm_hour == 6 && timeinfo.tm_min == 0 &&
displayedDigits[0] == 0 && displayedDigits[1] == 5 && displayedDigits[2] == 5 && displayedDigits[3] == 9) {
groundhogMessageUntil = millis() + 2000;
}
} else if (displayedDigits[0] < 0) {
for (int i = 0; i < 4; ++i) startDigitFlip(digits[i], nextDigits[i], 0);
}
for (int i = 0; i < 4; ++i) displayedDigits[i] = nextDigits[i];
char date[56];
snprintf(date, sizeof(date), "%s, %d. %s", weekdayName(timeinfo.tm_wday), timeinfo.tm_mday, monthName(timeinfo.tm_mon));
lv_label_set_text(dateLabel, date);
char seconds[8];
snprintf(seconds, sizeof(seconds), "%02d SEC", timeinfo.tm_sec);
lv_label_set_text(secondsLabel, seconds);
}
}
void createClockRadioScreen()
{
if (screen) return;
lv_obj_t *root = lv_screen_active();
screen = makeObj(root, 0, 0, DISPLAY_W, DISPLAY_H, COL_BG, 0);
wifiLabel = makeLabel(screen, 10, 4, 42, &lv_font_montserrat_10, 0xA8A8A8);
for (int i = 0; i < 4; ++i) {
wifiBars[i] = makeObj(screen, 52 + i * 6, 15 - i * 3, 4, 5 + i * 3, 0x555555, 1);
}
brandLabel = makeLabel(screen, 118, 4, 244, &lv_font_montserrat_10, 0xA8A8A8, LV_TEXT_ALIGN_CENTER);
lv_label_set_text(brandLabel, "TOMS WEB RADIO");
batteryLabel = makeLabel(screen, 362, 4, 110, &lv_font_montserrat_10, 0xA8A8A8, LV_TEXT_ALIGN_RIGHT);
headerStatusLabel = makeLabel(screen, 8, 4, 464, &lv_font_montserrat_10, 0xB9C7D8, LV_TEXT_ALIGN_CENTER);
lv_label_set_long_mode(headerStatusLabel, LV_LABEL_LONG_CLIP);
lv_obj_set_height(headerStatusLabel, 17);
lv_obj_set_style_bg_color(headerStatusLabel, lv_color_hex(0x0B111A), 0);
lv_obj_set_style_bg_opa(headerStatusLabel, LV_OPA_COVER, 0);
lv_obj_set_style_radius(headerStatusLabel, 3, 0);
lv_obj_set_style_pad_top(headerStatusLabel, 2, 0);
setHidden(wifiLabel, true);
setHidden(brandLabel, true);
setHidden(batteryLabel, true);
for (lv_obj_t *bar : wifiBars) setHidden(bar, true);
caseBox = makeObj(screen, 0, 0, 480, 222, COL_CASE, 0);
lv_obj_set_style_border_width(caseBox, 0, 0);
lv_obj_move_background(caseBox);
flipOverlay = lv_obj_create(screen);
lv_obj_set_size(flipOverlay, DISPLAY_W, DISPLAY_H);
lv_obj_align(flipOverlay, LV_ALIGN_TOP_LEFT, 0, 0);
lv_obj_set_style_bg_opa(flipOverlay, LV_OPA_TRANSP, 0);
lv_obj_set_style_border_width(flipOverlay, 0, 0);
lv_obj_set_style_pad_all(flipOverlay, 0, 0);
lv_obj_clear_flag(flipOverlay, LV_OBJ_FLAG_SCROLLABLE);
lv_obj_clear_flag(flipOverlay, LV_OBJ_FLAG_CLICKABLE);
constexpr int colonX = (DISPLAY_W - DIGIT_COLON_W) / 2;
const int digit0X = DIGIT_EDGE;
const int digit1X = digit0X + DIGIT_W + DIGIT_OUTER_GAP;
const int digit2X = colonX + DIGIT_COLON_W + DIGIT_INNER_GAP;
const int digit3X = DISPLAY_W - DIGIT_EDGE - DIGIT_W;
setupDigit(digits[0], 0, digit0X, 26);
setupDigit(digits[1], 1, digit1X, 26);
setupDigit(digits[2], 2, digit2X, 26);
setupDigit(digits[3], 3, digit3X, 26);
lv_obj_move_foreground(flipOverlay);
if (!flipTimer) flipTimer = lv_timer_create(advanceFlipAnimations, 8, nullptr);
colonTop = makeObj(screen, colonX, 74, 20, 20, COL_DIGIT, 2);
colonBottom = makeObj(screen, colonX, 117, 20, 20, COL_DIGIT, 2);
startColonPulse();
secondsLabel = makeLabel(screen, 395, 175, 62, &lv_font_montserrat_12, 0x777777, LV_TEXT_ALIGN_RIGHT);
lv_label_set_text(secondsLabel, "00 SEC");
dateLabel = makeLabel(screen, 8, 204, 144, &lv_font_montserrat_10, 0xA8A8A8);
stationLabel = makeLabel(screen, 170, 204, 217, &lv_font_montserrat_10, 0xB8B8B8, LV_TEXT_ALIGN_LEFT);
titleLabel = makeLabel(screen, 170, 214, 217, &lv_font_montserrat_10, 0xB8B8B8, LV_TEXT_ALIGN_LEFT);
messageLabel = makeLabel(screen, 92, 203, 296, &lv_font_montserrat_12, COL_ACCENT, LV_TEXT_ALIGN_CENTER);
lv_label_set_text(messageLabel, "");
setHidden(messageLabel, true);
setHidden(titleLabel, true);
volumeOverlay = makeObj(screen, 186, 84, 108, 42, 0x18130F, 5);
lv_obj_set_style_bg_opa(volumeOverlay, 220, 0);
lv_obj_set_style_border_width(volumeOverlay, 1, 0);
lv_obj_set_style_border_color(volumeOverlay, lv_color_hex(COL_ACCENT), 0);
volumeOverlayLabel = makeLabel(volumeOverlay, 0, 10, 108, &lv_font_montserrat_16, COL_CASE_LIGHT, LV_TEXT_ALIGN_CENTER);
lv_label_set_text(volumeOverlayLabel, "VOL --%");
setHidden(volumeOverlay, true);
settingsOverlay = makeObj(screen, 94, 42, 292, 142, 0x17130F, 8);
lv_obj_set_style_bg_opa(settingsOverlay, 245, 0);
lv_obj_set_style_border_width(settingsOverlay, 1, 0);
lv_obj_set_style_border_color(settingsOverlay, lv_color_hex(COL_ACCENT), 0);
settingsTitleLabel = makeLabel(settingsOverlay, 16, 10, 260, &lv_font_montserrat_14, COL_TEXT, LV_TEXT_ALIGN_CENTER);
settingsStateLabel = makeLabel(settingsOverlay, 15, 38, 70, &lv_font_montserrat_16, COL_DIGIT, LV_TEXT_ALIGN_CENTER);
settingsValue1Label = makeLabel(settingsOverlay, 96, 34, 62, &lv_font_montserrat_28, COL_DIGIT, LV_TEXT_ALIGN_CENTER);
settingsColonLabel = makeLabel(settingsOverlay, 158, 41, 28, &lv_font_montserrat_28, COL_DIGIT, LV_TEXT_ALIGN_CENTER);
lv_label_set_text(settingsColonLabel, ":");
settingsValue2Label = makeLabel(settingsOverlay, 186, 34, 62, &lv_font_montserrat_28, COL_DIGIT, LV_TEXT_ALIGN_CENTER);
settingsSaveLabel = makeLabel(settingsOverlay, 75, 99, 142, &lv_font_montserrat_12, COL_DIGIT, LV_TEXT_ALIGN_CENTER);
lv_label_set_text(settingsSaveLabel, "SPEICHERN");
placeSettingLabel(settingsStateLabel, SETTINGS_STATE_X, SETTINGS_ROW_Y, SETTINGS_STATE_W, 42, 11);
placeSettingLabel(settingsValue1Label, 104, SETTINGS_VALUE_Y, 62, SETTINGS_VALUE_H, 9);
placeSettingLabel(settingsValue2Label, 200, SETTINGS_VALUE_Y, 62, SETTINGS_VALUE_H, 9);
placeSettingLabel(settingsSaveLabel, SETTINGS_SAVE_X, SETTINGS_SAVE_Y, SETTINGS_SAVE_W, SETTINGS_SAVE_H, 7);
setHidden(settingsOverlay, true);
hideClockRadioScreen();
}
void showClockRadioScreen()
{
createClockRadioScreen();
setHidden(screen, false);
}
void hideClockRadioScreen()
{
setHidden(screen, true);
}
void updateClockRadioScreen()
{
if (!screen || lv_obj_has_flag(screen, LV_OBJ_FLAG_HIDDEN)) return;
struct tm demoTime;
if (getDemoTime(demoTime)) {
applyTime(demoTime, true);
}
updateStatusVisuals();
updateRadioText();
bool showVolume = volumeOverlayUntil && (int32_t)(millis() - volumeOverlayUntil) < 0;
setHidden(volumeOverlay, !showVolume);
if (groundhogMessageUntil && (int32_t)(millis() - groundhogMessageUntil) < 0) {
lv_label_set_text(messageLabel, "GOOD MORNING, CAMPERS!");
setHidden(messageLabel, false);
} else {
groundhogMessageUntil = 0;
lv_label_set_text(messageLabel, "");
setHidden(messageLabel, true);
}
}
void setClockRadioStation(const String &stationName)
{
stationText = stationName.length() ? stationName : "RADIO OFF";
}
void setClockRadioTitle(const String &title)
{
titleText = title;
}
void setClockRadioStatus(bool wifiConnected, int rssi, const String &batteryText, bool streamActive, bool timeValid)
{
wifiOk = wifiConnected;
wifiRssi = rssi;
batteryStatus = batteryText.length() ? batteryText : "Akku --";
streamOk = streamActive;
clockValid = timeValid;
}
void setClockRadioTime(const struct tm &timeinfo, bool valid)
{
clockValid = valid;
if (!CLOCK_DEMO_MODE) applyTime(timeinfo, valid);
}
void setClockRadioHeader(const String &statusText)
{
if (headerStatusLabel) lv_label_set_text(headerStatusLabel, statusText.c_str());
}
void showClockRadioVolume(uint8_t volumePercent)
{
if (volumePercent > 100) volumePercent = 100;
char text[12];
snprintf(text, sizeof(text), "VOL %u%%", volumePercent);
lv_label_set_text(volumeOverlayLabel, text);
volumeOverlayUntil = millis() + 2000;
setHidden(volumeOverlay, false);
}
void showClockRadioAlarm(bool enabled, uint8_t hour, uint8_t minute, uint8_t activeField)
{
if (!settingsOverlay) return;
char value[8];
lv_label_set_text(settingsTitleLabel, "WECKER");
lv_label_set_text(settingsStateLabel, enabled ? "EIN" : "AUS");
snprintf(value, sizeof(value), "%02u", hour);
lv_label_set_text(settingsValue1Label, value);
snprintf(value, sizeof(value), "%02u", minute);
lv_label_set_text(settingsValue2Label, value);
placeSettingLabel(settingsStateLabel, SETTINGS_STATE_X, SETTINGS_ROW_Y, SETTINGS_STATE_W, 42, 11);
placeSettingLabel(settingsValue1Label, 104, SETTINGS_VALUE_Y, 62, SETTINGS_VALUE_H, 9);
placeSettingLabel(settingsColonLabel, 166, 41, 28, 40, 0);
placeSettingLabel(settingsValue2Label, 200, SETTINGS_VALUE_Y, 62, SETTINGS_VALUE_H, 9);
placeSettingLabel(settingsSaveLabel, SETTINGS_SAVE_X, SETTINGS_SAVE_Y, SETTINGS_SAVE_W, SETTINGS_SAVE_H, 7);
styleSettingState(settingsStateLabel, enabled, activeField == 0);
styleSettingField(settingsValue1Label, activeField == 1);
styleSettingField(settingsValue2Label, activeField == 2);
styleSettingField(settingsSaveLabel, activeField == 3);
setHidden(settingsColonLabel, false);
setHidden(settingsValue2Label, false);
setHidden(volumeOverlay, true);
setHidden(settingsOverlay, false);
}
void hideClockRadioAlarm()
{
setHidden(settingsOverlay, true);
}
void showClockRadioSleep(bool enabled, uint8_t minutes, uint8_t activeField)
{
if (!settingsOverlay) return;
char value[12];
lv_label_set_text(settingsTitleLabel, "SLEEP-TIMER");
lv_label_set_text(settingsStateLabel, enabled ? "EIN" : "AUS");
snprintf(value, sizeof(value), "%02u MIN", minutes);
lv_label_set_text(settingsValue1Label, value);
placeSettingLabel(settingsStateLabel, SETTINGS_STATE_X, SETTINGS_ROW_Y, SETTINGS_STATE_W, 42, 11);
placeSettingLabel(settingsValue1Label, 104, SETTINGS_VALUE_Y, 158, SETTINGS_VALUE_H, 9);
placeSettingLabel(settingsSaveLabel, SETTINGS_SAVE_X, SETTINGS_SAVE_Y, SETTINGS_SAVE_W, SETTINGS_SAVE_H, 7);
styleSettingState(settingsStateLabel, enabled, activeField == 0);
styleSettingField(settingsValue1Label, activeField == 1);
styleSettingField(settingsSaveLabel, activeField == 2);
setHidden(settingsColonLabel, true);
setHidden(settingsValue2Label, true);
setHidden(volumeOverlay, true);
setHidden(settingsOverlay, false);
}
void hideClockRadioSleep()
{
setHidden(settingsOverlay, true);
}