-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUImanager.cpp
1517 lines (1367 loc) · 52.3 KB
/
UImanager.cpp
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
/**************************************************************************/
/*!
@file UImanager.cpp
Arduino K197Display sketch
Copyright (C) 2022 by ALX2009
License: MIT (see LICENSE)
This file is part of the Arduino K197Display sketch, please see
https://github.com/alx2009/K197Display for more information
This file implements the UImanager class, see UImanager.h for the class
definition uses u8g2 library to control the SSD1322 display module via HW SPI
Because of the size of the display, the 16 bit mode of u8g2 MUST be used
This is not the default for the AVR architecture (despite the new chips having
more than enough RAM for the job), so one header file in the u8g2 must be
edited to uncomment the "#define U8G2_16BIT" statement. See also
https://github.com/olikraus/u8g2/blob/master/doc/faq.txt and search for
U8G2_16BIT
If 16 bit mode is not active we will trigger a compilation error as otherwise
it would be rather difficult to figure out why it does not diplay correctly...
*/
/**************************************************************************/
/*=================Font Usage=======================
Fonts used in the application:
u8g2_font_5x7_mr ==> terminal window, local T, Bluetooth status
u8g2_font_6x12_mr ==> BAT, all menus
u8g2_font_8x13_mr ==> AUTO, REL, dB, STO, RCL, Cal, RMT, most annunciators
in split screen
u8g2_font_9x15_m_symbols ==> meas. unit, AC, Split screen:
AC u8g2_font_inr30_mr ==> main message u8g2_font_inr16_mr ==> main
message in minmax mode
mr ==> monospace restricted
m ==> monospace
*/
#include <Arduino.h>
#include <U8g2lib.h>
#define DEFAULT_CONTRAST \
0x00 ///< defines the default contrast. Can be changed via serial port
#ifndef U8G2_16BIT // Trigger an error if u8g2 is not in 16 bit mode, as
// explained above
#error you MUST define U8G2_16BIT in u8g2.h ! See comment at the top of UImanager.cpp
#endif // U8G2_16BIT
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
#include "UImenu.h"
UImenu UImainMenu(130, true); ///< the main menu for this application
UImenu UIlogMenu(130); ///< the submenu to set logging options
UImenu UIgraphMenu(130); ///< the submenu to set graph options
#include "BTmanager.h"
#include "K197PushButtons.h"
#include "UImanager.h"
#include "debugUtil.h"
#include "dxUtil.h"
#include "pinout.h"
UImanager uiman; ///< defines the UImanager instance to use in the application
/*!
@brief Constructor, see
https://github.com/olikraus/u8g2/wiki/setup_tutorial
@return no return value
*/
// At the moment we do not use the RESET pin. It does seem to work correctly
// without.
#ifdef OLED_DC // 4Wire SPI
U8G2_SSD1322_NHD_256X64_F_4W_HW_SPI
u8g2(U8G2_R0, OLED_SS,
OLED_DC /*,reset_pin*/); ///< u8g2 object. See pinout.h for pin definition.
#else // 3Wire SPI
U8G2_SSD1322_NHD_256X64_F_3W_HW_SPI
u8g2(U8G2_R0, OLED_SS
/*,reset_pin*/); ///< u8g2 object. See pinout.h for pin definition.
#endif
#define U8LOG_WIDTH 25 ///< Size of the log window
#define U8LOG_HEIGHT 5 ///< Height of the log window
uint8_t u8log_buffer[U8LOG_WIDTH * U8LOG_HEIGHT]; ///< buffer for the log window
U8G2LOG u8g2log; ///< the log window
// ***************************************************************************************
// Graphics utility functions
// ***************************************************************************************
/*!
@brief utility function: draw an horizontal dotted line
@param x0 Coordinate x of the starting point
@param y0 Coordinate y (only one y coord needed, line is horizontal)
@param x1 Coordinate x of the ending point
@param dotsize the size of the "dot" (default 10)
@param dotDistance the distance between "dots" (default 20)
*/
void drawDottedHLine(uint16_t x0, uint16_t y0, uint16_t x1,
uint16_t dotsize = 10, uint16_t dotDistance = 20) {
if (dotsize == 0)
dotsize = 1;
if (dotDistance == 0)
dotDistance = dotsize * 5;
uint16_t xdot;
do {
xdot = x0 + dotsize;
if (xdot > x1)
xdot = x1;
u8g2.drawLine(x0, y0, xdot, y0);
x0 += dotDistance;
} while (x0 <= x1);
}
// ***************************************************************************************
// UI Setup
// ***************************************************************************************
/*!
@brief this function is intended to include any drawing command that may be
needed the very first time the display is used
Called from setup, it should not be called from elsewhere
*/
void setup_draw(void) {
u8g2.setFont(
u8g2_font_inr30_mr); // width =25 points (7 characters=175 points)
u8g2.setFontMode(0);
u8g2.setDrawColor(1);
u8g2.setFontPosTop();
u8g2.setFontRefHeightExtendedText();
u8g2.setFontDirection(0);
CHECK_FREE_STACK();
}
/*!
@brief setup the display and clears the screen. Must be called before any
other member function
*/
void UImanager::setup() {
pinMode(OLED_MOSI, OUTPUT); // Needed to work around a bug in the micro or
// dxCore with certain swap options
bool pmap = SPI.swap(OLED_SPI_SWAP_OPTION); // See pinout.h for pin definition
if (!pmap) {
DebugOut.println(F("SPI map!"));
DebugOut.flush();
while (true)
;
}
u8g2.setBusClock(12000000);
u8g2.begin();
setContrast(DEFAULT_CONTRAST);
u8g2.enableUTF8Print();
u8g2log.begin(U8LOG_WIDTH, U8LOG_HEIGHT, u8log_buffer);
u8g2.clearBuffer();
setup_draw();
u8g2.sendBuffer();
setupMenus();
}
// ***************************************************************************************
// Display update
// ***************************************************************************************
/*!
@brief update the display.
@details This function will not cause the K197device object to read new
data, it will use whatever data is already stored in the object.
Therefore, it should not be called before the first data has been received
from the K197/197A
If you want to add an initial scren/text, the best way would be to add this
to setup();
@param stepDoodle if true and the doodle animation is enabled, the animation
is updated
*/
void UImanager::updateDisplay(bool stepDoodle) {
u8g2.clearBuffer(); // Clear display area
if (k197dev.isNotCal() && isSplitScreen())
updateSplitScreen();
else if (k197dev.isCal() || (getScreenMode() == K197sc_normal))
updateNormalScreen();
else if (getScreenMode() == K197sc_minmax)
updateMinMaxScreen();
else
updateGraphScreen();
displayDoodle(doodle_x_coord, doodle_y_coord, stepDoodle);
u8g2.sendBuffer();
CHECK_FREE_STACK();
}
/*!
@brief update the display, used when in debug and other modes with split
screen.
@details this screen always show the current measured value, even if we are
in hold mode This is by design so that it is possible to see the current
value without exiting the hold mode
*/
void UImanager::updateSplitScreen() {
// temporary buffer used for number formatting
char buf[K197_RAW_MSG_SIZE + 1]; // +1 needed to account for '.'
u8g2_uint_t x = 140;
u8g2_uint_t y = 5;
u8g2.setFont(u8g2_font_8x13_mr);
u8g2.setCursor(x, y);
if (k197dev.isAuto())
u8g2.print(F("AUTO "));
if (k197dev.isBAT())
u8g2.print(F("BAT "));
if (k197dev.isREL())
u8g2.print(F("REL "));
if (k197dev.isCal())
u8g2.print(F("Cal "));
y += u8g2.getMaxCharHeight();
u8g2.setCursor(x, y);
u8g2.setFont(u8g2_font_9x15_m_symbols);
if (k197dev.isNumeric()) {
u8g2.print(formatNumber(buf, k197dev.getValue()));
} else
u8g2.print(k197dev.getRawMessage());
u8g2.print(CH_SPACE);
u8g2.setFont(u8g2_font_9x15_m_symbols);
u8g2.print(k197dev.getUnit(true));
y += u8g2.getMaxCharHeight();
u8g2.setFont(u8g2_font_9x15_m_symbols);
if (k197dev.isAC())
u8g2.print(F(" AC "));
u8g2.setCursor(x, y);
u8g2.setFont(u8g2_font_8x13_mr);
if (k197dev.isSTO())
u8g2.print(F("STO "));
if (k197dev.isRCL())
u8g2.print(F("RCL "));
if (k197dev.isRMT())
u8g2.print(F("RMT "));
if (isSplitScreen() && !isMenuVisible()) { // Show the debug log
u8g2.setFont(u8g2_font_5x7_mr); // set the font for the terminal window
u8g2.drawLog(0, 0, u8g2log); // draw the terminal window on the display
} else { // For all other modes we show the settings menu when in split mode
UIwindow::getcurrentWindow()->draw(&u8g2, 0, 10);
}
CHECK_FREE_STACK();
}
/*!
@brief update the display, used when in normal screen mode. See also
updateDisplay().
*/
void UImanager::updateNormalScreen() {
u8g2.setFont(
u8g2_font_inr30_mr); // width =25 points (7 characters=175 points)
const unsigned int xraw = 49;
const unsigned int yraw = 15;
const unsigned int dpsz_x = 3; // decimal point size in x direction
const unsigned int dpsz_y = 3; // decimal point size in y direction
const unsigned int dphsz_x = 2; // decimal point "half size" in x direction
const unsigned int dphsz_y = 2; // decimal point "half size" in y direction
bool hold = k197dev.getDisplayHold();
u8g2.drawStr(xraw, yraw, k197dev.getRawMessage(hold));
for (byte i = 1; i <= 7; i++) {
if (k197dev.isDecPointOn(i, hold)) {
u8g2.drawBox(xraw + i * u8g2.getMaxCharWidth() - dphsz_x,
yraw + u8g2.getAscent() - dphsz_y, dpsz_x, dpsz_y);
}
}
// set the unit
u8g2.setFont(u8g2_font_9x15_m_symbols);
const unsigned int xunit = 229;
const unsigned int yunit = 20;
u8g2.setCursor(xunit, yunit);
u8g2.print(k197dev.getUnit(false, hold));
// set the AC/DC indicator
u8g2.setFont(u8g2_font_9x15_m_symbols);
const unsigned int xac = xraw + 3;
const unsigned int yac = 40;
u8g2.setCursor(xac, yac);
if (k197dev.isAC(hold))
u8g2.print(F("AC"));
// set the other announciators
u8g2.setFont(u8g2_font_8x13_mr);
unsigned int x = 0;
unsigned int y = 5;
u8g2.setCursor(x, y);
if (k197dev.isAuto())
u8g2.print(F("AUTO"));
x = u8g2.tx;
x += u8g2.getMaxCharWidth() * 2;
u8g2.setFont(u8g2_font_6x12_mr);
u8g2.setCursor(x, y);
if (k197dev.isBAT())
u8g2.print(F("BAT"));
u8g2.setFont(u8g2_font_8x13_mr);
y += u8g2.getMaxCharHeight();
x = 0;
u8g2.setCursor(x, y);
if (k197dev.isREL(hold))
u8g2.print(F("REL"));
x += u8g2.getMaxCharWidth() * 3;
x += (u8g2.getMaxCharWidth() / 2);
u8g2.setCursor(x, y);
if (k197dev.isdB(hold))
u8g2.print(F("dB"));
y += u8g2.getMaxCharHeight();
x = 0;
u8g2.setCursor(x, y);
if (hold) {
u8g2.print(F("HOLD"));
// not enough space, go to the next line
x = 0;
y = u8g2.tx + u8g2.getMaxCharHeight();
u8g2.setCursor(x, y);
}
if (k197dev.isSTO()) {
u8g2.print(F("STO"));
x = u8g2.tx + (u8g2.getMaxCharWidth() / 2);
} else {
x = u8g2.tx + (u8g2.getMaxCharWidth() * 7 / 2);
}
if (!hold) { // then move to Next line now
x = 0;
y += u8g2.getMaxCharHeight();
}
u8g2.setCursor(x, y);
if (k197dev.isRCL())
u8g2.print(F("RCL"));
x = 229;
y = 0;
u8g2.setCursor(x, y);
if (k197dev.isCal())
u8g2.print(F("Cal"));
y += u8g2.getMaxCharHeight() * 3;
u8g2.setCursor(x, y);
if (k197dev.isRMT())
u8g2.print(F("RMT"));
x = 140;
y = 2;
u8g2.setCursor(x, y);
u8g2.setFont(u8g2_font_5x7_mr);
if (k197dev.isTKModeActive(hold)) { // Display local temperature
char buf[K197_RAW_MSG_SIZE + 1];
dtostrf(k197dev.getTColdJunction(hold), K197_RAW_MSG_SIZE, 2, buf);
u8g2.print(buf);
u8g2.print(k197dev.getUnit(false, hold));
}
updateBtStatus();
CHECK_FREE_STACK();
}
/*!
@brief update the display, used when in minmax screen mode. See also
updateDisplay().
*/
void UImanager::updateMinMaxScreen() {
// temporary buffer used for number formatting
char buf[K197_RAW_MSG_SIZE + 1]; // +1 needed to account for '.'
u8g2.setFont(
u8g2_font_inr16_mr); // width =25 points (7 characters=175 points)
const unsigned int xraw = 130; // x coordinate for raw_msg
const unsigned int yraw = 15; // y coordinate for raw_msg
const unsigned int dpsz_x = 3; // decimal point size in x direction
const unsigned int dpsz_y = 3; // decimal point size in y direction
const unsigned int dphsz_x = 2; // decimal point "half size" in x direction
const unsigned int dphsz_y = 2; // decimal point "half size" in y direction
const unsigned int xstat = 28; // x coordinate for the statistics
const unsigned int ystat = 5; // y coordinate for the statistics (1st line)
const unsigned int xunit = 229; // x coordinate for the unit
const unsigned int yunit = 20; // y coordinate for the unit
bool hold = k197dev.getDisplayHold();
u8g2.drawStr(xraw, yraw, k197dev.getRawMessage(hold));
for (byte i = 1; i <= 7; i++) {
if (k197dev.isDecPointOn(i, hold)) {
u8g2.drawBox(xraw + i * u8g2.getMaxCharWidth() - dphsz_x,
yraw + u8g2.getAscent() - dphsz_y, dpsz_x, dpsz_y);
}
}
// set the unit
u8g2.setFont(u8g2_font_9x15_m_symbols);
u8g2.setCursor(xunit, yunit);
u8g2.print(k197dev.getUnit(true, hold));
// set the AC/DC indicator
u8g2.setFont(u8g2_font_9x15_m_symbols);
int char_height_9x15 = u8g2.getMaxCharHeight(); // needed later on
const unsigned int xac = 229;
const unsigned int yac = 35;
u8g2.setCursor(xac, yac);
if (k197dev.isAC(hold))
u8g2.print(F("AC"));
// set the REL announciator
u8g2.setFont(u8g2_font_6x12_mr);
unsigned int x = 0;
unsigned int y = 5;
u8g2.setCursor(x, y);
if (k197dev.isREL(hold))
u8g2.print(F("REL"));
// Write Min/average/Max labels
u8g2.setFont(u8g2_font_5x7_mr);
x = xstat;
y = ystat;
u8g2.setCursor(x, y);
u8g2.print(F("Max "));
y += char_height_9x15;
u8g2.setCursor(x, y);
u8g2.print(F("Avg "));
y += char_height_9x15;
u8g2.setCursor(x, y);
u8g2.print(F("Min "));
u8g2.setFont(u8g2_font_9x15_m_symbols);
x = u8g2.tx;
y = 3;
u8g2.setCursor(x, y);
u8g2.print(formatNumber(buf, k197dev.getMax(hold)));
y += char_height_9x15;
u8g2.setCursor(x, y);
u8g2.print(formatNumber(buf, k197dev.getAverage(hold)));
y += char_height_9x15;
u8g2.setCursor(x, y);
u8g2.print(formatNumber(buf, k197dev.getMin(hold)));
x = 170;
y = 2;
u8g2.setCursor(x, y);
u8g2.setFont(u8g2_font_5x7_mr);
if (k197dev.isTKModeActive(hold)) { // Display local temperature
char buf[K197_RAW_MSG_SIZE + 1];
dtostrf(k197dev.getTColdJunction(hold), K197_RAW_MSG_SIZE, 2, buf);
u8g2.print(buf);
u8g2.print(k197dev.getUnit(false, hold));
}
u8g2.setFont(u8g2_font_8x13_mr);
x = 0;
y = 5 + u8g2.getMaxCharHeight() * 2;
u8g2.setCursor(x, y);
u8g2.setFont(u8g2_font_5x7_mr);
if (hold)
u8g2.print(F("HOLD"));
// set the other announciators
x = 0;
y = 63 - u8g2.getMaxCharHeight() - 3;
u8g2.setCursor(x, y);
if (k197dev.isSTO())
u8g2.print(F("STO "));
if (k197dev.isRCL())
u8g2.print(F("RCL "));
if (k197dev.isBAT())
u8g2.print(F("BAT "));
if (k197dev.isRMT())
u8g2.print(F("RMT "));
if (k197dev.isCal())
u8g2.print(F("Cal "));
if (k197dev.isOvrange())
u8g2.print(F("ovRange "));
if (k197dev.isAuto())
u8g2.print(F("AUTO"));
CHECK_FREE_STACK();
}
/*!
@brief display the BT module status (detected or not detected)
@param present true if a BT module is detected, false otherwise
@param connected true if a BT connection is detected, false otherwise
*/
void UImanager::updateBtStatus() {
unsigned int x = 95;
unsigned int y = 2;
u8g2.setCursor(x, y);
u8g2.setFont(u8g2_font_5x7_mr);
if (BTman.isPresent()) {
u8g2.print(F("bt "));
}
x += u8g2.getStrWidth(" ");
u8g2.setCursor(x, y);
bool connected = BTman.isConnected();
if (connected && isLogging()) {
u8g2.print(F("<=>"));
} else if (connected) {
u8g2.print(F("<->"));
}
CHECK_FREE_STACK();
}
// ***************************************************************************************
// Screen mode & clear screen
// ***************************************************************************************
/*!
@brief set the screen mode
@details Three modes are defined: normal mode, menu mode and debug mode.
In debug and menu mode the measurements are displays on the right of the
screen (split screen), while the left part is reserved for debug messages and
menu items respectively. Normal mode is a full screen mode equivalent to the
original K197 display. As the name suggest debug mode is intended for
debugging the code that interacts with the serial port/bluetooth module
itself, so that Serial cannot be used.
@param mode the new display mode. Only the modes defined in K197screenMode
< 0x0f are valid. Using an invalid mode will have no effect
*/
void UImanager::setScreenMode(K197screenMode mode) {
if ((mode <= 0) || (mode > K197sc_graph))
return;
screen_mode =
(K197screenMode)(screen_mode &
K197sc_AttributesBitMask); // clear current screen mode
screen_mode = (K197screenMode)(screen_mode | mode); // set the mode bits to
// enter the new mode
clearScreen();
}
/*!
@brief clear the display
@details This is done automatically when the screen mode changes, there should
be no need to call this function elsewhere
*/
void UImanager::clearScreen() {
u8g2.clearBuffer();
u8g2.sendBuffer();
CHECK_FREE_STACK();
}
// ***************************************************************************************
// Message box definitions
// ***************************************************************************************
DEF_MESSAGE_BOX(EEPROM_save_msg_box, 100,
"config saved"); ///< Config saved message box
DEF_MESSAGE_BOX(EEPROM_reload_msg_box, 100,
"config reloaded"); ///< Config reloaded msg. box
DEF_MESSAGE_BOX(ERROR_msg_box, 100, "Error (see log)"); ///< Error message box
// ***************************************************************************************
// Menu definition/handling
// ***************************************************************************************
DEF_MENU_CLOSE(closeMenu, 15,
"< Back"); ///< Menu close action (used in multiple menus)
DEF_MENU_ACTION(
exitMenu, 15, "Exit",
uiman.showFullScreen();); ///< Menu close action (used in multiple menus)
// Main menu
DEF_MENU_SEPARATOR(mainSeparator0, 15, "< Options >"); ///< Menu separator
DEF_MENU_BOOL(additionalModes, 15, "Extra Modes"); ///< Menu input
DEF_MENU_BOOL(reassignStoRcl, 15, "Reassign STO/RCL"); ///< Menu input
DEF_MENU_OPEN(btDatalog, 15, "Data logger >>>", &UIlogMenu); ///< Open submenu
DEF_MENU_OPEN(btGraphOpt, 15, "Graph opt. >>>",
&UIgraphMenu); ///< Open submenu
DEF_MENU_BOOL_ACT(showDoodle, 15, "Doodle",
if (!getValue()) u8g2.drawGlyph(UImanager::doodle_x_coord,
UImanager::doodle_y_coord,
CH_SPACE);); ///< Menu input
DEF_MENU_BYTE_ACT(contrastCtrl, 15, "Contrast",
u8g2.setContrast(getValue());); ///< set contrast
DEF_MENU_ACTION(
saveSettings, 15, "Save settings",
if (permadata::store_to_EEPROM()) { EEPROM_save_msg_box.show(); } else {
ERROR_msg_box.show();
}); ///< save config to EEPROM and show result
DEF_MENU_ACTION(
reloadSettings, 15, "Reload settings",
if (permadata::retrieve_from_EEPROM()) {
EEPROM_reload_msg_box.show();
} else {
ERROR_msg_box.show();
}); ///< load config from EEPROM and show result
DEF_MENU_ACTION(openLog, 15, "Show log",
REPORT_FREE_STACK();
DebugOut.println(); uiman.showDebugLog();); ///< show debug log
DEF_MENU_ACTION(resetAVR, 15, "RESET",
_PROTECTED_WRITE(RSTCTRL.SWRR, 1);); ///< Menu input
UImenuItem *mainMenuItems[] = {
&mainSeparator0, &additionalModes, &reassignStoRcl,
&btDatalog, &btGraphOpt, &showDoodle,
&contrastCtrl, &exitMenu, &saveSettings,
&reloadSettings, &openLog, &resetAVR}; ///< Root menu items
// Logging/statistics menu
DEF_MENU_SEPARATOR(logSeparator0, 15, "< BT Datalogging >"); ///< Menu separator
DEF_MENU_BOOL(logEnable, 15, "Enabled"); ///< Menu input
DEF_MENU_BYTE(logSkip, 15, "Samples to skip"); ///< Menu input
DEF_MENU_BOOL(logSplitUnit, 15, "Split unit"); ///< Menu input
DEF_MENU_BOOL(logTimestamp, 15, "Log tstamp"); ///< Menu input
DEF_MENU_BOOL(logTamb, 15, "Incl. Tamb"); ///< Menu input
DEF_MENU_BOOL(logStat, 15, "Incl. Statistics"); ///< Menu input
DEF_MENU_BOOL(logError, 15, "Log errors"); ///< Menu input
DEF_MENU_SEPARATOR(logSeparator1, 15, "< Statistics >"); ///< Menu separator
DEF_MENU_BYTE_ACT(logStatSamples, 15, "Num. Samples",
k197dev.setNsamples(getValue());); ///< Menu input
UImenuItem *logMenuItems[] = {
&logSeparator0, &logEnable, &logSkip, &logSplitUnit, &logTimestamp,
&logTamb, &logStat, &logError, &logSeparator1, &logStatSamples,
&closeMenu, &exitMenu}; ///< Datalog menu items
// Graph menu
DEF_MENU_SEPARATOR(graphSeparator0, 15,
"< Graph options >"); ///< Menu separator
DEF_MENU_OPTION(opt_gr_type_lines, OPT_GRAPH_TYPE_LINES, 0,
"Lines"); ///< Menu input
DEF_MENU_OPTION(opt_gr_type_dots, OPT_GRAPH_TYPE_DOTS, 1,
"Dots"); ///< Menu input
DEF_MENU_OPTION_INPUT(opt_gr_type, 15, "Graph type", OPT(opt_gr_type_lines),
OPT(opt_gr_type_dots)); ///< Menu input
DEF_MENU_SEPARATOR(graphSeparator1, 15, "< Y axis >"); ///< Menu separator
DEF_MENU_BOOL_ACT(gr_yscale_full_range, 15, "Full range",
k197dev.setGraphFullRange(getValue());); ///< Menu input
BIND_MENU_OPTION(opt_gr_yscale_max, k197graph_yscale_zoom,
"zoom"); ///< Menu input
BIND_MENU_OPTION(opt_gr_yscale_zero, k197graph_yscale_zero,
"Incl. 0"); ///< Menu input
BIND_MENU_OPTION(opt_gr_yscale_prefsym, k197graph_yscale_prefsym,
"Symmetric"); ///< Menu input
BIND_MENU_OPTION(opt_gr_yscale_0sym, k197graph_yscale_0sym,
"0+symm"); ///< Menu input
BIND_MENU_OPTION(opt_gr_yscale_forcesym, k197graph_yscale_forcesym,
"Force symm."); ///< Menu input
BIND_MENU_OPTION(opt_gr_yscale_0forcesym, k197graph_yscale_0forcesym,
"0+force symm."); ///< Menu input
DEF_MENU_ENUM_INPUT(k197graph_yscale_opt, opt_gr_yscale, 15, "Y axis",
OPT(opt_gr_yscale_max), OPT(opt_gr_yscale_zero),
OPT(opt_gr_yscale_prefsym), OPT(opt_gr_yscale_0sym),
OPT(opt_gr_yscale_forcesym),
OPT(opt_gr_yscale_0forcesym)); ///< Menu input
DEF_MENU_BOOL(gr_yscale_show0, 15, "Show y=0"); ///< Menu input
DEF_MENU_SEPARATOR(graphSeparator2, 15, "< X axis >"); ///< Menu separator
DEF_MENU_BOOL_ACT(gr_xscale_autosample, 15, "Auto sample",
k197dev.setAutosample(getValue());); ///< Menu input
DEF_MENU_BYTE_SETGET(gr_sample_time, 15, "Sample time (s)",
k197dev.setGraphPeriod(newValue);
,
return k197dev.getGraphPeriod();); ///< Menu input
UImenuItem *graphMenuItems[] =
{&graphSeparator0, &opt_gr_type,
&graphSeparator1, &gr_yscale_full_range,
&opt_gr_yscale, &gr_yscale_show0,
&graphSeparator2, &gr_xscale_autosample,
&gr_sample_time, &closeMenu,
&exitMenu}; ///< Collects all items in the graph menu
/*!
@brief set the display contrast
@details in addition to setting the contrast value, this method takes care
of keeping the contrast menu item in synch
@param value contrast value (0 to 255)
*/
void UImanager::setContrast(uint8_t value) {
u8g2.setContrast(value);
contrastCtrl.setValue(value);
CHECK_FREE_STACK();
}
/*!
@brief setup the menu
@details this method setup all the menus. It must be called before the
menu can be displayed.
*/
void UImanager::setupMenus() {
additionalModes.setValue(true);
reassignStoRcl.setValue(true);
showDoodle.setValue(true);
UImainMenu.items = mainMenuItems;
UImainMenu.num_items = sizeof(mainMenuItems) / sizeof(UImenuItem *);
UImainMenu.selectFirstItem();
logSkip.setValue(0);
logSplitUnit.setValue(false);
logTimestamp.setValue(true);
logTamb.setValue(true);
logStatSamples.setValue(k197dev.getNsamples());
UIlogMenu.items = logMenuItems;
UIlogMenu.num_items = sizeof(logMenuItems) / sizeof(UImenuItem *);
UIlogMenu.selectFirstItem();
UIgraphMenu.items = graphMenuItems;
UIgraphMenu.num_items = sizeof(graphMenuItems) / sizeof(UImenuItem *);
UIgraphMenu.selectFirstItem();
gr_yscale_full_range.setValue(true);
gr_yscale_full_range.change();
gr_xscale_autosample.setValue(k197dev.getAutosample());
permadata::retrieve_from_EEPROM(true);
}
// ***************************************************************************************
// Logging
// ***************************************************************************************
/*!
@brief set data logging to Serial
@param yesno true to enabl, false to disable
*/
void UImanager::setLogging(bool yesno) {
if (!yesno)
logskip_counter = 0;
logEnable.setValue(yesno);
CHECK_FREE_STACK();
}
/*!
@brief query data logging to Serial
@return returns true if logging is active
*/
bool UImanager::isLogging() { return logEnable.getValue(); }
/*!
@brief Utility function, print a ";" if the option logSplit is active,
otherwise a space
@details the options controls how the unit is handled when data is
imported into a spreadsheet
*/
inline void logU2U() {
if (logSplitUnit.getValue())
Serial.print(F(" ;"));
else
Serial.print(CH_SPACE);
}
/*!
@brief format a number
@details format a float so that it has the right lenght and the maximum
number of decimal digitas allowed in the available display space
@param buf a buffer where the formatted string should be written (size>=9)
@param f the number to be formatted
@return a nul terminated char array with the formatted number (same as
buf)
*/
const char *UImanager::formatNumber(char *buf, float f) {
if (f > 999999.0)
f = 999999.0;
else if (f < -999999.0)
f = -999999.0;
float f_abs = abs(f);
int ndec = 0;
if (f_abs <= 9.99999)
ndec = 5;
else if (f_abs <= 99.9999)
ndec = 4;
else if (f_abs <= 999.999)
ndec = 3;
else if (f_abs <= 9999.99)
ndec = 2;
else if (f_abs <= 99999.9)
ndec = 1;
// else we leave ndec=0
return dtostrf(f, K197_RAW_MSG_SIZE, ndec, buf);
}
/*!
@brief data logging to Serial
@details does the actual data logging when called. It has no effect if
datalogging is disabled or in no connection has been detected
*/
void UImanager::logData() {
// temporary buffer used for number formatting
char buf[K197_RAW_MSG_SIZE + 1]; // +1 needed to account for '.'
if (k197dev.isCal()) // No logging while in Cal mode
return;
if ((!logEnable.getValue()) || (!BTman.validconnection()))
return;
if (!k197dev.isNumeric()) {
if (!logError.getValue())
return;
}
if (logskip_counter < logSkip.getValue()) {
logskip_counter++;
return;
}
logskip_counter = 0;
if (logTimestamp.getValue()) {
Serial.print(millis());
logU2U();
Serial.print(F(" ms; "));
}
if (k197dev.isNumeric()) {
Serial.print(formatNumber(buf, k197dev.getValue()));
} else
Serial.print(k197dev.getRawMessage());
logU2U();
const __FlashStringHelper *unit = k197dev.getUnit(true);
Serial.print(unit);
if (k197dev.isAC())
Serial.print(F(" AC"));
if (k197dev.isTKModeActive() && logTamb.getValue()) {
Serial.print(F("; "));
Serial.print(k197dev.getTColdJunction());
logU2U();
Serial.print(unit);
}
if (logStat.getValue()) {
Serial.print(F("; "));
Serial.print(formatNumber(buf, k197dev.getMin()));
logU2U();
Serial.print(unit);
Serial.print(F("; "));
Serial.print(formatNumber(buf, k197dev.getAverage()));
logU2U();
Serial.print(unit);
Serial.print(F("; "));
Serial.print(formatNumber(buf, k197dev.getMax()));
logU2U();
Serial.print(unit);
}
Serial.println();
CHECK_FREE_STACK();
}
// ***************************************************************************************
// Display functions that have dependencies on menu options
// ***************************************************************************************
/*!
@brief display & update a small animation
@details This function is used to display a small animation at the selected
coordinate It should be called any time the display is updated due to a new
messdurement being taken. In this way the user see that the voltmeter SW is
running, even if the UI is not updated (e.g. in hold mode). Any stuttering
would instead indicate that the display card is skipping measurements (e.g.
when it's busy updating a slow bluetooth connection) Note that this function
changes the active font, however it does not change the cursor coordinates
@param x the x coordinate where to display the animation (upper-left corner)
@param y the y coordinate where to display the animation (upper-left corner)
@param stepDoodle if false the doodle animation is not updated
*/
void UImanager::displayDoodle(u8g2_uint_t x, u8g2_uint_t y, bool stepDoodle) {
static byte phase = 0;
if (!showDoodle.getValue())
return;
u8g2.setFont(u8g2_font_9x15_m_symbols);
u8g2.drawGlyph(x, y, ((u8g2_uint_t)0x25f4) + phase);
if (!stepDoodle)
return;
if (--phase > 3)
phase = 3;
}
// ***************************************************************************************
// Graph screen handling
// ***************************************************************************************
static k197_display_graph_type k197graph;
// 0 1 2 3 4 5 6
static const char prefix[] = {
'n', 'u', 'm', ' ', 'k', 'M', 'G'}; ///< Lookup table for unit prefixes
/*!
@brief get the unit prefix corresponding to a power of 10
@details for example, pow in the range 6-8 (1 000 000-100 000 000) returns
'M'
@param pow10 the power of 10
@return the unit prefix for that range of powers
*/
static inline char getPrefix(int8_t pow10) {
int8_t index = pow10 >= 0 ? pow10 / 3 + 3 : (pow10 + 1) / 3 + 2;
return prefix[index];
}
/*!
@brief get the zeroes that must be added to the prefix (see also getPrefix)
@details for example, pow = 7 (10 000 000) returns 10.
When the prefix returned by getPrefix(7) is added, we have 10M
@param pow10 the power of 10
@return the unit prefix for that range of powers
*/
static inline int8_t getZeroes(int8_t pow10) {
return pow10 >= 0 ? pow10 % 3 : 2 + ((pow10 + 1) % 3);
}
/*!
@brief Utility function, print the label for the Y axis
@param l the label to print
*/
static void printYLabel(k197graph_label_type l, bool hold) {
int8_t pow10_effective = l.pow10 + k197dev.getUnitPow10(hold);
u8g2.print(l.mult);
int8_t nzeroes = getZeroes(pow10_effective);
for (uint8_t i = 0; i < nzeroes; i++) {
u8g2.print('0');
}
u8g2.print(getPrefix(pow10_effective));
}
/*!
@brief Utility function, print the label for the X and Y axis
@param l the label to print for the Y axis
@param nseconds the value in seconds to print for the X axis
*/
static void printXYLabel(k197graph_label_type l, uint16_t nseconds, bool hold) {
bool hasHours = false;
bool hasMinutes = false;
if (nseconds >= 3600) {
u8g2.print(nseconds / 3600);
u8g2.print('h');
nseconds = nseconds % 3600;
hasHours = true;
}
if (nseconds >= (hasHours ? 60 : 901)) {
u8g2.print(nseconds / 60);
u8g2.print('\'');
nseconds = nseconds % 60;
hasMinutes = true;
}
if (nseconds > 0) {
u8g2.print(nseconds);
u8g2.print((hasHours || hasMinutes) ? '\"' : 's');
}
u8g2.print('/'); // separator between x and y labels
int8_t pow10_effective = l.pow10 + k197dev.getUnitPow10(hold);
u8g2.print(l.mult);
int8_t nzeroes = getZeroes(pow10_effective);
for (uint8_t i = 0; i < nzeroes; i++) {
u8g2.print('0');
}
u8g2.print(getPrefix(pow10_effective));
}
/*!
@brief draw a marker at a specific point in the graph
@details: can print one of the following marker types:
- CURSOR_A
- CURSOR_B
Note that the font used to print the identity of the cursor marker (A or B)
must be set before calling this function
@param x the x coordinate of the point where to place the mark
@param y the y coordinate of the point where to place the mark
@param marker_type market type
*/
void UImanager::drawMarker(u8g2_uint_t x, u8g2_uint_t y, char marker_type) {
static const u8g2_uint_t marker_size = 7;
// k197_display_graph_type::x_size
u8g2_uint_t x0 = x < marker_size ? 0 : x - marker_size;
u8g2_uint_t x1 = k197_display_graph_type::x_size < (x + marker_size)
? k197_display_graph_type::x_size
: x + marker_size;
u8g2_uint_t y0 = y < marker_size ? 0 : y - marker_size;
u8g2_uint_t y1 = k197_display_graph_type::y_size < (y + marker_size)
? k197_display_graph_type::y_size
: y + marker_size;
switch (marker_type) {
case UImanager::CURSOR_A:
u8g2.drawLine(x0, y0, x, y);
u8g2.drawLine(x, y, x1, y1);
u8g2.drawLine(x0, y1, x, y);
u8g2.drawLine(x, y, x1, y0);
if (int(y1) > (k197_display_graph_type::y_size - u8g2.getMaxCharHeight())) {