-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUARTCommandHandler.h
More file actions
71 lines (56 loc) · 2.03 KB
/
UARTCommandHandler.h
File metadata and controls
71 lines (56 loc) · 2.03 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
#ifndef UARTCOMMANDHANDLER_H
#define UARTCOMMANDHANDLER_H
#include <functional>
#include <queue>
#include <string>
#include <vector>
#include "ArduinoHAL.h"
#define SHELL_PROMPT "AS> " // AS = Avionics Shell
enum class AsciiKey : uint8_t {
Escape = 27,
UpArrow = 65,
DownArrow = 66,
Backspace = 8,
Delete = 127
};
constexpr int UART_BUFFER_SIZE = 128;
constexpr int MAX_ARGUMENTS = 5;
constexpr int MAX_ROW_LENGTH = 40;
/**
* @brief Lightweight UART command-line interface with history and parsing.
* @note When to use: add interactive commands for debugging or configuration
* over a serial terminal without bringing in a full shell.
*/
class CommandLine {
public:
CommandLine(Stream * UART); // Constructor
void addCommand(const std::string& longName, const std::string& shortName, std::function<void(std::queue<std::string> argumentQueue ,std::string&)> funcPtr);
void executeCommand(const std::string& command, std::queue<std::string> arugments);
void readInput();
void processCommand(const std::string& command);
void begin();
// Pass-through functions for the UART object
void println(const std::string& message){
UART->println(message.c_str());
}
void print(const std::string& message){
UART->print(message.c_str());
}
private:
Stream * UART; // Pointer to the UART object
struct Command {
std::string longName;
std::string shortName;
std::function<void(std::queue<std::string>, std::string&)> funcPtr; // Function pointer to the command handler
};
std::vector<Command> commands{}; // Vector to store the list of commands
// Class member variables for buffering and history
std::string fullLine = {""};
void help();
void trimSpaces(std::string& str);
void handleBackspace_();
void handleNewline_();
void handleChar_(char receivedChar);
bool lastWasCR_ = false; // Track if the last character was a carriage return for proper newline handling
};
#endif