-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCons.cpp
More file actions
106 lines (100 loc) · 3.36 KB
/
Cons.cpp
File metadata and controls
106 lines (100 loc) · 3.36 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
#include "Cons.hpp"
#include "Config.hpp"
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#define CONSOLEBUFFERSIZE 64
#define CR '\r'
#define LF '\n'
#define BS '\b'
#define NULLCHAR '\0'
#define SPACE ' '
/////////////////////////////////////////////////
/// \brief This is the consoles main function where commands are interpreted every tick.
/////////////////////////////////////////////////
bool Cons::getCommandLineFromSerialPort(char* commandLine) {
static uint8_t charsRead = 0; //note: COMAND_BUFFER_LENGTH must be less than 255 chars long
//read asynchronously until full command input
while (Serial.available()) {
//Serial.println("Available");
char c = Serial.read();
//Serial.printf("Received %d\n", c);
switch (c) {
case CR: //likely have full command in buffer now, commands are terminated by CR and/or LF
case LF:
commandLine[charsRead] = NULLCHAR; //null terminate our command char array
if (charsRead >= 0) {
charsRead = 0; //charsRead is static, so have to reset
//Serial.println(commandLine);
Serial.println("");
return true;
}
break;
case BS: // handle backspace in input: put a space in last char
case 0x7f:
if (charsRead > 0) { //and adjust commandLine and charsRead
commandLine[--charsRead] = NULLCHAR;
Serial.print("\b \b");
}
break;
default:
// c = tolower(c);
if (charsRead < COMMAND_BUFFER_LENGTH) {
commandLine[charsRead++] = c;
Serial.print(commandLine[charsRead - 1]);
}
commandLine[charsRead] = NULLCHAR; //just in case
break;
}
}
return false;
}
void Cons::doConsole() {
char* ptrToCommandName;
if (getCommandLineFromSerialPort(cmdLine)) {
ptrToCommandName = strtok(cmdLine, delimiters);
if (ptrToCommandName != NULL) {
auto i = cliCommands.begin();
for (; i != cliCommands.end(); i++) {
if (strcmp(ptrToCommandName, (*i)->tokenLong) == 0 || strcmp(ptrToCommandName, (*i)->tokenShort) == 0) {
if ((*i)->doCommand() != 0) {
Serial.printf(" Command failed: %s\n", ptrToCommandName);
}
break;
}
}
if (i == cliCommands.end()) {
Serial.printf(" Command not found: %s\n", ptrToCommandName);
}
}
Serial.print("BMS> ");
}
}
/////////////////////////////////////////////////
/// \brief Constructor
/////////////////////////////////////////////////
Cons::Cons(Controller* cont_inst_ptr)
: commandPrintMenu(&cliCommands),
showConfig(cont_inst_ptr->getSettingsPtr()),
setParam(cont_inst_ptr),
setDateTime(),
showStatus(cont_inst_ptr),
showGraph(cont_inst_ptr),
showCSV(cont_inst_ptr),
resetDefaultValues(cont_inst_ptr->getSettingsPtr()),
reboot() {
// initialize serial communication at 115200 bits per second:
SERIALCONSOLE.begin(115200);
SERIALCONSOLE.setTimeout(15);
controller_inst_ptr = cont_inst_ptr;
cliCommands.push_back(&commandPrintMenu);
cliCommands.push_back(&showConfig);
cliCommands.push_back(&resetDefaultValues);
cliCommands.push_back(&setParam);
cliCommands.push_back(&setDateTime);
cliCommands.push_back(&showStatus);
cliCommands.push_back(&showGraph);
cliCommands.push_back(&showCSV);
cliCommands.push_back(&reboot);
//Serial.print("Console instantiated\n");
}