add loglevel commands
This commit is contained in:
22
logging.hpp
22
logging.hpp
@@ -11,7 +11,7 @@ enum class LogLevel {
|
||||
TRACE,
|
||||
};
|
||||
|
||||
inline std::string_view get_loglevel_string(LogLevel level) {
|
||||
inline constexpr std::string_view get_loglevel_string(LogLevel level) {
|
||||
switch (level) {
|
||||
case (LogLevel::ERROR): {
|
||||
return "[Error]";
|
||||
@@ -61,23 +61,35 @@ class log {
|
||||
|
||||
template <typename... Args>
|
||||
static void error(Args&&... args) {
|
||||
log_impl_begin(LogLevel::ERROR);
|
||||
if (!log_impl_begin(LogLevel::ERROR)) {
|
||||
return;
|
||||
}
|
||||
|
||||
log_impl(std::forward<Args&&>(args)...);
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
static void debug(Args&&... args) {
|
||||
log_impl_begin(LogLevel::DEBUG);
|
||||
if (!log_impl_begin(LogLevel::DEBUG)) {
|
||||
return;
|
||||
}
|
||||
|
||||
log_impl(std::forward<Args&&>(args)...);
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
static void info(Args&&... args) {
|
||||
log_impl_begin(LogLevel::INFO);
|
||||
if (!log_impl_begin(LogLevel::INFO)) {
|
||||
return;
|
||||
}
|
||||
|
||||
log_impl(std::forward<Args&&>(args)...);
|
||||
}
|
||||
|
||||
static void set_loglevel(LogLevel level) { log_level = level; }
|
||||
static void set_loglevel(LogLevel level) {
|
||||
info("New LogLevel: ", get_loglevel_string(level));
|
||||
log_level = level;
|
||||
}
|
||||
|
||||
private:
|
||||
static bool log_impl_begin(LogLevel level) {
|
||||
|
||||
31
main.cpp
31
main.cpp
@@ -28,6 +28,10 @@ extern SPI_HandleTypeDef hspi1;
|
||||
void print_version(void) { log::info("running PentaTrack v0.1.3"); }
|
||||
void print_help(void);
|
||||
|
||||
void loglevel_error(void) { log::set_loglevel(LogLevel::ERROR); }
|
||||
void loglevel_info(void) { log::set_loglevel(LogLevel::INFO); }
|
||||
void loglevel_debug(void) { log::set_loglevel(LogLevel::DEBUG); }
|
||||
|
||||
template <size_t Size>
|
||||
class cmd_holder {
|
||||
public:
|
||||
@@ -63,6 +67,7 @@ class cmd_holder {
|
||||
std::array<command_t, Size> commands;
|
||||
};
|
||||
|
||||
template <size_t Size>
|
||||
class cmd_handler {
|
||||
public:
|
||||
static constexpr auto MaxCmdLength = 24;
|
||||
@@ -70,11 +75,9 @@ class cmd_handler {
|
||||
using iterator_t = array_t::iterator;
|
||||
using const_iterator_t = array_t::const_iterator;
|
||||
|
||||
consteval cmd_handler()
|
||||
: commands{std::make_tuple("ver", "Prints current version.",
|
||||
&print_version),
|
||||
std::make_tuple("help", "Prints available commands",
|
||||
&print_help)},
|
||||
template <typename... Args>
|
||||
consteval cmd_handler(Args &&...command_list)
|
||||
: commands{std::forward<Args &&>(command_list)...},
|
||||
symbols{},
|
||||
iterator{symbols.begin()} {}
|
||||
|
||||
@@ -121,19 +124,21 @@ class cmd_handler {
|
||||
void print_help_() const { commands.print_help(); }
|
||||
|
||||
private:
|
||||
cmd_holder<2> commands;
|
||||
cmd_holder<Size> commands;
|
||||
array_t symbols;
|
||||
iterator_t iterator;
|
||||
bool ShouldExecute = false;
|
||||
};
|
||||
|
||||
static cmd_handler commands{};
|
||||
static cmd_handler<5> commands{
|
||||
std::make_tuple("ver", "Prints current version.", &print_version),
|
||||
std::make_tuple("error", "Set LogLevel to Error.", &loglevel_error),
|
||||
std::make_tuple("info", "Set LogLevel to Info.", &loglevel_info),
|
||||
std::make_tuple("debug", "Set LogLevel to Debug.", &loglevel_debug),
|
||||
std::make_tuple("help", "Prints available commands", &print_help)};
|
||||
|
||||
void print_help(void) { commands.print_help_(); }
|
||||
|
||||
// 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) {
|
||||
@@ -183,7 +188,8 @@ void initGPIO() {
|
||||
GPIO_ConfigSPI.Pin = LoRa_CS_Pin | LoRa_RESET_Pin;
|
||||
|
||||
// bare metal init of led1:
|
||||
// volatile uint32_t* CRH = reinterpret_cast<uint32_t*>(0x40011000 + 0x04);
|
||||
// volatile uint32_t* CRH = reinterpret_cast<uint32_t*>(0x40011000 +
|
||||
// 0x04);
|
||||
//*CRH |= 0x3;
|
||||
//*CRH &= (~0xC);
|
||||
|
||||
@@ -191,7 +197,8 @@ void initGPIO() {
|
||||
HAL_GPIO_Init(BTN_PORT, &GPIO_Config2);
|
||||
HAL_GPIO_Init(LoRa_CS_GPIO_Port, &GPIO_ConfigSPI);
|
||||
// HAL_GPIO_WritePin(LoRa_CS_GPIO_Port, LoRa_CS_Pin, GPIO_PIN_SET);
|
||||
// HAL_GPIO_WritePin(LoRa_RESET_GPIO_Port, LoRa_RESET_Pin, GPIO_PIN_SET);
|
||||
// HAL_GPIO_WritePin(LoRa_RESET_GPIO_Port, LoRa_RESET_Pin,
|
||||
// GPIO_PIN_SET);
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
|
||||
Reference in New Issue
Block a user