-
Notifications
You must be signed in to change notification settings - Fork 1
/
commands.cpp
253 lines (208 loc) · 6.91 KB
/
commands.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
// Please format this file with clang before check-in to GitHub
/*
File: morse_dac.cpp
Date: 2022-08-20 created
Software: Barry Hansen, K7BWH, barry@k7bwh.com, Seattle, WA
Hardware: John Vanderbeck, KM7O, Seattle, WA
Purpose: Listen for incoming USB commands and handle them.
*/
#include <Arduino.h> // for Serial
#include "constants.h" // Griduino constants and colors
#include <elapsedMillis.h> // Scheduling intervals in main loop
#include "logger.h" // conditional printing to Serial port
#include "model_breadcrumbs.h" // breadcrumb trail
#include "model_gps.h" // Model of a GPS for model-view-controller
#include "model_baro.h" // Model of a barometer that stores 3-day history
#include "view.h" // View base class, public interface
// ========== extern ===========================================
extern Logger logger; // Griduino.ino
extern bool showTouchTargets; // Griduino.ino
extern Model *model; // "model" portion of model-view-controller
extern Breadcrumbs trail; // model of breadcrumb trail
extern BarometerModel baroModel; // singleton instance of the barometer model
extern void selectNewView(int cmd); // Griduino.ino
extern View *pView; // Griduino.ino
// ----- forward references
void help(), version();
void dump_kml(), dump_gps_history(), erase_gps_history(), list_files(), type_gpshistory();
void start_nmea(), stop_nmea(), start_gmt(), stop_gmt();
void view_help(), view_screen1(), view_splash(), view_crossings(), view_events();
void show_touch(), hide_touch();
void show_centerline(), hide_centerline();
void run_unittest();
// ----- table of commands
#define Newline true // use this to insert a CRLF before listing this command in help text
struct Command {
char text[20];
simpleFunction function;
bool crlf;
};
Command cmdList[] = {
{"help", help, 0},
{"version", version, 0},
{"dump kml", dump_kml, Newline},
{"dump gps", dump_gps_history, 0},
{"erase history", erase_gps_history, 0},
{"start nmea", start_nmea, Newline},
{"stop nmea", stop_nmea, 0},
{"start gmt", start_gmt, Newline},
{"stop gmt", stop_gmt, 0},
{"show touch", show_touch, Newline},
{"hide touch", hide_touch, 0},
{"show centerline", show_centerline, Newline},
{"hide centerline", hide_centerline, 0},
{"view help", view_help, Newline},
{"view splash", view_splash, 0},
{"view screen1", view_screen1, 0},
{"view crossings", view_crossings, 0},
{"view events", view_events, 0},
{"list files", list_files, Newline},
{"type gpshistory", type_gpshistory, Newline},
{"run unittest", run_unittest, 0},
};
const int numCmds = sizeof(cmdList) / sizeof(cmdList[0]);
// ----- functions to implement commands
void help() {
Serial.print("Available commands are:\n");
for (int ii = 0; ii < numCmds; ii++) {
if (cmdList[ii].crlf) {
Serial.println();
}
Serial.print(cmdList[ii].text);
if (ii < numCmds - 1) {
Serial.print(", ");
}
}
Serial.println();
}
void version() {
Serial.println(PROGRAM_TITLE " " PROGRAM_VERSION);
Serial.println("Compiled " PROGRAM_COMPILED);
Serial.println(PROGRAM_LINE1 " " PROGRAM_LINE2);
Serial.println(PROGRAM_FILE);
Serial.println(PROGRAM_GITHUB);
}
void dump_kml() {
trail.dumpHistoryKML();
}
void dump_gps_history() {
trail.dumpHistoryGPS();
}
void erase_gps_history() {
trail.clearHistory();
trail.rememberPUP();
trail.deleteFile(); // out with the old history file
logger.fencepost("commands.cpp", __LINE__);
trail.saveGPSBreadcrumbTrail(); // start over with new history file
}
void list_files() {
SaveRestore saver("x", "y"); // dummy config object, we won't actually save anything
saver.listFiles("/"); // list all files starting at root
}
void type_gpshistory() {
SaveRestoreStrings saver("/Griduino/gpshistory.csv", "No Version");
saver.typeFile();
}
void start_nmea() {
Serial.println("started NMEA");
logger.print_nmea = true;
}
void stop_nmea() {
Serial.println("stopped NMEA");
logger.print_nmea = false;
}
void start_gmt() {
Serial.println("started");
logger.print_gmt = true;
}
void stop_gmt() {
Serial.println("stopped");
logger.print_gmt = false;
}
void view_help() {
Serial.println("view Help screen");
extern /*const*/ int help_view; // Griduino.ino
extern uint viewHelpTimeout; // Griduino.ino
extern elapsedSeconds viewHelpTimer;
viewHelpTimer = 0; // reset timer
viewHelpTimeout = SECS_PER_1MIN; // a loooong time for user to read the 3-word help screen
selectNewView(help_view); // Griduino.ino
}
void view_splash() {
Serial.println("view Splash screen");
extern /*const*/ int splash_view; // see "Griduino.ino"
selectNewView(splash_view); // see "Griduino.ino"
}
void view_screen1() {
Serial.println("view Screen 1");
extern /*const*/ int screen1_view; // see "Griduino.ino"
selectNewView(screen1_view); // see "Griduino.ino"
}
void view_crossings() {
logger.info("view grid crossings");
extern /*const*/ int grid_crossings_view; // see Griduino.com
selectNewView(grid_crossings_view);
}
void view_events() {
logger.info("view calendar events");
extern /*const*/ int events_view; // see Griduino.com
selectNewView(events_view);
}
void show_touch() {
Serial.println("showing touch targets");
showTouchTargets = true;
pView->startScreen();
pView->updateScreen();
}
void hide_touch() {
Serial.println("hiding touch targets");
showTouchTargets = false;
pView->startScreen();
pView->updateScreen();
}
void show_centerline() {
Serial.println("showing centerline");
showCenterline = true;
pView->startScreen();
pView->updateScreen();
}
void hide_centerline() {
Serial.println("hiding centerline");
showCenterline = false;
pView->startScreen();
pView->updateScreen();
}
void run_unittest() {
Serial.println("running unit test suite");
void runUnitTest(); // extern declaration
runUnitTest(); // see "unit_test.cpp"
}
void removeCRLF(char *pBuffer) {
// remove 0x0d and 0x0a from character arrays, shortening the array in-place
const char key[] = "\r\n";
char *pch = strpbrk(pBuffer, key);
while (pch != NULL) {
strcpy(pch, pch + 1);
pch = strpbrk(pBuffer, key);
}
}
// do the thing
void processCommand(char *cmd) {
for (char *p = cmd; *p != '\0'; ++p) { // convert to lower case
*p = tolower(*p);
}
removeCRLF(cmd); // Arduino IDE can optionally add \r\n
Serial.print(cmd);
Serial.print(": ");
bool found = false;
for (int ii = 0; ii < numCmds; ii++) { // loop through table of commands
if (strcmp(cmd, cmdList[ii].text) == 0) { // look for it
cmdList[ii].function(); // found it! call the subroutine
found = true;
break;
}
}
if (!found) {
Serial.println("Unsupported");
}
}