-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstopwatch.cpp
1923 lines (1643 loc) · 64 KB
/
stopwatch.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
/* implement a simple stopwatch with lap timer, countdown timer, 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"
// set to show all boxes for debugging
// #define _SHOW_ALL
// countdown ranges, including flashing states
typedef enum {
SWCDS_OFF, // idle or dark
SWCDS_RUNOK, // more than SW_CD_WARNDT remaining
SWCDS_WARN_ON, // > 0 but < SW_CD_WARNDT remaining
SWCDS_TIMEOUT_ON, // timed out
} SWCDState;
/* only systems with GPIO can control LEDs and have start switch for countdown control.
* ESP has a few GPIO but not enough for everything the Pi can do so this was left off.
*/
#if defined(_SUPPORT_GPIO) && defined(_IS_UNIX)
/* set the LEDs to indicate the given countdown range
*/
static void setLEDState (SWCDState cds)
{
// ignore if not supposed to use GPIO
if (!GPIOOk())
return;
// access pins
GPIO& gpio = GPIO::getGPIO();
if (!gpio.isReady())
return;
gpio.setAsOutput (SW_GRN_GPIO);
gpio.setAsOutput (SW_RED_GPIO);
switch (cds) {
case SWCDS_RUNOK: // fallthru
case SWCDS_WARN_ON:
// green on
gpio.setLo (SW_GRN_GPIO);
gpio.setHi (SW_RED_GPIO);
break;
case SWCDS_OFF:
// both off
gpio.setHi (SW_GRN_GPIO);
gpio.setHi (SW_RED_GPIO);
break;
case SWCDS_TIMEOUT_ON:
// red on
gpio.setHi (SW_GRN_GPIO);
gpio.setLo (SW_RED_GPIO);
break;
}
}
/* return whether the countdown pin has toggled low,
* ie, this is an edge-triggered state.
*/
static bool countdownPinIsTrue()
{
// ignore if not supposed to use GPIO
if (!GPIOOk())
return (false);
static bool prev_pin_state;
static bool prev_pin_known;
// read pin
GPIO& gpio = GPIO::getGPIO();
if (!gpio.isReady())
return (false);
gpio.setAsInput (SW_COUNTDOWN_GPIO);
bool pin_state = gpio.readPin(SW_COUNTDOWN_GPIO);
// init history if first time
if (!prev_pin_known) {
prev_pin_state = pin_state;
prev_pin_known = true;
}
// return whether went low
if (pin_state != prev_pin_state) {
prev_pin_state = pin_state;
return (!pin_state);
} else
return (false);
}
/* return state of alarm clock reset input pin.
* N.B. pin is active-low
*/
static bool alarmPinIsSet(void)
{
// ignore if not supposed to use GPIO
if (!GPIOOk())
return (false);
GPIO& gpio = GPIO::getGPIO();
if (!gpio.isReady())
return (false);
gpio.setAsInput (SW_ALARMOFF_GPIO);
return (!gpio.readPin(SW_ALARMOFF_GPIO));
}
/* control the alarm clock output pin
*/
static void setAlarmPin (bool set)
{
// ignore if not supposed to use GPIO
if (!GPIOOk())
return;
GPIO& gpio = GPIO::getGPIO();
gpio.setAsOutput (SW_ALARMOUT_GPIO);
gpio.setHiLo (SW_ALARMOUT_GPIO, set);
}
#else // !_SUPPORT_GPIO
// dummies
static void setLEDState (SWCDState cds) { (void) cds; }
static bool countdownPinIsTrue() { return (false); }
static bool alarmPinIsSet(void) {return (false); }
static void setAlarmPin (bool set) { (void) set; }
#endif // _SUPPORT_GPIO
// 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_FLT 45 // line thickness as fraction of SW_DW
#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 40 // 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
#define SW_BCDATEBIT 1 // NV_BCFLAGS bit mask for showing bigclock date
#define SW_BCWXBIT 2 // NV_BCFLAGS bit mask for showing bigclock weather
#define SW_BCDIGBIT 4 // NV_BCFLAGS bit mask for whether big clock is digital
#define SW_DB12HBIT 8 // NV_BCFLAGS bit mask for whether digital clock is 12 else 24
#define SW_ANOSHBIT 16 // NV_BCFLAGS bit mask for whether no analog second hand
// alarm clock params
#define ALM_X0 180 // alarm control button x
#define ALM_Y0 25 // alarm control button y
#define ALM_W 200 // alarm control button width
#define ALM_EX 420 // alarm time display box x
#define ALM_EY ALM_Y0 // alarm time display box y
#define ALM_EW SW_CDP_W // alarm time display box w
#define ALM_TOVFLOW (24U*60U) // hrmn overflow value
#define ALM_RINGTO 30000 // alarm clock ringing timeout, millis
// countdown params
#define SW_CD_X ALM_X0 // countdown button x
#define SW_CD_Y (ALM_Y0+2*SW_BH) // countdown button y
#define SW_CD_W ALM_W // countdown button width
#define SW_CDP_X ALM_EX // countdown period display box x
#define SW_CDP_W ALM_W // countdown period display box width
#define SW_CD_WARNDT 60000 // countdown warning time, 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 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_BKCOL RA8875_BLUE // Back button 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-PLOTBOX_W-1) // weather box X
#define BAC_WXY 5 // weather box Y
#define BAC_WXW PLOTBOX_W // weather box width
#define BAC_WXH PLOTBOX_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 params
#define BDC_W 100 // digit width
#define BDC_H (2*BDC_W) // digit height
#define BDC_X0 (400-3*BDC_W) // left x
#define BDC_Y0 (BAC_WXY+BAC_WXH+20) // top y
#define BDC_FLT 5 // segment thickness as fraction of BDC_W
#define BDC_GAP (BDC_W/2) // gap between adjacent digits
#define BDC_CR (BDC_W/BDC_FLT/2) // colon radius
// contols common to both big clock styles
#define BC_ALM_X 2 // x coord of alarm time box
#define BC_ALM_Y 380 // y coord of alarm time box
#define BC_CDP_X 2 // countdown period x
#define BC_CDP_Y (BC_ALM_Y+SW_BH) // countdown period y
#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_EXIT_X 500 // exit area x coord
#define BC_EXIT_Y 300 // exit area y coord
// sanity checks
#if BDC_X0+BDC_W+BDC_GAP+BDC_W+2*BDC_GAP+BDC_W+BDC_GAP+BDC_W+BDC_GAP > 800
#error Big Digital Clock digits too wide
#endif
#if BDC_Y0+BDC_H > 480
#error Big Digital Clock digits too tall
#endif
// 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
static uint16_t alarm_hrmn; // alarm time, hr*60 + min
static time_t alarm_ringtime; // now() when alarm started ringing
static AlarmState alarm_state; // whether off, armed or ringing
// 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 cdtime_dsp_b = {SW_CDP_X, SW_CD_Y, SW_CDP_W, SW_BH};
static SBox cdtime_up_b = {SW_CDP_X, SW_CD_Y-SW_BH/2, SW_CDP_W, SW_BH};
static SBox cdtime_dw_b = {SW_CDP_X, SW_CD_Y+SW_BH/2, SW_CDP_W, SW_BH};
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 controls
static SBox bcdate_b = {BAC_DATEX, BAC_DATEY, BAC_DATEW, BAC_DATEH};
static SBox bcwx_b = {BAC_WXX, BAC_WXY, BAC_WXW, BAC_WXH}; // weather
static SBox bccd_b = {BC_CDP_X, BC_CDP_Y, SW_BW, SW_BH}; // countdown remaining and control
static SBox bcalarm_b = {BC_ALM_X, BC_ALM_Y, SW_BW, SW_BH}; // alarm time 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
// alarm clock controls on main sw page
static SBox alarm_lbl_b = {ALM_X0, ALM_Y0, ALM_W, SW_BH};
static SBox alarm_hrmn_b = {ALM_EX, ALM_EY, ALM_EW, SW_BH};
static SBox alarm_up_b = {ALM_EX, ALM_EY-SW_BH/2, ALM_EW, SW_BH};
static SBox alarm_dw_b = {ALM_EX, ALM_EY+SW_BH/2, ALM_EW, SW_BH};
/* save persistent state and log
*/
static void saveSWNV()
{
NVWriteUInt16 (NV_BCFLAGS, bc_bits);
NVWriteUInt32 (NV_CD_PERIOD, countdown_period);
uint16_t acode = alarm_hrmn;
if (alarm_state != ALMS_OFF)
acode += ALM_TOVFLOW;
NVWriteUInt16 (NV_ALARMCLOCK, acode);
logState();
}
/* 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()
{
uint8_t r, g, b;
hsvtorgb (&r, &g, &b, sw_hue, SW_HSV_S, SW_HSV_V);
sw_col = RGB565 (r, g, b);
}
/* 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, cdtime_dsp_b, false, sw_col);
}
}
/* draw the color control box
*/
static void drawColorScale()
{
// erase to remove tick marks
tft.fillRect (color_b.x, color_b.y, color_b.w, color_b.h, RA8875_BLACK);
// rainbow
for (uint16_t dx = 0; dx < color_b.w; dx++) {
uint8_t r, g, b;
uint8_t h = 255*dx/color_b.w;
hsvtorgb (&r, &g, &b, h, SW_HSV_S, SW_HSV_V);
uint16_t c = RGB565 (r, g, b);
// tft.drawLine (color_b.x + dx, color_b.y, color_b.x + dx, color_b.y + color_b.h, c);
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 digit in the given bounding box with lines the given fractional thickness of box width.
*/
static void drawDigit (const SBox &b, int digit, int frac_thick)
{
uint16_t lt = b.w/frac_thick;
uint16_t l2 = b.w/(2*frac_thick);
// erase
tft.fillRect (b.x, b.y, b.w, b.h, SW_BG);
// draw digit -- replace with drawRect to check boundaries
switch (digit) {
case 0:
tft.fillRect (b.x, b.y, b.w, lt, sw_col);
tft.fillRect (b.x, b.y+lt, lt, b.h-2*lt, sw_col);
tft.fillRect (b.x, b.y+b.h-lt, b.w, lt, sw_col);
tft.fillRect (b.x+b.w-lt, b.y+lt, lt, b.h-2*lt, sw_col);
break;
case 1:
tft.fillRect (b.x+b.w/2-l2, b.y, lt, b.h, sw_col); // center column
break;
case 2:
tft.fillRect (b.x, b.y, b.w, lt, sw_col);
tft.fillRect (b.x+b.w-lt, b.y+lt, lt, b.h/2-lt-l2, sw_col);
tft.fillRect (b.x, b.y+b.h/2-l2, b.w, lt, sw_col);
tft.fillRect (b.x, b.y+b.h/2+l2, lt, b.h/2-lt-l2, sw_col);
tft.fillRect (b.x, b.y+b.h-lt, b.w, lt, sw_col);
break;
case 3:
tft.fillRect (b.x, b.y, b.w, lt, sw_col);
tft.fillRect (b.x+b.w-lt, b.y+lt, lt, b.h-2*lt, sw_col);
tft.fillRect (b.x, b.y+b.h/2-l2, b.w-lt, lt, sw_col);
tft.fillRect (b.x, b.y+b.h-lt, b.w, lt, sw_col);
break;
case 4:
tft.fillRect (b.x, b.y, lt, b.h/2+l2, sw_col);
tft.fillRect (b.x+lt, b.y+b.h/2-l2, b.w-2*lt, lt, sw_col);
tft.fillRect (b.x+b.w-lt, b.y, lt, b.h, sw_col);
break;
case 5:
tft.fillRect (b.x, b.y, b.w, lt, sw_col);
tft.fillRect (b.x, b.y+lt, lt, b.h/2-lt-l2, sw_col);
tft.fillRect (b.x, b.y+b.h/2-l2, b.w, lt, sw_col);
tft.fillRect (b.x+b.w-lt, b.y+b.h/2+l2, lt, b.h/2-lt-l2, sw_col);
tft.fillRect (b.x, b.y+b.h-lt, b.w, lt, sw_col);
break;
case 6:
tft.fillRect (b.x, b.y, lt, b.h, sw_col);
tft.fillRect (b.x+lt, b.y, b.w-lt, lt, sw_col);
tft.fillRect (b.x+lt, b.y+b.h/2-l2, b.w-lt, lt, sw_col);
tft.fillRect (b.x+b.w-lt, b.y+b.h/2+l2, lt, b.h/2-l2-lt, sw_col);
tft.fillRect (b.x+lt, b.y+b.h-lt, b.w-lt, lt, sw_col);
break;
case 7:
tft.fillRect (b.x, b.y, b.w, lt, sw_col);
tft.fillRect (b.x+b.w-lt, b.y+lt, lt, b.h-lt, sw_col);
break;
case 8:
tft.fillRect (b.x, b.y, lt, b.h, sw_col);
tft.fillRect (b.x+b.w-lt, b.y, lt, b.h, sw_col);
tft.fillRect (b.x+lt, b.y, b.w-2*lt, lt, sw_col);
tft.fillRect (b.x+lt, b.y+b.h/2-l2, b.w-2*lt, lt, sw_col);
tft.fillRect (b.x+lt, b.y+b.h-lt, b.w-2*lt, lt, sw_col);
break;
case 9:
tft.fillRect (b.x, b.y, lt, b.h/2+l2, sw_col);
tft.fillRect (b.x+b.w-lt, b.y, lt, b.h, sw_col);
tft.fillRect (b.x+lt, b.y, b.w-2*lt, lt, sw_col);
tft.fillRect (b.x+lt, b.y+b.h/2-l2, b.w-2*lt, lt, sw_col);
break;
default:
Serial.printf (_FX("Bug! drawDigit %d\n"), digit);
break;
}
}
/* 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_FLT);
}
/* 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 (_FX("Bug! 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)
{
if (ms_left >= SW_CD_WARNDT) {
cds = SWCDS_RUNOK;
color = RA8875_GREEN;
return;
}
bool flash_on = (millis()%500) < 250; // flip at 2 Hz
if (ms_left > 0) {
if (flash_on) {
cds = SWCDS_WARN_ON;
color = DYELLOW;
} else {
cds = SWCDS_OFF;
color = RA8875_BLACK;
}
} else {
if (flash_on) {
cds = SWCDS_TIMEOUT_ON;
color = RA8875_RED;
} else {
cds = SWCDS_OFF;
color = RA8875_BLACK;
}
}
}
/* draw alarm_hrmn, pin and label if requested in various ways depending on sws_display
*/
static void drawAlarmIndicator (bool label_too)
{
// pin
setAlarmPin (alarm_state == ALMS_RINGING);
// prep
char buf[30];
selectFontStyle (BOLD_FONT, SMALL_FONT);
uint16_t a_hr = alarm_hrmn/60;
uint16_t a_mn = alarm_hrmn%60;
if (sws_display == SWD_MAIN) {
snprintf (buf, sizeof(buf), "%02d:%02d", a_hr, a_mn);
drawStringInBox (buf, alarm_hrmn_b, false, sw_col);
if (label_too) {
const char *lbl = "?";
switch (alarm_state) {
case ALMS_OFF: lbl = "Alarm off"; break;
case ALMS_ARMED: lbl = "Alarm armed"; break;
case ALMS_RINGING: lbl = "Alarm!"; break;
}
drawStringInBox (lbl, alarm_lbl_b, alarm_state == ALMS_RINGING, sw_col);
}
} else if (sws_display == SWD_BCDIGITAL || sws_display == SWD_BCANALOG) {
if (alarm_state == ALMS_OFF) {
if (label_too) {
// this is so web command set_alarm?off can actually erase the alarm box
tft.fillRect (bcalarm_b.x, bcalarm_b.y, bcalarm_b.w, bcalarm_b.h, RA8875_BLACK);
}
} else if (alarm_state == ALMS_ARMED) {
snprintf (buf, sizeof(buf), "A: %02d:%02d", a_hr, a_mn);
drawStringInBox (buf, bcalarm_b, false, sw_col);
} else if (alarm_state == ALMS_RINGING) {
drawStringInBox ("Alarm!", bcalarm_b, true, sw_col);
}
#if defined(_SHOW_ALL)
tft.drawRect (bcalarm_b.x, bcalarm_b.y, bcalarm_b.w, bcalarm_b.h, RA8875_WHITE);
#endif
}
}
/* return whether alarm has gone off since previous call.
* N.B. we assume this will be called more than once per minute
*/
static bool checkAlarm()
{
// get de time hrmn
time_t de_t0 = nowWO() + de_tz.tz_secs;
uint16_t de_hrmn = hour(de_t0)*60U + minute(de_t0);
// wentoff unless still in same minute
static uint16_t prev_de_hrmn = 24*60; // init to impossible hrmn
bool wentoff = de_hrmn == alarm_hrmn && de_hrmn != prev_de_hrmn;
prev_de_hrmn = de_hrmn;
return (wentoff);
}
/* 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
setLEDState (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_ON || cds == SWCDS_TIMEOUT_ON;
// 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)
sprintf (buf, "%d:%02d", mn, sc);
else
sprintf (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);
tft.fillRect (stopwatch_b.x, stopwatch_b.y, stopwatch_b.w, stopwatch_b.h, 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 timer";
selectFontStyle (BOLD_FONT, FAST_FONT);
uint16_t w = getTextWidth(title);
tft.setCursor (box.x + (box.w - w)/2, box.y + 3);
tft.setTextColor (RA8875_GREEN);
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, bccd_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 (F("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) {
static const char msg[] = "Time is offset";
tft.print (msg);
Serial.printf ("SW: %s\n", msg);
} else {
static const char msg[] = "Time unlocked";
tft.print (msg);
Serial.printf ("SW: %s\n", msg);
}
}
}
// persist
time_was_ok = time_ok_now;
}
/* refresh detailed date info in bcdate_b.
* N.B. we never erase because Wednesday overlays clock
*/
static void drawBCDate (int hr, int dy, int wd, int mo)
{
// day
selectFontStyle (BOLD_FONT, LARGE_FONT);
tft.setTextColor (BAC_FCOL);
tft.setCursor (bcdate_b.x, bcdate_b.y + 50);
tft.print (dayStr(wd));
// month
selectFontStyle (LIGHT_FONT, SMALL_FONT);
tft.setCursor (bcdate_b.x, bcdate_b.y + 90);
if (useMetricUnits())
tft.printf (_FX("%d %s"), dy, monthStr(mo));
else
tft.printf (_FX("%s %d"), monthStr(mo), dy);
// AM/PM only for analog or 12 hour digital
if (sws_display == SWD_BCANALOG || (bc_bits & SW_DB12HBIT)) {
tft.setCursor (bcdate_b.x, bcdate_b.y + 125);
tft.print (hr < 12 ? "AM" : "PM");
}
#if defined(_SHOW_ALL)
tft.drawRect (bcdate_b.x, bcdate_b.y, bcdate_b.w, bcdate_b.h, RA8875_WHITE);
#endif
}
/* refresh DE weather in bcwx_b, return whether successful
*/
static bool drawBCWx(void)
{
WXInfo wi;
char ynot[100];
bool ok = getCurrentWX (de_ll, true, &wi, ynot);
if (ok)
plotWX (bcwx_b, BAC_FCOL, wi);
else
plotMessage (bcwx_b, RA8875_RED, ynot);
// undo border
tft.drawRect (bcwx_b.x, bcwx_b.y, bcwx_b.w, bcwx_b.h, RA8875_BLACK);
return (ok);
}
/* draw the given big digital clock digit in the given position 0 .. 3
*/
static void drawBDCDigit (uint8_t position, uint8_t digit)
{
// bounding box
SBox b;
b.x = BDC_X0 + (BDC_W+BDC_GAP)*position + (position >= 2)*BDC_GAP;
b.y = BDC_Y0;
b.w = BDC_W;
b.h = BDC_H;
// draw
drawDigit (b, digit, BDC_FLT);
}
/* draw the digital Big Clock
*/
static void drawDigitalBigClock (bool all)
{
// persist to avoid drawing the same digits again
static time_t prev_t0; // previous report time
static uint8_t prev_mnten, prev_mnmn; // previous mins tens and unit
static uint8_t prev_hr, prev_mo, prev_dy; // previous drawn date info
// get local time now, including any user offset
time_t t0 = nowWO() + de_tz.tz_secs;
// done if same second unless all
if (!all && t0 == prev_t0)
return;
// crack open
uint8_t hr = hour(t0);
uint8_t mn = minute(t0);
int dy = day(t0);
int mo = month(t0);
uint8_t mnmn = mn%10;
// initial erase or showing date and it's a new day
if (all || ((bc_bits & SW_BCDATEBIT) && (dy != prev_dy || mo != prev_mo))) {
eraseScreen();
all = true; // insure everything gets redrawn
// date
if (bc_bits & SW_BCDATEBIT) {
drawBCDate (hr, dy, weekday(t0), mo);
prev_dy = dy;
prev_mo = mo;
}
}
// toggle punctuation every second
if (all || t0 != prev_t0) {
uint16_t x = BDC_X0 + BDC_W + BDC_GAP + BDC_W + BDC_GAP;
uint16_t color = all || (t0&1) ? sw_col : SW_BG;
tft.fillCircle (x, BDC_Y0 + BDC_H/3, BDC_CR, color);
tft.fillCircle (x, BDC_Y0 + 2*BDC_H/3, BDC_CR, color);
prev_t0 = t0;
}
// update minutes every minute
if (all || mnmn != prev_mnmn) {
// minute for sure
drawBDCDigit (3, mnmn);
prev_mnmn = mnmn;
// and tens of minutes too if changed
uint8_t mnten = mn/10;
if (all || mnten != prev_mnten) {
drawBDCDigit (2, mnten);
prev_mnten = mnten;
}
}
// update hour every hour
if (all || hr != prev_hr) {
prev_hr = hr;
if (bc_bits & SW_DB12HBIT) {
uint8_t hr12 = hr%12;
if (hr12 == 0)
hr12 = 12;
if (hr12 >= 10)
drawBDCDigit (0, hr12/10);
drawBDCDigit (1, hr12%10);
} else {
drawBDCDigit (0, hr/10);
drawBDCDigit (1, hr%10);
}
}
// update awareness
drawBCAwareness (all);
// init countdown if first call
if (all) {
drawCDTimeRemaining(true);
drawAlarmIndicator(false);
}
// update weather if desired and all or new
if ((bc_bits & SW_BCWXBIT) && (timesUp(&bc_prev_wx, bc_wxdt) || all))
bc_wxdt = drawBCWx() ? BAC_WXGDT : BAC_WXFDT;
#if defined(_SHOW_ALL)
tft.drawRect (bccd_b.x, bccd_b.y, bccd_b.w, bccd_b.h, RA8875_WHITE);
#endif
// immediate
tft.drawPR();
}
/* draw analog Big Clock
*/
static void drawAnalogBigClock (bool all)
{
// points 1 and 2 are the fat positions part way out, point 3 is the far tip, "point" 0 is the center
// persistent time measures
static time_t prev_t0; // detect change of secs
static uint8_t prev_mo, prev_dy; // previously drawn date info
// previous hand positions for motion detection and exact erasing
static int16_t prev_hrdx1, prev_hrdx2, prev_hrdx3, prev_hrdy1, prev_hrdy2, prev_hrdy3;
static int16_t prev_mndx1, prev_mndx2, prev_mndx3, prev_mndy1, prev_mndy2, prev_mndy3;
static int16_t prev_scdx3, prev_scdy3;
// get local time now, including any user offset
time_t t0 = nowWO() + de_tz.tz_secs;
// wait for second to change unless all
if (!all && t0 == prev_t0)
return;
prev_t0 = t0;
// crack open
int hr = hour(t0);
int mn = minute(t0);
int sc = second(t0);
int dy = day(t0);
int mo = month(t0);
// refresh if desired or new date (since we never erase the date)
if (all || ((bc_bits & SW_BCDATEBIT) && (dy != prev_dy || mo != prev_mo))) {
// fresh face
eraseScreen();
// face perimeter
#if defined (_IS_ESP8266)
// avoids bright flash of circle filling but doesn't fill at higher display sizes
for (uint16_t r = BAC_FR+1; r <= BAC_BEZR; r++)
tft.drawCircle (BAC_X0, BAC_Y0, r, BAC_BEZCOL);
#else
tft.fillCircle (BAC_X0, BAC_Y0, BAC_BEZR, BAC_BEZCOL);
tft.fillCircle (BAC_X0, BAC_Y0, BAC_FR, RA8875_BLACK);
#endif
tft.drawCircle (BAC_X0, BAC_Y0, BAC_FR, BAC_FCOL);
// hour points
for (int i = 0; i < 12; i++) {
float a = deg2rad(360.0F*i/12.0F);
uint16_t x0 = roundf(BAC_X0 + (BAC_FR-BAC_HTR) * cosf(a));
uint16_t y0 = roundf(BAC_Y0 + (BAC_FR-BAC_HTR) * sinf(a));
uint16_t x1 = roundf(BAC_X0 + BAC_FR * cosf(a-BAC_HTTH));
uint16_t y1 = roundf(BAC_Y0 + BAC_FR * sinf(a-BAC_HTTH));
uint16_t x2 = roundf(BAC_X0 + BAC_FR * cosf(a+BAC_HTTH));
uint16_t y2 = roundf(BAC_Y0 + BAC_FR * sinf(a+BAC_HTTH));
tft.drawLine (x0, y0, x1, y1, 1, BAC_FCOL);