-
Notifications
You must be signed in to change notification settings - Fork 2
/
Cons.hpp
287 lines (268 loc) · 7.14 KB
/
Cons.hpp
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
#include "TimeLib.h"
#include "Logger.hpp"
#include "Controller.hpp"
#include <string.h>
#include <list>
#include <TimeLib.h>
class CliCommand {
public:
virtual int doCommand() = 0;
const char* name;
const char* tokenLong;
const char* tokenShort;
const char* help;
protected:
Controller* controller_inst_ptr;
};
class CommandPrintMenu : public CliCommand {
public:
CommandPrintMenu(std::list<CliCommand*>* cliComm) {
name = "Help";
tokenLong = "help";
tokenShort = "h";
help = " | show this help";
cliCommands = cliComm;
}
int doCommand() {
//uint32_t i;
uint32_t printed;
Serial.print("\n\\||||||||/ \\||||||||/ |||||||||/ || \\||||||||/\n");
Serial.print(" || || ||\n");
Serial.print(" || \\||||||||/ |||||||||| || ||||||||||\n");
Serial.print(" || || || || ||\n");
Serial.print(" || \\||||||||/ /||||||||| |||||||||/ || ||\n\n");
Serial.print(" Battery Management System by GuilT\n");
Serial.print("\n************************* SYSTEM MENU *************************\n");
for (auto i = (*cliCommands).begin(); i != (*cliCommands).end(); i++) {
printed = strlen((*i)->tokenShort) + strlen((*i)->tokenLong);
if (printed >= 18) {
printed = 17;
}
Serial.print((*i)->tokenShort);
Serial.print(" or ");
Serial.print((*i)->tokenLong);
for (; printed < 18; printed++) {
Serial.print(" ");
}
Serial.print((*i)->help);
Serial.print("\n");
}
Serial.print("\n");
return 0;
}
private:
std::list<CliCommand*>* cliCommands;
};
class ShowConfig : public CliCommand {
public:
ShowConfig(Settings* sett) {
name = "Show Config";
tokenLong = "config";
tokenShort = "c";
help = " | show all configuration settings";
settings = sett;
}
int doCommand() {
Serial.print("GENERAL SYSTEM CONFIGURATION\n\n");
settings->printSettings();
return 0;
}
private:
Settings* settings;
};
class SetParam : public CliCommand {
public:
SetParam(Controller* cont_inst_ptr) {
name = "Set Param";
tokenLong = "set";
tokenShort = "s";
help = " | set a config parameter to a specific value";
controller_inst_ptr = cont_inst_ptr;
settings = cont_inst_ptr->getSettingsPtr();
}
int doCommand() {
char *paramName, *valStr;
Param* param;
paramName = strtok(0, 0);
if (paramName == 0) {
return -1;
} else {
paramName = strtok(paramName, " ");
param = settings->getParam(paramName);
if (param == 0) {
return -2;
} else {
valStr = strtok(0, 0);
if (valStr == 0) {
return -3;
} else {
if (param->setVal(valStr) == 0) {
if (controller_inst_ptr->saveSettings() == 0) {
return 0;
} else {
return -5;
}
} else {
return -4;
}
}
}
}
return -6;
}
private:
Settings* settings;
};
class SetDateTime : public CliCommand {
public:
SetDateTime() {
name = "Set Date Time";
tokenLong = "time";
tokenShort = "t";
help = " | set date and time using following format: yyyy-mm-ddThh:mm:ss (eg.: time 2022-10-07T09:14:24)";
}
int doCommand() {
char *yStr, *mStr, *dStr, *hStr, *minStr, *sStr;
int ret = 0;
yStr = strtok(0, "-T:");
mStr = strtok(0, "-T:");
dStr = strtok(0, "-T:");
hStr = strtok(0, "-T:");
minStr = strtok(0, "-T:");
sStr = strtok(0, "-T:");
if (yStr == 0 ) {
ret = 0;
} else if (yStr == 0 || mStr == 0 || dStr == 0 || hStr == 0 || minStr == 0 || sStr == 0) {
ret = -1;
} else {
setTime(atoi(hStr), atoi(minStr), atoi(sStr), atoi(dStr), atoi(mStr), atoi(yStr));
Teensy3Clock.set(now());
ret = 0;
}
LOG_CONSOLE("Current Time: ");
LOG_TIMESTAMP_LN(now());
return ret;
}
};
class ShowStatus : public CliCommand {
public:
ShowStatus(Controller* cont_inst_ptr) {
name = "Show Status";
tokenLong = "status";
tokenShort = "1";
help = " | shortcut to show BMS status summary";
controller_inst_ptr = cont_inst_ptr;
}
int doCommand() {
controller_inst_ptr->getBMSPtr()->printPackSummary();
LOG_CONSOLE("12V Battery: %.2fV \n", controller_inst_ptr->bat12vVoltage);
controller_inst_ptr->printControllerState();
return 0;
}
};
class ShowGraph : public CliCommand {
public:
ShowGraph(Controller* cont_inst_ptr) {
name = "Show Graph";
tokenLong = "graph";
tokenShort = "2";
help = " | show cell volatage graph";
controller_inst_ptr = cont_inst_ptr;
}
int doCommand() {
controller_inst_ptr->getBMSPtr()->printPackGraph();
return 0;
}
};
class ShowCSV : public CliCommand {
public:
ShowCSV(Controller* cont_inst_ptr) {
name = "Show CSV";
tokenLong = "CSV";
tokenShort = "3";
help = " | show BMS details in CSV format";
controller_inst_ptr = cont_inst_ptr;
}
int doCommand() {
controller_inst_ptr->getBMSPtr()->printAllCSV();
return 0;
}
};
class ResetDefaultValues : public CliCommand {
public:
ResetDefaultValues(Settings* sett) {
name = "reset default values";
tokenLong = "reset";
tokenShort = "re";
help = " | Reset all default configuration values";
settings = sett;
}
int doCommand() {
settings->reloadDefaultSettings();
return 0;
}
private:
Settings* settings;
};
class SetVerbose : public CliCommand {
public:
SetVerbose() {
name = "Verbose";
tokenLong = "verbose";
tokenShort = "v";
help = " | set verbosity eg.: v X (X=0:debug, X=1:info, X=2:warn, X=3:error, X=4:Cons)";
}
int doCommand() {
char* verbosity;
uint32_t verbo;
verbosity = strtok(0, 0);
if (verbosity != 0) {
verbo = atoi(verbosity);
if (verbo >= 0 && verbo <= 5) {
log_inst.setLoglevel((Logger::LogLevel)(verbo));
return 0;
} else {
Serial.print("logLevel out of bounds (0-5)\n");
return 2;
}
}
Serial.print("missing logLevel eg.: BMS> v 3\n");
return 1;
}
};
class Reboot : public CliCommand {
public:
Reboot(void) {
name = "reboot BMS";
tokenLong = "reboot";
tokenShort = "reboot";
help = " | performs a soft reboot.";
}
int doCommand() {
CPU_RESTART;
return 0;
}
};
class Cons {
public:
Cons(Controller* cont_inst_ptr);
void doConsole();
static const uint32_t NUMBER_OF_COMMANDS = 6;
static const uint32_t COMMAND_BUFFER_LENGTH = 64; //length of serial buffer for incoming commands
private:
char cmdLine[COMMAND_BUFFER_LENGTH + 1]; //Read commands into this buffer from Serial. +1 in length for a termination char
CommandPrintMenu commandPrintMenu;
ShowConfig showConfig;
SetParam setParam;
SetDateTime setDateTime;
ShowStatus showStatus;
ShowGraph showGraph;
ShowCSV showCSV;
SetVerbose setVerbose;
ResetDefaultValues resetDefaultValues;
Reboot reboot;
std::list<CliCommand*> cliCommands;
Controller* controller_inst_ptr;
const char* delimiters = ", \n";
bool getCommandLineFromSerialPort(char* commandLine);
};