-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.cpp
More file actions
423 lines (381 loc) · 10.8 KB
/
commands.cpp
File metadata and controls
423 lines (381 loc) · 10.8 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
#include <ArduinoJson.h>
#include "commands.h"
#include "constants.h"
#include "debug.h"
#include "fileSystem.h"
#include "flightLogger.h"
#include "flightLoggerData.h"
#ifdef DEV
#include "i2cscanner.h"
#endif
#include "sensor.h"
#ifdef DEV_SIM
#include "simulation.h"
#endif
#include "utilities.h"
int _readIndex;
char commandbuffer[100];
void handleFlightsListing(char *commandbuffer) {
char command = commandbuffer[0];
char command1 = commandbuffer[1];
#ifdef DEV
if (command1 == 'j') {
JsonDocument doc;
deserializeJson(doc, "[]");
JsonArray flightLogs = doc.as<JsonArray>();
_flightLogger.instance.listAsJson(flightLogs);
if (flightLogs && flightLogs.size() > 0) {
serializeJson(flightLogs, Serial);
Serial.println();
return;
}
Serial.println(F("[]"));
return;
}
#endif
_flightLogger.instance.outputSerialList();
}
void handleFlightsNumberNax(char *commandbuffer) {
Serial.print(F("$start;\n"));
char flightData[30] = "";
char temp[9] = "";
strcat(flightData, "nbrOfFlight,");
sprintf(temp, "%i,", (int)_flightLogger.instance.geFlightNbrsLast());
strcat(flightData, temp);
unsigned int chk = msgChk(flightData, sizeof(flightData));
sprintf(temp, "%i", chk);
strcat(flightData, temp);
strcat(flightData, ";\n");
Serial.print(F("$"));
Serial.print(flightData);
Serial.print(F("$end;\n"));
}
void handleFlightOutputSerial(char *commandbuffer) {
char temp[4];
bool expanded = false;
int offset = 0;
if (commandbuffer[1] == 'e') {
expanded = true;
offset = 1;
temp[0] = commandbuffer[2];
if (commandbuffer[3] != '\0') {
temp[1] = commandbuffer[3];
temp[2] = '\0';
}
else
temp[1] = '\0';
}
temp[0] = commandbuffer[1 + offset];
if (commandbuffer[3 + offset] != '\0') {
temp[1] = commandbuffer[3 + offset];
temp[2] = commandbuffer[2 + offset];
temp[3] = '\0';
}
else if (commandbuffer[2 + offset] != '\0') {
temp[1] = commandbuffer[2 + offset];
temp[2] = '\0';
}
else
temp[1] = '\0';
// Serial.print(F("Flight temp: "));
// Serial.println(temp);
// Serial.print(F("Flight expanded: "));
// Serial.println(expanded);
long number = atol(temp);
if (atol(temp) <= 0) {
Serial.print(F("Value '"));
Serial.print(temp);
Serial.println(F("' is an invalid flight number."));
return;
}
int last = _flightLogger.instance.geFlightNbrsLast();
// Serial.println(F("Last flight #"));
// Serial.println(number);
if (number > last) {
Serial.print(F("Flight #"));
Serial.print(number);
Serial.println(F(" is not a valid flight."));
return;
}
Serial.print(F("$start;\n"));
if (!expanded)
_flightLogger.instance.outputSerial(number);
else
_flightLogger.instance.outputSerialExpanded(number);
Serial.print(F("$end;\n"));
}
void handleFightsOutputSerial(char *commandbuffer) {
bool expanded = false;
if (commandbuffer[1] == 'e')
expanded = true;
// debug("expanded", expanded);
Serial.print(F("$start;\n"));
flightDataNumberStruct results = _flightLogger.instance.geFlightNbrs();
// debug("results.size", results.size);
for (int index = 0; index < results.size; index++) {
int number = results.numbers[index];
// debug("number", number);
if (!expanded)
_flightLogger.instance.outputSerial(number);
else
_flightLogger.instance.outputSerialExpanded(number);
}
Serial.print(F("$end;\n"));
}
void interpretCommandBufferHelp() {
Serial.println(F(""));
Serial.print(BOARD_NAME);
Serial.println(F(" help..."));
Serial.print(F("version\t"));
Serial.print(MAJOR_VERSION);
Serial.print(F("."));
Serial.print(MINOR_VERSION);
#if defined(DEV) || defined(DEV_SIM)
Serial.print(F(" (DEV)"));
#endif
Serial.println(F(""));
Serial.println(F("command\tdescription"));
Serial.println(F("h;\thelp menu"));
Serial.println(F("a;\toutput to serial all flight logs"));
Serial.println(F("ae;\texpanded output to serial all flight logs"));
Serial.println(F("e;\terase all recorded flights"));
#ifdef DEV
Serial.println(F("i;\tI2C scanner"));
#endif
Serial.println(F("l;\toutput to seriali a list of all flight logs"));
#ifdef DEV
Serial.println(F("lj;\toutput to seriali a list of all flight logs - json"));
#endif
Serial.println(F("n;\toutput to serial the number of recorded flight logs"));
Serial.println(F("r<#>;\toutput to serial data for flight log #"));
Serial.println(F("re<#>;\texpanded output to serial data for flight log #"));
#ifdef DEV_SIM
Serial.println(F("s;\tstart simulation with profile 1"));
Serial.println(F("s<#>;\tstart simulation with simulator profile #"));
Serial.println(F("st;\tsimulation stop"));
#endif
// Serial.println(F("z;\ttoggle flight telemetry"));
Serial.println(F("x;\tdelete last recorded flight log"));
Serial.println(F("z;\treindex recorded flight log index"));
Serial.println(F(""));
#ifdef DEBUG
Serial.println(F(""));
Serial.println(F(""));
Serial.println(F(""));
Serial.println(F(""));
Serial.println(F(""));
#endif
}
#ifdef DEV
void interpretCommandBufferI2CScanner() {
i2CScanner();
}
#endif
#ifdef DEV_SIM
void interpretCommandBufferSimulation(char *commandbuffer) {
char command1 = commandbuffer[1];
if (command1 == 'l') {
_simulation.outputSerialList();
return;
}
if (command1 == 't') {
_simulation.stop();
return;
}
int requestedNumber = 1;
if (command1 != '\0') {
char temp[4];
temp[0] = command1;
if (commandbuffer[3] != '\0') {
temp[1] = commandbuffer[2];
temp[2] = commandbuffer[3];
temp[3] = '\0';
}
else if (commandbuffer[2] != '\0') {
temp[1] = commandbuffer[2];
temp[2] = '\0';
}
else
temp[1] = '\0';
requestedNumber = atol(temp);
if (atol(temp) <= 0) {
Serial.print(F("Value '"));
Serial.print(temp);
Serial.println(F("' is an invalid simulation config number."));
return;
}
}
_simulation.start(requestedNumber, _flightLogger.altitudeInitial, _flightLogger.altitudeInitial);
}
#endif
// Available commands.
// The commands can be used via the serial command line or via the Android console
// a output to serial all flight logs
// ae expanded output to serial all flight logs
// e erase all recorded flight logs
// h help
// i i2c scanner - DEV only
// l output to seriali a list of all flight logs
// lj output to seriali a list of all flight logs - json - DEV only
// n output to serial the number of recorded flight logs
// r<#> output to serial data for the flight log #
// re<#> expanded output to serial data for flight log #
// s start simulation - DEV only
// s<#> start simulation with simulator profile #
// st stop simulation - DEV only
// t toggle flight log telemetry
// x delete last recorded flight log
// z reindex recorded flight log index
//
void interpretCommandBufferI() {
char command = commandbuffer[0];
char command1 = commandbuffer[1];
#if defined(DWBUG) && defined(DEBUG_COMMAND)
debug(F("interpretCommandBufferI.command"), command);
Serial.print(F("interpretCommandBufferI.commandBuffer="));
for (int i = 0; i < _readIndex; i++) {
Serial.print(commandbuffer[i]);
// Serial.printf("%c", commandbuffer[i]);
// Serial.printf("%d", commandbuffer[i]);
}
Serial.println(F(""));
#endif
#if defined(DWBUG) && defined(DEBUG_COMMAND)
Serial.println(F(""));
Serial.println(F(""));
Serial.println(commandbuffer[0]);
Serial.println(commandbuffer[1]);
Serial.println(F(""));
Serial.println(F(""));
Serial.println(F(""));
#endif
// output to serial all flight logs
if (command == 'a') {
handleFightsOutputSerial(commandbuffer);
return;
}
// erase all recorded flight logs
if (command == 'e') {
_flightLogger.instance.eraseFlights();
return;
}
// help
if (command == 'h') {
interpretCommandBufferHelp();
return;
}
#ifdef DEV
// i2c scanner - debug only
if (command == 'i') {
interpretCommandBufferI2CScanner();
return;
}
#endif
// output to seriali a list of all flight logs
if (command == 'l') {
handleFlightsListing(commandbuffer);
return;
}
// output to serial the number of recorded flight logs
if (command == 'n') {
handleFlightsNumberNax(commandbuffer);
return;
}
// output to serial data for the specified flight log
if (command == 'r') {
handleFlightOutputSerial(commandbuffer);
return;
}
#ifdef DEV_SIM
// start simulation - DEV only
// stop simulation - DEV only
else if (command == 's') {
interpretCommandBufferSimulation(commandbuffer);
return;
}
#endif
// toggle flight log telemetry
if (command == 't') {
// not implemented
return;
}
// recording
if (command == 'w') {
// readSensorAltitude();
return;
}
// delete last recorded flight log
if (command == 'x') {
_flightLogger.instance.eraseLast();
return;
}
#ifdef DEV
// test command
if (command == 'y') {
return;
}
#endif
// reindex recorded flight log index
if (command == 'z') {
_flightLogger.instance.reindexFlights();
return;
}
Serial.print(F("$UNKNOWN command: "));
Serial.println(commandbuffer);
}
void resetCommandBuffer() {
memset(commandbuffer, 0, sizeof(commandbuffer));
_readIndex = 0;
}
void interpretCommandBuffer() {
// #if defined(DWBUG) && defined(DEBUG_COMMAND)
// Serial.print(F("interpretCommandBuffer.commandBuffer="));
// for (int i = 0; i < _readIndex; i++) {
// Serial.print(commandbuffer[i]);
// // Serial.printf("%c", commandbuffer[i]);
// // Serial.printf("%d", commandbuffer[i]);
// }
// Serial.println(F(""));
// #endif
interpretCommandBufferI();
resetCommandBuffer();
}
bool readSerial(unsigned long timestamp, unsigned long delta) {
char readVal = ' ';
// lookup to get data from serial port...
while (Serial.available()) {
readVal = Serial.read();
// #if defined(DWBUG) && defined(DEBUG_COMMAND)
// debug(F("readSerial.readVal"), readVal);
// #endif
if (readVal == ';') {
commandbuffer[_readIndex++] = '\0';
// #if defined(DWBUG) && defined(DEBUG_COMMAND)
// Serial.print(F()"readSerial.commandBuffer.final="));
// for (int i = 0; i < _readIndex; i++)
// Serial.print(commandbuffer[i]);
// Serial.println(F(""));
// #endif
return true;
}
if (readVal != '\n') {
// #if defined(DWBUG) && defined(DEBUG_COMMAND)
// debug(F("readSerial._readIndex="), _readIndex);
// #endif
if (_readIndex >= sizeof(commandbuffer)) {
Serial.println(F("Command buffer overflow, resetting..."));
resetCommandBuffer();
return false;
}
commandbuffer[_readIndex++] = readVal;
// #if defined(DWBUG) && defined(DEBUG_COMMAND)
// Serial.print(F()"readSerial.commandBuffer="));
// for (int i = 0; i < _readIndex; i++) {
// Serial.print(commandbuffer[i]);
// }
// Serial.println(F(""));
// #endif
}
}
return false;
}