add simple cmd handler
This commit is contained in:
69
main.cpp
69
main.cpp
@@ -1,6 +1,9 @@
|
|||||||
#include <stm32f1xx_hal.h>
|
#include <stm32f1xx_hal.h>
|
||||||
#include <stm32f1xx_hal_uart.h>
|
#include <stm32f1xx_hal_uart.h>
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <tuple>
|
||||||
|
|
||||||
#include "logging.hpp"
|
#include "logging.hpp"
|
||||||
#include "spi.hpp"
|
#include "spi.hpp"
|
||||||
|
|
||||||
@@ -22,6 +25,72 @@
|
|||||||
extern uint8_t LoRa_buff[RH_RF95_FIFO_SIZE];
|
extern uint8_t LoRa_buff[RH_RF95_FIFO_SIZE];
|
||||||
extern SPI_HandleTypeDef hspi1;
|
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<std::tuple<std::string_view, functionPointerType>, 12> commands;
|
||||||
|
size_t amount_cmds = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
class cmd_handler {
|
||||||
|
public:
|
||||||
|
using array_t = std::array<uint8_t, 24>;
|
||||||
|
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<const char *>(symbols.data()),
|
||||||
|
static_cast<size_t>(
|
||||||
|
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.
|
// This prevent name mangling for functions used in C/assembly files.
|
||||||
extern "C" {
|
extern "C" {
|
||||||
void SysTick_Handler(void) {
|
void SysTick_Handler(void) {
|
||||||
|
|||||||
Reference in New Issue
Block a user