-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathdisplay.cpp
More file actions
1914 lines (1687 loc) · 65.7 KB
/
display.cpp
File metadata and controls
1914 lines (1687 loc) · 65.7 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 "display.h"
#include "core/wifi/webInterface.h" // for server
#include "core/wifi/wg.h" //for isConnectedWireguard to print wireguard lock
#include "mykeyboard.h"
#include "settings.h" //for timeStr
#include "utils.h"
#include <JPEGDecoder.h>
#include <interface.h> //for charging ischarging to print charging indicator
#include <memory>
#define MAX_MENU_SIZE (int)(tftHeight / 25)
// Send the ST7789 into or out of sleep mode
void panelSleep(bool on) {
#if defined(ST7789_2_DRIVER) || defined(ST7789_DRIVER)
if (on) {
tft.writecommand(0x10); // SLPIN: panel off
delay(5);
} else {
tft.writecommand(0x11); // SLPOUT: panel on
delay(120);
}
#endif
// Disables tft writings on the display
tft.setSleepMode(on);
}
bool __attribute__((weak)) isCharging() { return false; }
/***************************************************************************************
** Function name: displayScrollingText
** Description: Scroll large texts into screen
***************************************************************************************/
void displayScrollingText(const String &text, Opt_Coord &coord) {
int len = text.length();
static String _lastText = "";
static int i = 0;
static long _lastmillis = 0;
// Reset scroll position when the selected item changes
if (text != _lastText) {
i = 0;
_lastText = text;
_lastmillis = 0;
}
tft.setTextColor(coord.fgcolor, coord.bgcolor);
if (len < coord.size) {
// Text fits within limit, no scrolling needed
return;
} else if (millis() > _lastmillis + 200) {
String displayText = text + " ";
int scrollLen = len + 8;
String scrollingPart = displayText.substring(i, i + (coord.size - 1));
tft.fillRect(
coord.x, coord.y,
(coord.size - 1) * LW * tft.getTextSize(),
LH * tft.getTextSize(),
bruceConfig.bgColor
);
tft.setCursor(coord.x, coord.y);
tft.print(scrollingPart);
if (i >= scrollLen - coord.size) i = -1;
_lastmillis = millis();
i++;
if (i == 1) _lastmillis = millis() + 1000;
}
}
/***************************************************************************************
** Function name: TouchFooter
** Description: Draw touch screen footer
***************************************************************************************/
void TouchFooter(uint16_t color) {
tft.drawRoundRect(5, tftHeight + 2, tftWidth - 10, 43, 5, color);
tft.setTextColor(color);
tft.setTextSize(FM);
tft.drawCentreString("PREV", tftWidth / 6, tftHeight + 4, 1);
tft.drawCentreString("SEL", tftWidth / 2, tftHeight + 4, 1);
tft.drawCentreString("NEXT", 5 * tftWidth / 6, tftHeight + 4, 1);
}
/***************************************************************************************
** Function name: TouchFooter
** Description: Draw touch screen footer
***************************************************************************************/
void MegaFooter(uint16_t color) {
tft.drawRoundRect(5, tftHeight + 2, tftWidth - 10, 43, 5, color);
tft.setTextColor(color);
tft.setTextSize(FM);
tft.drawCentreString("Exit", tftWidth / 6, tftHeight + 4, 1);
tft.drawCentreString("UP", tftWidth / 2, tftHeight + 4, 1);
tft.drawCentreString("DOWN", 5 * tftWidth / 6, tftHeight + 4, 1);
}
/***************************************************************************************
** Function name: resetTftDisplay
** Description: set cursor to 0,0, screen and text to default color
***************************************************************************************/
void resetTftDisplay(int x, int y, uint16_t fc, int size, uint16_t bg, uint16_t screen) {
tft.setCursor(x, y);
tft.fillScreen(screen);
tft.setTextSize(size);
tft.setTextColor(fc, bg);
tft.setTextDatum(0);
}
/***************************************************************************************
** Function name: setTftDisplay
** Description: set cursor, font color, size and bg font color
***************************************************************************************/
void setTftDisplay(int x, int y, uint16_t fc, int size, uint16_t bg) {
if (x >= 0 && y < 0) tft.setCursor(x, tft.getCursorY()); // if -1 on x, sets only y
else if (x < 0 && y >= 0) tft.setCursor(tft.getCursorX(), y); // if -1 on y, sets only x
else if (x >= 0 && y >= 0) tft.setCursor(x, y); // if x and y > 0, sets both
tft.setTextSize(size);
tft.setTextColor(fc, bg);
}
void turnOffDisplay() { setBrightness(0, false); }
bool wakeUpScreen() {
previousMillis = millis();
if (isScreenOff) {
isScreenOff = false;
dimmer = false;
getBrightness();
vTaskDelay(pdMS_TO_TICKS(200));
return true;
} else if (dimmer) {
dimmer = false;
getBrightness();
vTaskDelay(pdMS_TO_TICKS(200));
return true;
}
return false;
}
/***************************************************************************************
** Function name: displayRedStripe
** Description: Display Red Stripe with information
***************************************************************************************/
void displayRedStripe(String text, uint16_t fgcolor, uint16_t bgcolor) {
// detect if not running in interactive mode -> show nothing onscreen and return immediately
// if (server || isSleeping || isScreenOff) return; // webui is running
int size;
if (fgcolor == bgcolor && fgcolor == TFT_WHITE) fgcolor = TFT_BLACK;
if (text.length() * LW * FM < (tftWidth - 2 * FM * LW)) size = FM;
else size = FP;
tft.drawPixel(0, 0, 0);
tft.fillRoundRect(10, tftHeight / 2 - 13, tftWidth - 20, 26, 7, bgcolor);
tft.setTextColor(fgcolor, bgcolor);
if (size == FM) {
tft.setTextSize(FM);
tft.drawCentreString(text, tftWidth / 2, tftHeight / 2 - 8);
} else {
tft.setTextSize(FP);
int text_size = text.length();
if (text_size < (tftWidth - 20) / (LW * FP))
tft.drawCentreString(text, tftWidth / 2, tftHeight / 2 - 8);
else {
tft.drawCentreString(text.substring(0, text_size / 2), tftWidth / 2, tftHeight / 2 - 9);
tft.drawCentreString(text.substring(text_size / 2), tftWidth / 2, tftHeight / 2 + 1);
}
}
}
void drawButton(
int32_t x, int32_t y, int32_t w, int32_t h, uint32_t color, const char *text, bool inverted = false
) {
if (inverted) {
tft.fillRoundRect(x, y, w, h, 5, color);
} else {
tft.fillRoundRect(x, y, w, h, 5, TFT_BLACK);
tft.drawRoundRect(x, y, w, h, 5, color);
}
tft.setTextColor(inverted ? TFT_BLACK : color);
tft.drawString(text, x + w / 2, y + h);
}
int8_t displayMessage(
const char *message, const char *leftButton, const char *centerButton, const char *rightButton,
uint16_t color
) {
#ifdef HAS_SCREEN
uint8_t oldTextDatum = tft.getTextDatum();
#endif
tft.setTextColor(color);
tft.setTextSize(FM);
tft.setTextDatum(TC_DATUM);
// Handle newline characters in message
String msg = String(message);
int y = tftHeight / 2 - 20;
int start = 0;
int end = msg.indexOf('\n');
while (end != -1) {
tft.drawString(msg.substring(start, end), tftWidth / 2, y);
y += FM * 8;
start = end + 1;
end = msg.indexOf('\n', start);
}
tft.drawString(msg.substring(start), tftWidth / 2, y);
tft.setTextDatum(BC_DATUM);
int16_t buttonHeight = 20;
int16_t buttonY = tftHeight - buttonHeight - 5;
int16_t buttonWidth = tftWidth / 3 - 10;
int8_t totalButtons = (leftButton ? 1 : 0) + (centerButton ? 1 : 0) + (rightButton ? 1 : 0);
int8_t selected = 0; // Start at first available button
bool redraw = true;
while (true) {
if (check(PrevPress) || check(EscPress)) {
selected = (selected - 1 + totalButtons) % totalButtons; // Cycle backward
redraw = true;
}
if (check(NextPress)) {
selected = (selected + 1) % totalButtons; // Cycle forward
redraw = true;
}
if (check(SelPress)) { break; }
// Draw buttons with selection highlighting
int8_t index = 0;
if (redraw) {
if (leftButton) {
drawButton(5, buttonY, buttonWidth, buttonHeight, color, leftButton, selected == index);
index++;
}
if (centerButton) {
drawButton(
tftWidth / 3 + 5,
buttonY,
buttonWidth,
buttonHeight,
color,
centerButton,
selected == index
);
index++;
}
if (rightButton) {
drawButton(
tftWidth * 2 / 3 + 5,
buttonY,
buttonWidth,
buttonHeight,
color,
rightButton,
selected == index
);
}
redraw = false;
}
delay(10);
}
#ifdef HAS_SCREEN
tft.setTextDatum(oldTextDatum);
#endif
return selected;
}
void displayError(String txt, bool waitKeyPress) {
displayRedStripe(txt);
#ifndef HAS_SCREEN
Serial.println("ERR: " + txt);
return;
#endif
delay(200);
while (waitKeyPress && !check(AnyKeyPress)) vTaskDelay(10 / portTICK_PERIOD_MS);
}
void displayWarning(String txt, bool waitKeyPress) {
displayRedStripe(txt, TFT_BLACK, TFT_YELLOW);
#ifndef HAS_SCREEN
Serial.println("WARN: " + txt);
return;
#endif
delay(200);
while (waitKeyPress && !check(AnyKeyPress)) vTaskDelay(10 / portTICK_PERIOD_MS);
}
void displayInfo(String txt, bool waitKeyPress) {
// todo: add newlines to txt if too long
displayRedStripe(txt, TFT_WHITE, TFT_BLUE);
#ifndef HAS_SCREEN
Serial.println("INFO: " + txt);
return;
#endif
delay(200);
while (waitKeyPress && !check(AnyKeyPress)) vTaskDelay(10 / portTICK_PERIOD_MS);
}
void displaySuccess(String txt, bool waitKeyPress) {
// todo: add newlines to txt if too long
displayRedStripe(txt, TFT_WHITE, TFT_DARKGREEN);
#ifndef HAS_SCREEN
Serial.println("SUCCESS: " + txt);
return;
#endif
delay(200);
while (waitKeyPress && !check(AnyKeyPress)) vTaskDelay(10 / portTICK_PERIOD_MS);
}
void displayTextLine(String txt, bool waitKeyPress) {
// todo: add newlines to txt if too long
displayRedStripe(txt, getComplementaryColor2(bruceConfig.priColor), bruceConfig.priColor);
#ifndef HAS_SCREEN
Serial.println("MESSAGE: " + txt);
return;
#endif
delay(200);
while (waitKeyPress && !check(AnyKeyPress)) vTaskDelay(10 / portTICK_PERIOD_MS);
}
void setPadCursor(int16_t padx, int16_t pady) {
for (int y = 0; y < pady; y++) tft.println();
tft.setCursor(padx * BORDER_PAD_X, tft.getCursorY());
}
void padprintf(int16_t padx, const char *format, ...) {
char buffer[64];
va_list args;
va_start(args, format);
vsnprintf(buffer, sizeof(buffer), format, args);
va_end(args);
tft.setCursor(padx * BORDER_PAD_X, tft.getCursorY());
tft.printf("%s", buffer);
}
void padprintf(const char *format, ...) {
char buffer[64];
va_list args;
va_start(args, format);
vsnprintf(buffer, sizeof(buffer), format, args);
va_end(args);
tft.setCursor(BORDER_PAD_X, tft.getCursorY());
tft.printf("%s", buffer);
}
void padprint(const String &s, int16_t padx) {
tft.setCursor(padx * BORDER_PAD_X, tft.getCursorY());
tft.print(s);
}
void padprint(const char str[], int16_t padx) {
tft.setCursor(padx * BORDER_PAD_X, tft.getCursorY());
tft.print(str);
}
void padprint(char c, int16_t padx) {
tft.setCursor(padx * BORDER_PAD_X, tft.getCursorY());
tft.print(c);
}
void padprint(unsigned char b, int base, int16_t padx) {
tft.setCursor(padx * BORDER_PAD_X, tft.getCursorY());
tft.print(b, base);
}
void padprint(int n, int base, int16_t padx) {
tft.setCursor(padx * BORDER_PAD_X, tft.getCursorY());
tft.print(n, base);
}
void padprint(unsigned int n, int base, int16_t padx) {
tft.setCursor(padx * BORDER_PAD_X, tft.getCursorY());
tft.print(n, base);
}
void padprint(long n, int base, int16_t padx) {
tft.setCursor(padx * BORDER_PAD_X, tft.getCursorY());
tft.print(n, base);
}
void padprint(unsigned long n, int base, int16_t padx) {
tft.setCursor(padx * BORDER_PAD_X, tft.getCursorY());
tft.print(n, base);
}
void padprint(long long n, int base, int16_t padx) {
tft.setCursor(padx * BORDER_PAD_X, tft.getCursorY());
tft.print(n, base);
}
void padprint(unsigned long long n, int base, int16_t padx) {
tft.setCursor(padx * BORDER_PAD_X, tft.getCursorY());
tft.print(n, base);
}
void padprint(double n, int digits, int16_t padx) {
tft.setCursor(padx * BORDER_PAD_X, tft.getCursorY());
tft.print(n, digits);
}
void padprintln(const String &s, int16_t padx) {
if (s.isEmpty()) {
tft.setCursor(padx * BORDER_PAD_X, tft.getCursorY());
tft.println(s);
return;
}
String buff;
size_t start = 0;
int _maxCharsInLine = (tftWidth - (padx + 1) * BORDER_PAD_X) / (FP * LW);
// automatically split into multiple lines
while (!(buff = s.substring(start, start + _maxCharsInLine)).isEmpty()) {
tft.setCursor(padx * BORDER_PAD_X, tft.getCursorY());
tft.println(buff);
start += buff.length();
}
}
void padprintln(const char str[], int16_t padx) {
if (strcmp(str, "") == 0) {
tft.setCursor(padx * BORDER_PAD_X, tft.getCursorY());
tft.println(str);
return;
}
String buff;
size_t start = 0;
int _maxCharsInLine = (tftWidth - (padx + 1) * BORDER_PAD_X) / (FP * LW);
// automatically split into multiple lines
while (!(buff = String(str).substring(start, start + _maxCharsInLine)).isEmpty()) {
tft.setCursor(padx * BORDER_PAD_X, tft.getCursorY());
tft.println(buff);
start += buff.length();
}
}
void padprintln(char c, int16_t padx) {
tft.setCursor(padx * BORDER_PAD_X, tft.getCursorY());
tft.println(c);
}
void padprintln(unsigned char b, int base, int16_t padx) {
tft.setCursor(padx * BORDER_PAD_X, tft.getCursorY());
tft.println(b, base);
}
void padprintln(int n, int base, int16_t padx) {
tft.setCursor(padx * BORDER_PAD_X, tft.getCursorY());
tft.println(n, base);
}
void padprintln(unsigned int n, int base, int16_t padx) {
tft.setCursor(padx * BORDER_PAD_X, tft.getCursorY());
tft.println(n, base);
}
void padprintln(long n, int base, int16_t padx) {
tft.setCursor(padx * BORDER_PAD_X, tft.getCursorY());
tft.println(n, base);
}
void padprintln(unsigned long n, int base, int16_t padx) {
tft.setCursor(padx * BORDER_PAD_X, tft.getCursorY());
tft.println(n, base);
}
void padprintln(long long n, int base, int16_t padx) {
tft.setCursor(padx * BORDER_PAD_X, tft.getCursorY());
tft.println(n, base);
}
void padprintln(unsigned long long n, int base, int16_t padx) {
tft.setCursor(padx * BORDER_PAD_X, tft.getCursorY());
tft.println(n, base);
}
void padprintln(double n, int digits, int16_t padx) {
tft.setCursor(padx * BORDER_PAD_X, tft.getCursorY());
tft.println(n, digits);
}
/*********************************************************************
** Function: loopOptions
** Where you choose among the options in menu
**********************************************************************/
int loopOptions(
std::vector<Option> &options, uint8_t menuType, const char *subText, int index, bool interpreter
) {
Opt_Coord coord;
bool redraw = true;
bool exit = false;
int menuSize = options.size();
int devModeCounter = 0;
static unsigned long _clock_bat_timer = millis();
if (options.size() > MAX_MENU_SIZE) { menuSize = MAX_MENU_SIZE; }
if (index > 0)
tft.fillRoundRect(
tftWidth * 0.10,
tftHeight / 2 - menuSize * (FM * 8 + 4) / 2 - 5,
tftWidth * 0.8,
(FM * 8 + 4) * menuSize + 10,
5,
bruceConfig.bgColor
);
if (index >= options.size()) index = 0;
bool firstRender = true;
drawMainBorder();
while (1) {
// Check for shutdown before drawing menu to avoid drawing a black bar on the screen
if (exit) break;
if (menuType == MENU_TYPE_MAIN) {
checkReboot();
if (devModeCounter >= 5 && !bruceConfig.devMode) {
bruceConfig.setDevMode(true);
displayInfo("Dev Mode Enabled", true);
}
if (millis() - _clock_bat_timer > 30000) {
_clock_bat_timer = millis();
drawStatusBar(); // update clock and battery status each 30s
}
}
if (redraw) {
menuOptionType = menuType; // updates menutype to the remote controller
menuOptionLabel = subText;
// update the hovered
for (auto &opt : options) opt.hovered = false;
options[index].hovered = true;
bool renderedByLambda = false;
if (options[index].hover)
renderedByLambda = options[index].hover(options[index].hoverPointer, true);
if (!renderedByLambda) {
if (menuType == MENU_TYPE_SUBMENU) drawSubmenu(index, options, subText);
else
coord = drawOptions(
index,
options,
bruceConfig.priColor,
bruceConfig.secColor,
bruceConfig.bgColor,
firstRender
);
}
firstRender = false;
redraw = false;
}
// handleSerialCommands(); // always use serial task for it
#ifdef HAS_KEYBOARD
checkShortcutPress(); // shortctus to quickly start apps without navigating the menus
#endif
if (menuType == MENU_TYPE_REGULAR) {
String txt = options[index].label;
displayScrollingText(txt, coord);
}
// Checks ESC Press first, to not exit after PrevPress is processed
// PrevPress condition is a StickCPlus workaround, as it uses the same button for Prev and Esc
// Same happens to Core and some other boards
if (EscPress && PrevPress) EscPress = false;
if (menuType != MENU_TYPE_MAIN && check(EscPress)) {
index = -1;
break;
}
if (PrevPress || check(UpPress)) {
devModeCounter = 0;
#ifdef HAS_KEYBOARD
check(PrevPress);
if (index == 0) index = options.size() - 1;
else if (index > 0) index--;
redraw = true;
#else
long _tmp = millis();
#ifndef HAS_ENCODER // T-Embed doesn't need it
LongPress = true;
while (PrevPress && menuType != MENU_TYPE_MAIN) {
if (millis() - _tmp > 200)
tft.drawArc(
tftWidth / 2,
tftHeight / 2,
25,
15,
0,
360 * (millis() - (_tmp + 200)) / 500,
getColorVariation(bruceConfig.priColor),
bruceConfig.bgColor
);
vTaskDelay(10 / portTICK_RATE_MS);
}
tft.drawArc(
tftWidth / 2, tftHeight / 2, 25, 15, 0, 360, bruceConfig.bgColor, bruceConfig.bgColor
);
LongPress = false;
#endif
if (millis() - _tmp > 700) { // longpress detected to exit
index = -1;
break;
} else {
check(PrevPress);
if (index == 0) index = options.size() - 1;
else if (index > 0) index--;
redraw = true;
}
#endif
}
/* DW Btn to next item */
if (check(NextPress) || check(DownPress)) {
index++;
if ((index + 1) > options.size()) {
if (!bruceConfig.devMode) devModeCounter++;
index = 0;
}
redraw = true;
}
vTaskDelay(10 / portTICK_PERIOD_MS);
/* Select and run function
forceMenuOption is set by a SerialCommand to force a selection within the menu
*/
if (check(SelPress) || forceMenuOption >= 0) {
uint16_t chosen = index;
if (forceMenuOption >= 0) {
chosen = forceMenuOption;
forceMenuOption = -1; // reset SerialCommand navigation option
Serial.print("Forcely ");
}
Serial.println("Selected: " + String(options[chosen].label));
options[chosen].operation();
break;
}
// interpreter_start -> running the interpreter
// interpreter -> loopOptions helper inside the Javascript
if (interpreter_state > 0 && !interpreter) { break; }
}
return index;
}
/***************************************************************************************
** Function name: progressHandler
** Description: Função para manipular o progresso da atualização
** Dependencia: prog_handler =>> 0 - Flash, 1 - LittleFS
***************************************************************************************/
void progressHandler(int progress, size_t total, String message) {
int barWidth = map(progress, 0, total, 0, tftWidth - 40);
if (barWidth < 3) {
tft.fillRect(6, 27, tftWidth - 12, tftHeight - 33, bruceConfig.bgColor);
tft.drawRect(18, tftHeight - 47, tftWidth - 36, 17, bruceConfig.priColor);
displayRedStripe(message, TFT_WHITE, bruceConfig.priColor);
}
tft.fillRect(20, tftHeight - 45, barWidth, 13, bruceConfig.priColor);
}
/***************************************************************************************
** Function name: drawOptions
** Description: Função para desenhar e mostrar as opçoes de contexto
***************************************************************************************/
Opt_Coord drawOptions(
int index, std::vector<Option> &options, uint16_t fgcolor, uint16_t selcolor, uint16_t bgcolor,
bool firstRender
) {
Opt_Coord coord;
int menuSize = options.size();
if (options.size() > MAX_MENU_SIZE) { menuSize = MAX_MENU_SIZE; }
// Uncomment to update the statusBar (causes flickering)
// drawStatusBar();
int32_t optionsTopY = tftHeight / 2 - menuSize * (FM * 8 + 4) / 2 - 5;
tft.drawPixel(0, 0, bruceConfig.bgColor);
if (firstRender) {
tft.fillRoundRect(
tftWidth * 0.10, optionsTopY, tftWidth * 0.8, (FM * 8 + 4) * menuSize + 10, 5, bgcolor
);
tft.drawRoundRect(
tftWidth * 0.10,
tftHeight / 2 - menuSize * (FM * 8 + 4) / 2 - 5,
tftWidth * 0.8,
(FM * 8 + 4) * menuSize + 10,
5,
fgcolor
);
}
tft.setTextColor(fgcolor, bgcolor);
tft.setTextSize(FM);
tft.setCursor(tftWidth * 0.10 + 5, tftHeight / 2 - menuSize * (FM * 8 + 4) / 2);
int i = 0;
int init = 0;
int cont = 1;
menuSize = options.size();
if (index >= MAX_MENU_SIZE) init = index - MAX_MENU_SIZE + 1;
for (i = 0; i < menuSize; i++) {
if (i >= init) {
if (options[i].selected) tft.setTextColor(selcolor, bgcolor); // if selected, change Text color
else tft.setTextColor(fgcolor, bgcolor);
String text = "";
if (i == index) {
text += ">";
coord.x = tftWidth * 0.10 + 5 + FM * LW;
coord.y = tft.getCursorY() + 4;
coord.size = (tftWidth * 0.8 - 10) / (LW * FM) - 1;
coord.fgcolor = fgcolor;
coord.bgcolor = bgcolor;
} else text += " ";
text += String(options[i].label) + " ";
tft.setCursor(tftWidth * 0.10 + 5, tft.getCursorY() + 4);
tft.println(text.substring(0, (tftWidth * 0.8 - 10) / (LW * FM) - 1));
cont++;
}
if (cont > MAX_MENU_SIZE) goto Exit;
}
Exit:
if (options.size() > MAX_MENU_SIZE) menuSize = MAX_MENU_SIZE;
#if defined(HAS_TOUCH)
TouchFooter();
#endif
return coord;
}
/***************************************************************************************
** Function name: drawSubmenu
** Description: Função para desenhar e mostrar as opçoes de contexto
***************************************************************************************/
void drawSubmenu(int index, std::vector<Option> &options, const char *title) {
drawStatusBar();
int menuSize = options.size();
tft.setTextColor(bruceConfig.priColor, bruceConfig.bgColor);
tft.setTextSize(FP);
tft.drawPixel(0, 0, 0);
tft.fillRect(6, 30, tftWidth - 12, 8 * FP, bruceConfig.bgColor);
tft.drawString(title, 12, 30);
// middle of the drawing area
int middle = 25 /*status*/ + (tftHeight - 30 /*status + bottom margin*/) / 2;
// drawCentreString uses TC_DATUM, so we need to adjust the Y position
// 42 ensures that title isnt touched( 30 + 8 (LH) + 4(Margin))
int middle_up = middle - (tftHeight - 42) / 3 - FM * LH / 2 + 4;
int middle_down = middle + (tftHeight - 42) / 3 - FM * LH / 2;
tft.setTextSize(FM);
#if defined(HAS_TOUCH)
tft.drawCentreString("/\\", tftWidth / 2, middle_up - (FM * LH + 6), 1);
#endif
// Previous item
const char *firstOption =
index - 1 >= 0 ? options[index - 1].label.c_str() : options[menuSize - 1].label.c_str();
tft.setTextColor(bruceConfig.secColor);
tft.fillRect(6, middle_up, tftWidth - 12, 8 * FM, bruceConfig.bgColor);
tft.drawCentreString(firstOption, tftWidth / 2, middle_up, SMOOTH_FONT);
// Selected item
int selectedTextSize = options[index].label.length() <= tftWidth / (LW * FG) - 1 ? FG : FM;
tft.setTextSize(selectedTextSize);
tft.setTextColor(bruceConfig.priColor);
tft.fillRect(6, middle - FG * LH / 2 - 1, tftWidth - 12, FG * LH + 5, bruceConfig.bgColor);
tft.drawCentreString(options[index].label, tftWidth / 2, middle - selectedTextSize * LH / 2, SMOOTH_FONT);
tft.drawFastHLine(
tftWidth / 2 - strlen(options[index].label.c_str()) * selectedTextSize * LW / 2,
middle + selectedTextSize * LH / 2 + 1,
strlen(options[index].label.c_str()) * selectedTextSize * LW,
bruceConfig.priColor
);
// Next Item
const char *thirdOption =
index + 1 < menuSize ? options[index + 1].label.c_str() : options[0].label.c_str();
tft.setTextSize(FM);
tft.setTextColor(bruceConfig.secColor);
tft.fillRect(6, middle_down, tftWidth - 12, 8 * FM, bruceConfig.bgColor);
tft.drawCentreString(thirdOption, tftWidth / 2, middle_down, SMOOTH_FONT);
tft.fillRect(tftWidth - 5, 0, 5, tftHeight, bruceConfig.bgColor);
tft.fillRect(tftWidth - 5, index * tftHeight / menuSize, 5, tftHeight / menuSize, bruceConfig.priColor);
#if defined(HAS_TOUCH)
tft.drawCentreString("\\/", tftWidth / 2, middle_down + (FM * LH + 6), 1);
tft.setTextColor(getColorVariation(bruceConfig.priColor), bruceConfig.bgColor);
tft.drawString("[ x ]", 7, 7, 1);
TouchFooter();
#endif
}
void drawStatusBar() {
int i = 0;
uint8_t bat = getBattery();
uint8_t bat_margin = 85;
if (bat > 0) {
drawBatteryStatus(bat);
} else bat_margin = 26;
if (sdcardMounted) {
tft.setTextColor(bruceConfig.priColor, bruceConfig.bgColor);
tft.setTextSize(FP);
tft.drawString("SD", tftWidth - (bat_margin), 12);
i++;
} // Indication for SD card on screen
if (gpsConnected) {
drawGpsSmall(tftWidth - (bat_margin + 23 * i), 7);
i++;
}
if (WiFi.getMode()) {
drawWifiSmall(tftWidth - (bat_margin + 23 * i), 7);
i++;
} // Draw Wifi Symbol beside battery
if (isWebUIActive) {
drawWebUISmall(tftWidth - (bat_margin + 23 * i), 7);
i++;
} // Draw Wifi Symbol beside battery
if (BLEConnected) {
drawBLESmall(tftWidth - (bat_margin + 23 * i), 7);
i++;
} // Draw BLE beside Wifi
if (isConnectedWireguard) {
drawWireguardStatus(tftWidth - (bat_margin + 24 * i), 7);
i++;
} // Draw Wg bedide BLE, if the others exist, if not, beside battery
if (bruceConfig.theme.border) {
tft.drawRoundRect(5, 5, tftWidth - 10, tftHeight - 10, 5, bruceConfig.priColor);
tft.drawLine(5, 25, tftWidth - 6, 25, bruceConfig.priColor);
}
if (clock_set) {
int clock_fontsize = 1; // Font size of the clock / BRUCE + BRUCE_VERSION
setTftDisplay(12, 12, bruceConfig.priColor, clock_fontsize, bruceConfig.bgColor);
tft.fillRect(12, 12, 100, clock_fontsize * LH, bruceConfig.bgColor);
#if defined(HAS_RTC)
updateTimeStr(_rtc.getTimeStruct());
#else
updateTimeStr(rtc.getTimeStruct());
#endif
tft.print(timeStr);
} else {
setTftDisplay(12, 12, bruceConfig.priColor, 1, bruceConfig.bgColor);
tft.print("BRUCE " + String(BRUCE_VERSION));
}
}
void drawMainBorder(bool clear) {
if (clear) {
tft.drawPixel(0, 0, 0);
tft.fillScreen(bruceConfig.bgColor);
}
setTftDisplay(12, 12, bruceConfig.priColor, 1, bruceConfig.bgColor);
tft.setTextDatum(0);
// if(wifiConnected) {tft.print(timeStr);} else {tft.print("BRUCE 1.0b");}
drawStatusBar();
#if defined(HAS_TOUCH)
TouchFooter();
#endif
}
void drawMainBorderWithTitle(String title, bool clear) {
drawMainBorder(clear);
printTitle(title);
}
void printTitle(String title) {
tft.setCursor((tftWidth - (title.length() * FM * LW)) / 2, BORDER_PAD_Y);
tft.setTextColor(bruceConfig.priColor, bruceConfig.bgColor);
tft.setTextSize(FM);
title.toUpperCase();
tft.println(title);
tft.setTextSize(FP);
}
void printSubtitle(String subtitle, bool withLine) {
int16_t cursorX = (tftWidth - (subtitle.length() * FP * LW)) / 2;
tft.setTextColor(bruceConfig.priColor, bruceConfig.bgColor);
tft.setTextSize(FP);
tft.setCursor(cursorX, BORDER_PAD_Y + FM * LH);
tft.println(subtitle);
if (withLine) {
String line = "";
for (byte i = 0; i < subtitle.length(); i++) line += "-";
tft.setCursor(cursorX, tft.getCursorY());
tft.println(line);
}
}
void printFootnote(String text) {
tft.setTextSize(FP);
tft.drawRightString(text, tftWidth - BORDER_PAD_X, tftHeight - BORDER_PAD_X - FP * LH, SMOOTH_FONT);
}
void printCenterFootnote(String text) {
tft.fillRect(10, tftHeight - BORDER_PAD_X - FP * LH, tftWidth - 20, FP * LH, bruceConfig.bgColor);
tft.setTextSize(FP);
tft.drawCentreString(text, tftWidth / 2, tftHeight - BORDER_PAD_X - FP * LH, SMOOTH_FONT);
}
/***************************************************************************************
** Function name: drawBatteryStatus()
** Description: Draws battery info into the Status bar
***************************************************************************************/
void drawBatteryStatus(uint8_t bat) {
if (bat == 0) return;
bool charging = isCharging();
uint16_t color = bruceConfig.priColor;
uint16_t barcolor = bruceConfig.priColor;
if (bat < 16) barcolor = color = TFT_RED;
else if (bat < 34) barcolor = color = TFT_YELLOW;
if (charging) color = TFT_GREEN;
tft.drawRoundRect(tftWidth - 43, 6, 36, 19, 2, charging ? color : bruceConfig.bgColor); // (bolder border)
tft.drawRoundRect(tftWidth - 42, 7, 34, 17, 2, color);
tft.setTextSize(FP);
tft.setTextColor(bruceConfig.priColor, bruceConfig.bgColor);
tft.drawRightString((bat == 100 ? "" : " ") + String(bat) + "%", tftWidth - 45, 12, 1);
tft.fillRoundRect(tftWidth - 40, 9, 30 * bat / 100, 13, 2, barcolor);
tft.drawLine(tftWidth - 30, 9, tftWidth - 30, 9 + 13, bruceConfig.bgColor);
tft.drawLine(tftWidth - 20, 9, tftWidth - 20, 9 + 13, bruceConfig.bgColor);
}
/***************************************************************************************
** Function name: drawWireguardStatus()
** Description: Draws a padlock when connected
***************************************************************************************/
void drawWireguardStatus(int x, int y) {
tft.fillRect(x, y, 20, 17, bruceConfig.bgColor);
if (isConnectedWireguard) {
tft.drawRoundRect(11 + x, 0 + y, 8, 12, 5, TFT_GREEN);
tft.fillRoundRect(10 + x, 8 + y, 10, 8, 0, TFT_GREEN);
} else {
tft.drawRoundRect(1 + x, 0 + y, 8, 12, 5, bruceConfig.priColor);
tft.fillRoundRect(0 + x, 8 + y, 10, 8, 0, bruceConfig.bgColor);
tft.fillRoundRect(6 + x, 8 + y, 10, 10, 0, bruceConfig.priColor);
}
}
/***************************************************************************************
** Function name: listFiles
** Description: Função para desenhar e mostrar o menu principal
***************************************************************************************/
#define MAX_ITEMS (int)(tftHeight - 20) / (LH * FM)
static bool _listFilesForceReset = true;
void resetListFilesCache() { _listFilesForceReset = true; }
// Draw one line with text+bg atomically — no flicker, no separate fillRect
static void _drawLine(int visPos, bool selected, const FileList &item, int nchars) {
uint16_t fg;
if (item.folder) fg = getColorVariation(bruceConfig.priColor);
else if (item.operation) fg = ALCOLOR;
else fg = bruceConfig.priColor;
tft.setTextColor(fg, bruceConfig.bgColor);
tft.setCursor(10, 10 + visPos * LH * FM);
String txt = selected ? ">" : " ";
txt += item.filename;
while ((int)txt.length() < nchars) txt += ' ';
tft.print(txt.substring(0, nchars));
}
Opt_Coord listFiles(int index, std::vector<FileList> fileList) {
Opt_Coord coord;
tft.setTextSize(FM);
int arraySize = fileList.size();
int nchars = (tftWidth - 20) / (6 * FM);
int start = 0;
if (index >= MAX_ITEMS) {
start = index - MAX_ITEMS + 1;
if (start < 0) start = 0;
}
static int _lastStart = -1;
static int _lastIndex = -1;
bool pageChanged = (_listFilesForceReset || start != _lastStart);
if (pageChanged) {
// Full redraw only when page changes or forced (folder change)
if (_listFilesForceReset) {
tft.fillScreen(bruceConfig.bgColor);
tft.drawRoundRect(5, 5, tftWidth - 10, tftHeight - 10, 5, bruceConfig.priColor);
}
for (int visPos = 0; visPos < MAX_ITEMS; visPos++) {
int i = start + visPos;
if (i >= arraySize) {
// Clear empty slot
tft.setTextColor(bruceConfig.priColor, bruceConfig.bgColor);
tft.setCursor(10, 10 + visPos * LH * FM);
String blank(nchars, ' ');
tft.print(blank);
} else {
_drawLine(visPos, i == index, fileList[i], nchars);
}
}
_listFilesForceReset = false;
} else if (index != _lastIndex) {
// Partial redraw: only the 2 lines that changed
int oldVis = _lastIndex - start;
int newVis = index - start;
if (oldVis >= 0 && oldVis < MAX_ITEMS && _lastIndex < arraySize)
_drawLine(oldVis, false, fileList[_lastIndex], nchars);
if (newVis >= 0 && newVis < MAX_ITEMS)
_drawLine(newVis, true, fileList[index], nchars);