-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathGEM_u8g2.cpp
More file actions
1339 lines (1217 loc) · 43.7 KB
/
GEM_u8g2.cpp
File metadata and controls
1339 lines (1217 loc) · 43.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
/*
GEM (a.k.a. Good Enough Menu) - Arduino library for creation of graphic multi-level menu with
editable menu items, such as variables (supports int, byte, float, double, bool, char[17] data types)
and option selects. User-defined callback function can be specified to invoke when menu item is saved.
Supports buttons that can invoke user-defined actions and create action-specific
context, which can have its own enter (setup) and exit callbacks as well as loop function.
Supports:
- AltSerialGraphicLCD library by Jon Green (http://www.jasspa.com/serialGLCD.html);
- U8g2 library by olikraus (https://github.com/olikraus/U8g2_Arduino);
- Adafruit GFX library by Adafruit (https://github.com/adafruit/Adafruit-GFX-Library).
For documentation visit:
https://github.com/Spirik/GEM
Copyright (c) 2018-2026 Alexander 'Spirik' Spiridonov
This file is part of GEM library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include <Arduino.h>
#include "GEM_u8g2.h"
#ifdef GEM_ENABLE_U8G2_VERSION
// AVR-based Arduinos have suppoort for dtostrf, some others may require manual inclusion (e.g. SAMD),
// see https://github.com/plotly/arduino-api/issues/38#issuecomment-108987647
#if defined(GEM_SUPPORT_FLOAT_EDIT) && (defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_SAM) || defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_ARCH_NRF52840))
#include <avr/dtostrf.h>
#endif
// Macro constants (aliases) for some of the ASCII character codes
#define GEM_CHAR_CODE_9 57
#define GEM_CHAR_CODE_0 48
#define GEM_CHAR_CODE_MINUS 45
#define GEM_CHAR_CODE_DOT 46
#define GEM_CHAR_CODE_SPACE 32
#define GEM_CHAR_CODE_UNDERSCORE 95
#define GEM_CHAR_CODE_TILDA 126
#define GEM_CHAR_CODE_BANG 33
#define GEM_CHAR_CODE_a 97
#define GEM_CHAR_CODE_ACCENT 96
/*
// WIP for Cyrillic values support
#define GEM_CHAR_CODE_CYR_YO 1025
#define GEM_CHAR_CODE_CYR_A 1040
#define GEM_CHAR_CODE_CYR_E 1045
#define GEM_CHAR_CODE_CYR_E_SM 1077
#define GEM_CHAR_CODE_CYR_YA_SM 1103
#define GEM_CHAR_CODE_CYR_YO_SM 1105
*/
// XBM image of the default GEM _splash screen (GEM logo v1)
/*
#define logo_width 12
#define logo_height 12
static const unsigned char logo_bits [] U8X8_PROGMEM = {
0xc0,0xf7,0x60,0xfe,0x30,0xfe,0x18,0xff,0x8c,0xff,0xc6,0xff,
0xe3,0xff,0xf1,0xff,0x19,0xff,0x7f,0xfc,0xff,0xfd,0xfe,0xf7
};
*/
// XBM image of the default GEM _splash screen (GEM logo v2)
#define logo_width 20
#define logo_height 8
static const unsigned char logo_bits [] U8X8_PROGMEM = {
0x8f,0x4f,0xf4,0x00,0x40,0xf4,0x00,0x40,0xf5,0x98,0x47,0xf5,
0x00,0x40,0xf4,0x00,0x40,0xf4,0x9f,0x4f,0xf4,0x00,0x00,0xf0
};
const GEMSprite logo = {logo_width, logo_height, logo_bits};
// Sprites of the UI elements used to draw menu
#define sprite_height 8
#define arrowRight_width 6
#define arrowRight_height 8
static const unsigned char arrowRight_bits [] U8X8_PROGMEM = {
0xc0,0xc4,0xcc,0xdc,0xcc,0xc4,0xc0,0xc0
};
#define arrowLeft_width 6
#define arrowLeft_height 8
static const unsigned char arrowLeft_bits [] U8X8_PROGMEM = {
0xc0,0xc4,0xc6,0xc7,0xc6,0xc4,0xc0,0xc0
};
#define arrowBtn_width 6
#define arrowBtn_height 8
static const unsigned char arrowBtn_bits [] U8X8_PROGMEM = {
0xc0,0xc3,0xc5,0xc9,0xc5,0xc3,0xc0,0xc0
};
#define checkboxUnchecked_width 7
#define checkboxUnchecked_height 8
static const unsigned char checkboxUnchecked_bits [] U8X8_PROGMEM = {
0x80,0xbf,0xa1,0xa1,0xa1,0xa1,0xbf,0x80
};
#define checkboxChecked_width 7
#define checkboxChecked_height 8
static const unsigned char checkboxChecked_bits [] U8X8_PROGMEM = {
0xc0,0xaf,0xb1,0xab,0xa5,0xa1,0xbf,0x80
};
#define selectArrows_width 6
#define selectArrows_height 8
static const unsigned char selectArrows_bits [] U8X8_PROGMEM = {
0xc0,0xc4,0xce,0xc0,0xce,0xc4,0xc0,0xc0
};
const GEMSprite arrowRight = {arrowRight_width, arrowRight_height, arrowRight_bits};
const GEMSprite arrowLeft = {arrowLeft_width, arrowLeft_height, arrowLeft_bits};
const GEMSprite arrowBtn = {arrowBtn_width, arrowBtn_height, arrowBtn_bits};
const GEMSprite checkboxUnchecked = {checkboxUnchecked_width, checkboxUnchecked_height, checkboxUnchecked_bits};
const GEMSprite checkboxChecked = {checkboxChecked_width, checkboxChecked_height, checkboxChecked_bits};
const GEMSprite selectArrows = {selectArrows_width, selectArrows_height, selectArrows_bits};
GEM_u8g2::GEM_u8g2(U8G2& u8g2_, byte menuPointerType_, byte menuItemsPerScreen_, byte menuItemHeight_, byte menuPageScreenTopOffset_, byte menuValuesLeftOffset_)
: _u8g2(u8g2_)
{
_appearance.menuPointerType = menuPointerType_;
_appearance.menuItemsPerScreen = menuItemsPerScreen_;
_appearance.menuItemHeight = menuItemHeight_;
_appearance.menuPageScreenTopOffset = menuPageScreenTopOffset_;
_appearance.menuValuesLeftOffset = menuValuesLeftOffset_;
_appearanceCurrent = &_appearance;
_splash = logo;
clearContext();
_editValueMode = false;
_editValueCursorPosition = 0;
memset(_valueString, '\0', GEM_STR_LEN - 1);
_valueSelectNum = -1;
}
GEM_u8g2::GEM_u8g2(U8G2& u8g2_, GEMAppearance appearance_)
: _u8g2(u8g2_)
, _appearance(appearance_)
{
_appearanceCurrent = &_appearance;
_splash = logo;
clearContext();
_editValueMode = false;
_editValueCursorPosition = 0;
memset(_valueString, '\0', GEM_STR_LEN - 1);
_valueSelectNum = -1;
}
//====================== APPEARANCE OPERATIONS
GEM_u8g2& GEM_u8g2::setAppearance(GEMAppearance appearance) {
_appearance = appearance;
return *this;
}
GEMAppearance* GEM_u8g2::getCurrentAppearance() {
return (_menuPageCurrent != nullptr && _menuPageCurrent->_appearance != nullptr) ? _menuPageCurrent->_appearance : &_appearance;
}
byte GEM_u8g2::getMenuItemsPerScreen() {
return getCurrentAppearance()->menuItemsPerScreen == GEM_ITEMS_COUNT_AUTO ? (_u8g2.getDisplayHeight() - getCurrentAppearance()->menuPageScreenTopOffset) / getCurrentAppearance()->menuItemHeight : getCurrentAppearance()->menuItemsPerScreen;
}
byte GEM_u8g2::getMenuItemFontSize() {
return getCurrentAppearance()->menuItemHeight >= _menuItemFont[0].height ? 0 : 1;
}
byte GEM_u8g2::getMenuItemTitleLength() {
return (getCurrentAppearance()->menuValuesLeftOffset - 5) / _menuItemFont[getMenuItemFontSize()].width;
}
byte GEM_u8g2::getMenuItemValueLength() {
return (_u8g2.getDisplayWidth() - getCurrentAppearance()->menuValuesLeftOffset - 6) / _menuItemFont[getMenuItemFontSize()].width;
}
//====================== INIT OPERATIONS
GEM_u8g2& GEM_u8g2::setSplash(byte width, byte height, const unsigned char *image) {
_splash = {width, height, image};
return *this;
}
GEM_u8g2& GEM_u8g2::setSplashDelay(uint16_t value) {
_splashDelay = value;
return *this;
}
GEM_u8g2& GEM_u8g2::hideVersion(bool flag) {
_enableVersion = !flag;
return *this;
}
GEM_u8g2& GEM_u8g2::enableUTF8(bool flag) {
_UTF8Enabled = flag;
if (_UTF8Enabled) {
_u8g2.enableUTF8Print();
} else {
_u8g2.disableUTF8Print();
}
return *this;
}
GEM_u8g2& GEM_u8g2::enableCyrillic(bool flag) {
enableUTF8(flag);
if (_UTF8Enabled) {
_fontFamilies = {(uint8_t *)GEM_FONT_BIG_CYR, (uint8_t *)GEM_FONT_SMALL_CYR};
} else {
_fontFamilies = {(uint8_t *)GEM_FONT_BIG, (uint8_t *)GEM_FONT_SMALL};
}
_menuItemFont[0] = {6, 8};
_menuItemFont[1] = {4, 6};
return *this;
}
GEM_u8g2& GEM_u8g2::setFontBig(const uint8_t* font, uint8_t width, uint8_t height) {
_fontFamilies.big = font;
_menuItemFont[0] = {width, height};
return *this;
}
GEM_u8g2& GEM_u8g2::setFontBig() {
_fontFamilies.big = _UTF8Enabled ? GEM_FONT_BIG_CYR : GEM_FONT_BIG;
_menuItemFont[0] = {6, 8};
return *this;
}
GEM_u8g2& GEM_u8g2::setFontSmall(const uint8_t* font, uint8_t width, uint8_t height) {
_fontFamilies.small = font;
_menuItemFont[1] = {width, height};
return *this;
}
GEM_u8g2& GEM_u8g2::setFontSmall() {
_fontFamilies.small = _UTF8Enabled ? GEM_FONT_SMALL_CYR : GEM_FONT_SMALL;
_menuItemFont[1] = {4, 6};
return *this;
}
GEM_u8g2& GEM_u8g2::invertKeysDuringEdit(bool invert) {
_invertKeysDuringEdit = invert;
return *this;
}
GEM_u8g2& GEM_u8g2::init() {
_u8g2.clear();
_u8g2.setDrawColor(1);
_u8g2.setFontPosTop();
if (_splashDelay > 0) {
_u8g2.firstPage();
do {
_u8g2.drawXBMP((_u8g2.getDisplayWidth() - _splash.width) / 2, (_u8g2.getDisplayHeight() - _splash.height) / 2, _splash.width, _splash.height, _splash.image);
} while (_u8g2.nextPage());
if (_enableVersion) {
delay(_splashDelay / 2);
_u8g2.firstPage();
do {
_u8g2.drawXBMP((_u8g2.getDisplayWidth() - _splash.width) / 2, (_u8g2.getDisplayHeight() - _splash.height) / 2, _splash.width, _splash.height, _splash.image);
_u8g2.setFont(_fontFamilies.small);
byte x = _u8g2.getDisplayWidth() - strlen(GEM_VER)*4;
byte y = _u8g2.getDisplayHeight() - 7;
if (_splash.image != logo_bits) {
_u8g2.setCursor(x - 12, y);
_u8g2.print("GEM");
} else {
_u8g2.setCursor(x, y);
}
_u8g2.print(GEM_VER);
} while (_u8g2.nextPage());
delay(_splashDelay / 2);
} else {
delay(_splashDelay);
}
_u8g2.clear();
}
return *this;
}
GEM_u8g2& GEM_u8g2::reInit() {
_u8g2.initDisplay();
_u8g2.setPowerSave(0);
_u8g2.clear();
_u8g2.setDrawColor(1);
_u8g2.setFontPosTop();
if (_UTF8Enabled) {
_u8g2.enableUTF8Print();
} else {
_u8g2.disableUTF8Print();
}
return *this;
}
GEM_u8g2& GEM_u8g2::setMenuPageCurrent(GEMPage& menuPageCurrent) {
_menuPageCurrent = &menuPageCurrent;
return *this;
}
GEMPage* GEM_u8g2::getCurrentMenuPage() {
return _menuPageCurrent;
}
//====================== CONTEXT OPERATIONS
GEM_u8g2& GEM_u8g2::clearContext() {
context.loop = nullptr;
context.enter = nullptr;
context.exit = nullptr;
context.allowExit = true;
return *this;
}
//====================== DRAW OPERATIONS
GEM_u8g2& GEM_u8g2::drawMenu() {
// _u8g2.clear(); // Not clearing for better performance
_u8g2.firstPage();
do {
drawTitleBar();
printMenuItems();
drawMenuPointer();
drawScrollbar();
if (drawMenuCallback != nullptr) {
drawMenuCallback();
}
} while (_u8g2.nextPage());
return *this;
}
GEM_u8g2& GEM_u8g2::setDrawMenuCallback(void (*drawMenuCallback_)()) {
drawMenuCallback = drawMenuCallback_;
return *this;
}
GEM_u8g2& GEM_u8g2::removeDrawMenuCallback() {
drawMenuCallback = nullptr;
return *this;
}
void GEM_u8g2::drawTitleBar() {
_u8g2.setFont(_fontFamilies.small);
_u8g2.setCursor(5, 0);
_u8g2.print(_menuPageCurrent->title);
_u8g2.setFont(getMenuItemFontSize() ? _fontFamilies.small : _fontFamilies.big);
}
void GEM_u8g2::drawSprite(u8g2_uint_t x, u8g2_uint_t y, const GEMSprite sprite) {
_u8g2.drawXBMP(x, y, sprite.width, sprite.height, sprite.image);
}
void GEM_u8g2::printMenuItemString(const char* str, byte num, byte startPos) {
if (_UTF8Enabled) {
byte j = 0;
byte p = 0;
while ((j < startPos || ((byte)str[p] >= 128 && (byte)str[p] <= 191)) && str[p] != '\0') {
if ((byte)str[p] <= 127 || (byte)str[p] >= 194) {
j++;
}
p++;
}
byte startPosReal = p;
byte i = j;
byte k = startPosReal;
while ((i < num + j || ((byte)str[k] >= 128 && (byte)str[k] <= 191)) && str[k] != '\0') {
_u8g2.print(str[k]);
if ((byte)str[k] <= 127 || (byte)str[k] >= 194) {
i++;
}
k++;
}
} else {
byte i = startPos;
while (i < num + startPos && str[i] != '\0') {
_u8g2.print(str[i]);
i++;
}
}
}
void GEM_u8g2::printMenuItemTitle(const char* str, int offset) {
printMenuItemString(str, getMenuItemTitleLength() + offset);
}
void GEM_u8g2::printMenuItemValue(const char* str, int offset, byte startPos) {
printMenuItemString(str, getMenuItemValueLength() + offset, startPos);
}
void GEM_u8g2::printMenuItemFull(const char* str, int offset) {
printMenuItemString(str, getMenuItemTitleLength() + getMenuItemValueLength() + offset);
}
byte GEM_u8g2::getMenuItemInsetOffset(bool forSprite) {
byte menuItemFontSize = getMenuItemFontSize();
byte menuItemInsetOffset = (getCurrentAppearance()->menuItemHeight - _menuItemFont[menuItemFontSize].height) / 2;
return menuItemInsetOffset + (forSprite ? (_menuItemFont[menuItemFontSize].height - sprite_height) / 2 : -1); // With additional offset for sprites and text for better visual alignment
}
byte GEM_u8g2::getCurrentItemTopOffset(bool withInsetOffset, bool forSprite) {
return (_menuPageCurrent->currentItemNum % getMenuItemsPerScreen()) * getCurrentAppearance()->menuItemHeight + getCurrentAppearance()->menuPageScreenTopOffset + (withInsetOffset ? getMenuItemInsetOffset(forSprite) : 0);
}
void GEM_u8g2::printMenuItems() {
byte menuItemsPerScreen = getMenuItemsPerScreen();
byte currentPageScreenNum = _menuPageCurrent->currentItemNum / menuItemsPerScreen;
GEMItem* menuItemTmp = _menuPageCurrent->getMenuItem(currentPageScreenNum * menuItemsPerScreen);
byte y = getCurrentAppearance()->menuPageScreenTopOffset;
byte i = 0;
char valueStringTmp[GEM_STR_LEN];
while (menuItemTmp != nullptr && i < menuItemsPerScreen) {
byte yText = y + getMenuItemInsetOffset();
byte yDraw = y + getMenuItemInsetOffset(true);
switch (menuItemTmp->type) {
case GEM_ITEM_VAL:
{
_u8g2.setCursor(5, yText);
if (menuItemTmp->readonly) {
printMenuItemTitle(menuItemTmp->title, -1);
_u8g2.print("^");
} else {
printMenuItemTitle(menuItemTmp->title);
}
byte menuValuesLeftOffset = getCurrentAppearance()->menuValuesLeftOffset;
_u8g2.setCursor(menuValuesLeftOffset, yText);
switch (menuItemTmp->linkedType) {
case GEM_VAL_INTEGER:
if (_editValueMode && menuItemTmp == _menuPageCurrent->getCurrentMenuItem()) {
printMenuItemValue(_valueString, 0, _editValueVirtualCursorPosition - _editValueCursorPosition);
drawEditValueCursor();
} else {
itoa(*(int*)menuItemTmp->linkedVariable, valueStringTmp, 10);
printMenuItemValue(valueStringTmp);
}
break;
case GEM_VAL_BYTE:
if (_editValueMode && menuItemTmp == _menuPageCurrent->getCurrentMenuItem()) {
printMenuItemValue(_valueString, 0, _editValueVirtualCursorPosition - _editValueCursorPosition);
drawEditValueCursor();
} else {
itoa(*(byte*)menuItemTmp->linkedVariable, valueStringTmp, 10);
printMenuItemValue(valueStringTmp);
}
break;
case GEM_VAL_CHAR:
if (_editValueMode && menuItemTmp == _menuPageCurrent->getCurrentMenuItem()) {
printMenuItemValue(_valueString, 0, _editValueVirtualCursorPosition - _editValueCursorPosition);
drawEditValueCursor();
} else {
printMenuItemValue((char*)menuItemTmp->linkedVariable);
}
break;
case GEM_VAL_BOOL:
if (*(bool*)menuItemTmp->linkedVariable) {
drawSprite(menuValuesLeftOffset, yDraw, checkboxChecked);
} else {
drawSprite(menuValuesLeftOffset, yDraw, checkboxUnchecked);
}
break;
case GEM_VAL_SELECT:
{
GEMSelect* select = menuItemTmp->select;
if (_editValueMode && menuItemTmp == _menuPageCurrent->getCurrentMenuItem()) {
printMenuItemValue(select->getOptionNameByIndex(_valueSelectNum));
drawSprite(_u8g2.getDisplayWidth() - 7, yDraw, selectArrows);
drawEditValueCursor();
} else {
printMenuItemValue(select->getSelectedOptionName(menuItemTmp->linkedVariable));
drawSprite(_u8g2.getDisplayWidth() - 7, yDraw, selectArrows);
}
}
break;
#ifdef GEM_SUPPORT_SPINNER
case GEM_VAL_SPINNER:
{
GEMSpinner* spinner = menuItemTmp->spinner;
if (_editValueMode && menuItemTmp == _menuPageCurrent->getCurrentMenuItem()) {
GEMSpinnerValue valueTmp = spinner->getOptionNameByIndex(menuItemTmp->linkedVariable, _valueSelectNum);
switch (spinner->getType()) {
case GEM_VAL_BYTE:
itoa(valueTmp.valByte, valueStringTmp, 10);
break;
case GEM_VAL_INTEGER:
itoa(valueTmp.valInt, valueStringTmp, 10);
break;
#ifdef GEM_SUPPORT_FLOAT_EDIT
case GEM_VAL_FLOAT:
dtostrf(valueTmp.valFloat, menuItemTmp->precision + 1, menuItemTmp->precision, valueStringTmp);
break;
case GEM_VAL_DOUBLE:
dtostrf(valueTmp.valDouble, menuItemTmp->precision + 1, menuItemTmp->precision, valueStringTmp);
break;
#endif
}
printMenuItemValue(valueStringTmp);
drawSprite(_u8g2.getDisplayWidth() - 7, yDraw, selectArrows);
drawEditValueCursor();
} else {
switch (spinner->getType()) {
case GEM_VAL_BYTE:
itoa(*(byte*)menuItemTmp->linkedVariable, valueStringTmp, 10);
break;
case GEM_VAL_INTEGER:
itoa(*(int*)menuItemTmp->linkedVariable, valueStringTmp, 10);
break;
#ifdef GEM_SUPPORT_FLOAT_EDIT
case GEM_VAL_FLOAT:
dtostrf(*(float*)menuItemTmp->linkedVariable, menuItemTmp->precision + 1, menuItemTmp->precision, valueStringTmp);
break;
case GEM_VAL_DOUBLE:
dtostrf(*(double*)menuItemTmp->linkedVariable, menuItemTmp->precision + 1, menuItemTmp->precision, valueStringTmp);
break;
#endif
}
printMenuItemValue(valueStringTmp);
drawSprite(_u8g2.getDisplayWidth() - 7, yDraw, selectArrows);
}
}
break;
#endif
#ifdef GEM_SUPPORT_FLOAT_EDIT
case GEM_VAL_FLOAT:
if (_editValueMode && menuItemTmp == _menuPageCurrent->getCurrentMenuItem()) {
printMenuItemValue(_valueString, 0, _editValueVirtualCursorPosition - _editValueCursorPosition);
drawEditValueCursor();
} else {
// sprintf(valueStringTmp,"%.6f", *(float*)menuItemTmp->linkedVariable); // May work for non-AVR boards
dtostrf(*(float*)menuItemTmp->linkedVariable, menuItemTmp->precision + 1, menuItemTmp->precision, valueStringTmp);
printMenuItemValue(valueStringTmp);
}
break;
case GEM_VAL_DOUBLE:
if (_editValueMode && menuItemTmp == _menuPageCurrent->getCurrentMenuItem()) {
printMenuItemValue(_valueString, 0, _editValueVirtualCursorPosition - _editValueCursorPosition);
drawEditValueCursor();
} else {
// sprintf(valueStringTmp,"%.6f", *(double*)menuItemTmp->linkedVariable); // May work for non-AVR boards
dtostrf(*(double*)menuItemTmp->linkedVariable, menuItemTmp->precision + 1, menuItemTmp->precision, valueStringTmp);
printMenuItemValue(valueStringTmp);
}
break;
#endif
}
break;
}
case GEM_ITEM_LINK:
_u8g2.setCursor(5, yText);
if (menuItemTmp->readonly) {
printMenuItemFull(menuItemTmp->title, -1);
_u8g2.print("^");
} else {
printMenuItemFull(menuItemTmp->title);
}
drawSprite(_u8g2.getDisplayWidth() - 8, yDraw, arrowRight);
break;
case GEM_ITEM_BACK:
drawSprite(5, yDraw, arrowLeft);
break;
case GEM_ITEM_BUTTON:
_u8g2.setCursor(11, yText);
if (menuItemTmp->readonly) {
printMenuItemFull(menuItemTmp->title, -1);
_u8g2.print("^");
} else {
printMenuItemFull(menuItemTmp->title);
}
drawSprite(5, yDraw, arrowBtn);
break;
case GEM_ITEM_LABEL:
_u8g2.setCursor(5, yText);
printMenuItemFull(menuItemTmp->title);
break;
}
menuItemTmp = menuItemTmp->getMenuItemNext();
y += getCurrentAppearance()->menuItemHeight;
i++;
}
memset(valueStringTmp, '\0', GEM_STR_LEN - 1);
}
void GEM_u8g2::drawMenuPointer() {
if (_menuPageCurrent->itemsCount > 0) {
GEMItem* menuItemTmp = _menuPageCurrent->getCurrentMenuItem();
int pointerPosition = getCurrentItemTopOffset(false);
byte menuItemHeight = getCurrentAppearance()->menuItemHeight;
if (getCurrentAppearance()->menuPointerType == GEM_POINTER_DASH) {
if (menuItemTmp->readonly || menuItemTmp->type == GEM_ITEM_LABEL) {
for (byte i = 0; i < (menuItemHeight - 1) / 2; i++) {
_u8g2.drawPixel(0, pointerPosition + i * 2);
_u8g2.drawPixel(1, pointerPosition + i * 2 + 1);
}
} else {
_u8g2.drawBox(0, pointerPosition, 2, menuItemHeight - 1);
}
} else if (!_editValueMode) {
_u8g2.setDrawColor(2);
_u8g2.drawBox(0, pointerPosition - 1, _u8g2.getDisplayWidth() - 2, menuItemHeight + 1);
_u8g2.setDrawColor(1);
if (menuItemTmp->readonly || menuItemTmp->type == GEM_ITEM_LABEL) {
_u8g2.setDrawColor(0);
for (byte i = 0; i < (menuItemHeight + 2) / 2; i++) {
_u8g2.drawPixel(0, pointerPosition + i * 2);
_u8g2.drawPixel(1, pointerPosition + i * 2 - 1);
}
_u8g2.setDrawColor(1);
}
}
}
}
void GEM_u8g2::drawScrollbar() {
byte menuItemsPerScreen = getMenuItemsPerScreen();
byte screensCount = (_menuPageCurrent->itemsCount % menuItemsPerScreen == 0) ? _menuPageCurrent->itemsCount / menuItemsPerScreen : _menuPageCurrent->itemsCount / menuItemsPerScreen + 1;
if (screensCount > 1) {
byte currentScreenNum = _menuPageCurrent->currentItemNum / menuItemsPerScreen;
byte menuPageScreenTopOffset = getCurrentAppearance()->menuPageScreenTopOffset;
byte scrollbarHeight = (_u8g2.getDisplayHeight() - menuPageScreenTopOffset + 1) / screensCount;
byte scrollbarPosition = currentScreenNum * scrollbarHeight + menuPageScreenTopOffset - 1;
_u8g2.drawLine(_u8g2.getDisplayWidth() - 1, scrollbarPosition, _u8g2.getDisplayWidth() - 1, scrollbarPosition + scrollbarHeight);
}
}
//====================== MENU ITEMS NAVIGATION
void GEM_u8g2::nextMenuItem() {
if (_menuPageCurrent->itemsCount > 0) {
if (_menuPageCurrent->currentItemNum == _menuPageCurrent->itemsCount-1) {
_menuPageCurrent->currentItemNum = 0;
} else {
_menuPageCurrent->currentItemNum++;
}
drawMenu();
}
}
void GEM_u8g2::prevMenuItem() {
if (_menuPageCurrent->itemsCount > 0) {
if (_menuPageCurrent->currentItemNum == 0) {
_menuPageCurrent->currentItemNum = _menuPageCurrent->itemsCount-1;
} else {
_menuPageCurrent->currentItemNum--;
}
drawMenu();
}
}
void GEM_u8g2::menuItemSelect() {
GEMItem* menuItemTmp = _menuPageCurrent->getCurrentMenuItem();
if (menuItemTmp != nullptr) {
switch (menuItemTmp->type) {
case GEM_ITEM_VAL:
if (!menuItemTmp->readonly) {
enterEditValueMode();
}
break;
case GEM_ITEM_LINK:
if (!menuItemTmp->readonly) {
_menuPageCurrent = menuItemTmp->linkedPage;
drawMenu();
}
break;
case GEM_ITEM_BACK:
_menuPageCurrent->currentItemNum = (_menuPageCurrent->itemsCount > 1) ? 1 : 0;
_menuPageCurrent = menuItemTmp->linkedPage;
drawMenu();
break;
case GEM_ITEM_BUTTON:
if (!menuItemTmp->readonly) {
if (menuItemTmp->callbackWithArgs) {
menuItemTmp->callbackActionArg(menuItemTmp->callbackData);
} else {
menuItemTmp->callbackAction();
}
}
break;
}
}
}
//====================== VALUE EDIT
void GEM_u8g2::enterEditValueMode() {
_editValueMode = true;
GEMItem* menuItemTmp = _menuPageCurrent->getCurrentMenuItem();
_editValueType = menuItemTmp->linkedType;
switch (_editValueType) {
case GEM_VAL_INTEGER:
itoa(*(int*)menuItemTmp->linkedVariable, _valueString, 10);
_editValueLength = 6;
initEditValueCursor();
break;
case GEM_VAL_BYTE:
itoa(*(byte*)menuItemTmp->linkedVariable, _valueString, 10);
_editValueLength = 3;
initEditValueCursor();
break;
case GEM_VAL_CHAR:
strcpy(_valueString, (char*)menuItemTmp->linkedVariable);
_editValueLength = GEM_STR_LEN - 1;
initEditValueCursor();
break;
case GEM_VAL_BOOL:
checkboxToggle();
drawMenu();
break;
case GEM_VAL_SELECT:
{
GEMSelect* select = menuItemTmp->select;
_valueSelectNum = select->getSelectedOptionNum(menuItemTmp->linkedVariable);
initEditValueCursor();
}
break;
#ifdef GEM_SUPPORT_SPINNER
case GEM_VAL_SPINNER:
{
GEMSpinner* spinner = menuItemTmp->spinner;
_valueSelectNum = spinner->getSelectedOptionNum(menuItemTmp->linkedVariable);
initEditValueCursor();
}
break;
#endif
#ifdef GEM_SUPPORT_FLOAT_EDIT
case GEM_VAL_FLOAT:
// sprintf(_valueString,"%.6f", *(float*)menuItemTmp->linkedVariable); // May work for non-AVR boards
dtostrf(*(float*)menuItemTmp->linkedVariable, menuItemTmp->precision + 1, menuItemTmp->precision, _valueString);
_editValueLength = GEM_STR_LEN - 1;
initEditValueCursor();
break;
case GEM_VAL_DOUBLE:
// sprintf(_valueString,"%.6f", *(double*)menuItemTmp->linkedVariable); // May work for non-AVR boards
dtostrf(*(double*)menuItemTmp->linkedVariable, menuItemTmp->precision + 1, menuItemTmp->precision, _valueString);
_editValueLength = GEM_STR_LEN - 1;
initEditValueCursor();
break;
#endif
}
}
void GEM_u8g2::checkboxToggle() {
GEMItem* menuItemTmp = _menuPageCurrent->getCurrentMenuItem();
bool checkboxValue = *(bool*)menuItemTmp->linkedVariable;
*(bool*)menuItemTmp->linkedVariable = !checkboxValue;
if (menuItemTmp->callbackAction != nullptr) {
resetEditValueState(); // Explicitly reset edit value state to be more predictable before user-defined callback is called
if (menuItemTmp->callbackWithArgs) {
menuItemTmp->callbackActionArg(menuItemTmp->callbackData);
} else {
menuItemTmp->callbackAction();
}
drawEditValueCursor();
drawMenu();
} else {
_editValueMode = false;
}
}
void GEM_u8g2::initEditValueCursor() {
_editValueCursorPosition = 0;
_editValueVirtualCursorPosition = 0;
drawMenu();
}
void GEM_u8g2::nextEditValueCursorPosition() {
if ((_editValueCursorPosition != getMenuItemValueLength() - 1) && (_editValueCursorPosition != _editValueLength - 1) && (_valueString[_editValueCursorPosition] != '\0')) {
_editValueCursorPosition++;
}
if ((_editValueVirtualCursorPosition != _editValueLength - 1) && (_valueString[_editValueVirtualCursorPosition] != '\0')) {
_editValueVirtualCursorPosition++;
}
drawMenu();
}
void GEM_u8g2::prevEditValueCursorPosition() {
if (_editValueCursorPosition != 0) {
_editValueCursorPosition--;
}
if (_editValueVirtualCursorPosition != 0) {
_editValueVirtualCursorPosition--;
}
drawMenu();
}
void GEM_u8g2::drawEditValueCursor() {
int pointerPosition = getCurrentItemTopOffset(false);
byte menuItemFontSize = getMenuItemFontSize();
byte cursorLeftOffset = getCurrentAppearance()->menuValuesLeftOffset + _editValueCursorPosition * _menuItemFont[menuItemFontSize].width;
_u8g2.setDrawColor(2);
if (_editValueType == GEM_VAL_SELECT || _editValueType == GEM_VAL_SPINNER) {
_u8g2.drawBox(cursorLeftOffset - 1, pointerPosition - 1, _u8g2.getDisplayWidth() - cursorLeftOffset - 1, getCurrentAppearance()->menuItemHeight + 1);
} else {
_u8g2.drawBox(cursorLeftOffset - 1, pointerPosition - 1, _menuItemFont[menuItemFontSize].width + 1, getCurrentAppearance()->menuItemHeight + 1);
}
_u8g2.setDrawColor(1);
}
void GEM_u8g2::nextEditValueDigit() {
GEMItem* menuItemTmp = _menuPageCurrent->getCurrentMenuItem();
char chr = _valueString[_editValueVirtualCursorPosition];
byte code = (byte)chr;
if (_editValueType == GEM_VAL_CHAR) {
if (menuItemTmp->adjustedAsciiOrder) {
switch (code) {
case 0:
code = GEM_CHAR_CODE_a;
break;
case GEM_CHAR_CODE_SPACE:
code = GEM_CHAR_CODE_a;
break;
case GEM_CHAR_CODE_ACCENT:
code = GEM_CHAR_CODE_SPACE;
break;
case GEM_CHAR_CODE_TILDA:
code = GEM_CHAR_CODE_BANG;
break;
default:
code++;
break;
}
} else {
switch (code) {
case 0:
code = GEM_CHAR_CODE_SPACE;
break;
case GEM_CHAR_CODE_TILDA:
code = GEM_CHAR_CODE_SPACE;
break;
/*
// WIP for Cyrillic values support
case GEM_CHAR_CODE_TILDA:
code = _cyrillicEnabled ? GEM_CHAR_CODE_CYR_A : GEM_CHAR_CODE_SPACE;
break;
case GEM_CHAR_CODE_CYR_YA_SM:
code = GEM_CHAR_CODE_SPACE;
break;
case GEM_CHAR_CODE_CYR_E:
code = GEM_CHAR_CODE_CYR_YO;
break;
case GEM_CHAR_CODE_CYR_YO:
code = GEM_CHAR_CODE_CYR_E + 1;
break;
case GEM_CHAR_CODE_CYR_E_SM:
code = GEM_CHAR_CODE_CYR_YO_SM;
break;
case GEM_CHAR_CODE_CYR_YO_SM:
code = GEM_CHAR_CODE_CYR_E_SM + 1;
break;
*/
default:
code++;
break;
}
}
} else {
switch (code) {
case 0:
code = GEM_CHAR_CODE_0;
break;
case GEM_CHAR_CODE_9:
code = (_editValueCursorPosition == 0 && (_editValueType == GEM_VAL_INTEGER || _editValueType == GEM_VAL_FLOAT || _editValueType == GEM_VAL_DOUBLE)) ? GEM_CHAR_CODE_MINUS : GEM_CHAR_CODE_SPACE;
break;
case GEM_CHAR_CODE_MINUS:
code = GEM_CHAR_CODE_SPACE;
break;
case GEM_CHAR_CODE_SPACE:
code = (_editValueCursorPosition != 0 && (_editValueType == GEM_VAL_FLOAT || _editValueType == GEM_VAL_DOUBLE)) ? GEM_CHAR_CODE_DOT : GEM_CHAR_CODE_0;
break;
case GEM_CHAR_CODE_DOT:
code = GEM_CHAR_CODE_0;
break;
default:
code++;
break;
}
}
drawEditValueDigit(code);
}
void GEM_u8g2::prevEditValueDigit() {
GEMItem* menuItemTmp = _menuPageCurrent->getCurrentMenuItem();
char chr = _valueString[_editValueVirtualCursorPosition];
byte code = (byte)chr;
if (_editValueType == GEM_VAL_CHAR) {
if (menuItemTmp->adjustedAsciiOrder) {
switch (code) {
case 0:
code = GEM_CHAR_CODE_ACCENT;
break;
case GEM_CHAR_CODE_BANG:
code = GEM_CHAR_CODE_TILDA;
break;
case GEM_CHAR_CODE_a:
code = GEM_CHAR_CODE_SPACE;
break;
case GEM_CHAR_CODE_SPACE:
code = GEM_CHAR_CODE_ACCENT;
break;
default:
code--;
break;
}
} else {
switch (code) {
case 0:
code = GEM_CHAR_CODE_TILDA;
break;
case GEM_CHAR_CODE_SPACE:
code = GEM_CHAR_CODE_TILDA;
break;
/*
// WIP for Cyrillic values support
case 0:
code = _cyrillicEnabled ? GEM_CHAR_CODE_CYR_YA_SM : GEM_CHAR_CODE_TILDA;
break;
case GEM_CHAR_CODE_SPACE:
code = _cyrillicEnabled ? GEM_CHAR_CODE_CYR_YA_SM : GEM_CHAR_CODE_TILDA;
break;
case GEM_CHAR_CODE_CYR_A:
code = GEM_CHAR_CODE_TILDA;
break;
case GEM_CHAR_CODE_CYR_E + 1:
code = GEM_CHAR_CODE_CYR_YO;
break;
case GEM_CHAR_CODE_CYR_YO:
code = GEM_CHAR_CODE_CYR_E;
break;
case GEM_CHAR_CODE_CYR_E_SM + 1:
code = GEM_CHAR_CODE_CYR_YO_SM;
break;
case GEM_CHAR_CODE_CYR_YO_SM:
code = GEM_CHAR_CODE_CYR_E_SM;
break;
*/
default:
code--;
break;
}
}
} else {
switch (code) {
case 0:
code = (_editValueCursorPosition == 0 && (_editValueType == GEM_VAL_INTEGER || _editValueType == GEM_VAL_FLOAT || _editValueType == GEM_VAL_DOUBLE)) ? GEM_CHAR_CODE_MINUS : GEM_CHAR_CODE_9;
break;
case GEM_CHAR_CODE_MINUS:
code = GEM_CHAR_CODE_9;
break;
case GEM_CHAR_CODE_0:
code = (_editValueCursorPosition != 0 && (_editValueType == GEM_VAL_FLOAT || _editValueType == GEM_VAL_DOUBLE)) ? GEM_CHAR_CODE_DOT : GEM_CHAR_CODE_SPACE;
break;
case GEM_CHAR_CODE_SPACE:
code = (_editValueCursorPosition == 0 && (_editValueType == GEM_VAL_INTEGER || _editValueType == GEM_VAL_FLOAT || _editValueType == GEM_VAL_DOUBLE)) ? GEM_CHAR_CODE_MINUS : GEM_CHAR_CODE_9;
break;
case GEM_CHAR_CODE_DOT:
code = GEM_CHAR_CODE_SPACE;
break;
default:
code--;
break;
}
}
drawEditValueDigit(code);
}
#ifdef GEM_SUPPORT_PREVIEW_CALLBACKS
void GEM_u8g2::callPreviewCallback(bool reset) {
GEMItem* menuItemTmp = _menuPageCurrent->getCurrentMenuItem();
if (menuItemTmp->previewCallbackAction != nullptr) {
GEMPreviewCallbackData previewCallbackData;
previewCallbackData.callbackData = menuItemTmp->callbackData;
if (!reset) {
previewCallbackData.type = menuItemTmp->linkedType;
switch (menuItemTmp->linkedType) {