Avionics
Core avionics package for CURE flight computers
Loading...
Searching...
No Matches
UARTCommandHandler.h
Go to the documentation of this file.
1#ifndef UARTCOMMANDHANDLER_H
2#define UARTCOMMANDHANDLER_H
3
4#include <functional>
5#include <queue>
6#include <string>
7#include <vector>
8
9#include "ArduinoHAL.h"
10
11enum class AsciiKey : uint8_t {
12 Escape = 27,
13 UpArrow = 65,
16 Delete = 127
17};
18
19constexpr int UART_BUFFER_SIZE = 128;
20constexpr int MAX_HISTORY = 20;
21constexpr int MAX_ARGUMENTS = 5;
22constexpr int MAX_ROW_LENGTH = 40;
23
30
31public:
32 CommandLine(Stream * UART); // Constructor
33 void addCommand(const std::string& longName, const std::string& shortName, std::function<void(std::queue<std::string> argumentQueue ,std::string&)> funcPtr);
34 void executeCommand(const std::string& command, std::queue<std::string> arugments);
35 void readInput();
36 void processCommand(const std::string& command);
37 void begin();
38
39 // Pass-through functions for the UART object
40 void println(const std::string& message){
41 UART->println(message.c_str());
42 }
43 void print(const std::string& message){
44 UART->print(message.c_str());
45 }
46
47private:
48 Stream * UART; // Pointer to the UART object
49 struct Command {
50 std::string longName;
51 std::string shortName;
52 std::function<void(std::queue<std::string>, std::string&)> funcPtr; // Function pointer to the command handler
53 };
54 std::vector<Command> commands{}; // Vector to store the list of commands
55
56 // Class member variables for buffering and history
57 std::string fullLine = {""};
58 std::string inputBuffer = ""; // Current input buffer
59 std::string argBuffer = "";
60 std::string command = "";
61 int historyIndex = 0; // Keeps track of the current position in the command history
62 std::queue<std::string> argumentQueue{};
63 bool isCommandParsed = false;
64
65 std::array<std::string, MAX_HISTORY> fullLineHistory = {};
66 std::array<std::string, MAX_HISTORY> commandHistory = {};
67 std::array<std::array<std::string, MAX_ARGUMENTS>, MAX_HISTORY> argumentHistory = {};
68 std::array<int, MAX_HISTORY> argSize = {};
69
70
71 void help();
72 void displayCommandFromHistory();
73 std::string combineArguments(std::queue<std::string> arguments);
74 void trimSpaces(std::string& str);
75};
76
77#endif
constexpr int MAX_ARGUMENTS
constexpr int UART_BUFFER_SIZE
constexpr int MAX_ROW_LENGTH
constexpr int MAX_HISTORY
void print(const std::string &message)
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 std::string &message)
void processCommand(const std::string &command)