-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUARTCommandHandler.h
More file actions
78 lines (63 loc) · 2.49 KB
/
UARTCommandHandler.h
File metadata and controls
78 lines (63 loc) · 2.49 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
#ifndef UARTCOMMANDHANDLER_H
#define UARTCOMMANDHANDLER_H
#include <functional>
#include <queue>
#include <string>
#include <vector>
#include "ArduinoHAL.h"
constexpr const char* kShellPrompt = "AS> "; // AS = Avionics Shell
enum class AsciiKey : uint8_t {
Escape = 27,
UpArrow = 65,
DownArrow = 66,
Backspace = 8,
Delete = 127
};
constexpr int kUartBufferSize = 128;
constexpr int kMaxArguments = 5;
constexpr int kMaxRowLength = 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* uartStream); // 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> arguments);
void readInput();
void processCommand(const std::string& command);
void begin();
void switchUART(Stream* newUart);
void useDefaultUART();
Stream* getDefaultUART() const { return defaultUart_; }
Stream* getActiveUART() const { return uart_; }
uint32_t getLastInteractionTimestamp() const { return lastInteractionTimestamp_; }
// 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
Stream* defaultUart_; // Stream provided at construction; used by useDefaultUART()
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
uint32_t lastInteractionTimestamp_ = 0; // millis() when input bytes were last consumed
};
#endif