17 while (UART->available() > 0) {
18 char incomingByte =
static_cast<char>(UART->read());
24 incomingByte =
static_cast<char>(UART->read());
25 if(incomingByte ==
'[')
28 incomingByte =
static_cast<char>(UART->read());
30 if (incomingByte ==
'A') {
32 if (historyIndex > 0) {
35 displayCommandFromHistory();
36 }
else if (incomingByte ==
'B') {
41 displayCommandFromHistory();
44 UART->println(
"Unknown escape sequence: ");
50 if (!fullLine.empty()) {
54 if(!inputBuffer.empty()){
55 inputBuffer.pop_back();
62 else if (incomingByte ==
' ')
64 UART->print(incomingByte);
65 fullLine += incomingByte;
67 if (!inputBuffer.empty()) {
68 if (!isCommandParsed) {
69 command = inputBuffer;
70 isCommandParsed =
true;
73 argumentQueue.push(inputBuffer);
74 argSize[historyIndex]++;
80 else if (incomingByte ==
'\n' || incomingByte ==
'\r')
85 if (!inputBuffer.empty()) {
86 if(isCommandParsed || !command.empty())
88 argumentQueue.push(inputBuffer);
89 argSize[historyIndex]++;
93 command = inputBuffer;
97 if(!command.empty() || !argumentQueue.empty())
100 commandHistory[historyIndex] = command;
101 fullLineHistory[historyIndex] = fullLine;
104 while (!argumentQueue.empty()) {
105 argumentHistory[historyIndex][idx] = argumentQueue.front();
115 isCommandParsed =
false;
122 inputBuffer += incomingByte;
123 fullLine += incomingByte;
124 UART->print(incomingByte);
127 UART->println(
"Buffer overflow, input ignored.");
134void CommandLine::displayCommandFromHistory() {
147 argumentHistory[i][idx] =
"";
149 commandHistory[i] =
"";
150 fullLineHistory[i] =
"";
154 if (historyIndex >= 0 && historyIndex <
MAX_HISTORY) {
161 while (!argumentQueue.empty()) {
162 argumentQueue.front();
166 fullLine = fullLineHistory[historyIndex];
167 command = commandHistory[historyIndex];
170 for (
int idx = 0; idx < argSize[historyIndex]; idx++) {
171 argumentQueue.push(argumentHistory[historyIndex][idx]);
184 UART->print(fullLine.c_str());
191void CommandLine::addCommand(
const std::string& longName,
const std::string& shortName, std::function<
void(std::queue<std::string>, std::string&)> funcPtr) {
192 Command newCommand{ longName, shortName, funcPtr };
193 commands.push_back(newCommand);
199 if (command ==
"help" || command ==
"?") {
204 std::string response;
205 for (
const auto& cmd : commands) {
206 if (cmd.longName == command || cmd.shortName == command) {
208 cmd.funcPtr(arguments, response);
213 UART->println(
"Command not found");
218void CommandLine::help(){
219 if (commands.empty()) {
220 UART->
println(
"No commands available.");
225 for (
const auto& cmd : commands) {
226 UART->println(String(cmd.longName.c_str()) +
"<" + String(cmd.shortName.c_str()) +
">");
228 UART->println(
"help<?>");
232void CommandLine::trimSpaces(std::string& str) {
234 size_t start = str.find_first_not_of(
" ");
236 if (start != std::string::npos) {
238 size_t end = str.find_last_not_of(
" ");
239 str = str.substr(start, end - start + 1);
constexpr int MAX_ARGUMENTS
constexpr int UART_BUFFER_SIZE
constexpr int MAX_ROW_LENGTH
constexpr int MAX_HISTORY
void executeCommand(const std::string &command, std::queue< std::string > arugments)
void addCommand(const std::string &longName, const std::string &shortName, std::function< void(std::queue< std::string > argumentQueue, std::string &)> funcPtr)
CommandLine(Stream *UART)
void println(const T &message)