-
Notifications
You must be signed in to change notification settings - Fork 1
/
view_time.h
252 lines (218 loc) · 10 KB
/
view_time.h
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
#pragma once // Please format this file with clang before check-in to GitHub
/*
File: view_time.h
GMT_clock - bright colorful Greenwich Mean Time based on GPS
Version history:
2021-07-11 fixed saving local time zone offset
2020-10-15 refactored from .cpp to .h
2020-04-22 created
2020-04-29 added touch adjustment of local time zone
2020-05-01 added save/restore to nonvolatile RAM
2020-05-12 updated TouchScreen code
2020-05-13 proportional fonts
2020-06-02 merged from standalone example into Griduino view
Software: Barry Hansen, K7BWH, barry@k7bwh.com, Seattle, WA
Hardware: John Vanderbeck, KM7O, Seattle, WA
Purpose: This is a bright colorful GMT clock for your shack
or dashboard. After all, once a rover arrives at a
destination they no longer need grid square navigation.
+-----------------------------------------+
| Griduino GMT Clock |
| HHHHH HHHH : MMMM MMMM : SSSS SSSS |
| HHHHH HHHH : MMMM MMMM : SSSS SSSS |
| HHHHH HHHH : MMMM MMMM : SSSS SSSS |
| HHHHH HHHH : MMMM MMMM : SSSS SSSS |
| HHHHH HHHH : MMMM MMMM : SSSS SSSS |
| |
| GMT Date: Dec 19, 2020 |
| |
| Internal Temp: 101.7 F |
| |
+-------+ +-------+
| +1 hr | Local: HH:MM:SS | -1 hr |
+-------+-------------------------+-------+
*/
#include <Adafruit_ILI9341.h> // TFT color display library
#include "constants.h" // Griduino constants and colors
#include "logger.h" // conditional printing to Serial port
#include "model_gps.h" // Model of a GPS for model-view-controller
#include "save_restore.h" // Save configuration in non-volatile RAM
#include "TextField.h" // Optimize TFT display text for proportional fonts
#include "view.h" // Base class for all views
// ========== extern ===========================================
extern Logger logger; // Griduino.ino
extern Model *model; // "model" portion of model-view-controller
extern void showDefaultTouchTargets(); // Griduino.ino
extern void getDate(char *result, int maxlen); // model_gps.h
// ========== class ViewTime ===================================
class ViewTime : public View {
public:
// ---------- public interface ----------
// This derived class must implement the public interface:
ViewTime(Adafruit_ILI9341 *vtft, int vid) // ctor
: View{vtft, vid} {
background = cBACKGROUND; // every view can have its own background color
}
void updateScreen();
void startScreen();
void endScreen();
bool onTouch(Point touch);
// void loadConfig();
// void saveConfig();
protected:
// ---------- local data for this derived class ----------
// color scheme: see constants.h
// ========== text screen layout ===================================
// these are names for the array indexes, must be named in same order as array below
enum txtIndex {
TITLE = 0,
HOURS,
COLON1,
MINUTES,
COLON2,
SECONDS,
GMTDATE,
DEGREES,
LOCALTIME,
TIMEZONE,
NUMSATS,
};
#define numClockFields 11
TextField txtClock[numClockFields] = {
// text x,y color align font
{"Griduino GMT", -1, 18, cTITLE, ALIGNCENTER, eFONTSMALLEST}, // [TITLE] program title, centered
{"hh", 12, 94, cVALUE, ALIGNLEFT, eFONTGIANT}, // [HOURS] giant clock hours
{":", 94, 94, cVALUE, ALIGNLEFT, eFONTGIANT}, // [COLON1] :
{"mm", 120, 94, cVALUE, ALIGNLEFT, eFONTGIANT}, // [MINUTES] giant clock minutes
{":", 204, 94, cVALUE, ALIGNLEFT, eFONTGIANT}, // [COLON2] :
{"ss", 230, 94, cVALUE, ALIGNLEFT, eFONTGIANT}, // [SECONDS] giant clock seconds
{"MMM dd, yyyy", -1, 140, cVALUE, ALIGNCENTER, eFONTSMALL}, // [GMTDATE] GMT date
{"", -1, 174, cVALUE, ALIGNCENTER, eFONTSMALL}, // [DEGREES] Temperature e.g. "12.3 F"
{"hh:mm:ss", 118, 226, cTEXTCOLOR, ALIGNLEFT, eFONTSMALL}, // [LOCALTIME] Local time
{"-7h", 8, 226, cFAINT, ALIGNLEFT, eFONTSMALLEST}, // [TIMEZONE] addHours time zone
{"6#", 308, 226, cFAINT, ALIGNRIGHT, eFONTSMALLEST}, // [NUMSATS] numSats
};
enum buttonID {
etimeZonePlus,
etimeZoneMinus,
};
#define nTimeButtons 2
FunctionButton timeButtons[nTimeButtons] = {
// For "GMT_clock" we have rather small modest +/- buttons, meant to visually
// fade a little into the background. However, we want larger touch-targets to
// make them easy to press.
//
// 3.2" display is 320 x 240 pixels, landscape, (y=239 reserved for activity bar)
//
// label origin size touch-target
// text x,y w,h x,y w,h radius color functionID
{"+", 66, 204, 36, 30, {30, 180, 110, 59}, 4, cTEXTCOLOR, etimeZonePlus}, // Up
{"-", 226, 204, 36, 30, {190, 180, 110, 59}, 4, cTEXTCOLOR, etimeZoneMinus}, // Down
};
}; // end class ViewTime
// ============== implement public interface ================
void ViewTime::updateScreen() {
// called on every pass through main()
// GMT Time
char sHour[8], sMinute[8], sSeconds[8];
snprintf(sHour, sizeof(sHour), "%02d", GPS.hour);
snprintf(sMinute, sizeof(sMinute), "%02d", GPS.minute);
snprintf(sSeconds, sizeof(sSeconds), "%02d", GPS.seconds);
txtClock[HOURS].print(sHour);
txtClock[MINUTES].print(sMinute);
txtClock[SECONDS].print(sSeconds);
txtClock[COLON2].dirty = true; // re-draw COLON2 because it was erased by SECONDS field
txtClock[COLON2].print();
// GMT Date
char sDate[16]; // strlen("Jan 12, 2020 ") = 14
model->getDate(sDate, sizeof(sDate));
txtClock[GMTDATE].print(sDate);
// Hours to add/subtract from GMT for local time
char sign[2] = {0, 0}; // prepend a plus-sign when >=0
sign[0] = (model->gTimeZone >= 0) ? '+' : 0; // (don't need to add a minus-sign bc the print stmt does that for us)
char sTimeZone[6]; // strlen("-10h") = 4
snprintf(sTimeZone, sizeof(sTimeZone), "%s%dh", sign, model->gTimeZone);
txtClock[TIMEZONE].print(sTimeZone);
// Local Time
char sTime[10]; // strlen("01:23:45") = 8
model->getTimeLocal(sTime, sizeof(sTime));
txtClock[LOCALTIME].print(sTime);
// Satellite Count
char sBirds[4]; // strlen("5#") = 2
snprintf(sBirds, sizeof(sBirds), "%d#", model->gSatellites);
// change colors by number of birds
txtClock[NUMSATS].color = (model->gSatellites < 1) ? cWARN : cFAINT;
txtClock[NUMSATS].print(sBirds);
// txtClock[NUMSATS].dump(); // debug
} // end updateScreen
void ViewTime::startScreen() {
// called once each time this view becomes active
this->clearScreen(this->background); // clear screen
txtClock[0].setBackground(this->background); // set background for all TextFields in this view
TextField::setTextDirty(txtClock, numClockFields); // make sure all fields get re-printed on screen change
drawAllIcons(); // draw gear (settings) and arrow (next screen)
showDefaultTouchTargets(); // optionally draw box around default button-touch areas
showMyTouchTargets(timeButtons, nTimeButtons);
showScreenBorder(); // optionally outline visible area
showScreenCenterline(); // optionally draw visual alignment bar
// ----- draw page title
txtClock[TITLE].print();
// ----- draw giant fields
setFontSize(36);
for (int ii = HOURS; ii <= SECONDS; ii++) {
txtClock[ii].print();
}
// ----- draw text fields
setFontSize(eFONTSMALLEST);
for (int ii = GMTDATE; ii < numClockFields; ii++) {
txtClock[ii].print();
}
// ----- draw buttons
setFontSize(eFONTSMALL);
for (int ii = 0; ii < nTimeButtons; ii++) {
FunctionButton item = timeButtons[ii];
tft->fillRoundRect(item.x, item.y, item.w, item.h, item.radius, cBUTTONFILL);
tft->drawRoundRect(item.x, item.y, item.w, item.h, item.radius, cBUTTONOUTLINE);
// ----- label on top of button
int xx = getOffsetToCenterTextOnButton(item.text, item.x, item.w);
tft->setCursor(xx, item.y + item.h / 2 + 5); // place text centered inside button
tft->setTextColor(item.color);
tft->print(item.text);
}
updateScreen(); // update UI immediately, don't wait for laggy mainline loop
} // end startScreen()
void ViewTime::endScreen() {
// Called once each time this view becomes INactive
// This is a 'goodbye kiss' to do cleanup work
// For the GMT time view, the local timezone is part of the model.
// We save our settings here instead of on each button press
// because writing to NVR is slow (0.5 sec) and would delay the user
// while trying to press a button many times in a row.
//model->save(); // BUT! this slows down showing the next view
// TODO: schedule the "model->save" until some idle time with no
// user input, say 10 seconds of no touches
}
bool ViewTime::onTouch(Point touch) {
logger.info("->->-> Touched time screen.");
bool handled = false; // assume a touch target was not hit
for (int ii = 0; ii < nTimeButtons; ii++) {
FunctionButton item = timeButtons[ii];
if (item.hitTarget.contains(touch)) {
handled = true; // hit!
switch (item.functionIndex) // do the thing
{
case etimeZonePlus:
model->timeZonePlus();
break;
case etimeZoneMinus:
model->timeZoneMinus();
break;
default:
logger.error("Error, unknown function ", item.functionIndex);
break;
}
updateScreen(); // show the result
}
}
return handled; // true=handled, false=controller uses default action
} // end onTouch()