-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerialMonitor.cpp
536 lines (460 loc) · 11.4 KB
/
SerialMonitor.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
/*
* Copyright (C) 2014 Lars Marowsky-Bree <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "SerialMonitor.h"
#include "Sources.h"
#include "Outputs.h"
#include "Lowlevel.h"
#include <DueTimer.h>
static char serialCmd[128] = "\0";
// Ugly global variables for parsing strings
static char *P_s, *P_r;
static int parse_start() {
P_r = NULL;
P_s = strtok_r(serialCmd, DELIM, &P_r);
/* Skip initial command */
P_s = strtok_r(NULL, DELIM, &P_r);
}
static bool parse_char(char *c) {
if (!c)
return false;
if (!P_s)
return false;
if (*P_s == 0) {
return false;
} else {
*c = *P_s;
P_s = strtok_r(NULL, DELIM, &P_r);
return true;
}
}
static int parse_int(int *v) {
if (!v)
return false;
if (!P_s)
return false;
if (*P_s == 0) {
return false;
} else {
*v = atoi(P_s);
P_s = strtok_r(NULL, DELIM, &P_r);
return true;
}
}
static bool parse_str(char *c, int len) {
if (!c)
return false;
if (!P_s)
return false;
if (*P_s == 0) {
return false;
} else {
strlcpy(c, P_s, len);
P_s = strtok_r(NULL, DELIM, &P_r);
return true;
}
}
static void cmd_stop() {
Master.started = false;
Timer1.stop();
}
static void cmd_start() {
Master.started = true;
Timer1.start(Master.period);
}
static void cmd_source_add() {
char k;
char portname[16];
int period;
int avg;
int mode;
int delta;
if (!parse_char(&k))
return;
if (!parse_str(portname, sizeof(portname)))
return;
if (!parse_int(&period))
return;
if (!parse_int(&avg))
return;
if (!parse_int(&mode))
return;
if (!parse_int(&delta))
return;
if (debug) {
SerialUSB.print("DEBUG Adding source: ");
SerialUSB.print(k);
SerialUSB.print(DELIM);
SerialUSB.print(portname);
SerialUSB.print(DELIM);
SerialUSB.print(period);
SerialUSB.print(DELIM);
SerialUSB.print(avg);
SerialUSB.print(DELIM);
SerialUSB.print(mode);
SerialUSB.print(DELIM);
SerialUSB.println(delta);
}
source_add(k, portname, period, avg, mode, delta);
}
// Technically, this also only modifies the Sources table
// But that function really, really was getting too large
static void cmd_source_attach_irq() {
char k;
char portname[16];
int trigger;
int count_ticks;
if (!parse_char(&k))
return;
if (!parse_str(portname, sizeof(portname)))
return;
if (!parse_int(&trigger))
return;
if (!parse_int(&count_ticks))
return;
if (debug) {
SerialUSB.print("DEBUG Attaching interupt to source: ");
SerialUSB.print(k);
SerialUSB.print(DELIM);
SerialUSB.print(portname);
SerialUSB.print(DELIM);
SerialUSB.print(trigger);
SerialUSB.print(DELIM);
SerialUSB.println(count_ticks);
}
source_attach_irq(k, portname, trigger, count_ticks);
}
static void cmd_source_del() {
char k;
if (!parse_char(&k))
return;
if (debug) {
SerialUSB.print("DEBUG Deleting source: ");
SerialUSB.println(k);
}
source_del(k);
}
static void cmd_output_add() {
char k;
char portname[16];
int period;
int step;
int offset;
int mode;
char name[32];
if (!parse_char(&k))
return;
if (!parse_str(portname, sizeof(portname)))
return;
if (!parse_int(&period))
return;
if (!parse_int(&step))
return;
if (!parse_int(&offset))
return;
if (!parse_int(&mode))
return;
if (!parse_str(name, sizeof(name)))
return;
if (debug) {
SerialUSB.print("DEBUG Adding output: ");
SerialUSB.print(k);
SerialUSB.print(" port: ");
SerialUSB.print(portname);
SerialUSB.print(" period: ");
SerialUSB.print(period);
SerialUSB.print(" step: ");
SerialUSB.print(step);
SerialUSB.print(" offset: ");
SerialUSB.print(offset);
SerialUSB.print(" mode: ");
SerialUSB.print(mode);
SerialUSB.print(" pattern: ");
SerialUSB.println(name);
}
output_add(k, portname, period, step, offset, mode, name);
}
static void cmd_output_del() {
char k;
if (!parse_char(&k))
return;
if (debug) {
SerialUSB.print("DEBUG Deleting output: ");
SerialUSB.println(k);
}
output_del(k);
}
// It's because of this function primarily that so many data structures
// are exposed globally, but it is quite useful for debugging.
static void cmd_dump() {
int i;
SerialUSB.print("INFO Timer period: ");
SerialUSB.print(Master.period);
SerialUSB.print(" Currently enabled: ");
SerialUSB.println(Master.started);
SerialUSB.print("INFO Sources: ");
SerialUSB.println((int)Sources.entries);
for (i = 0; i < Sources.entries; i++) {
tSourceEntry *s = &Sources.s[i];
SerialUSB.print(" Key: ");
SerialUSB.print(s->k);
SerialUSB.print(" Port: ");
SerialUSB.print(PortList[s->p].name);
SerialUSB.print(" Method: ");
SerialUSB.print(s->method);
SerialUSB.print(" Period: ");
SerialUSB.print(s->period);
SerialUSB.print(" Avg: ");
SerialUSB.print(s->avg);
SerialUSB.print(" Mode: ");
SerialUSB.print(s->mode);
SerialUSB.print(" Delta: ");
SerialUSB.println(s->delta);
if (s->irq) {
SerialUSB.print(" IRQ: ");
SerialUSB.print(s->irq);
SerialUSB.print(" Trigger: ");
SerialUSB.print(s->trigger);
SerialUSB.print(" Counter: ");
SerialUSB.println(s->count_ticks);
}
SerialUSB.print(" Index: ");
SerialUSB.println(i);
}
SerialUSB.print("INFO Outputs: ");
SerialUSB.println((int)Outputs.entries);
for (i = 0; i < Outputs.entries; i++) {
tOutputEntry *out = &Outputs.out[i];
SerialUSB.print(i);
SerialUSB.print(DELIM);
SerialUSB.print("output_add ");
SerialUSB.print(out->k);
SerialUSB.print(DELIM);
SerialUSB.print(PortList[out->p].name);
SerialUSB.print(DELIM);
SerialUSB.print(out->period);
SerialUSB.print(DELIM);
SerialUSB.print(out->step);
SerialUSB.print(DELIM);
SerialUSB.print(out->offset);
SerialUSB.print(DELIM);
SerialUSB.print(out->mode);
SerialUSB.print(DELIM);
SerialUSB.println(out->v->name);
SerialUSB.print(" Status: Countdown: ");
SerialUSB.print(out->countdown);
SerialUSB.print(" pos: ");
SerialUSB.println(out->last_step);
}
SerialUSB.print("INFO Ringbuffer entries: ");
SerialUSB.print(rb.entries());
SerialUSB.print(" Has overflown: ");
SerialUSB.println((int)rb.overflow());
}
static void cmd_pattern_list() {
int i, j;
// Not included in general output dump because it's rather
// annoying long
SerialUSB.print("INFO Patterns: ");
SerialUSB.print((int)PatternCount);
for (i = 0; i < PatternCount; i++) {
SerialUSB.print("Name: ");
SerialUSB.print(Patterns[i].name);
SerialUSB.print(" Length: ");
SerialUSB.print(Patterns[i].len);
SerialUSB.print(" Pattern:");
for (j = 0; j < Patterns[i].len; j++) {
SerialUSB.print(DELIM);
SerialUSB.print(Patterns[i].v[j]);
}
SerialUSB.println("");
}
SerialUSB.println("");
}
static void cmd_write() {
int value;
char portname[16];
if (!parse_str(portname, sizeof(portname)))
return;
if (!parse_int(&value))
return;
if (debug) {
SerialUSB.print("DEBUG Write: ");
SerialUSB.print(portname);
SerialUSB.print(DELIM);
SerialUSB.println(value);
}
port_write(portname, value);
}
static void cmd_writed() {
int port;
int value;
if (!parse_int(&port))
return;
if (!parse_int(&value))
return;
if (debug) {
SerialUSB.print("DEBUG Write Digital: ");
SerialUSB.print(port);
SerialUSB.print(DELIM);
SerialUSB.println(value);
}
if (value > 0)
digitalWrite(port, HIGH);
else
digitalWrite(port, LOW);
}
static void cmd_read() {
int v;
char k;
char portname[16];
if (!parse_char(&k))
return;
if (!parse_str(portname, sizeof(portname)))
return;
v = port_read(portname);
SerialMonitor_log(micros(), k, v);
}
static void cmd_output_reset() {
if (debug) SerialUSB.println("DEBUG All outputs back to 0.");
outputs_reset();
}
static void cmd_pin() {
char portname[16];
int mode;
int port;
if (!parse_str(portname, sizeof(portname)))
return;
if (!parse_int(&mode))
return;
port = port_name2id(portname);
if (!PIN_OK(port)) {
SerialUSB.println("WARN Port not valid");
return;
}
switch (mode) {
case 0: pinMode(port, INPUT);
if (debug) SerialUSB.println("DEBUG Pin set to INPUT");
break;
case 1: pinMode(port, INPUT_PULLUP);
if (debug) SerialUSB.println("DEBUG Pin set to INPUT_PULLUP");
break;
case 2: pinMode(port, OUTPUT);
if (debug) SerialUSB.println("DEBUG Pin set to OUTPUT");
break;
default:
if (debug) SerialUSB.println("WARN Unknown pin mode.");
break;
}
}
static void cmd_debug() {
int lvl;
if (!parse_int(&lvl))
return;
if (debug) {
SerialUSB.print("DEBUG Setting debug level ");
SerialUSB.println(lvl);
}
debug = lvl;
}
static void cmd_clear() {
Timer1.stop();
Master.period = 0;
Master.started = 0;
sources_setup();
outputs_setup();
SerialUSB.println("INFO All clear");
}
static void cmd_help(void);
typedef struct tCmdTableEntry {
const char *cmd;
void (*handler)();
} tCmdTableEntry;
static tCmdTableEntry CmdTable[] = {
{ .cmd = "stop", .handler = &cmd_stop },
{ .cmd = "start", .handler = &cmd_start },
{ .cmd = "source_add", .handler = &cmd_source_add },
{ .cmd = "source_attach_irq", .handler = &cmd_source_attach_irq },
{ .cmd = "source_del", .handler = &cmd_source_del },
{ .cmd = "output_add", .handler = &cmd_output_add },
{ .cmd = "output_reset", .handler = &cmd_output_reset },
{ .cmd = "output_del", .handler = &cmd_output_del },
{ .cmd = "pattern_list", .handler = &cmd_pattern_list },
{ .cmd = "pin", .handler = &cmd_pin },
{ .cmd = "debug", .handler = &cmd_debug },
{ .cmd = "dump", .handler = &cmd_dump },
{ .cmd = "clear", .handler = &cmd_clear },
{ .cmd = "help", .handler = &cmd_help },
{ .cmd = "writed", .handler = &cmd_writed },
{ .cmd = "write", .handler = &cmd_write },
{ .cmd = "read", .handler = &cmd_read }
};
static void cmd_help() {
int i;
SerialUSB.println("INFO Valid commands:");
for (i = 0; i < sizeof(CmdTable) / sizeof(tCmdTableEntry); i++) {
SerialUSB.print(" ");
SerialUSB.println(CmdTable[i].cmd);
}
}
static void cmd_execute() {
int i;
if (debug) {
SerialUSB.print("DEBUG Command was: ");
SerialUSB.println(serialCmd);
}
for (i = 0; i < sizeof(CmdTable) / sizeof(tCmdTableEntry); i++) {
if (strncmp(serialCmd, CmdTable[i].cmd, strlen(CmdTable[i].cmd)) == 0) {
parse_start();
CmdTable[i].handler();
}
}
}
void SerialMonitor_log(unsigned long t, char k, int v) {
static unsigned long last_t = 0;
if (!last_t)
last_t = t;
SerialUSB.print("VAL");
SerialUSB.print(DELIM);
SerialUSB.print((unsigned long)(t-last_t));
SerialUSB.print(DELIM);
SerialUSB.print(k);
SerialUSB.print(DELIM);
SerialUSB.println(v);
last_t = t;
}
void SerialMonitor_poll(void) {
int c;
if (SerialUSB.available()) {
c = SerialUSB.read();
switch (c) {
case '\r': cmd_execute();
case '\b': memset(serialCmd, 0, sizeof(serialCmd));
break;
default:
int l = strlen(serialCmd);
if (l < sizeof(serialCmd)-2)
serialCmd[l] = c;
else
SerialUSB.println("ERROR Input buffer overflow");
}
}
}
void SerialMonitor_setup(void) {
memset(serialCmd, 0, sizeof(serialCmd));
SerialUSB.begin(115200);
}