From 7a3859f9324c0a523afb8ba987a2edbc17716370 Mon Sep 17 00:00:00 2001 From: kalipso Date: Sat, 18 Feb 2023 11:00:01 +0100 Subject: [PATCH] add simple cmd handler --- main.cpp | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/main.cpp b/main.cpp index 6828099..78892a7 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,9 @@ #include #include +#include +#include + #include "logging.hpp" #include "spi.hpp" @@ -22,6 +25,72 @@ extern uint8_t LoRa_buff[RH_RF95_FIFO_SIZE]; extern SPI_HandleTypeDef hspi1; +void print_version(void) { log::info("running PentaTrack v0.1.3"); } + +class cmd_holder { + public: + typedef void (*functionPointerType)(void); + + void add_cmd(std::string_view msg, functionPointerType func) { + commands[amount_cmds++] = std::make_tuple(msg, func); + } + + functionPointerType get_func(std::string_view message) const { + for (const auto &[msg, func] : commands) { + if (message == msg) { + return func; + } + } + + return nullptr; + } + + private: + std::array, 12> commands; + size_t amount_cmds = 0; +}; + +class cmd_handler { + public: + using array_t = std::array; + using iterator_t = array_t::iterator; + using const_iterator_t = array_t::const_iterator; + + cmd_handler() : symbols{}, iterator{symbols.begin()} { + commands.add_cmd("ver", &print_version); + } + + static cmd_handler *get_instance() { + static cmd_handler handler{}; + + return &handler; + } + + void add_symbol(uint8_t symbol) { + *iterator++ = symbol; + if (iterator == symbols.end()) { + iterator = symbols.begin(); + } + } + + std::string_view get_current_cmd() const { + return {reinterpret_cast(symbols.data()), + static_cast( + std::distance(symbols.begin(), const_iterator_t{iterator}))}; + } + + void log_current_command() { log::debug(get_current_cmd()); } + + private: + cmd_holder commands; + + array_t symbols; + iterator_t iterator; +}; + +// cmd_handler::array_t cmd_handler::symbols = cmd_handler::array_t{}; +// cmd_handler::iterator_t cmd_handler::iterator = cmd_handler::iterator_t{}; + // This prevent name mangling for functions used in C/assembly files. extern "C" { void SysTick_Handler(void) {