forked from collin80/TeensyRET
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SerialConsole.cpp
773 lines (727 loc) · 27.9 KB
/
SerialConsole.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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
/*
* SerialConsole.cpp
*
Copyright (c) 2014 Collin Kidder
Shamelessly copied from the version in GEVCU
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "SerialConsole.h"
#include "config.h"
#include <FlexCAN_T4.h>
extern FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> Can0;
extern FlexCAN_T4<CAN2, RX_SIZE_256, TX_SIZE_16> Can1;
SerialConsole::SerialConsole() {
init();
}
void SerialConsole::init() {
//State variables for serial console
ptrBuffer = 0;
state = STATE_ROOT_MENU;
}
void SerialConsole::printMenu() {
char buff[80];
//Show build # here as well in case people are using the native port and don't get to see the start up messages
Serial.print("Build number: ");
Serial.println(CFG_BUILD_NUM);
Serial.println("System Menu:");
Serial.println();
Serial.println("Enable line endings of some sort (LF, CR, CRLF)");
Serial.println();
Serial.println("Short Commands:");
Serial.println("h = help (displays this message)");
Serial.println("K = set all outputs high");
Serial.println("J = set all outputs low");
Serial.println("R = reset to factory defaults");
Serial.println("s = Start logging to file");
Serial.println("S = Stop logging to file");
Serial.println();
Serial.println("Config Commands (enter command=newvalue). Current values shown in parenthesis:");
Serial.println();
Logger::console("LOGLEVEL=%i - set log level (0=debug, 1=info, 2=warn, 3=error, 4=off)", settings.logLevel);
Logger::console("SYSTYPE=%i - set board type (0 = Teensy 3.1/3.2/3.5, 1 = Teensy 3.6)", settings.sysType);
Serial.println();
Logger::console("CAN0EN=%i - Enable/Disable CAN0 (0 = Disable, 1 = Enable)", settings.CAN0_Enabled);
Logger::console("CAN0SPEED=%i - Set speed of CAN0 in baud (125000, 250000, etc)", settings.CAN0Speed);
Logger::console("CAN0LISTENONLY=%i - Enable/Disable Listen Only Mode (0 = Dis, 1 = En)", settings.CAN0ListenOnly);
for (int i = 0; i < 8; i++) {
sprintf(buff, "CAN0FILTER%i=0x%%x,0x%%x,%%i,%%i (ID, Mask, Extended, Enabled)", i);
Logger::console(buff, settings.CAN0Filters[i].id, settings.CAN0Filters[i].mask,
settings.CAN0Filters[i].extended, settings.CAN0Filters[i].enabled);
}
Serial.println();
Logger::console("CAN1EN=%i - Enable/Disable CAN1 (0 = Disable, 1 = Enable)", settings.CAN1_Enabled);
Logger::console("CAN1SPEED=%i - Set speed of CAN1 in baud (125000, 250000, etc)", settings.CAN1Speed);
Logger::console("CAN1LISTENONLY=%i - Enable/Disable Listen Only Mode (0 = Dis, 1 = En)", settings.CAN1ListenOnly);
for (int i = 0; i < 8; i++) {
sprintf(buff, "CAN1FILTER%i=0x%%x,0x%%x,%%i,%%i (ID, Mask, Extended, Enabled)", i);
Logger::console(buff, settings.CAN1Filters[i].id, settings.CAN1Filters[i].mask,
settings.CAN1Filters[i].extended, settings.CAN1Filters[i].enabled);
}
Logger::console("CAN0SEND=ID,LEN,<BYTES SEPARATED BY COMMAS> - Ex: C0SEND=0x200,4,1,2,3,4");
Logger::console("CAN1SEND=ID,LEN,<BYTES SEPARATED BY COMMAS> - Ex: C1SEND=0x200,8,00,00,00,10,0xAA,0xBB,0xA0,00");
Logger::console("MARK=<Description of what you are doing> - Set a mark in the log file about what you are about to do.");
Serial.println();
Logger::console("BINSERIAL=%i - Enable/Disable Binary Sending of CANBus Frames to Serial (0=Dis, 1=En)", settings.useBinarySerialComm);
Logger::console("FILETYPE=%i - Set type of file output (0=None, 1 = Binary, 2 = GVRET, 3 = CRTD)", settings.fileOutputType);
Serial.println();
Logger::console("FILEBASE=%s - Set filename base for saving", (char *)settings.fileNameBase);
Logger::console("FILEEXT=%s - Set filename ext for saving", (char *)settings.fileNameExt);
Logger::console("FILENUM=%i - Set incrementing number for filename", settings.fileNum);
Logger::console("FILEAPPEND=%i - Append to file (no numbers) or use incrementing numbers after basename (0=Incrementing Numbers, 1=Append)", settings.appendFile);
Logger::console("FILEAUTO=%i - Automatically start logging at startup (0=No, 1 = Yes)", settings.autoStartLogging);
Serial.println();
Logger::console("DIGTOGEN=%i - Enable digital toggling system (0 = Dis, 1 = En)", digToggleSettings.enabled);
Logger::console("DIGTOGMODE=%i - Set digital toggle mode (0 = Read pin, send CAN, 1 = Receive CAN, set pin)", digToggleSettings.mode & 1);
Logger::console("DIGTOGLEVEL=%i - Set default level of digital pin (0 = LOW, 1 = HIGH)", digToggleSettings.mode >> 7);
Logger::console("DIGTOGPIN=%i - Pin to use for digital toggling system (Use Arduino Digital Pin Number)", digToggleSettings.pin);
Logger::console("DIGTOGID=%X - CAN ID to use for Rx or Tx", digToggleSettings.rxTxID);
Logger::console("DIGTOGCAN0=%i - Use CAN0 with Digital Toggling System? (0 = No, 1 = Yes)", (digToggleSettings.mode >> 1) & 1);
Logger::console("DIGTOGCAN1=%i - Use CAN1 with Digital Toggling System? (0 = No, 1 = Yes)", (digToggleSettings.mode >> 2) & 1);
Logger::console("DIGTOGLEN=%i - Length of frame to send (Tx) or validate (Rx)", digToggleSettings.length);
Logger::console("DIGTOGPAYLOAD=%X,%X,%X,%X,%X,%X,%X,%X - Payload to send or validate against (comma separated list)", digToggleSettings.payload[0],
digToggleSettings.payload[1], digToggleSettings.payload[2], digToggleSettings.payload[3], digToggleSettings.payload[4],
digToggleSettings.payload[5], digToggleSettings.payload[6], digToggleSettings.payload[7]);
}
/* There is a help menu (press H or h or ?)
This is no longer going to be a simple single character console.
Now the system can handle up to 80 input characters. Commands are submitted
by sending line ending (LF, CR, or both)
*/
void SerialConsole::rcvCharacter(uint8_t chr) {
if (chr == 10 || chr == 13) { //command done. Parse it.
handleConsoleCmd();
ptrBuffer = 0; //reset line counter once the line has been processed
} else {
cmdBuffer[ptrBuffer++] = (unsigned char) chr;
if (ptrBuffer > 79)
ptrBuffer = 79;
}
}
void SerialConsole::handleConsoleCmd() {
if (state == STATE_ROOT_MENU) {
if (ptrBuffer == 1)
{ //command is a single ascii character
handleShortCmd();
}
else //at least two bytes
{
boolean equalSign = false;
for (int i = 0; i < ptrBuffer; i++) if (cmdBuffer[i] == '=') equalSign = true;
if (equalSign) handleConfigCmd();
else handleLawicelCmd();
}
ptrBuffer = 0; //reset line counter once the line has been processed
}
}
void SerialConsole::handleLawicelCmd()
{
cmdBuffer[ptrBuffer] = 0; //make sure to null terminate
CAN_message_t outFrame;
// char buff[80];
int val;
switch (cmdBuffer[0])
{
case 't': //transmit standard frame
outFrame.id = parseHexString(cmdBuffer + 1, 3);
outFrame.len = cmdBuffer[4] - '0';
outFrame.flags.extended = 0;
if (outFrame.len < 0) outFrame.len = 0;
if (outFrame.len > 8) outFrame.len = 8;
for (int data = 0; data < outFrame.len; data++)
{
outFrame.buf[data] = parseHexString(cmdBuffer + 5 + (2 * data), 2);
}
Can0.write(outFrame);
if (SysSettings.lawicelAutoPoll) Serial.print("z");
break;
case 'T': //transmit extended frame
outFrame.id = parseHexString(cmdBuffer + 1, 8);
outFrame.len = cmdBuffer[9] - '0';
outFrame.flags.extended = 1;
if (outFrame.len < 0) outFrame.len = 0;
if (outFrame.len > 8) outFrame.len = 8;
for (int data = 0; data < outFrame.len; data++)
{
outFrame.buf[data] = parseHexString(cmdBuffer + 10 + (2 * data), 2);
}
Can0.write(outFrame);
if (SysSettings.lawicelAutoPoll) Serial.print("Z");
break;
case 'S': //setup canbus baud via predefined speeds
val = parseHexCharacter(cmdBuffer[1]);
switch (val)
{
case 0:
settings.CAN0Speed = 10000;
break;
case 1:
settings.CAN0Speed = 20000;
break;
case 2:
settings.CAN0Speed = 50000;
break;
case 3:
settings.CAN0Speed = 100000;
break;
case 4:
settings.CAN0Speed = 125000;
break;
case 5:
settings.CAN0Speed = 250000;
break;
case 6:
settings.CAN0Speed = 500000;
break;
case 7:
settings.CAN0Speed = 800000;
break;
case 8:
settings.CAN0Speed = 1000000;
break;
}
case 's': //setup canbus baud via register writes (we can't really do that...)
//settings.CAN0Speed = 250000;
break;
case 'r': //send a standard RTR frame (don't really... that's so deprecated its not even funny)
break;
case 'R': //send extended RTR frame (NO! DON'T DO IT!)
break;
case 'X': //Set autopoll off/on
if (cmdBuffer[1] == '1') SysSettings.lawicelAutoPoll = true;
else SysSettings.lawicelAutoPoll = false;
break;
case 'W': //Dual or single filter mode
break; //don't actually support this mode
case 'm': //set acceptance mask - these things seem to be odd and aren't actually implemented yet
case 'M': //set acceptance code
break; //don't do anything here yet either
case 'U': //set uart speed. We just ignore this. You can't set a baud rate on a USB CDC port
break; //also no action here
case 'Z': //Turn timestamp off/on
if (cmdBuffer[1] == '1') SysSettings.lawicelTimestamping = true;
else SysSettings.lawicelTimestamping = false;
break;
case 'Q': //turn auto start up on/off - probably don't need to actually implement this at the moment.
break; //no action yet or maybe ever
}
Serial.write(13);
}
/*For simplicity the configuration setting code uses four characters for each configuration choice. This makes things easier for
comparison purposes.
*/
void SerialConsole::handleConfigCmd() {
int i;
int newValue;
char *newString;
bool writeEEPROM = false;
bool writeDigEE = false;
char *dataTok;
//Logger::debug("Cmd size: %i", ptrBuffer);
if (ptrBuffer < 6)
return; //4 digit command, =, value is at least 6 characters
cmdBuffer[ptrBuffer] = 0; //make sure to null terminate
String cmdString = String();
unsigned char whichEntry = '0';
i = 0;
while (cmdBuffer[i] != '=' && i < ptrBuffer) {
cmdString.concat(String(cmdBuffer[i++]));
}
i++; //skip the =
if (i >= ptrBuffer)
{
Logger::console("Command needs a value..ie TORQ=3000");
Logger::console("");
return; //or, we could use this to display the parameter instead of setting
}
// strtol() is able to parse also hex values (e.g. a string "0xCAFE"), useful for enable/disable by device id
newValue = strtol((char *) (cmdBuffer + i), NULL, 0); //try to turn the string into a number
newString = (char *)(cmdBuffer + i); //leave it as a string
cmdString.toUpperCase();
if (cmdString == String("CAN0EN")) {
if (newValue < 0) newValue = 0;
if (newValue > 1) newValue = 1;
Logger::console("Setting CAN0 Enabled to %i", newValue);
settings.CAN0_Enabled = newValue;
if (newValue == 1) {
Can0.begin();
Can0.setBaudRate(settings.CAN0Speed);
if (SysSettings.CAN0EnablePin < 255) {
pinMode(SysSettings.CAN0EnablePin, OUTPUT);
digitalWrite(SysSettings.CAN0EnablePin, HIGH);
}
}
else {
// Can0.reset();
digitalWrite(SysSettings.CAN0EnablePin, LOW);
}
writeEEPROM = true;
} else if (cmdString == String("CAN1EN")) {
if (newValue < 0) newValue = 0;
if (newValue > 1) newValue = 1;
Logger::console("Setting CAN1 Enabled to %i", newValue);
if (newValue == 1) {
Can1.begin();
Can1.setBaudRate(settings.CAN1Speed);
if (SysSettings.CAN1EnablePin < 255)
{
pinMode(SysSettings.CAN1EnablePin, OUTPUT);
digitalWrite(SysSettings.CAN1EnablePin, HIGH);
}
}
else {
// Can1.reset();
digitalWrite(SysSettings.CAN1EnablePin, LOW);
}
settings.CAN1_Enabled = newValue;
writeEEPROM = true;
} else if (cmdString == String("CAN0SPEED")) {
if (newValue > 0 && newValue <= 1000000)
{
Logger::console("Setting CAN0 Baud Rate to %i", newValue);
settings.CAN0Speed = newValue;
Can0.begin();
Can0.setBaudRate(settings.CAN0Speed);
writeEEPROM = true;
}
else Logger::console("Invalid baud rate! Enter a value 1 - 1000000");
} else if (cmdString == String("CAN1SPEED")) {
if (newValue > 0 && newValue <= 1000000)
{
Logger::console("Setting CAN1 Baud Rate to %i", newValue);
settings.CAN1Speed = newValue;
Can1.begin();
Can1.setBaudRate(settings.CAN1Speed);
writeEEPROM = true;
}
else Logger::console("Invalid baud rate! Enter a value 1 - 1000000");
} else if (cmdString == String("CAN0LISTENONLY")) {
if (newValue >= 0 && newValue <= 1)
{
Logger::console("Setting CAN0 Listen Only to %i", newValue);
settings.CAN0ListenOnly = newValue;
if (settings.CAN0ListenOnly)
{
// Can0.setListenOnly(true);
}
else
{
// Can0.setListenOnly(false);
}
writeEEPROM = true;
}
else Logger::console("Invalid setting! Enter a value 0 - 1");
} else if (cmdString == String("CAN1LISTENONLY")) {
if (newValue >= 0 && newValue <= 1)
{
Logger::console("Setting CAN1 Listen Only to %i", newValue);
settings.CAN1ListenOnly = newValue;
if (settings.CAN1ListenOnly)
{
// Can1.setListenOnly(true);
}
else
{
// Can1.setListenOnly(false);
}
writeEEPROM = true;
}
else Logger::console("Invalid setting! Enter a value 0 - 1");
} else if (cmdString == String("CAN0FILTER0")) { //someone should kick me in the face for this laziness... FIX THIS!
handleFilterSet(0, 0, newString);
}
else if (cmdString == String("CAN0FILTER1")) {
if (handleFilterSet(0, 1, newString)) writeEEPROM = true;
}
else if (cmdString == String("CAN0FILTER2")) {
if (handleFilterSet(0, 2, newString)) writeEEPROM = true;
}
else if (cmdString == String("CAN0FILTER3")) {
if (handleFilterSet(0, 3, newString)) writeEEPROM = true;
}
else if (cmdString == String("CAN0FILTER4")) {
if (handleFilterSet(0, 4, newString)) writeEEPROM = true;
}
else if (cmdString == String("CAN0FILTER5")) {
if (handleFilterSet(0, 5, newString)) writeEEPROM = true;
}
else if (cmdString == String("CAN0FILTER6")) {
if (handleFilterSet(0, 6, newString)) writeEEPROM = true;
}
else if (cmdString == String("CAN0FILTER7")) {
if (handleFilterSet(0, 7, newString)) writeEEPROM = true;
}
else if (cmdString == String("CAN1FILTER0")) {
if (handleFilterSet(1, 0, newString)) writeEEPROM = true;
}
else if (cmdString == String("CAN1FILTER1")) {
if (handleFilterSet(1, 1, newString)) writeEEPROM = true;
}
else if (cmdString == String("CAN1FILTER2")) {
if (handleFilterSet(1, 2, newString)) writeEEPROM = true;
}
else if (cmdString == String("CAN1FILTER3")) {
if (handleFilterSet(1, 3, newString)) writeEEPROM = true;
}
else if (cmdString == String("CAN1FILTER4")) {
if (handleFilterSet(1, 4, newString)) writeEEPROM = true;
}
else if (cmdString == String("CAN1FILTER5")) {
if (handleFilterSet(1, 5, newString)) writeEEPROM = true;
}
else if (cmdString == String("CAN1FILTER6")) {
if (handleFilterSet(1, 6, newString)) writeEEPROM = true;
}
else if (cmdString == String("CAN1FILTER7")) {
if (handleFilterSet(1, 7, newString)) writeEEPROM = true;
}
else if (cmdString == String("CAN0SEND")) {
handleCANSend(Can0, newString);
}
else if (cmdString == String("CAN1SEND")) {
handleCANSend(Can1, newString);
}
else if (cmdString == String("MARK")) { //just ascii based for now
if (settings.fileOutputType == GVRET) Logger::file("Mark: %s", newString);
if (settings.fileOutputType == CRTD)
{
uint8_t buff[40];
sprintf((char *)buff, "%f CEV ", millis() / 1000.0f);
Logger::fileRaw(buff, strlen((char *)buff));
Logger::fileRaw((uint8_t *)newString, strlen(newString));
buff[0] = '\r';
buff[1] = '\n';
Logger::fileRaw(buff, 2);
}
if (!settings.useBinarySerialComm) Logger::console("Mark: %s", newString);
} else if (cmdString == String("BINSERIAL")) {
if (newValue < 0) newValue = 0;
if (newValue > 1) newValue = 1;
Logger::console("Setting Serial Binary Comm to %i", newValue);
settings.useBinarySerialComm = newValue;
writeEEPROM = true;
} else if (cmdString == String("FILETYPE")) {
if (newValue < 0) newValue = 0;
if (newValue > 3) newValue = 3;
Logger::console("Setting File Output Type to %i", newValue);
settings.fileOutputType = (FILEOUTPUTTYPE)newValue; //the numbers all intentionally match up so this works
writeEEPROM = true;
} else if (cmdString == String("FILEBASE")) {
Logger::console("Setting File Base Name to %s", newString);
strcpy((char *)settings.fileNameBase, newString);
writeEEPROM = true;
} else if (cmdString == String("FILEEXT")) {
Logger::console("Setting File Extension to %s", newString);
strcpy((char *)settings.fileNameExt, newString);
writeEEPROM = true;
} else if (cmdString == String("FILENUM")) {
Logger::console("Setting File Incrementing Number Base to %i", newValue);
settings.fileNum = newValue;
writeEEPROM = true;
} else if (cmdString == String("FILEAPPEND")) {
if (newValue < 0) newValue = 0;
if (newValue > 1) newValue = 1;
Logger::console("Setting File Append Mode to %i", newValue);
settings.appendFile = newValue;
writeEEPROM = true;
}
else if (cmdString == String("FILEAUTO")) {
if (newValue < 0) newValue = 0;
if (newValue > 1) newValue = 1;
Logger::console("Setting Auto File Logging Mode to %i", newValue);
settings.autoStartLogging = newValue;
writeEEPROM = true;
}
else if (cmdString == String("SYSTYPE")) {
if (newValue < 2 && newValue >= 0) {
settings.sysType = newValue;
writeEEPROM = true;
Logger::console("System type updated. Power cycle to apply.");
}
else Logger::console("Invalid system type. Please enter a value of 0 for CanDue or 1 for GEVCU");
} else if (cmdString == String("DIGTOGEN")) {
if (newValue >= 0 && newValue <= 1)
{
Logger::console("Setting Digital Toggle System Enable to %i", newValue);
digToggleSettings.enabled = newValue;
writeDigEE = true;
}
else Logger::console("Invalid enable value. Must be either 0 or 1");
} else if (cmdString == String("DIGTOGMODE")) {
if (newValue >= 0 && newValue <= 1)
{
Logger::console("Setting Digital Toggle Mode to %i", newValue);
if (newValue == 0) digToggleSettings.mode &= ~1;
if (newValue == 1) digToggleSettings.mode |= 1;
writeDigEE = true;
}
else Logger::console("Invalid mode. Must be either 0 or 1");
} else if (cmdString == String("DIGTOGLEVEL")) {
if (newValue >= 0 && newValue <= 1)
{
Logger::console("Setting Digital Toggle Starting Level to %i", newValue);
if (newValue == 0) digToggleSettings.mode &= ~0x80;
if (newValue == 1) digToggleSettings.mode |= 0x80;
writeDigEE = true;
}
else Logger::console("Invalid default level. Must be either 0 or 1");
} else if (cmdString == String("DIGTOGPIN")) {
if (newValue >= 0 && newValue <= 77)
{
Logger::console("Setting Digital Toggle Pin to %i", newValue);
digToggleSettings.pin = newValue;
writeDigEE = true;
}
else Logger::console("Invalid pin. Must be between 0 and 77");
} else if (cmdString == String("DIGTOGID")) {
if (newValue >= 0 && newValue < (1 << 30))
{
Logger::console("Setting Digital Toggle CAN ID to %X", newValue);
digToggleSettings.rxTxID = newValue;
writeDigEE = true;
}
else Logger::console("Invalid CAN ID. Must be either an 11 or 29 bit ID");
} else if (cmdString == String("DIGTOGCAN0")) {
if (newValue >= 0 && newValue <= 1)
{
Logger::console("Setting Digital Toggle CAN0 Usage to %i", newValue);
if (newValue == 0) digToggleSettings.mode &= ~2;
if (newValue == 1) digToggleSettings.mode |= 2;
writeDigEE = true;
}
else Logger::console("Invalid value. Must be either 0 or 1");
} else if (cmdString == String("DIGTOGCAN1")) {
if (newValue >= 0 && newValue <= 1)
{
Logger::console("Setting Digital Toggle CAN1 Usage to %i", newValue);
if (newValue == 0) digToggleSettings.mode &= ~4;
if (newValue == 1) digToggleSettings.mode |= 4;
writeDigEE = true;
}
else Logger::console("Invalid value. Must be either 0 or 1");
} else if (cmdString == String("DIGTOGLEN")) {
if (newValue >= 0 && newValue <= 8)
{
Logger::console("Setting Digital Toggle Frame Length to %i", newValue);
digToggleSettings.length = newValue;
writeDigEE = true;
}
else Logger::console("Invalid length. Must be between 0 and 8");
} else if (cmdString == String("DIGTOGPAYLOAD")) {
dataTok = strtok(newString, ",");
if (dataTok)
{
digToggleSettings.payload[0] = strtol(dataTok, NULL, 0);
i = 1;
while (i < 8 && dataTok)
{
dataTok = strtok(NULL, ",");
if (dataTok) {
digToggleSettings.payload[i] = strtol(dataTok, NULL, 0);
i += 1;
}
}
writeDigEE = true;
Logger::console("Set new payload bytes");
}
else Logger::console("Error processing payload");
} else if (cmdString == String("LOGLEVEL")) {
switch (newValue) {
case 0:
Logger::setLoglevel(Logger::Debug);
Logger::console("setting loglevel to 'debug'");
writeEEPROM = true;
break;
case 1:
Logger::setLoglevel(Logger::Info);
Logger::console("setting loglevel to 'info'");
writeEEPROM = true;
break;
case 2:
Logger::console("setting loglevel to 'warning'");
Logger::setLoglevel(Logger::Warn);
writeEEPROM = true;
break;
case 3:
Logger::console("setting loglevel to 'error'");
Logger::setLoglevel(Logger::Error);
writeEEPROM = true;
break;
case 4:
Logger::console("setting loglevel to 'off'");
Logger::setLoglevel(Logger::Off);
writeEEPROM = true;
break;
}
}
else {
Logger::console("Unknown command");
}
if (writeEEPROM)
{
EEPROM.put(EEPROM_ADDRESS, settings);
}
if (writeDigEE)
{
EEPROM.put(EEPROM_ADDRESS + 500, digToggleSettings);
}
}
/*
LAWICEL single letter commands are now mixed in with the other commands here.
*/
void SerialConsole::handleShortCmd() {
uint8_t val;
switch (cmdBuffer[0]) {
case 'h':
case '?':
case 'H':
printMenu();
break;
case 'R': //reset to factory defaults.
settings.version = 0xFF;
EEPROM.put(EEPROM_ADDRESS, settings);
Logger::console("Power cycle to reset to factory defaults");
break;
case 's': //start logging canbus to file
SysSettings.logToFile = true;
break;
case 'S': //stop logging canbus to file
SysSettings.logToFile = false;
break;
case 'O': //LAWICEL open canbus port (first one only because LAWICEL has no concept of dual canbus
//Can0.begin(settings.CAN0Speed, SysSettings.CAN1EnablePin);
//Can0.enable();
Serial.write(13); //send CR to mean "ok"
SysSettings.lawicelMode = true;
break;
case 'C': //LAWICEL close canbus port (First one)
// Can0.reset();
digitalWrite(SysSettings.CAN0EnablePin, LOW);
Serial.write(13); //send CR to mean "ok"
break;
case 'L': //LAWICEL open canbus port in listen only mode
Can0.begin(); //this is NOT really listen only mode but it isn't supported yet so for now...
Can0.setBaudRate(settings.CAN0Speed);
if (SysSettings.CAN0EnablePin < 255)
{
pinMode(SysSettings.CAN0EnablePin, OUTPUT);
digitalWrite(SysSettings.CAN0EnablePin, HIGH);
}
// Can0.setListenOnly(true);
Serial.write(13); //send CR to mean "ok"
SysSettings.lawicelMode = true;
break;
case 'P': //LAWICEL - poll for one waiting frame. Or, just CR if no frames
// if (Can0.available()) SysSettings.lawicelPollCounter = 1;
// else Serial.write(13); //no waiting frames
break;
case 'A': //LAWICEL - poll for all waiting frames - CR if no frames
// SysSettings.lawicelPollCounter = Can0.available();
if (SysSettings.lawicelPollCounter == 0) Serial.write(13);
break;
case 'F': //LAWICEL - read status bits
Serial.print("F00"); //bit 0 = RX Fifo Full, 1 = TX Fifo Full, 2 = Error warning, 3 = Data overrun, 5= Error passive, 6 = Arb. Lost, 7 = Bus Error
Serial.write(13);
break;
case 'V': //LAWICEL - get version number
Serial.print("V1013\n");
SysSettings.lawicelMode = true;
break;
case 'N': //LAWICEL - get serial number
Serial.print("ND00D\n");
SysSettings.lawicelMode = true;
break;
}
}
//CAN0FILTER%i=%%i,%%i,%%i,%%i (ID, Mask, Extended, Enabled)", i);
bool SerialConsole::handleFilterSet(uint8_t bus, uint8_t filter, char *values)
{
if (filter < 0 || filter > 7) return false;
if (bus < 0 || bus > 1) return false;
//there should be four tokens
char *idTok = strtok(values, ",");
char *maskTok = strtok(NULL, ",");
char *extTok = strtok(NULL, ",");
char *enTok = strtok(NULL, ",");
if (!idTok) return false; //if any of them were null then something was wrong. Abort.
if (!maskTok) return false;
if (!extTok) return false;
if (!enTok) return false;
int idVal = strtol(idTok, NULL, 0);
int maskVal = strtol(maskTok, NULL, 0);
int extVal = strtol(extTok, NULL, 0);
int enVal = strtol(enTok, NULL, 0);
Logger::console("Setting CAN%iFILTER%i to ID 0x%x Mask 0x%x Extended %i Enabled %i", bus, filter, idVal, maskVal, extVal, enVal);
if (bus == 0)
{
settings.CAN0Filters[filter].id = idVal;
settings.CAN0Filters[filter].mask = maskVal;
settings.CAN0Filters[filter].extended = extVal;
settings.CAN0Filters[filter].enabled = enVal;
//Can0.setRXFilter(filter, idVal, maskVal, extVal);
}
else if (bus == 1)
{
settings.CAN1Filters[filter].id = idVal;
settings.CAN1Filters[filter].mask = maskVal;
settings.CAN1Filters[filter].extended = extVal;
settings.CAN1Filters[filter].enabled = enVal;
//Can1.setRXFilter(filter, idVal, maskVal, extVal);
}
return true;
}
bool SerialConsole::handleCANSend(FlexCAN_T4_Base &port, char *inputString)
{
char *idTok = strtok(inputString, ",");
char *lenTok = strtok(NULL, ",");
char *dataTok;
CAN_message_t frame;
if (!idTok) return false;
if (!lenTok) return false;
int idVal = strtol(idTok, NULL, 0);
int lenVal = strtol(lenTok, NULL, 0);
for (int i = 0; i < lenVal; i++)
{
dataTok = strtok(NULL, ",");
if (!dataTok) return false;
frame.buf[i] = strtol(dataTok, NULL, 0);
}
//things seem good so try to send the frame.
frame.id = idVal;
if (idVal >= 0x7FF) frame.flags.extended = 1;
else frame.flags.extended = 0;
frame.len = lenVal;
port.write(frame);
Logger::console("Sending frame with id: 0x%x len: %i", frame.id, frame.len);
SysSettings.txToggle = !SysSettings.txToggle;
//setLED(SysSettings.LED_CANTX, SysSettings.txToggle);
return true;
}
unsigned int SerialConsole::parseHexCharacter(char chr)
{
unsigned int result = 0;
if (chr >= '0' && chr <= '9') result = chr - '0';
else if (chr >= 'A' && chr <= 'F') result = 10 + chr - 'A';
else if (chr >= 'a' && chr <= 'f') result = 10 + chr - 'a';
return result;
}
unsigned int SerialConsole::parseHexString(char *str, int length)
{
unsigned int result = 0;
for (int i = 0; i < length; i++) result += parseHexCharacter(str[i]) << (4 * (length - i - 1));
return result;
}