-
Notifications
You must be signed in to change notification settings - Fork 1
/
cfg_nmea.h
271 lines (237 loc) · 9.86 KB
/
cfg_nmea.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#pragma once // Please format this file with clang before check-in to GitHub
/*
File: cfg_nmea.h
Software: Barry Hansen, K7BWH, barry@k7bwh.com, Seattle, WA
Hardware: John Vanderbeck, KM7O, Seattle, WA
Purpose: This is one of the 'control panel' screens for Griduino setup.
Since it's not intended for a driver in motion, we can use
a smaller font and cram more stuff onto the screen.
+-------------------------------------------+
| * NMEA Broadcasting > |
| |
| Send GPS position ( )[ Send NMEA ] |... yRow1
| position to |... yRow2
| USB port: (o)[ None ] |... yRow3
| |
| NMEA sentences can be used by |... yRow4
| a USB-attached computer. |... yRow5
| |
| v1.14, Feb 12 2024 |... yRow9
+-------------------------------------------+
*/
#include <Adafruit_ILI9341.h> // TFT color display library
#include "constants.h" // Griduino constants and colors
#include "logger.h" // conditional printing to Serial port
#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 start_nmea(); // commands.cpp
extern void stop_nmea(); // commands.cpp
// ========== class ViewCfgNMEA ===================================
class ViewCfgNMEA : public View {
public:
// ---------- public interface ----------
// This derived class must implement the public interface:
ViewCfgNMEA(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();
enum functionID {
SEND_NMEA = 0,
SILENT,
};
functionID selectedOption; // <-- this is the whole reason for this module's existence
protected:
// ---------- local data for this derived class ----------
// color scheme: see constants.h
// vertical placement of text rows
const int space = 30;
const int yRow1 = 80;
const int yRow2 = yRow1 + 24;
const int yRow3 = yRow2 + 24;
const int yRow4 = yRow3 + 50;
const int yRow5 = yRow4 + 24;
const int yRow9 = gScreenHeight - 10; // "v1.14, Feb 15 2024"
const int yButton1 = 62;
const int yButton2 = yButton1 + 54;
#define col1 10 // left-adjusted column of text
#define xButton 160 // indented column of buttons
#define bWidth 140 //
#define bHeight 40 //
// clang-format off
#define nTextNMEA 6
TextField txtStatic[nTextNMEA] = {
// text x, y color
{"GPS NMEA Sentences", -1, 20, cHIGHLIGHT, ALIGNCENTER},
{"Report GPS ", col1, yRow1, cVALUE},
{"positions", col1, yRow2, cVALUE},
{"NMEA sentences are available", -1, yRow4, cFAINT, ALIGNCENTER},
{"to a USB-attached computer", -1, yRow5, cFAINT, ALIGNCENTER},
{PROGRAM_VERDATE, -1, yRow9, cLABEL, ALIGNCENTER},
};
// clang-format on
enum buttonID {
eSTART_NMEA = 0,
eSTOP_NMEA,
};
// clang-format off
#define nButtonsNMEA 2
FunctionButton myButtons[nButtonsNMEA] = {
// label origin size touch-target
// text x,y w,h x,y w,h radius color functionID
{"Send NMEA", xButton, yButton1, 150, 40, {130, yButton1, 184, 50}, 4, cVALUE, eSTART_NMEA},
{"No report", xButton, yButton2, 150, 40, {130, yButton2, 184, 60}, 4, cVALUE, eSTOP_NMEA},
};
// clang-format on
// ---------- local functions for this derived class ----------
void fStartNMEA() {
selectedOption = SEND_NMEA;
start_nmea(); // start sending nmea sentences
this->updateScreen();
}
void fStopNMEA() {
selectedOption = SILENT;
stop_nmea(); // stop nmea
this->updateScreen();
}
}; // end class ViewCfgNMEA
// ============== implement public interface ================
void ViewCfgNMEA::updateScreen() {
// called on every pass through main()
// ----- show selected radio buttons by filling in the circle
for (int ii = eSTART_NMEA; ii <= eSTOP_NMEA; ii++) {
FunctionButton item = myButtons[ii];
int xCenter = item.x - 16;
int yCenter = item.y + (item.h / 2);
int buttonFillColor = cBACKGROUND;
if (ii == eSTART_NMEA && logger.print_nmea) {
buttonFillColor = cLABEL;
}
if (ii == eSTOP_NMEA && !logger.print_nmea) {
buttonFillColor = cLABEL;
}
tft->fillCircle(xCenter, yCenter, 4, buttonFillColor);
}
} // end updateScreen
void ViewCfgNMEA::startScreen() {
// called once each time this view becomes active
this->clearScreen(this->background); // clear screen
txtStatic[0].setBackground(this->background); // set background for all TextFields in this view
TextField::setTextDirty(txtStatic, nTextNMEA); // 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(myButtons, nButtonsNMEA); // optionally show this view's touch targets
showScreenBorder(); // optionally outline visible area
showScreenCenterline(); // optionally draw visual alignment bar
// ----- draw text fields
for (int ii = 0; ii < nTextNMEA; ii++) {
txtStatic[ii].print();
}
// ----- draw buttons
setFontSize(eFONTSMALLEST);
for (int ii = 0; ii < nButtonsNMEA; ii++) {
FunctionButton item = myButtons[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);
}
// ----- draw outlines of radio buttons
for (int ii = eSTART_NMEA; ii <= eSTOP_NMEA; ii++) {
FunctionButton item = myButtons[ii];
int xCenter = item.x - 16;
int yCenter = item.y + (item.h / 2);
// outline the radio button
// the active button will be indicated in updateScreen()
tft->drawCircle(xCenter, yCenter, 7, cVALUE);
}
showProgressBar(5, 8); // draw marker for advancing through settings
updateScreen(); // update UI immediately, don't wait for laggy mainline loop
} // end startScreen()
void ViewCfgNMEA::endScreen() {
// Called once each time this view becomes INactive
// This is a 'goodbye kiss' to do cleanup work
// For the current configuration screen; 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.
saveConfig();
}
bool ViewCfgNMEA::onTouch(Point touch) {
bool handled = false; // assume a touch target was not hit
for (int ii = 0; ii < nButtonsNMEA; ii++) {
FunctionButton item = myButtons[ii];
if (item.hitTarget.contains(touch)) {
handled = true; // hit!
switch (item.functionIndex) // do the thing
{
case eSTART_NMEA:
fStartNMEA();
break;
case eSTOP_NMEA:
fStopNMEA();
break;
default:
logger.error("Error, unknown function ", item.functionIndex);
break;
}
updateScreen(); // update UI immediately, don't wait for laggy mainline loop
}
}
return handled; // true=handled, false=controller uses default action
} // end onTouch()
// ========== load/save config setting =========================
#define NMEA_CONFIG_FILE CONFIG_FOLDER "/nmea.cfg"
#define NMEA_CONFIG_VERSION "NMEA Broadcast v01"
// ----- load from SDRAM -----
void ViewCfgNMEA::loadConfig() {
// Load NMEA on/off setting from NVR, and do minimal amount of work
// Since this is called first thing during setup, we can't use
// resource-heavy functions like updateScreen()
SaveRestore config(NMEA_CONFIG_FILE, NMEA_CONFIG_VERSION);
int tempOption = 0;
int result = config.readConfig((byte *)&tempOption, sizeof(tempOption));
if (result) {
switch (tempOption) {
case SEND_NMEA:
this->selectedOption = SEND_NMEA;
start_nmea(); // commands.cpp
break;
case SILENT:
this->selectedOption = SILENT;
stop_nmea(); // commands.cpp
break;
default:
// should not happen unless config file is corrupt
char msg[256];
snprintf(msg, sizeof(msg), "%s has unexpected setting: %d",
NMEA_CONFIG_FILE, tempOption);
logger.error(msg);
this->selectedOption = SILENT;
stop_nmea();
break;
}
logger.info("Loaded NMEA value: %d", this->selectedOption);
} else {
logger.error("Failed to load, re-initializing config file");
this->selectedOption = SILENT;
saveConfig();
}
}
// ----- save to SDRAM -----
void ViewCfgNMEA::saveConfig() {
SaveRestore config(NMEA_CONFIG_FILE, NMEA_CONFIG_VERSION);
logger.info("Saving value: %d", selectedOption);
int rc = config.writeConfig((byte *)&selectedOption, sizeof(selectedOption));
logger.info("Finished ViewCfgRotation::saveConfig(%d) with rc = %d", selectedOption, rc); // debug
}