-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathGUI.h
More file actions
2899 lines (2449 loc) · 91.8 KB
/
GUI.h
File metadata and controls
2899 lines (2449 loc) · 91.8 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
/*
Copyright © 2019, 2020, 2021, 2022, 2023 HackEDA, Inc.
Licensed under the WiPhone Public License v.1.0 (the "License"); you
may not use this file except in compliance with the License. You may
obtain a copy of the License at
https://wiphone.io/WiPhone_Public_License_v1.0.txt.
Unless required by applicable law or agreed to in writing, software,
hardware or documentation distributed under the License is distributed
on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
#ifndef _WIPHONE_GUI_H_
#define _WIPHONE_GUI_H_
#define _(X) X //FIXME: move this into a header file.
#include "src/TFT_eSPI/TFT_eSPI.h"
#include <Arduino.h>
#include "Storage.h"
#include <random>
#include "Networks.h"
#include "WiPhoneConfig.h"
#include "helpers.h"
#include "Hardware.h"
#include "src/assets/fonts.h"
#include "src/assets/icons.h"
#include "src/ringbuff.h"
#include "clock.h"
#include "Audio.h"
#include "FairyMax.h"
#include "ota.h"
#include "lora.h"
#include "driver/uart.h"
#include "soc/uart_struct.h"
using namespace std;
#define LCD TFT_eSPI
extern LCD* static_lcd; // a hack for the LCD to be usable from static callbacks
extern Lora lora;
extern bool backupWiFiConnected;
extern char *backupConnectedSsid;
// Pixel colors
#define WHITE TFT_WHITE // 0xFFFF
#define GRAY TFT_DARKGREY // 0x7BEF // 50%
#define BLACK TFT_BLACK // 0x0000
#define BLUE TFT_BLUE // 0x001F
#define GREEN TFT_GREEN // 0x07E0
#define RED TFT_RED // 0xF800
#define YELLOW TFT_YELLOW // 0XFFE0 // red + green
#define MAGENTA TFT_MAGENTA // 0xF81F // blue + red
#define CYAN TFT_CYAN // 0x7FFF // blue + green
#define NONE TFT_BLACK // 0x0000
#define GETBLUE(x) (x & BLUE)
#define GETRED(x) ((x & RED)>>11)
#define GETGREEN(x) ((x & GREEN)>>6) // only 5 highest bits
// 13 shades of gray
// (BLUE & (uint16_t)(BLUE*k)) | (GREEN & (uint16_t)(GREEN*k)) | (RED & (uint16_t)(RED*k))
// Python: (BLUE & int(BLUE*k)) | (GREEN & int(GREEN*k)) | (RED & int(RED*k))
#define GRAY_05 0x0861
#define GRAY_10 0x18C3
#define GRAY_15 0x2124
#define GRAY_20 0x3186
#define GRAY_25 0x39E7
#define GRAY_33 0x528A
#define GRAY_50 GRAY
#define GRAY_67 0xA554
#define GRAY_75 0xBDF7
#define GRAY_80 0xC658
#define GRAY_85 0xD6BA
#define GRAY_90 0xDF1B
#define GRAY_95 0xEF7D
typedef uint16_t colorType;
#ifdef WIPHONE_PRODUCTION
//#define GUI_DEBUG(fmt, ...)
//#define GUI_DEBUG0(a1, ...)
//#define GUI_DEBUG2(a1,a2, ...)
//#define DIAGNOSTICS_REPORT(fmt, ...)
#else
//#define GUI_DEBUG(fmt, ...) DEBUG(fmt, ##__VA_ARGS__)
//#define GUI_DEBUG0(a1, ...) Serial.print(a1, ##__VA_ARGS__)
//#define GUI_DEBUG2(a1,a2, ...) do { Serial.print(a1); Serial.println(a2, ##__VA_ARGS__); } while(0)
//#define DIAGNOSTICS_REPORT(fmt, ...) log_i("[Diagnostics] " fmt, ##__VA_ARGS__)
#endif // WIPHONE_PRODUCTION
#define BUTTON_PADDING 26
// Theme colors
// Python: rgb = lambda r,g,b: hex((((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3)))
#define RGB_COLOR(r,g,b) (((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3))
#define THEME_HEADER_SIZE 30
#define THEME_FOOTER_SIZE 40
#define THEME_COLOR 0x7BFF // RGB = 50%, 50%, 100%
#define THEME_APP_COLOR 0xFBEF // RGB = 100%, 50%, 50%
#define THEME_BG BLACK
#define THEME_TEXT_COLOR WHITE
#define THEME_CURSOR WHITE
#define TOMATO 0xFBEF // RGB = 100%, 50%, 50%
#define SALAD 0x57EA // RGB = 33%, 100%, 33%
#define REDDISH 0xFBF5 // RGB = 100%, 50%, 70%
// New palette colors
#define WP_COLOR_0 0x0000 // Black
#define WP_ACCENT_0 0x4CDB // Lighter blue (#4C9ADD)
#define WP_COLOR_1 0xFFFF // White
#define WP_ACCENT_1 0x0379 // Darker blue (#006FCE)
#define WP_DISAB_0 0x632C // Darker gray (#646464)
#define WP_DISAB_1 0xB596 // Lighter gray (#B2B2B2)
#define WP_ACCENT_S 0xFA40 // Light orange (#FF4C00)
#define WP_ACCENT_G TFT_GREEN // Completely green (#0x07E0)
#define N_MAX_ITEMS 0 // fit as many items in menu as possible
#define N_MENU_ITEMS 5 // items per screen in regular menus
#define N_OPTION_ITEMS 7 // items per screen in option menus
#define KEYBOARD_TIMEOUT_EVENT 0x7f
#define IS_KEYBOARD(event) (event <= 0x7f)
// Non-keyboard events (>=0x80)
// TODO: use a struct with bit fields for the same purpose; this got a bit confusing
#define APP_TIMER_EVENT 0x80 // also: non-keyboard event flag
#define BATTERY_UPDATE_EVENT 0x81 // each event should have only two bits set
#define CALL_UPDATE_EVENT 0x82
#define WIFI_ICON_UPDATE_EVENT 0x84 // triggered when RSSI level changed SIGNIFICANTLY and icon has to be redrawn
#define TIME_UPDATE_EVENT 0x88
#define USER_SERIAL_EVENT 0x180
#define REGISTRATION_UPDATE_EVENT 0x280
#define BATTERY_BLINK_EVENT 0x480
#define USB_UPDATE_EVENT 0x880
#define POWER_OFF_EVENT 0x1080 // power off is about to happen (user has been pressing the POWER OFF/END button for a few seconds now)
#define POWER_NOT_OFF_EVENT 0x2080 // power off was expected to happen, but user released the OFF/END button
#define NEW_MESSAGE_EVENT 0x4080
#define CUSTOM_EVENT 0x8080 // these are flags for a custom event. AppID must be stored in bits 0-6, bits 8-14 are for custom event identification
#define SCREEN_DIM_EVENT 0x8780 // AppID == 0 means GUI class itself
#define SCREEN_SLEEP_EVENT 0x8680 // AppID == 0 means GUI class itself
#define UNLOCK_CLEAR_EVENT 0x8880 // AppID == 0 means GUI class itself
#define MESSAGES_ACK_RECVD_EVENT 0x4082
#define MESSAGE_SENT_EVENT 0x4083
typedef uint16_t EventType;
#define NONKEY_EVENT_ONE_OF(e, flags) ((e & 0x80) && ((e & 0xFF7F) & (flags))) // only for non-keyboard events
// Keypad
#define MAX_INPUT_SEQUENCE 18 // how many characters AT MOST can one button represent
// Process event results
typedef uint8_t appEventResult;
#define DO_NOTHING 0x00
#define REDRAW_SCREEN 0x01
#define REDRAW_HEADER 0x02
#define REDRAW_FOOTER 0x04
#define ENTER_DIAL_APP 0x08 // 0x88 - exit and enter DialApp (to be returned by ClockApp and MenuApp)
#define LOCK_UNLOCK 0x10 // one of two events: screen locked or screen unlocked
#define EXIT_APP 0x80
#define REDRAW_ALL (REDRAW_SCREEN | REDRAW_HEADER | REDRAW_FOOTER) // TODO: change these into more appropriate types
typedef enum {
OPENSANS_COND_BOLD_20 = 0,
AKROBAT_BOLD_16,
AKROBAT_BOLD_18,
AKROBAT_BOLD_20,
AKROBAT_BOLD_22,
AKROBAT_BOLD_24,
AKROBAT_SEMIBOLD_20,
AKROBAT_SEMIBOLD_22,
AKROBAT_EXTRABOLD_22,
AKROBAT_BOLD_32,
AKROBAT_BOLD_90,
} FontIndex_t;
typedef enum {
SIP = 0,
LORA
} MessageType_t;
enum class InputType { Numeric, AlphaNum, IPv4 };
enum class CallState {
NotInited = 0, // connection with proxy not established yet
Idle,
InvitingCallee, // INVITE needs to be sent
InvitedCallee, // UAC: INVITE(s) sent, waiting for any reply / UAS: 200 OK response sent, waiting for ACK
RemoteRinging, // callee's phone is ringing; TODO: produce beeps
Call, // audio session in progress
HangUp, // the user pressed HANG UP/REJECT button (assume that this event can be triggered from any other state) / the user has pressed REJECT button TODO
HangingUp, // waiting for confirmation of BYE/CANCEL request, resending
HungUp, // the call has ended, display CALL ENDED to user for some short period of time
// Callee states
BeingInvited, // notifying user of the incoming invite (the phone rings)
Accept, // the user has pressed ACCEPT button -> send 200 OK
Decline, // user declined incoming call
Busy,
Error,
};
struct QueuedEvent {
uint32_t msTriggerAt;
EventType event;
};
class ControlState {
public:
ControlState();
~ControlState();
// Keyboard input state
char inputCurKey; // current physical button active
InputType inputType;
uint8_t inputCurSel;
bool inputShift;
char inputSeq[MAX_INPUT_SEQUENCE+1];
int32_t msAppTimerEventPeriod;
uint32_t msAppTimerEventLast;
bool disableAutoTurnOnKeypadLights = false;
//NEW APP STATES
int myappstate = 0;
void setInputState(InputType newInputType);
// SIP account
char* fromNameDyn; // display name
char* fromUriDyn; // SIP URI
char* proxyPassDyn; // proxy password
char* global_UDP_TCP_SIP; //UDP-SIP or TCP-SIP selected
bool sipAccountChanged = false; // does the SIP proxy requires (re)connecting? (this is set to true whenever user changes preferred account)
bool sipEnabled = false; // is the phone set to connect to the SIP proxy? (used for the SIP icon)
bool sipRegistered = false; // is the phone registered at the SIP proxy? (used for the SIP icon)
bool loadSipAccount(); // load primary (preferred / default) SIP account from flash to RAM
void setSipAccount(const char* dispName, const char* uri, const char* passwd, const char* UDP_TCP_SIP_Selection); // use the supplied SIP account (store it in RAM)
void removeSipAccount(); // remove account from RAM (and don't reconnect in future)
bool isCallPossible() {
return this->sipRegistered && ! this->sipAccountChanged;
};
bool hasSipAccount() {
return fromUriDyn != NULL && *fromUriDyn;
};
// SIP/Call state
CallState sipState = CallState::NotInited;
char* calleeNameDyn;
char* calleeUriDyn;
char* lastReasonDyn;
void setRemoteNameUri(const char* dispName, const char* uri);
void setSipState(CallState state);
void setSipReason(const char* text);
// Ringtone & ringtone vibration
bool ringing = false;
bool vibroOn = false; // is vibration motor ON?
uint32_t vibroToggledMs = 0; // last time the vibration motor got toggled
uint16_t vibroOnPeriodMs = 500; // period of vibration for the vibration motor at a time (ON period)
uint16_t vibroOffPeriodMs = 2500; // amount of time the vibration motor rests (this should be longer than the ON period)
uint16_t vibroDelayMs = 3000; // time before the vibration motor starts vibrating for the first time (usually it's sum of ON and OFF periods)
uint16_t vibroNextDelayMs; // next time delay before toggling the vibration motor state
// Messages
bool unreadMessages = false; // are there any unread messages?
MessagesArray outgoingMessages; // Messages to be sent via TinySIP class
MessagesArray outgoingLoraMessages;
// RSSI
int16_t wifiRssi = 0;
// Battery & power
bool battUpdated;
float battVoltage;
float battSoc; // state of charge
bool battCharged;
bool usbConnected = false;
bool cardPresent = false;
bool battBlinkOn = false; // is the additional section of battery is ON? (while blinking the battery indicator)
// ICs inited or not?
bool psramInited = false;
bool gaugeInited = false;
bool codecInited = false;
bool scannerInited = false;
bool extenderInited = false;
bool booted = false;
// Keyboard lock
bool locking = true; // Is the keyboard locking is ON?
bool locked = false; // Is the keyboard currently locked?
uint8_t unlockButton1 = 0; // What first unlock button was pressed? (Determines what needs to be pressed next.)
// Screen dimming & sleep
bool screenWakeUp = false; // event to signify that screen needs to be completely redrawn before restoring brightness
uint8_t screenBrightness = 100; // current brightness of the screen; if 0 - screen is sleeping
bool dimming = true; // is dimming enabled?
uint8_t brightLevel = 100; // maximum screen brightness (active)
uint8_t dimLevel = 15; // minimum sceeen brightness (dim)
uint32_t dimAfterMs = 20000; // after what time after key press to start dimming the screen
bool sleeping = true; // is sleeping enabled?
uint32_t sleepAfterMs = 30000; // after what time after key press to turn off the screen (should be higher than dimAfterMs)
bool doDimming() {
return this->dimming && this->dimAfterMs > 0 && this->dimAfterMs <= 86400000;
}
bool doSleeping() {
return this->sleeping && this->sleepAfterMs > 0 && this->sleepAfterMs <= 86400000;
}
// Event queue
LinearArray<QueuedEvent, LA_INTERNAL_RAM> eventQueue;
bool scheduleEvent(EventType event, uint32_t msTriggerAt);
void unscheduleEvent(EventType event); // remove all events of this type
EventType popEvent(uint32_t msNow);
public:
static constexpr unsigned MAX_EVENTS = 128;
// User serial
RingBuffer<char> userSerialBuffer; // size initialized in the constructor
// LED app
// TODO: make a simple and nice API for controlling pins
bool ledPleaseTurnOn = false;
bool ledPleaseTurnOff = false;
protected:
void clearDynamicSip();
void clearDynamicCallee();
};
// # # # # # # # # # # # # # # # # # # # # # # # # # # # # MENUS # # # # # # # # # # # # # # # # # # # # # # # # # # # #
// Menu actions
typedef enum ActionID : uint16_t {
NO_ACTION = 0,
GUI_ACTION_MAINMENU,
GUI_ACTION_SUBMENU,
GUI_ACTION_RESTART,
// Specific applications
GUI_BASE_APP = 0x4000, // application flag
// My Section
GUI_APP_MYAPP, // add your app like this
GUI_APP_OTA,
GUI_APP_LEDEXAMPLEAPP,
GUI_APP_ButtonPressApp,
GUI_APP_BatteryApp,
GUI_APP_PeriTestApp,
//GUI_APP_ScreenshotApp, Not requested to be merged
GUI_APP_RealTimeApp,
GUI_APP_LoRaApp,
// Interface
GUI_APP_MENU,
GUI_APP_CLOCK,
GUI_APP_SPLASH,
// Calling
GUI_APP_CALL,
GUI_APP_DIALING,
GUI_APP_PHONEBOOK,
GUI_APP_SIP_ACCOUNTS,
// Messages
GUI_APP_MESSAGES,
GUI_APP_VIEW_MESSAGE,
GUI_APP_CREATE_MESSAGE,
// Tools
GUI_APP_NOTEPAD,
GUI_APP_UDP,
GUI_APP_MOTOR,
GUI_APP_LED_MIC,
GUI_APP_PARCEL,
GUI_APP_PIN_CONTROL,
GUI_APP_DIAGNOSTICS,
GUI_APP_RECORDER,
// Configs
GUI_APP_WIFISETTINGS,
GUI_APP_EDITWIFI,
GUI_APP_NETWORKS,
GUI_APP_AUDIO_CONFIG,
GUI_APP_WIFI_CONFIG,
GUI_APP_TIME_CONFIG,
GUI_APP_SCREEN_CONFIG,
GUI_APP_LORA_CONFIG,
// Test apps
GUI_APP_CIRCLES,
GUI_APP_WIDGETS,
GUI_APP_PICS_DEMO,
GUI_APP_FONT_DEMO,
GUI_APP_DESIGN_DEMO,
GUI_APP_MIC_TEST,
GUI_APP_DIGITAL_RAIN,
GUI_APP_UART_PASS,
// Games
GUI_APP_FIDE_CHESS,
GUI_APP_CHESS960,
GUI_APP_HILL_CHESS,
GUI_APP_ACKMAN,
} ActionID_t;
typedef struct GUIMenuItem {
const int16_t ID;
const int16_t parent;
const char* const title;
const char* const leftButton;
const char* const rightButton;
const ActionID_t action;
} GUIMenuItem;
typedef struct GUIMenuItemIcons {
const int16_t ID;
const unsigned char* icon1; // icon in regular (not selected) state
const uint16_t iconSize1;
const unsigned char* icon2; // icon in selected state
const uint16_t iconSize2;
} GUIMenuItemIcons;
// # # # # # # # # # # # # # # # # # # # # # # # # # # # # WIDGETS # # # # # # # # # # # # # # # # # # # # # # # # # # # #
// Widget object bearing no data about its position and size
class AbstractWidget {
public:
virtual bool processEvent(EventType event) = 0; // return true if the event was relevant (processed); false - if ignored
virtual void redraw(LCD &lcd, uint16_t screenOffX, uint16_t screenOffY, uint16_t windowWidth, uint16_t windowHeight) = 0;
};
// Base widget class with position and size data
class GUIWidget : public AbstractWidget {
protected:
uint16_t parentOffX; // position inside parent: screen or other widget (not supported yet)
uint16_t parentOffY;
uint16_t widgetWidth; // widget size
uint16_t widgetHeight;
bool updated = true; // TODO: is this used? remove?
public:
GUIWidget(uint16_t posX, uint16_t posY, uint16_t width, uint16_t height) : parentOffX(posX), parentOffY(posY), widgetWidth(width), widgetHeight(height) {}
virtual ~GUIWidget() {}
// this widget has no parent -> draw directly on screen (visibility window exactly matches dimensions)
void redraw(LCD &lcd) {
redraw(lcd, parentOffX, parentOffY, widgetWidth, widgetHeight);
};
virtual void redraw(LCD &lcd, uint16_t screenOffX, uint16_t screenOffY, uint16_t windowWidth, uint16_t windowHeight) = 0;
void refresh(LCD &lcd, bool redrawAll) {
refresh(lcd, redrawAll, parentOffX, parentOffY, widgetWidth, widgetHeight);
}
void refresh(LCD &lcd, bool redrawAll, uint16_t screenOffX, uint16_t screenOffY, uint16_t windowWidth, uint16_t windowHeight) {
if (this->updated || redrawAll) {
redraw(lcd, parentOffX, parentOffY, widgetWidth, widgetHeight);
this->updated = false;
}
}
bool isUpdated() {
return this->updated;
}
virtual bool focusable() = 0;
virtual void setFocus(bool focus) = 0;
virtual bool getFocus() = 0;
// Methods to get position (inside parent) and size
void getPositionSize(uint16_t &px, uint16_t &py, uint16_t &ww, uint16_t &wh) {
px = parentOffX;
py = parentOffY;
ww = widgetWidth;
wh = widgetHeight;
}
inline uint16_t getParentOffX() {
return parentOffX;
}
inline uint16_t getParentOffY() {
return parentOffY;
}
inline uint16_t width() {
return widgetWidth;
}
inline uint16_t height() {
return widgetHeight;
}
// A helper function to handle the extra (321st or 241st) pixel on the screen when drawing rectangles
static void corrRect(LCD &lcd, uint16_t screenOffX, uint16_t screenOffY, uint16_t windowWidth, uint16_t windowHeight, uint16_t color);
protected:
void clear(LCD &lcd, uint16_t col=BLACK); // useful for updated labels
};
class FocusableWidget : public GUIWidget {
public:
FocusableWidget(uint16_t posX, uint16_t posY, uint16_t width, uint16_t height) : GUIWidget(posX, posY, width, height) {
this->focused = false;
this->active = true;
}
bool focusable() {
return true;
}
void setFocus(bool focus) {
this->focused = focus;
this->updated = true;
}
bool getFocus() {
return this->focused;
}
bool getActive() {
return this->active;
}
void activate() {
this->active = true;
}
void deactivate() {
this->active = false;
}
protected:
bool focused;
bool active; // can focus be set on this widget at this time?
};
class NonFocusableWidget : public GUIWidget {
public:
NonFocusableWidget(uint16_t posX, uint16_t posY, uint16_t width, uint16_t height) : GUIWidget(posX, posY, width, height) {}
bool focusable() {
return false;
}
void setFocus(bool focus) {}
bool getFocus() {
return false;
}
};
class RectWidget : public NonFocusableWidget {
public:
RectWidget(uint16_t posX, uint16_t posY, uint16_t width, uint16_t height, uint16_t color);
/* virtuals from GUIWidget */
bool processEvent(EventType event) {
return false;
}
void redraw(LCD &lcd, uint16_t screenOffX, uint16_t screenOffY, uint16_t windowWidth, uint16_t windowHeight);
protected:
uint16_t color;
};
// Rectangle with an icon in the middle
class RectIconWidget : public RectWidget {
public:
RectIconWidget(uint16_t posX, uint16_t posY, uint16_t width, uint16_t height, uint16_t color, const uint8_t* iconData, const size_t iconSize);
virtual ~RectIconWidget();
/* virtuals from GUIWidget */
bool processEvent(EventType event) {
return false;
}
void redraw(LCD &lcd, uint16_t screenOffX, uint16_t screenOffY, uint16_t windowWidth, uint16_t windowHeight);
protected:
IconRle3* icon = NULL;
};
// Horizontal line to separate different groups of widgets visually (could be called LineWidget)
class RulerWidget : public NonFocusableWidget {
public:
RulerWidget(uint16_t posX, uint16_t posY, uint16_t width, uint16_t color=GRAY_75);
/* virtuals from GUIWidget */
bool processEvent(EventType event) {
return false;
}
void redraw(LCD &lcd, uint16_t screenOffX, uint16_t screenOffY, uint16_t windowWidth, uint16_t windowHeight);
protected:
uint16_t color;
};
class LabelWidget : public NonFocusableWidget {
public:
// Text is centered vertically. These are the settings for horizontal alignment.
typedef enum TextDirection {
LEFT_TO_RIGHT = 0,
RIGHT_TO_LEFT,
CENTER,
} TextDirection_t;
LabelWidget(uint16_t posX, uint16_t posY, uint16_t width, uint16_t height,
const char* p,
uint16_t col=WHITE, uint16_t bg=BLACK, SmoothFont* font=NULL, TextDirection_t orient=LEFT_TO_RIGHT, uint16_t xPadding=0);
virtual ~LabelWidget();
/* GUIWidget virtuals */
bool processEvent(EventType event) {
return false;
}
void redraw(LCD &lcd, uint16_t screenOffX, uint16_t screenOffY, uint16_t windowWidth, uint16_t windowHeight);
void setText(const char* p);
void setColors(colorType textColor, colorType bgColor);
protected:
SmoothFont* widgetFont;
colorType textColor;
colorType bgColor;
uint8_t textDirection;
uint16_t xPadding;
char* textDyn;
};
class ButtonWidget : public FocusableWidget {
public:
ButtonWidget(uint16_t posX, uint16_t posY, const char* title,
uint16_t width = 0, uint16_t height = 30, // if width is zero, the widget width will be inferred from text width
colorType col=WP_COLOR_0, colorType bgCol=WP_ACCENT_0, colorType border=GRAY_95, colorType sel=WP_COLOR_1, colorType selBg=WP_COLOR_0);
virtual ~ButtonWidget();
void setText(const char* str);
void setColors(colorType fg, colorType bg, colorType border=GRAY_95, colorType sel=WP_COLOR_1, colorType selBg=WP_COLOR_0);
bool processEvent(EventType event);
void redraw(LCD &lcd, uint16_t screenOffX, uint16_t screenOffY, uint16_t windowWidth, uint16_t windowHeight);
bool readPressed();
static int32_t textWidth(const char* str);
void setWidth(uint16_t w) {
widgetWidth = w;
}
protected:
const char* titleDyn = NULL;
uint8_t fontSize;
bool pressed;
// Colors
colorType textColor;
colorType bgColor;
colorType borderColor;
colorType selTextColor;
colorType selBgColor;
};
class SliderWidget : public FocusableWidget {
public:
SliderWidget(uint16_t posX, uint16_t posY,
uint16_t width, uint16_t height,
colorType color=WP_ACCENT_0, colorType selectedColor=WP_ACCENT_S, colorType bgColor=WP_COLOR_1, colorType textColor=WP_COLOR_0);
protected:
static const uint16_t dotRadius = 6;
static const uint16_t lineHeight = 2;
void drawSlider(LCD &lcd, uint16_t screenOffX, uint16_t screenOffY, uint16_t windowWidth, uint16_t windowHeight, colorType color, float pos);
colorType mainColor;
colorType selectedColor;
colorType bgColor;
colorType textColor;
};
class IntegerSliderWidget : public SliderWidget {
public:
IntegerSliderWidget(uint16_t posX, uint16_t posY,
uint16_t width, uint16_t height,
int minValue, int maxValue, int step, bool showText, const char* unit = NULL,
colorType color=WP_ACCENT_0, colorType selectedColor=WP_ACCENT_S, colorType bgColor=WP_COLOR_1, colorType textColor=WP_COLOR_0);
virtual ~IntegerSliderWidget();
bool processEvent(EventType event);
void redraw(LCD &lcd, uint16_t screenOffX, uint16_t screenOffY, uint16_t windowWidth, uint16_t windowHeight);
void setValue(int value);
int getValue() {
return val;
};
protected:
const uint16_t smoothFont = AKROBAT_BOLD_18;
const char* unit = NULL;
int minVal;
int maxVal;
int val;
int step;
uint16_t maxTextWidth = 0; // if zero - don't show text
};
//class ScreenWidget : public NonFocusableWidget {
// public:
// RectWidget(uint16_t posX, uint16_t posY, uint16_t width, uint16_t height, uint16_t col) : NonFocusableWidget(posX, posY, width, height) {
// bg = col;
// };
// bool processEvent(EventType event);
// void redraw(LCD &lcd, uint16_t screenOffX, uint16_t screenOffY, uint16_t windowWidth, uint16_t windowHeight);
// void append(GUIWidget* w);
// protected:
// GUIWidget** widgets;
// uint16_t bg;
//};
class HeaderWidget : public NonFocusableWidget {
public:
HeaderWidget(const char* s, ControlState& state)
: NonFocusableWidget(0, 0, TFT_WIDTH, THEME_HEADER_SIZE), title(s), controlState(state) {
}; // TODO: remove TFT_WIDTH?
void setTitle(const char* s) {
title = s;
}
bool processEvent(EventType event) {
return false;
}
void redraw(LCD &lcd, uint16_t screenOffX, uint16_t screenOffY, uint16_t windowWidth, uint16_t windowHeight);
protected:
const char* title;
ControlState& controlState;
};
class FooterWidget : public NonFocusableWidget {
public:
FooterWidget(const char* left, const char* right, ControlState& state) :
NonFocusableWidget(0, TFT_HEIGHT - THEME_FOOTER_SIZE, TFT_WIDTH, THEME_FOOTER_SIZE), // TODO: remove TFT_HEIGHT and TFT_WIDTH?
leftButtonName(left), rightButtonName(right), controlState(state) {};
void setButtons(const char* left, const char* right) {
leftButtonName = left;
rightButtonName = right;
};
const char* getLeftButton() {
return leftButtonName;
}
bool processEvent(EventType event) {
return false;
}
void redraw(LCD &lcd, uint16_t screenOffX, uint16_t screenOffY, uint16_t windowWidth, uint16_t windowHeight);
protected:
const char* leftButtonName;
const char* rightButtonName;
ControlState& controlState;
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - Choice widget logic - - - - - - - - - - - - - - - - - - - - - - - - - - - -
class ChoiceWidget : public FocusableWidget {
public:
ChoiceWidget(uint16_t posX, uint16_t posY, uint16_t width, uint16_t height, SmoothFont* font=NULL,
colorType textColor=WP_COLOR_0, colorType bgColor=WP_COLOR_1, colorType regColor=WP_ACCENT_1, colorType selectedColor=WP_ACCENT_S);
~ChoiceWidget();
bool processEvent(EventType event);
void redraw(LCD &lcd, uint16_t screenOffX, uint16_t screenOffY, uint16_t windowWidth, uint16_t windowHeight);
void addChoice(const char* name);
void setFocus(bool focus) {
this->focused = focus;
this->updated = true;
}
typedef uint16_t ChoiceValue; // type of indexes of the choices
void setValue(ChoiceValue val);
ChoiceValue getValue() {
return this->curChoice;
}
protected:
ChoiceValue curChoice; // index of the current choice
LinearArray<char*, LA_EXTERNAL_RAM> choices;
// Appearance
const uint8_t arrowWidth = 6;
const uint8_t arrowPad = 5;
SmoothFont* widgetFont;
colorType textColor; // color of text when not selected
colorType bgColor; // background color
colorType regColor; // color of arrows when not selected
colorType selColor; // color of arrows and text when selected
};
class YesNoWidget : public ChoiceWidget {
public:
YesNoWidget(uint16_t posX, uint16_t posY, uint16_t width, uint16_t height, SmoothFont* font=NULL,
colorType textColor=WP_COLOR_0, colorType bgColor=WP_COLOR_1, colorType regColor=WP_ACCENT_1, colorType selectedColor=WP_ACCENT_S);
void setValue(bool val) {
((ChoiceWidget*)this)->setValue((ChoiceValue) val);
}
bool getValue() {
return (bool)this->curChoice;
}
};
class MessageBox : public FocusableWidget {
public:
MessageBox(uint16_t xPos, uint16_t yPos, uint16_t width, uint16_t height,
const char* message, const char** buttons, size_t buttons_len);
const char* show();
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - Text input logic - - - - - - - - - - - - - - - - - - - - - - - - - - - -
class TextInputAbstract : public FocusableWidget {
public:
TextInputAbstract(uint16_t xPos, uint16_t yPos, uint16_t width, uint16_t height,
ControlState& state,
SmoothFont* font=NULL, uint32_t maxInputSize=64000, InputType typ=InputType::Numeric);
virtual void setText(const char* str) = 0;
virtual const char* getText() = 0;
void setFocus(bool focus);
void setColors(colorType fg, colorType bg);
protected:
uint32_t maxInputSize;
// Appearance
SmoothFont* widgetFont;
colorType fgColor = WP_COLOR_0;
colorType bgColor = WP_DISAB_1;
// Input type control
ControlState& controlState;
InputType inputType;
void drawCursor(LCD &lcd, uint16_t posX, uint16_t posY, uint16_t charHeight, uint16_t color); // short vertical line
};
// Text input built around a single linear string of text
class TextInputBase : public TextInputAbstract {
public:
TextInputBase(uint16_t xPos, uint16_t yPos, uint16_t width, uint16_t height,
ControlState& state,
SmoothFont* font=NULL, uint32_t maxInputSize=100, InputType typ=InputType::Numeric);
virtual ~TextInputBase();
bool getInt(int32_t &i);
void setInt(int32_t i);
/* TextInputAbstract virtuals */
virtual void setText(const char* str);
virtual const char* getText() {
return inputStringDyn != NULL ? inputStringDyn : "";
};
/* Own virtuals */
virtual bool insertCharacter(char c) = 0;
virtual bool allocateMore(uint32_t minSize=0);
protected:
// Text string
char* inputStringDyn;
uint32_t inputStringSize;
// Text & cursor offsets
uint32_t textOffset; // inputStringDyn[textOffset] - first visible character
uint32_t cursorOffset; // inputStringDyn[cursorOffset] - character next after cursor
};
// Widget similar to HTML <textarea>, can be use to display and input text
class MultilineTextWidget : public TextInputAbstract {
public:
MultilineTextWidget(uint16_t xPos, uint16_t yPos, uint16_t width, uint16_t height,
const char* emptyText, ControlState& state,
uint32_t maxInputSize, SmoothFont* font=NULL, InputType typ=InputType::AlphaNum, uint16_t xPadding=0, uint16_t yPadding=0);
virtual ~MultilineTextWidget();
int getCursorRow() {
return this->cursRow;
};
void verticalCentering(bool p) {
centering = p;
};
void cursorToStart();
/* TextInputAbstract virtuals */
void setText(const char* str);
const char* getText();
void appendText(const char* str);
/* GUIWidget virtuals */
void redraw(LCD &lcd, uint16_t screenOffX, uint16_t screenOffY, uint16_t windowWidth, uint16_t windowHeight);
bool processEvent(EventType event);
protected:
char** rowsDyn; // lines of text TODO: maybe use LinearArray?
int maxRows; // maximum number of rows (by allocated size of the rowsDyn array)
char* retTextDyn; // resulting full text (for getText)
char* emptyTextDyn = NULL; // text to be shown if the widget is empty
uint16_t visibleRows; // number of visible rows per widget
int firstVisibleRow;
int cursRow; // row where the cursor is
uint16_t cursOffset; // cursor offset inside a row
uint16_t xPadding; // appearance
uint16_t yPadding;
bool centering = false;
bool allocateMore(int minSize=0);
void revealCursor();
bool emptyRow(int row) {
return row < 0 || row >= maxRows || !rowsDyn[row] || !rowsDyn[row][0];
};
bool notEmptyRow(int row) {
return !emptyRow(row);
};
bool newLineRow(int row) {
return notEmptyRow(row) && rowsDyn[row][strlen(rowsDyn[row])-1]=='\n';
};
};
// Widget similar to HTML <input>
class TextInputWidget : public TextInputBase {
public:
TextInputWidget(uint16_t xPos, uint16_t yPos, uint16_t width, uint16_t height,
ControlState& state,
uint32_t maxInputSize, SmoothFont* font=NULL, InputType typ=InputType::AlphaNum, uint16_t sidePadding=0);
/* TextInputBase virtuals */
bool insertCharacter(char c);
/* GUIWidget virtuals */
bool processEvent(EventType event);
void redraw(LCD &lcd, uint16_t screenOffX, uint16_t screenOffY, uint16_t windowWidth, uint16_t windowHeight);
protected:
uint16_t xPad; // side padding
void revealCursor();
void shiftCursor(int16_t shift);
};
// Widget similar to HTML <input type="password">
class PasswordInputWidget : public TextInputBase {
public:
PasswordInputWidget(uint16_t xPos, uint16_t yPos, uint16_t width, uint16_t height,
ControlState& state,
uint32_t maxInputSize, SmoothFont* font=NULL, InputType typ=InputType::AlphaNum, uint16_t sidePadding=0);
~PasswordInputWidget();
/* TextInputBase virtuals */
bool insertCharacter(char c);
void setText(const char* str);
bool allocateMore(uint32_t minSize=0);
/* GUIWidget virtuals */
bool processEvent(EventType event);
void redraw(LCD &lcd, uint16_t screenOffX, uint16_t screenOffY, uint16_t windowWidth, uint16_t windowHeight);
protected:
uint16_t xPad; // side padding
char* outputStringDyn;
void revealCursor();
void shiftCursor(int16_t shift);
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - MenuWidget logic - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Menu option with text only. Also a base class for menu options.
class MenuOption {
public:
typedef uint32_t keyType;
MenuOption::keyType id; // MUST be positive
uint16_t style; // this refers to one of two styles in MenuWidget TODO: can make 8-bit
char* titleDyn;
MenuOption();
MenuOption(MenuOption::keyType pId, uint16_t pStyle, const char* title);
virtual ~MenuOption();
virtual void redraw(LCD &lcd, uint16_t screenOffX, uint16_t screenOffY, uint16_t windowWidth, uint16_t windowHeight,
colorType fgColor, colorType bgColor, bool opaque, bool selected, SmoothFont* font, uint16_t leftOffset);
};
// Menu option with icons and subtitles (used for Main Menu, main menu of the message app; allows transparency)
class MenuOptionIconned : public MenuOption {
public:
static const colorType IGNORED_COLOR = 0x0001;
colorType selectedBgColor = IGNORED_COLOR;
char* subTitleDyn;
uint8_t textLeftOffset;
IconRle3* icon = NULL;
IconRle3* iconSelected = NULL;
MenuOptionIconned(MenuOption::keyType pId, uint16_t pStyle, const char* title, const char* subTitle=NULL,
const unsigned char* iconData=NULL, const uint16_t iconSize=0,
const unsigned char* selIconData=NULL, const uint16_t selIconSize=0,