-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessenger.cpp
More file actions
1079 lines (899 loc) · 30.2 KB
/
Messenger.cpp
File metadata and controls
1079 lines (899 loc) · 30.2 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 "Messenger.h"
/**
* @brief Construct a new Messenger:: Messenger object
*
* @param screen Pointer to the LCD
* @param ts Pointer to the touchscreen
* @param keys pointer to the virtual keys
*/
Messenger::Messenger(Elegoo_TFTLCD *screen, TouchScreen *ts, VKeys *keys, Radio *r) {
_screen = screen;
_ts = ts;
_keys = keys;
_radio = r;
// Set defaults
_background = BLACK;
_textColor = WHITE;
_boxColor = WHITE;
_keyColor = WHITE;
_textSize = TEXTMEDIUM;
_menuBorderOffset = 10;
_minTouch = 3;
// Set Defaults on the keys
_keys->setKeyColor(_keyColor, _background);
_keys->setTextColor(BLACK, _background);
}
/**
* @brief
* Initializes the Messenger object and starts the infinite loop
*
*/
void Messenger::init(void) {
int selection = MAINMENU;
String message = ""; // Message to send
Serial.println("Screen Width: " + String(_screen->width()) + ", Screen Height: " + String(_screen->height()));
while (true) {
// Pinmodes because TS and Screen share these pins
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
_screen->fillScreen(_background);
// Start the corresponding menu
switch (selection) {
case MAINMENU: selection = mainMenu(); break;
case WRITEMESSAGE: message = writeMessage(); selection = MAINMENU; break;
case OPTS: optsMenu(); selection = MAINMENU; break;
case READ: readMenu(); selection = MAINMENU; break;
default: selection = mainMenu();
}
if (message != "") {
// TODO get real message here and send it, this stuff is just for testing!
_radio->sendMessage(message, _commType);
message = "";
}
}
}
/**
* @deprecated
* @brief
*
*/
void Messenger::reset(void) {
_screen->fillScreen(WHITE);
init();
}
/************************ PRIVATE ***************************/
/************************ PRIVATE MENU FUNCTIONS ***************************/
/**
* All menu functions work the exact same, therefore not all of them are commented. This comment
* shows the function of menu methods
*
* 1. Configure the menu with a menu struct
* 1.1 menuStart: Y-Value where the menu starts on display
* 1.2 menuThickness: How thick are the menu entries
* 1.3 menuOffset: how far are entries apart
* 1.4 header: Header text of the menu
* 1.5 extraText: smaller text below menu header
* 1.6 entries[5]: Text shown in the menu entries
* 2. Draw the menu
* 3. Infinite loop
* 3.1 Check, if messages are available on the air
* 3.2 Get touchpoint
* 3.3 analyse touchpoint
* 3.4 Call method or return value according to selection and break
*/
/**
* @brief
* Shows the main menu and handles the input
*
* @return int Selection in menu
*/
int Messenger::mainMenu(void) {
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
// Build menu defintion
Menu menu;
menu.menuStart = 100;
menu.menuThickness = 40;
menu.menuOffset = 10;
menu.header = "Messenger";
menu.extraText = checkCache();
menu.entries[0] = String("Schreiben");
menu.entries[1] = String("Lesen");
menu.entries[2] = String("Optionen");
menu.entries[3] = String("Verfuegbar");
menu.entries[4] = String("\0");
// Draw menu
drawMenu(menu);
while (true) {
String inc = receiveMessage();
if (inc != "\0") {
Serial.println(inc);
return -42;
}
int selection = -1;
// Get touchpoint
digitalWrite(13, HIGH);
TSPoint p = _ts->getPoint();
digitalWrite(13, LOW);
// If touch was recognized
if (p.z > _minTouch) {
// Get selected menu poin
selection = getSelection(menu.menuStart, menu.menuThickness, menu.menuOffset, 4, parseCoords(p));;
Serial.println("Selection: " + String(selection));
// Return the seection
switch (selection) {
case -1: break;
case 1: return WRITEMESSAGE;
case 2: return READ;
case 3: return OPTS;
case 4: checkNearby(); return MAINMENU;
default: break;
}
}
delay(10);
}
Serial.println("Returning from mainMenu");
}
void Messenger::checkNearby(void) {
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
_screen->fillScreen(_background);
_screen->setTextColor(_textColor);
_screen->setTextSize(_textSize);
_screen->setCursor(0,0);
_screen->print("Ueberpruefe\n...");
String availableMsg = "Es sind\n";
bool devicesAvailable = _radio->checkNearbyDevices() || _radio->getAck();
availableMsg += (!devicesAvailable ? String("keine\n") : String(""));
availableMsg += "Geraete\nverfuegbar.";
_screen->setTextColor(_textColor);
_screen->setTextSize(_textSize);
_screen->setCursor(0,120);
_screen->print(availableMsg);
delay(3000);
}
/**
* @brief
* Options menu
*
*/
void Messenger::optsMenu(void) {
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
Menu menu;
menu.menuStart = 60;
menu.menuThickness = 40;
menu.menuOffset = 20;
menu.header = "Optionen";
menu.entries[0] = String("Farben");
menu.entries[1] = String("Tastatur");
menu.entries[2] = String("Distanz");
menu.entries[3] = String("Zurueck");
menu.entries[4] = String("\0");
drawMenu(menu);
delay(100);
while (true) {
String inc = receiveMessage();
if (inc != "\0") Serial.println(inc);
int selection = -1;
// Get touchpoint
digitalWrite(13, HIGH);
TSPoint p = _ts->getPoint();
digitalWrite(13, LOW);
// If touch was recognized
if (p.z > _minTouch) {
// Get selected menu poin
selection = getSelection(menu.menuStart, menu.menuThickness, menu.menuOffset, 4, parseCoords(p));
Serial.println("Selection: " + String(selection));
switch (selection) {
case -1: break;
case 1: colorMenu(); return;
case 2: keysMenu(); return;
case 3: distanceMenu(); return;
case 4: return;
default: break;
}
}
delay(10);
}
Serial.println("Returning from optsMenu");
}
void Messenger::distanceMenu(void) {
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
Menu menu;
menu.menuStart = 80;
menu.menuThickness = 35;
menu.menuOffset = 10;
menu.header = "Distanz";
menu.entries[0] = String("Sehr nah");
menu.entries[1] = String("Nah");
menu.entries[2] = String("Weit");
menu.entries[3] = String("Sehr weit");
menu.entries[4] = String("Zurueck");
menu.entries[5] = String("\0");
drawMenu(menu);
delay(100);
while (true) {
String inc = receiveMessage();
if (inc != "\0") Serial.println(inc);
int selection = -1;
// Get touchpoint
digitalWrite(13, HIGH);
TSPoint p = _ts->getPoint();
digitalWrite(13, LOW);
// If touch was recognized
if (p.z > _minTouch) {
// Get selected menu poin
selection = getSelection(menu.menuStart, menu.menuThickness, menu.menuOffset, 5, parseCoords(p));
Serial.println("Selection: " + String(selection));
switch (selection) {
case -1: break;
case 1: _radio->setPALevel("MIN"); return;
case 2: _radio->setPALevel("LOW"); return;
case 3: _radio->setPALevel("HIGH"); return;
case 4: _radio->setPALevel("MAX"); return;
case 5: return;
default: break;
}
}
delay(10);
}
Serial.println("Returning from Distance");
}
/**
* @brief
* Submenu for all things keyboard related
*
*/
void Messenger::keysMenu(void) {
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
Menu menu;
menu.menuStart = 60;
menu.menuThickness = 40;
menu.menuOffset = 20;
menu.header = "Tastatur";
menu.entries[0] = String("Tastenfarbe");
menu.entries[1] = String("Buchs.-Far.");
menu.entries[2] = String("Stil");
menu.entries[3] = String("Zurueck");
menu.entries[4] = String("\0");
drawMenu(menu);
delay(100);
while (true) {
String inc = receiveMessage();
if (inc != "\0") Serial.println(inc);
int selection = -1;
// Get touchpoint
digitalWrite(13, HIGH);
TSPoint p = _ts->getPoint();
digitalWrite(13, LOW);
// If touch was recognized
if (p.z > _minTouch) {
// Get selected menu poin
selection = getSelection(menu.menuStart, menu.menuThickness, menu.menuOffset, 4, parseCoords(p));
Serial.println("Selection: " + String(selection));
ColorChooser cc(_screen, _ts, _background, _boxColor, _textColor);
switch (selection) {
case -1: break;
case 1: _keys->setKeyColor(cc.choose(), _background); return;
case 2: _keys->setTextColor(cc.choose(), _background); break;
case 3: _keys->setStyle(keyStyleMenu());
case 4: return;
default: break;
}
}
delay(10);
}
Serial.println("Returning from optsMenu");
}
/**
* @brief
* Menu for selecting layout of keys
*
* @return String Layout of the keys
*/
String Messenger::keyStyleMenu(void) {
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
Menu menu;
menu.menuStart = 60;
menu.menuThickness = 40;
menu.menuOffset = 20;
menu.header = "Layout";
menu.entries[0] = String("QWERTZ");
menu.entries[1] = String("QWERTY");
menu.entries[2] = String("ABCDE");
menu.entries[3] = String("Zurueck");
menu.entries[4] = String("\0");
drawMenu(menu);
delay(100);
while (true) {
String inc = receiveMessage();
if (inc != "\0") Serial.println(inc);
int selection = -1;
// Get touchpoint
digitalWrite(13, HIGH);
TSPoint p = _ts->getPoint();
digitalWrite(13, LOW);
// If touch was recognized
if (p.z > _minTouch) {
// Get selected menu poin
selection = getSelection(menu.menuStart, menu.menuThickness, menu.menuOffset, 4, parseCoords(p));
Serial.println("Selection: " + String(selection));
switch (selection) {
case -1: break;
case 1: return String("QWERTZ");
case 2: return String("QWERTY");
case 3: return String("ABCDE");
case 4: return String("\0");
default: break;
}
}
delay(10);
}
Serial.println("Returning from optsMenu");
}
/**
* @brief
* Submenu for different color menus
*
*/
void Messenger::colorMenu(void) {
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
Menu menu;
menu.menuStart = 60;
menu.menuThickness = 40;
menu.menuOffset = 20;
menu.header = "Farben";
menu.entries[0] = String("Hintergrund");
menu.entries[1] = String("Text");
menu.entries[2] = String("Boxen");
menu.entries[3] = String("Zurueck");
menu.entries[4] = String("\0");
drawMenu(menu);
delay(100);
while (true) {
String inc = receiveMessage();
if (inc != "\0") Serial.println(inc);
int selection = -1;
// Get touchpoint
digitalWrite(13, HIGH);
TSPoint p = _ts->getPoint();
digitalWrite(13, LOW);
// If touch was recognized
if (p.z > _minTouch) {
// Get selected menu poin
selection = getSelection(menu.menuStart, menu.menuThickness, menu.menuOffset, 4, parseCoords(p));
Serial.println("Selection: " + String(selection));
ColorChooser cc(_screen, _ts, _background, _boxColor, _textColor);
switch (selection) {
case -1: break;
case 1: setBackground(backGroundColorMenu()); return;
case 2: setTextColor(cc.choose()); return;
case 3: setBoxColor(cc.choose()); return;
case 4: return;
default: break;
}
}
delay(10);
}
Serial.println("Returning from colorMenu");
}
/**
* @brief
* Menu to set either light or dark mode
*
* @return uint16_t WHITE or BLACK
*/
uint16_t Messenger::backGroundColorMenu(void) {
Menu menu;
menu.menuStart = 60;
menu.menuThickness = 70;
menu.menuOffset = 20;
menu.header = "Hintergrund";
menu.entries[0] = String("Dark");
menu.entries[1] = String("Light");
menu.entries[2] = String("Zurueck");
menu.entries[3] = String("\0");
menu.entries[4] = String("\0");
drawMenu(menu);
delay(100);
while (true) {
String inc = receiveMessage();
if (inc != "\0") Serial.println(inc);
int selection = -1;
// Get touchpoint
digitalWrite(13, HIGH);
TSPoint p = _ts->getPoint();
digitalWrite(13, LOW);
// If touch was recognized
if (p.z > _minTouch) {
// Get selected menu poin
selection = getSelection(menu.menuStart, menu.menuThickness, menu.menuOffset, 3, parseCoords(p));
Serial.println("Selection: " + String(selection));
switch (selection) {
case -1: break;
case 1: return BLACK;
case 2: return WHITE;
case 3: return 0;
default: break;
}
}
delay(10);
}
Serial.println("Returning from bgMenu");
}
/************************ PRIVATE MENU DRAW ***************************/
/**
* @brief
* Draws the menu from definition passed in argument
*
* @param menu Definition of the menu
*/
void Messenger::drawMenu(Menu menu) {
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
_screen->fillScreen(_background);
// Print header
_screen->setTextColor(RED);
_screen->setTextSize(_textSize);
_screen->setCursor(20,10);
_screen->print(menu.header);
_screen->setTextColor(_textColor);
// Print menu boxes and texts
for (int i = 0; i < 5; i++) {
if (menu.entries[i] == "\0") continue;
_screen->drawRect(_menuBorderOffset,
(int16_t) menu.menuStart + i*(menu.menuThickness + menu.menuOffset),
_screen->width() - 2*_menuBorderOffset,
(int16_t) menu.menuThickness,
_boxColor
);
_screen->setCursor(20,menu.menuStart + i*(menu.menuThickness + menu.menuOffset) + ((int) (menu.menuThickness / _textSize)));
_screen->print(menu.entries[i]);
}
// Print extra text, if it exists
if (menu.extraText != "") {
int y = (10 + menu.menuStart) / 2;
_screen->setTextSize(_textSize - 1);
_screen->setTextColor(_textColor);
_screen->setCursor(20,y);
_screen->print(menu.extraText);
}
}
/****************************** PRIVATE READ MENU STUFF ****************************/
/**
* @brief
* Menu for reading received messages
*/
void Messenger::readMenu(void) {
// Couunter for which message screen is on
int currentMessage = 0;
// Set pinmodes
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
// Draw the menu
drawReadMenu();
// Set text config
_screen->setCursor(0,10);
_screen->setTextSize(_textSize);
_screen->setTextColor(_textColor);
// If there are no new messages, return
if (checkCache() == "") {
_screen->print("Keine neuen\nNachrichten");
delay(3000);
return;
}
// Print first message
_screen->print(_messages[currentMessage]);
while (true) {
String inc = receiveMessage();
if (inc != "\0") Serial.println(inc);
int selection = -1;
// Get touchpoint
digitalWrite(13, HIGH);
TSPoint p = _ts->getPoint();
digitalWrite(13, LOW);
if (p.z > _minTouch) {
// Get selected menu poin
// selection = getSelection(menu.menuStart, menu.menuThickness, menu.menuOffset, 3, parseCoords(p));
selection = readMenuSelection(parseCoords(p));
Serial.println("Selection: " + String(selection));
switch (selection) {
case -1: break;
case 1: switchMessageToRead(¤tMessage, false, false); break; // Message back
case 2: switchMessageToRead(¤tMessage, true, false); break; // Message forward
case 3: deleteMessage(currentMessage); switchMessageToRead(¤tMessage, false, true); break; // Delete current
case 4: return;
default: break;
}
delay(10);
}
continue;
}
}
/**
* @brief
* Method for drawing the read menu on screen
*/
void Messenger::drawReadMenu(void) {
int msgNo = 0;
for (int i = 0; i < 3; i++) {
if (_messages[i] != String("\0")) msgNo++;
}
// Set pinmodes
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
// Fill screen and set text config
_screen->fillScreen(_background);
_screen->setTextSize(_textSize -1);
_screen->setTextColor(_textColor);
// Draw function buttons
_screen->drawRect(_menuBorderOffset, 180, 100, 50, _boxColor);
_screen->drawRect(_screen->width() - _menuBorderOffset - 100, 180, 100, 50, _boxColor);
_screen->drawRect(_menuBorderOffset, 240, 100, 50, _boxColor);
_screen->drawRect(_screen->width() - _menuBorderOffset - 100, 240, 100, 50, _boxColor);
// Fill function buttons
_screen->setCursor(_menuBorderOffset, 160);
if (msgNo > 0) _screen->print(String(msgNo) + " Nachr.; Nr. 1");
_screen->setCursor(_menuBorderOffset + 40, 195);
_screen->print("<");
_screen->setCursor(_screen->width() - _menuBorderOffset - 100 + 48, 195);
_screen->print(">");
_screen->setCursor(_menuBorderOffset + 10, 255);
_screen->print("DEL");
_screen->setCursor(_menuBorderOffset + 140, 255);
_screen->print("BACK");
}
/**
* @brief
* Writes new message to display, after it has been switched
*
* @param msgCounter Pointer to the current messagenumber
* @param plus Message +1 or -1?
* @param afterDelete Triggered after a message has been deleted?
*/
void Messenger::switchMessageToRead(int *msgCounter, bool plus, bool afterDelete) {
// Set pinmodes
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
// Overprint old message and set text configuration
_screen->fillRect(0,0,_screen->width(), 175, _background);
_screen->setCursor(0,0);
_screen->setTextColor(_textColor);
_screen->setTextSize(_textSize);
// Set tempCounter for later comparison
int tempCounter = *msgCounter;
int noMessages = 0;
// Check, how many empty messages are in cache
for (int i = 0; i < 3; i++) {
if (String(_messages[i]) == String("\0")) noMessages++;
}
// If message cache is empty, set msgCounter back to 0 and print, that there are no messages
if (noMessages == 3) {
*msgCounter = 0;
_screen->print("Keine neuen\nNachrichten");
return;
}
// Modify tempcounter
plus ? tempCounter++ : tempCounter--;
if (tempCounter > 2) {
tempCounter = 2;
} else if (tempCounter < 0) {
tempCounter = 0;
}
Serial.println("Tempcounter: " + String(tempCounter));
// If method was called after deletion, do it a little differently
if (afterDelete) {
for (int i = 0; i < 3; i++) {
if (String(_messages[i]) == String("\0")) {
tempCounter = i-1;
if (tempCounter > 2 || tempCounter < 0) tempCounter = 0;
break;
}
}
}
Serial.println("Tempcounter after deletecheck: " + String(tempCounter));
// Handle tempcounter being outside of array indices
if (tempCounter > 2 || tempCounter < 0) return;
if (String(_messages[tempCounter]) == String("\0")) tempCounter = 0;
Serial.println("Tempcounter after boundcheck: " + String(tempCounter));
// Set msg counter and print message
*msgCounter = tempCounter;
if (_messages[*msgCounter] == String("\0")) return;
else {
_screen->print(_messages[*msgCounter]);
_screen->setCursor(_menuBorderOffset, 160);
_screen->setTextSize(_textSize - 1);
_screen->print(String(3 - noMessages) + " Nachr.; Nr. " + String(tempCounter + 1));
}
}
/**
* @brief
* Gets selection in the read menu
*
* @param parse x and y coordinates of pressed point
* @return int 1: top left, 2: top right, 3: bottom left, 4: bottom right
*/
int Messenger::readMenuSelection(ScreenParse parse) {
int selection = 1;
int y = 320 - parse.y;
Serial.println("Y: " + String(y) + "; parse.y: " + String(parse.y));
// Check, if point is outside of menu
if (y < 180) return -1;
if (y > 290) return -1;
if (parse.x < _menuBorderOffset) return -1;
if (parse.x > _screen->width() - _menuBorderOffset) return -1;
// Get row and add 2, if second row. Works because of numbering of menu entries
selection += (y > 235 ? 2 : 0);
// Get column and add 1, if second column
selection += (parse.x > (_screen->width() / 2) ? 1 : 0);
return selection;
}
/************************ PRIVATE SPECIALS ***************************/
/**
* @brief
* Parses Coordinates from touchpoint to screen coordinates
*
* @param p TSPoint, Point the user touched on display
* @return ScreenParse Struct containing parsed screen coordinates
*/
ScreenParse Messenger::parseCoords(TSPoint p) {
ScreenParse parse;
// Touch X: [120,940], Screen X: [0,240]
// Parse X
parse.x = (int) ((p.x - 120) / 3.5);
// Touch Y: [70,920], Screen Y: [0,320]
// Parse Y
parse.y = (int) ((p.y - 70) / 2.66);
return parse;
}
/**
* @brief
* Gets number of menupoint selected
*
* @param menuStart y-Value, where the menu starts
* @param menuThickness how thick one menupoint is
* @param menuOffset how far the menu entries are apart
* @param entries number of entries
* @param parse x and y values of the pressed point
* @return int number of the menu entry selected
*/
int Messenger::getSelection(int menuStart, int menuThickness, int menuOffset, int entries, ScreenParse parse) {
int x, y, selection;
// Screen and touchscreen have different values, parse ts to screen
// Get x and y coordinates from point
x = (int) parse.x;
// Invert display
y = (int) 320 - parse.y;
Serial.println("X: " + String(x) + ", Y: " + String(y));
// Check if touch occured out of menu
if (y < menuStart || y > menuStart + (entries*menuThickness) + (entries*menuOffset)) return -1;
if (x < _menuBorderOffset || x > (_screen->width() - _menuBorderOffset)) return -1;
// translate y to the start of the menu and get selection
y -= menuStart;
selection = (int) (y / (menuThickness+menuOffset)) + 1;
selection = selection;
if (selection < 1 || selection > entries) return -1;
return selection;
}
/**
* @brief
* Sets private field _background to color that you want
*
* @param color Color you want to set the background to
*/
void Messenger::setBackground(uint16_t color) {
if (color == _textColor) _textColor = (color == WHITE ? BLACK : WHITE);
if (color == _boxColor) _boxColor = (color == WHITE ? BLACK : WHITE);
_background = color;
}
/**
* @brief Prints the current message to the display
*
* @param msg {String} Current message
*/
void Messenger::printMessageOnDisplay(String msg) {
// Set pinmodes
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
// Print a filled rect over old message
_screen->fillRect(0,0,_screen->width(), 140, _background);
// Set config values for text and print it
_screen->setCursor(0,0);
_screen->setTextSize(_textSize);
_screen->setTextColor(_textColor);
_screen->print(msg);
}
/**
* @brief
* Function to write a message that you want to send
*
* @return String Written message
*/
String Messenger::writeMessage(void) {
// Initialize the keypad (draw it on screen)
if (_keys->getSpecial()) _keys->switchKeys();
else _keys->init();
// Initialize variables and get initial touchpoint
String msg = "", oldChar = "";
TSPoint oldP = _ts->getPoint();
// Clicks: Counter for how long since current char has been pressed. It works, I forgot how
int clicks = 0;
while (true) {
TSPoint p;
do {
// Get touchpoint, until the point differs from last touched point on display
digitalWrite(13, HIGH);
p = _ts->getPoint();
digitalWrite(13, LOW);
} while (oldP.x == p.x && oldP.y == p.y);
// Save new point in old point for later comparison
oldP.x = p.x;
oldP.y = p.y;
if (p.z > _minTouch) {
if (msg.length() >= 80) {
printMessageOnDisplay("Maximal 80\nZeichen!");
delay(1000);
printMessageOnDisplay(msg);
continue;
}
// hasChanged: records, whether the new char differs from last char
bool hasChanged = false;
// Get character from pressed point
String newChar = String(_keys->getInputChar(p));
// Check, if pressed char differs from last pressed char
if (newChar == oldChar) {
// if they don't differ, count how many times we have been at this point
if (clicks < 3) {
clicks++;
continue;
} else {
// else reset all variables
oldChar = "";
newChar = "";
clicks = 0;
}
} else {
// Unless a special key was pressed, add new char to message
if (newChar != "~" && newChar != "s" && newChar != "DONE" && newChar != "BACK") {
msg += newChar;
}
// record, that something has changed
oldChar = newChar;
hasChanged = true;
}
// If delete was pressed, delete last char from message
if (newChar == "~") {
msg = msg.substring(0,msg.length()-1);
printMessageOnDisplay(msg);
} else if (newChar == "s") {
// small s means, that we want to switch to special keys (numbers and non-alpha-numeric)
_keys->switchKeys();
} else if (newChar == "DONE") {
// If done, break infinite loop to return the message
break;
} else if (newChar == "BACK") {
// If cancelled, reset message and break infinite loop
msg = "";
break;
} else {
if (hasChanged) {
// If the input char has changed, print message to display
printMessageOnDisplay(msg);
}
}
Serial.println(msg);
}
// Without delay, this thing notices touches 3-5 Times per Touch
delay(10);
}
return msg;
}
/**
* @brief
* Gets message that was sent from other device
*
* @return String received message
*/
String Messenger::receiveMessage() {
// Try to get message and return, if message is \0
String msg = _radio->receiveMessage(_commType);
if (msg == "\0") return "\0";
// Cache that message and return it
Serial.println("Received message: " + String(msg));
cacheMessage(msg);
return msg;
}
/********************************** PRIVATE CACHE METHODS ********************************************/
/**
* @brief
* Caches incoming messages in message cache
*
* @param msg String to be cached
*/
void Messenger::cacheMessage(String msg) {
// Check, how many messages are in cache
int counter = 0;
for (int i = 0; i < 3; i++) {
if (_messages[i] != "\0") {
counter++;
continue;
} else {
break;
}
}
// If message already is in cache, return
for (int i = 0; i < counter; i++) {
if (msg == _messages[i]) return;