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
11constexpr const char* kShellPrompt = "AS> "; // AS = Avionics Shell
12
13enum class AsciiKey : uint8_t {
14 Escape = 27,
15 UpArrow = 65,
18 Delete = 127
19};
20
21constexpr int kUartBufferSize = 128;
22constexpr int kMaxArguments = 5;
23constexpr int kMaxRowLength = 40;
24
31
32public:
33 CommandLine(Stream* uartStream); // Constructor
34 void addCommand(const std::string& longName, const std::string& shortName, std::function<void(std::queue<std::string> argumentQueue ,std::string&)> funcPtr);
35 void executeCommand(const std::string& command, std::queue<std::string> arguments);
36 void readInput();
37 void processCommand(const std::string& command);
38 void begin();
39 void switchUART(Stream* newUart);
40 void useDefaultUART();
41 Stream* getDefaultUART() const { return defaultUart_; }
42 Stream* getActiveUART() const { return uart_; }
43 uint32_t getLastInteractionTimestamp() const { return lastInteractionTimestamp_; }
44
45 // Pass-through functions for the UART object
46 void println(const std::string& message){
47 uart_->println(message.c_str());
48 }
49 void print(const std::string& message){
50 uart_->print(message.c_str());
51 }
52
53private:
54 Stream* uart_; // Pointer to the UART object
55 Stream* defaultUart_; // Stream provided at construction; used by useDefaultUART()
56 struct Command {
57 std::string longName;
58 std::string shortName;
59 std::function<void(std::queue<std::string>, std::string&)> funcPtr; // Function pointer to the command handler
60 };
61 std::vector<Command> commands_{}; // Vector to store the list of commands
62
63 // Class member variables for buffering and history
64 std::string fullLine_ = {""};
65
66
67 void help();
68 void trimSpaces(std::string& str);
69
70 void handleBackspace_();
71 void handleNewline_();
72 void handleChar_(char receivedChar);
73
74 bool lastWasCR_ = false; // Track if the last character was a carriage return for proper newline handling
75 uint32_t lastInteractionTimestamp_ = 0; // millis() when input bytes were last consumed
76};
77
78#endif
constexpr int kMaxRowLength
constexpr int kUartBufferSize
constexpr int kMaxArguments
constexpr const char * kShellPrompt
CommandLine(Stream *uartStream)
void print(const std::string &message)
void switchUART(Stream *newUart)
void executeCommand(const std::string &command, std::queue< std::string > arguments)
void addCommand(const std::string &longName, const std::string &shortName, std::function< void(std::queue< std::string > argumentQueue, std::string &)> funcPtr)
Stream * getDefaultUART() const
Stream * getActiveUART() const
uint32_t getLastInteractionTimestamp() const
void println(const std::string &message)
void processCommand(const std::string &command)