-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstopwatch.cpp
More file actions
2835 lines (2435 loc) · 97.5 KB
/
stopwatch.cpp
File metadata and controls
2835 lines (2435 loc) · 97.5 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
/* implement a simple stopwatch with lap timer, countdown timer, daily alarm clock, a one-time
* alarm clock and a pair of Big Clocks.
*
* we maintain two separate states:
* SWDisplayState: a display state that indicates which page we are showing, if any.
* SWEngineState: an engine state that indicates what is running, if anything.
*/
#include "HamClock.h"
// countdown ranges, including flashing states
typedef enum {
SWCDS_OFF, // idle or dark
SWCDS_RUNOK, // more than SW_CD_WARNDT remaining
SWCDS_WARN, // > 0 but < SW_CD_WARNDT remaining
SWCDS_TIMEOUT, // timed out
} SWCDState;
// one-time alarm info
typedef struct {
time_t time; // set time(), always in UTC
bool utc; // user wants time in UTC, else DE
AlarmState state; // whether off, armed or ringing
SBox lbl_b; // main control label
SBox time_b; // main time display
SBox bc_alarm_b; // bc alarm control
char *title; // malloced title, if any
} AlarmOnce;
// daily alarm info
typedef struct {
uint16_t hrmn; // time as hr*60 + min, always in DE TZ
bool utc; // user wants time in UTC, else DE
AlarmState state; // whether off, armed or ringing
SBox lbl_b; // main control label
SBox time_b; // main time display
SBox bc_alarm_b; // bc alarm control
} AlarmDaily;
#define SW_CD_BLINKHZ 2 // LED warning blink rate, Hz
/* return all IO lines to benign state
*/
void SWresetIO()
{
disableBlinker (SW_CD_RED_PIN);
disableBlinker (SW_CD_GRN_PIN);
disableMCPPoller (SW_ALARMOUT_PIN);
disableMCPPoller (SW_ALARMOFF_PIN);
}
/* set the LEDs to indicate the given countdown range state
*/
static void setCDLEDState (SWCDState cds)
{
switch (cds) {
case SWCDS_OFF:
// both off
setBlinkerRate (SW_CD_GRN_PIN, BLINKER_OFF);
setBlinkerRate (SW_CD_RED_PIN, BLINKER_OFF);
break;
case SWCDS_RUNOK:
// green on
setBlinkerRate (SW_CD_GRN_PIN, BLINKER_ON);
setBlinkerRate (SW_CD_RED_PIN, BLINKER_OFF);
break;
case SWCDS_WARN:
// green blinking, red on
setBlinkerRate (SW_CD_GRN_PIN, SW_CD_BLINKHZ);
setBlinkerRate (SW_CD_RED_PIN, BLINKER_ON);
break;
case SWCDS_TIMEOUT:
// red blinking
setBlinkerRate (SW_CD_GRN_PIN, BLINKER_OFF);
setBlinkerRate (SW_CD_RED_PIN, SW_CD_BLINKHZ);
break;
}
}
/* return whether the countdown pin has toggled low,
* ie, this is an edge-triggered state in order that it does not stay active if used with a PTT switch.
*/
static bool countdownSwitchIsTrue()
{
static bool prev_pin_true;
static bool prev_pin_known;
// read pin, active low
bool pin_true = !readMCPPoller (SW_CD_RESET_PIN);
// init history if first time
if (!prev_pin_known) {
prev_pin_true = pin_true;
prev_pin_known = true;
}
// return whether went low
if (pin_true != prev_pin_true) {
prev_pin_true = pin_true;
return (pin_true);
} else
return (false);
}
/* return state of alarm clock reset switch.
*/
static bool alarmSwitchIsTrue(void)
{
// pin is active-low
return (!readMCPPoller (SW_ALARMOFF_PIN));
}
/* control the alarm clock output pin
*/
static void setAlarmPin (bool set)
{
mcp.digitalWrite (SW_ALARMOUT_PIN, set);
}
// stopwatch params
#define SW_NDIG 9 // number of display digits
#define SW_BG RA8875_BLACK // bg color
#define SW_ND 8 // number of digits
#define SW_DGAP 40 // gap between digits
#define SW_Y0 190 // upper left Y of all time digits
#define SW_DW 45 // digit width
#define SW_DH 100 // digit heigth
#define SW_X0 ((800-SW_ND*SW_DW-(SW_ND-1)*SW_DGAP)/2) // x coord of left-most digit to center
#define SW_LT 1 // line thickness
#define SW_PUNCR 3 // punctuation radius
#define SW_BAX 240 // control button A x
#define SW_BBX 440 // control button B x
#define SW_EXITX 670 // exit button x
#define SW_EXITY 420 // exit button y
#define SW_BCX 10 // big-clock button x
#define SW_BCY SW_EXITY // big-clock button y
#define SW_BY 350 // control button y
#define SW_BW 120 // button width
#define SW_BH 32 // button height
#define SW_CX SW_BAX // color scale x
#define SW_CY SW_EXITY // color scale y
#define SW_CW (SW_BBX+SW_BW-SW_CX) // color scale width
#define SW_CH SW_BH // color scale height
#define SW_HSV_S 200 // color scale HSV saturation, 0..255
#define SW_HSV_V 255 // color scale HSV value, 0..255
// alarm clock params
#define ALM_VGAP 10 // vertical gap between alarm controls
#define ALM_HGAP 10 // half central horizontal gap between alarm label and time
#define ALMD_Y0 15 // daily alarm buttons y
#define ALMD_LW 300 // daily alarm control button width
#define ALMD_LX0 (400-ALM_HGAP-ALMD_LW) // daily alarm label x
#define ALMD_TX0 (400+ALM_HGAP) // daily alarm time button x
#define ALMD_TW 160 // daily time control button width
#define ALMO_Y0 (ALMD_Y0+SW_BH+ALM_VGAP)// once-alarm buttons y
#define ALMO_LW 300 // once-alarm control button width
#define ALMO_LX0 (400-ALM_HGAP-ALMO_LW) // once-alarm label x
#define ALMO_TX0 (400+ALM_HGAP) // once-alarm time control button x
#define ALMO_TW 300 // once-alarm control button width
#define ALMO_TITY (ALMO_Y0+SW_BH+3) // once-alarm title Y
#define ALMD_NVARMED (24U*60U) // add to hrmn when stored in NV_DAILYALARM to indicate armed
#define ALMD_NVUTC (2*24U*60U) // add to hrmn when stored in NV_DAILYALARM to indicate UTC
#define ALM_RINGTO 60 // alarm clock ringing timeout, secs; NOT EASILY CHANGED
#define ALMO_ARMED_BIT 1 // NV_ONCEALARMMASK bit means armed
#define ALMO_UTC_BIT 2 // NV_ONCEALARMMASK bit means alarm_once.time is in utc
// countdown params
#define SW_CD_W 200 // countdown button width
#define SW_CD_X (400-ALM_HGAP-SW_CD_W) // countdown button x
#define SW_CD_Y (ALMO_TITY+2*ALM_VGAP) // countdown button y
#define SW_CDP_X (400+ALM_HGAP) // countdown period display box x
#define SW_CDP_W 200 // countdown period display box width
#define SW_CD_WARNDT 60000 // countdown warning time, ms
#define SW_CD_AGEDT 30000 // countdown aged period, ms
// big analog clock params
#define BAC_X0 400 // x center
#define BAC_Y0 240 // y center
#define BAC_MNR 210 // minute hand radius
#define BAC_SCR 180 // second hand radius
#define BAC_HRR 130 // hour hand radius
#define BAC_FR 232 // face radius
#define BAC_BEZR 238 // bezel radius
#define BAC_HTR 12 // hour tick radius
#define BAC_MTR 5 // minute tick radius
#define BACD_Y0 180 // when add digital y center
#define BACD_MNR 158 // when add digital minute hand radius
#define BACD_SCR 135 // when add digital second hand radius
#define BACD_HRR 97 // when add digital hour hand radius
#define BACD_FR 174 // when add digital face radius
#define BACD_BEZR 178 // when add digital bezel radius
#define BACD_HTR 10 // when add digital hour tick radius
#define BACD_MTR 4 // when add digital minute tick radius
#define BACD_DY 430 // when add digital y coord of digits
#define BACD_HMX0 336 // when add digital H:M starting x
#define BACD_HMSX0 305 // when add digital H:M:S starting x
#define BAC_DOTR 2 // center dot radius
#define BAC_HRTH deg2rad(15.0F) // hour hand thickness half-angle, rads
#define BAC_MNTH (BAC_HRTH*BAC_HRR/BAC_MNR) // minute hand thickness half-angle, rads
#define BAC_HTTH deg2rad(0.6F) // hour tick half-angle as seen from center, rads
#define BAC_FCOL sw_col // face color
#define BAC_HRCOL sw_col // hour hand color
#define BAC_MNCOL sw_col // minute hand color
#define BAC_SCCOL GRAY // second hand color
#define BAC_BEZCOL GRAY // bezel color
#define BAC_DATEX 2 // date box X -- just to anchor text
#define BAC_DATEY 2 // date box Y -- just to anchor text
#define BAC_DATEW 200 // date box width -- used just for tapping
#define BAC_DATEH 150 // date box height -- used just for tapping
#define BAC_WXX (800-PLOTBOX123_W-1) // weather box X
#define BAC_WXY 5 // weather box Y
#define BAC_WXW PLOTBOX123_W // weather box width
#define BAC_WXH PLOTBOX123_H // weather box height
#define BAC_WXGDT (30L*60*1000) // weather update period when good, millis
#define BAC_WXFDT (6*1000) // weather update period when fail, millis
// big digital clock
#define BDC_HMW 110 // digit width
#define BDC_HMCW (2*BDC_HMW/3) // colon spacing
#define BDC_HMH (2*BDC_HMW) // digit height
#define BDC_HMY0 (BAC_WXY+BAC_WXH+20) // top y
#define BDC_HMLT (BDC_HMW/6) // line thickness
#define BDC_HMGAP (BDC_HMW/3) // gap between adjacent digits
#define BDC_HMX0 (400-(4*BDC_HMW+2*BDC_HMGAP+BDC_HMCW)/2) // left x for hh:mm
#define BDC_HMCR (BDC_HMLT/2) // colon radius
#define BCD_SSZRED 3 // reduce seconds size by this factor
// contols common to both big clock styles
#define BC_CDP_X 0 // countdown period x
#define BC_CDP_Y (480-SW_BH) // countdown period y
#define BC_CDP_W 100 // countdown period width
#define BC_CDP_H SW_BH // countdown period height
#define BC_ALMD_X (BC_CDP_X+BC_CDP_W) // x coord of daily alarm time box
#define BC_ALMD_Y BC_CDP_Y // y coord of daily alarm time box
#define BC_ALMD_W SW_BW // daily alarm message width
#define BC_ALMD_H SW_BH // daily alarm message height
#define BC_ALMO_X 0 // x coord of one-time alarm time box
#define BC_ALMO_Y (BC_ALMD_Y-SW_BH) // y coord of one-time alarm time box
#define BC_ALMO_W 240 // one-time alarm message width
#define BC_ALMO_H SW_BH // one-time alarm message height
#define BC_BAD_W 200 // bad time message width
#define BC_BAD_H SW_BH // bad time message height
#define BC_BAD_X (800-BC_BAD_W-2) // x coord of bad time message
#define BC_BAD_Y BC_CDP_Y // y coord of bad time message
#define BC_SAT_W 280 // width of satellite state
#define BC_SAT_H SW_BH // height of satellite state
#define BC_SAT_X ((800-BC_SAT_W)/2) // x coord of satellite state
#define BC_SAT_Y BC_CDP_Y // y coord of satellite state
// current state
static SWEngineState sws_engine; // what is _running_
static SWDisplayState sws_display; // what is _displaying_
static uint32_t countdown_period; // count down from here, ms
static uint8_t swdigits[SW_NDIG]; // current digits
static uint32_t start_t, stop_dt; // millis() at start, since stop
AlarmOnce alarm_once = {
0, false, ALMS_OFF,
{ALMO_LX0, ALMO_Y0, ALMO_LW, SW_BH},
{ALMO_TX0, ALMO_Y0, ALMO_TW, SW_BH},
{BC_ALMO_X, BC_ALMO_Y, BC_ALMO_W, BC_ALMO_H},
NULL
};
static AlarmDaily alarm_daily = {
0, false, ALMS_OFF,
{ALMD_LX0, ALMD_Y0, ALMD_LW, SW_BH},
{ALMD_TX0, ALMD_Y0, ALMD_TW, SW_BH},
{BC_ALMD_X, BC_ALMD_Y, BC_ALMD_W, BC_ALMD_H},
};
// button labels
static char cd_lbl[] = "Count down";
static char lap_lbl[] = "Lap";
static char reset_lbl[] = "Reset";
static char resume_lbl[] = "Resume";
static char run_lbl[] = "Run";
static char stop_lbl[] = "Stop";
static char exit_lbl[] = "Exit";
static char bigclock_lbl[] = "Big Clock";
// stopwatch controls
static SBox countdown_lbl_b = {SW_CD_X, SW_CD_Y, SW_CD_W, SW_BH};
static SBox cd_time_dsp_b = {SW_CDP_X, SW_CD_Y, SW_CDP_W, SW_BH};
static SBox cd_time_up_b = {SW_CDP_X, SW_CD_Y, SW_CDP_W, SW_BH/2};
static SBox cd_time_dw_b = {SW_CDP_X, SW_CD_Y+SW_BH/2, SW_CDP_W, SW_BH/2};
static SBox A_b = {SW_BAX, SW_BY, SW_BW, SW_BH};
static SBox B_b = {SW_BBX, SW_BY, SW_BW, SW_BH};
static SBox exit_b = {SW_EXITX, SW_EXITY, SW_BW, SW_BH};
static SBox bigclock_b = {SW_BCX, SW_BCY, SW_BW, SW_BH};
static SBox color_b = {SW_CX, SW_CY, SW_CW, SW_CH};
static uint8_t sw_hue; // hue 0..255
static uint16_t sw_col; // color pixel
// big clock info
static SBox bc_date_b = {BAC_DATEX, BAC_DATEY, BAC_DATEW, BAC_DATEH};
static SBox bc_wx_b = {BAC_WXX, BAC_WXY, BAC_WXW, BAC_WXH}; // weather
static SBox bc_cd_b = {BC_CDP_X, BC_CDP_Y, BC_CDP_W, BC_CDP_H}; // countdown remaining and control
static uint16_t bc_bits; // see SWBCBits
static uint32_t bc_prev_wx; // time of prev drawn wx, millis
static uint32_t bc_wxdt = BAC_WXGDT; // weather update interval, millis
/* save persistent state and log
*/
static void saveSWNV(void)
{
NVWriteUInt16 (NV_BCFLAGS, bc_bits);
NVWriteUInt32 (NV_CD_PERIOD, countdown_period);
uint16_t acode = alarm_daily.hrmn;
if (alarm_daily.state != ALMS_OFF)
acode += ALMD_NVARMED;
if (alarm_daily.utc)
acode += ALMD_NVUTC;
NVWriteUInt16 (NV_DAILYALARM, acode);
uint8_t once_mask = 0;
if (alarm_once.state != ALMS_OFF)
once_mask |= ALMO_ARMED_BIT;
if (alarm_once.utc)
once_mask |= ALMO_UTC_BIT;
NVWriteUInt8 (NV_ONCEALARMMASK, once_mask);
NVWriteUInt32 (NV_ONCEALARM, (uint32_t)alarm_once.time);
}
/* load persistent values from NV
*/
static void loadSWNV(void)
{
if (!NVReadUInt16 (NV_BCFLAGS, &bc_bits)) {
bc_bits = SW_BCDATEBIT | SW_BCWXBIT;
NVWriteUInt16 (NV_BCFLAGS, bc_bits);
}
if (!NVReadUInt32 (NV_CD_PERIOD, &countdown_period)) {
countdown_period = 600000; // 10 mins default
NVWriteUInt32 (NV_CD_PERIOD, countdown_period);
}
/* read and unpack daily alarm time and whether armed or utc.
*
* Armed? UTC?
* 5759 \
* ......> yes yes
* NVA+NVU 4320 /
* 4319 \
* ......> no yes
* NVU 2880 /
* 2879 \
* ......> yes no
* NVA 1440 /
* 1439 \
* ......> no no
* 0 0 /
*/
if (!NVReadUInt16 (NV_DAILYALARM, &alarm_daily.hrmn)) {
alarm_daily.hrmn = 0;
NVWriteUInt16 (NV_DAILYALARM, 0);
}
if (alarm_daily.hrmn < ALMD_NVARMED) {
alarm_daily.state = ALMS_OFF;
alarm_daily.utc = false;
} else if (alarm_daily.hrmn < ALMD_NVUTC) {
alarm_daily.state = ALMS_ARMED;
alarm_daily.utc = false;
alarm_daily.hrmn -= ALMD_NVARMED;
} else if (alarm_daily.hrmn < ALMD_NVARMED+ALMD_NVUTC) {
alarm_daily.state = ALMS_OFF;
alarm_daily.utc = true;
alarm_daily.hrmn -= ALMD_NVUTC;
} else {
alarm_daily.state = ALMS_ARMED;
alarm_daily.utc = true;
alarm_daily.hrmn -= ALMD_NVARMED+ALMD_NVUTC;
}
// read and unpack the one-time alarm
uint8_t once_mask;
if (!NVReadUInt8 (NV_ONCEALARMMASK, &once_mask)) {
once_mask = 0;
NVWriteUInt8 (NV_ONCEALARMMASK, once_mask);
}
alarm_once.state = (once_mask & ALMO_ARMED_BIT) ? ALMS_ARMED : ALMS_OFF;
alarm_once.utc = (once_mask & ALMO_UTC_BIT) != 0;
uint32_t once_time;
if (!NVReadUInt32 (NV_ONCEALARM, &once_time)) {
once_time = (uint32_t)nowWO();
NVWriteUInt32 (NV_ONCEALARM, once_time);
}
alarm_once.time = once_time;
// beware past alarms
if (once_time < (uint32_t)nowWO())
alarm_once.state = ALMS_OFF;
}
/* return ms countdown time remaining, if any
*/
static uint32_t getCountdownLeft()
{
if (sws_engine == SWE_COUNTDOWN) {
uint32_t since_start = millis() - start_t;
if (since_start < countdown_period)
return (countdown_period - since_start);
}
return (0);
}
/* set sw_col from sw_hue
*/
static void setSWColor()
{
sw_col = HSV_2_RGB565 (sw_hue, SW_HSV_S, SW_HSV_V);
}
/* draw the current countdown_period if currently on the main SW page
*/
static void drawSWCDPeriod()
{
if (sws_display == SWD_MAIN) {
char buf[20];
int mins = countdown_period/60000;
snprintf (buf, sizeof(buf), "%d %s", mins, mins > 1 ? "mins" : "min");
drawStringInBox (buf, cd_time_dsp_b, false, sw_col);
}
}
/* draw the color control box
*/
static void drawColorScale()
{
// erase to remove tick marks
fillSBox (color_b, RA8875_BLACK);
// rainbow
for (uint16_t dx = 0; dx < color_b.w; dx++) {
uint16_t c = HSV_2_RGB565 (255*dx/color_b.w, SW_HSV_S, SW_HSV_V);
tft.drawPixel (color_b.x + dx, color_b.y + color_b.h/2, c);
}
// mark it
uint16_t hue_x = color_b.x + sw_hue*color_b.w/255;
tft.drawLine (hue_x, color_b.y+3*color_b.h/8, hue_x, color_b.y+5*color_b.h/8, RA8875_WHITE);
}
/* draw the given stopwatch digit in the given position 0 .. SW_NDIG-1.
* use swdigits[] to avoid erasing/redrawing the same digit again.
*/
static void drawSWDigit (uint8_t position, uint8_t digit)
{
// check for no change
if (swdigits[position] == digit)
return;
swdigits[position] = digit;
// bounding box
SBox b;
b.x = SW_X0 + (SW_DW+SW_DGAP)*position;
b.y = SW_Y0;
b.w = SW_DW;
b.h = SW_DH;
// draw
drawDigit (b, digit, SW_LT, SW_BG, sw_col);
}
/* display the given time value in millis()
*/
static void drawSWTime(uint32_t t)
{
int ndig = 0;
t %= (100UL*60UL*60UL*1000UL); // msec in SW_NDIG digits
uint8_t tenhr = t / (10UL*3600UL*1000UL);
t -= tenhr * (10UL*3600UL*1000UL);
drawSWDigit (0, tenhr);
ndig++;
uint8_t onehr = t / (3600UL*1000UL);
t -= onehr * (3600UL*1000UL);
drawSWDigit (1, onehr);
ndig++;
uint8_t tenmn = t / (600UL*1000UL);
t -= tenmn * (600UL*1000UL);
drawSWDigit (2, tenmn);
ndig++;
uint8_t onemn = t / (60UL*1000UL);
t -= onemn * (60UL*1000UL);
drawSWDigit (3, onemn);
ndig++;
uint8_t tensec = t / (10UL*1000UL);
t -= tensec * (10UL*1000UL);
drawSWDigit (4, tensec);
ndig++;
uint8_t onesec = t / (1UL*1000UL);
t -= onesec * (1UL*1000UL);
drawSWDigit (5, onesec);
ndig++;
uint8_t tenthsec = t / (100UL);
t -= tenthsec * (100UL);
drawSWDigit (6, tenthsec);
ndig++;
uint8_t hundsec = t / (10UL);
t -= hundsec * (10UL);
drawSWDigit (7, hundsec);
ndig++;
if (ndig != SW_ND)
fatalError ("stopwatch %d != %d", ndig, SW_ND);
}
/* given countdown time remaining, find range and button text color
*/
static void determineCDVisuals (uint32_t ms_left, SWCDState &cds, uint16_t &color)
{
// all good if beyond the warning time
if (ms_left >= SW_CD_WARNDT) {
cds = SWCDS_RUNOK;
color = RA8875_GREEN;
return;
}
// flash the GUI color but just rely on threadBlinker to handle the LED
bool flash_on = (millis()%500) < 250; // flip at 2 Hz
if (ms_left > 0) {
color = flash_on ? DYELLOW : RA8875_BLACK;
cds = SWCDS_WARN;
} else {
color = flash_on ? RA8875_RED : RA8875_BLACK;
cds = SWCDS_TIMEOUT;
}
}
/* draw the satellite indicator, if want
*/
static void drawSatIndicator(bool force)
{
// unused for now
(void) force;
// get sat info if want and defined
SatNow sn;
if (!(bc_bits & SW_BCSATBIT) || !getSatNow(sn))
return;
// prep for drawStringInBox
selectFontStyle (LIGHT_FONT, SMALL_FONT);
SBox sat_b;
sat_b.x = BC_SAT_X;
sat_b.y = BC_SAT_Y;
sat_b.w = BC_SAT_W;
sat_b.h = BC_SAT_H;
if (sn.raz == SAT_NOAZ)
drawStringInBox ("No rise", sat_b, false, sw_col);
else if (sn.saz == SAT_NOAZ)
drawStringInBox ("No set", sat_b, false, sw_col);
else {
// draw circumstances
// decide whether up or down
float dt; // hours to begin with
const char *prompt;
if (sn.rdt < 0 || sn.rdt > sn.sdt) {
// up now, show time to set
dt = sn.sdt;
prompt = "sets in";
} else {
// down now, show time to rise
dt = sn.rdt;
prompt = "rises in";
}
// format time
int a, b;
char sep;
formatSexa (dt, a, sep, b);
// draw
char buf[50];
snprintf (buf, sizeof(buf), "%s %s %d%c%02d", sn.name, prompt, a, sep, b);
drawStringInBox (buf, sat_b, false, sw_col);
}
}
/* erase the contest title beneath the one-time alarm time
*/
static void eraseContestTitle(void)
{
if (sws_display == SWD_MAIN)
tft.fillRect (ALMO_TX0, ALMO_TITY, ALMO_TW, 14, RA8875_BLACK);
}
/* draw the title of a contest beneath the one-time alarm if its time matches alarm_once.time
*/
static void drawContestTitle(void)
{
if (sws_display == SWD_MAIN && alarm_once.state == ALMS_ARMED && alarm_once.title) {
selectFontStyle (LIGHT_FONT, FAST_FONT);
tft.setTextColor (sw_col);
tft.setCursor (ALMO_TX0 + (ALMO_TW-getTextWidth(alarm_once.title))/2, ALMO_TITY+2);
tft.print (alarm_once.title);
}
}
/* draw alarm times, pin and label if requested in various ways depending on sws_display
*/
static void drawAlarmIndicators (bool label_too)
{
// pin
setAlarmPin (alarm_daily.state == ALMS_RINGING || alarm_once.state == ALMS_RINGING);
// prep
char buf[50];
selectFontStyle (BOLD_FONT, SMALL_FONT);
uint16_t a_hr = alarm_daily.hrmn/60;
uint16_t a_mn = alarm_daily.hrmn%60;
if (sws_display == SWD_MAIN) {
// N.B. coordinate with alarm_daily.time_b touch control locations
snprintf (buf, sizeof(buf), "%s %02d:%02d", alarm_daily.utc ? "UTC" : "DE ", a_hr, a_mn);
drawStringInBox (buf, alarm_daily.time_b, false, sw_col);
if (label_too) {
const char *lbl = "?";
switch (alarm_daily.state) {
case ALMS_OFF: lbl = "Daily Alarm off"; break;
case ALMS_ARMED: lbl = "Daily Alarm armed"; break;
case ALMS_RINGING: lbl = "Daily Alarm!"; break;
}
drawStringInBox (lbl, alarm_daily.lbl_b, alarm_daily.state == ALMS_RINGING, sw_col);
}
// N.B. coordinate with alarm_once.time_b touch control locations
tmElements_t tm;
time_t t = alarm_once.time;
if (!alarm_once.utc)
t += getTZ (de_tz);
breakTime (t, tm);
snprintf (buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d", alarm_once.utc ? "UTC" : "DE ",
tm.Year+1970, tm.Month, tm.Day, tm.Hour, tm.Minute);
drawStringInBox (buf, alarm_once.time_b, false, sw_col);
if (label_too) {
const char *lbl = "?";
switch (alarm_once.state) {
case ALMS_OFF: lbl = "One-Time Alarm off"; break;
case ALMS_ARMED: lbl = "One-Time Alarm armed"; break;
case ALMS_RINGING: lbl = "One-Time Alarm!"; break;
}
drawStringInBox (lbl, alarm_once.lbl_b, alarm_once.state == ALMS_RINGING, sw_col);
}
// erase contest title then draw if set
// N.B. do this after label_too because drawContestTitle calls selectFontStyle()
eraseContestTitle();
drawContestTitle();
} else if (sws_display == SWD_BCDIGITAL || sws_display == SWD_BCANALOG) {
if (alarm_daily.state == ALMS_OFF) {
if (label_too) {
// this is so web command set_alarm?off can actually erase the alarm box
fillSBox (alarm_daily.bc_alarm_b, RA8875_BLACK);
}
} else if (alarm_daily.state == ALMS_ARMED) {
snprintf (buf, sizeof(buf), "A:%02d:%02d", a_hr, a_mn);
drawStringInBox (buf, alarm_daily.bc_alarm_b, false, sw_col);
} else if (alarm_daily.state == ALMS_RINGING) {
drawStringInBox ("Alarm!", alarm_daily.bc_alarm_b, true, sw_col);
}
if (alarm_once.state == ALMS_OFF) {
if (label_too) {
// this is so web command set_alarm?off can actually erase the alarm box
fillSBox (alarm_once.bc_alarm_b, RA8875_BLACK);
}
} else if (alarm_once.state == ALMS_ARMED) {
tmElements_t tm;
time_t t = alarm_once.time;
if (!alarm_once.utc)
t += getTZ (de_tz);
breakTime (t, tm);
snprintf (buf, sizeof(buf), "A:%04d-%02d-%02d %02d:%02d", tm.Year+1970, tm.Month, tm.Day,
tm.Hour, tm.Minute);
drawStringInBox (buf, alarm_once.bc_alarm_b, false, sw_col);
} else if (alarm_once.state == ALMS_RINGING) {
drawStringInBox ("One-time Alarm!", alarm_once.bc_alarm_b, true, sw_col);
}
}
}
/* draw remaining count down time and manage the state of the count down button and LED.
* N.B. we handle all display states but assume sws_engine == SWE_COUNTDOWN
*/
static void drawCDTimeRemaining(bool force)
{
// sanity check: this function is only for countdown
if (sws_engine != SWE_COUNTDOWN)
return;
// not crazy fast unless force
static uint32_t gate;
if (!force && !timesUp (&gate, 31))
return;
// get ms remaining
uint32_t ms_left = getCountdownLeft();
// determine range and color
SWCDState cds;
uint16_t color;
determineCDVisuals (ms_left, cds, color);
// set LEDS
setCDLEDState (cds);
if (sws_display == SWD_MAIN) {
// showing main stopwatch page at full ms resolution
// show time using the 7-seg displays
drawSWTime(ms_left);
// determine whether to display inverted
static bool prev_inv;
bool inv = cds == SWCDS_RUNOK || cds == SWCDS_WARN || cds == SWCDS_TIMEOUT;
// update the countdown button if different or force
if (force || inv != prev_inv) {
drawStringInBox (cd_lbl, countdown_lbl_b, inv, sw_col);
prev_inv = inv;
}
} else {
// the other display states share a common time format so build that first
// break into H:M:S
ms_left += 500U; // round to nearest second
uint8_t hr = ms_left / 3600000U;
ms_left -= hr * 3600000U;
uint8_t mn = ms_left / 60000U;
ms_left -= mn * 60000U;
uint8_t sc = ms_left/1000U;
// avoid repeating the same time and color
static uint8_t prev_sc;
static uint16_t prev_color;
if (color == prev_color && sc == prev_sc && !force)
return;
// format
char buf[32];
if (hr == 0)
snprintf (buf, sizeof(buf), "%d:%02d", mn, sc);
else
snprintf (buf, sizeof(buf), "%dh%02d", hr, mn);
if (sws_display == SWD_NONE) {
// main Hamclock page
// overwrite stopwatch icon
selectFontStyle (LIGHT_FONT, FAST_FONT);
uint16_t cdw = getTextWidth(buf);
fillSBox (stopwatch_b, RA8875_BLACK);
tft.setTextColor (color);
tft.setCursor (stopwatch_b.x + (stopwatch_b.w-cdw)/2, stopwatch_b.y+stopwatch_b.h/4);
tft.print (buf);
// draw pane if showing
PlotPane cdp = findPaneChoiceNow(PLOT_CH_COUNTDOWN);
if (cdp != PANE_NONE) {
// find box
SBox box = plot_b[cdp];
// prep if force
if (force) {
prepPlotBox (box);
// title
static const char *title = "Countdown";
selectFontStyle (LIGHT_FONT, SMALL_FONT);
tft.setTextColor(BRGRAY);
uint16_t tw = getTextWidth(title);
tft.setCursor (box.x + (box.w-tw)/2, box.y + PANETITLE_H);
tft.print (title);
}
// time remaining, don't blink
static uint16_t prev_pane_color;
uint16_t pane_color = color == RA8875_BLACK ? prev_pane_color : color;
if (force || sc != prev_sc || pane_color != prev_pane_color) {
selectFontStyle (BOLD_FONT, LARGE_FONT);
uint16_t w = getTextWidth(buf);
tft.fillRect (box.x+10, box.y+box.h/3, box.w-20, box.h/3, RA8875_BLACK);
tft.setCursor (box.x + (box.w - w)/2, box.y + 2*box.h/3 - 5);
tft.setTextColor (pane_color);
tft.print(buf);
prev_pane_color = pane_color;
}
}
} else if (sws_display == SWD_BCDIGITAL || sws_display == SWD_BCANALOG) {
selectFontStyle (BOLD_FONT, SMALL_FONT);
drawStringInBox (buf, bc_cd_b, false, color);
}
// remember
prev_sc = sc;
prev_color = color;
}
}
/* draw either BigClock state awareness message as needed
*/
static void drawBCAwareness (bool force)
{
// whether time was ok last iteration
static bool time_was_ok;
// get current state
bool clock_ok = clockTimeOk();
bool ut_zero = utcOffset() == 0;
bool time_ok_now = clock_ok && ut_zero;
// update if force or new state
if (time_ok_now) {
if (force || !time_was_ok) {
// erase
tft.fillRect (BC_BAD_X, BC_BAD_Y, BC_BAD_W, BC_BAD_H, RA8875_BLACK);
Serial.print ("SW: time ok now\n");
}
} else {
if (force || time_was_ok) {
selectFontStyle (BOLD_FONT, SMALL_FONT);
tft.setCursor (BC_BAD_X, BC_BAD_Y+27);
tft.setTextColor (RA8875_RED);
if (clock_ok) {
const char *msg = "Time is offset";
tft.print (msg);
Serial.printf ("SW: %s\n", msg);
} else {
const char *msg = "Time is unknown";
tft.print (msg);
Serial.printf ("SW: %s\n", msg);
}
}
}
// persist
time_was_ok = time_ok_now;
}
/* refresh detailed date info in bc_date_b.
* N.B. we never erase because Wednesday overlaps clock
*/
static void drawBCDateInfo (int hr, int dy, int wd, int mo)
{
// day
selectFontStyle (BOLD_FONT, LARGE_FONT);
tft.setTextColor (BAC_FCOL);
tft.setCursor (bc_date_b.x, bc_date_b.y + 50);
tft.print (dayStr(wd));
// month
selectFontStyle (LIGHT_FONT, SMALL_FONT);
tft.setCursor (bc_date_b.x, bc_date_b.y + 90);
if (getDateFormat() == DF_MDY || getDateFormat() == DF_YMD)
tft.printf ("%s %d", monthStr(mo), dy);
else
tft.printf ("%d %s", dy, monthStr(mo));
// AM/PM/UTC
tft.setCursor (bc_date_b.x, bc_date_b.y + 125);
if (sws_display == SWD_BCANALOG || (bc_bits & (SW_DB12HBIT|SW_LSTBIT|SW_UTCBIT)) == SW_DB12HBIT) {
// AM/PM always for analog or 12 hour digital
tft.print (hr < 12 ? "AM" : "PM");
} else if (sws_display == SWD_BCDIGITAL && (bc_bits & SW_UTCBIT)) {
// UTC
tft.print ("UTC");
} else if (sws_display == SWD_BCDIGITAL && (bc_bits & SW_LSTBIT)) {
// LST
tft.print ("LST");
} else if (sws_display == SWD_BCDIGITAL && !(bc_bits & SW_UTCBIT)) {
// UTC + TZ
tft.printf ("UTC%+g", getTZ (de_tz)/3600.0F);
}
}
/* refresh DE weather in bc_wx_b, return whether successful
*/
static bool drawBCDEWxInfo(void)
{
WXInfo wi;
Message ynot;
bool ok = getCurrentWX (de_ll, true, &wi, ynot);
if (ok)
plotWX (bc_wx_b, BAC_FCOL, wi);
else
plotMessage (bc_wx_b, RA8875_RED, ynot.get());
// undo border
drawSBox (bc_wx_b, RA8875_BLACK);
return (ok);
}
/* draw space weather in upper right.
*/
static void drawBCSpaceWxInfo (bool all)
{
if (checkForNewSpaceWx() || all)
drawNCDXFSpcWxStats(sw_col);
}
/* mark each control or indicator box for debugging big clock layout
*/
static void drawBCShowAll()
{
if (debugLevel (DEBUG_BC, 1)) {
drawSBox (bc_cd_b, RA8875_RED);
drawSBox (alarm_daily.bc_alarm_b, RA8875_RED);
drawSBox (alarm_once.bc_alarm_b, RA8875_RED);
drawSBox (bc_date_b, RA8875_RED);
tft.drawRect (BC_BAD_X, BC_BAD_Y, BC_BAD_W, BC_BAD_H, RA8875_RED);
tft.drawRect (BC_SAT_X, BC_SAT_Y, BC_SAT_W, BC_SAT_H, RA8875_RED);
}
}
/* draw the digital Big Clock
*/
static void drawDigitalBigClock (bool all)
{
// debug
drawBCShowAll();
// persist to avoid drawing the same digits again
static time_t prev_am, prev_t0; // previous am/pm and report time
static uint8_t prev_mnten, prev_mnunit; // previous mins tens and unit
static uint8_t prev_scten; // previous seconds tens and unit
static uint8_t prev_hr, prev_mo, prev_dy; // previous drawn date info
static bool prev_leadhr; // previous whether leading hours tens digit
// get UTC time now, including any user offset
time_t t0 = nowWO();
// done if same second unless all
if (!all && t0 == prev_t0)
return;
prev_t0 = t0;
// time components
int hr, mn, sc, mo, dy, wd;
// break out hr, mn and sec as utc, local or lst
if (bc_bits & SW_LSTBIT) {
double lst; // hours
double astro_mjd = t0/86400.0 + 2440587.5 - 2415020.0; // just for now_lst()
now_lst (astro_mjd, de_ll.lng, &lst);
hr = lst;
lst = (lst - hr)*60; // now mins
mn = lst;
lst = (lst - mn)*60; // now secs
sc = lst;
t0 += getTZ (de_tz); // now local