From 8bd5604a3c66f39b79d2afaf8ca859a543568b32 Mon Sep 17 00:00:00 2001 From: kalipso Date: Tue, 28 Feb 2023 01:34:04 +0100 Subject: [PATCH] WIP add super basic gps handler --- commons.hpp | 71 ++++++++++++++++++++++++++++++++++++++++++++++++ logging.hpp | 5 ++++ main.cpp | 45 ++++++++++++++++++++---------- uart_handler.cpp | 2 +- uart_handler.hpp | 2 +- 5 files changed, 108 insertions(+), 17 deletions(-) diff --git a/commons.hpp b/commons.hpp index 1bfe234..081917d 100644 --- a/commons.hpp +++ b/commons.hpp @@ -99,6 +99,18 @@ struct buffer { return true; } + template + bool execute(const Func_t& executor) + { + const auto lk = commons::lock::get(m); + if(!lk) + { + return false; + } + + executor(buf); + } + bool print() const { const auto lk = commons::lock::get(m); @@ -120,3 +132,62 @@ private: mutable commons::mutex m; }; +class gps_data +{ +public: + void extract_gps_data(auto& buf) + { + buf.execute([this](auto& value) { this->copy_to_own_buf(value); }); + } + + void copy_to_own_buf(std::span other) + { + auto view = std::string_view{reinterpret_cast(other.data()), other.size()}; + + const auto it_begin = view.find("$GPGGA"); + + if(it_begin == view.npos) + { + return; + } + + view.remove_prefix(it_begin); + const auto it_end = view.find("\r\n", it_begin); + + if(it_end == view.npos) + { + return; + } + + view.remove_suffix(view.size() - it_end); + + if(view.size() > max_size) + { + return; + } + + std::copy(view.begin(), view.end(), current_line.begin()); + current_size = std::distance(view.begin(), view.end()); + valid = true; + } + + bool is_valid() const + { + return valid; + } + + void print() const + { + log::info("Current GPS Data:"); + uart_interface::write( + {reinterpret_cast(current_line.data()), current_size}); + log::linebreak(); + } + +private: + static constexpr uint8_t max_size = 82; //defined by GPS NMEA 0183 protocol + size_t current_size = 0; + std::array current_line; + bool valid = false; +}; + diff --git a/logging.hpp b/logging.hpp index 7d9acdb..8cbcc1f 100644 --- a/logging.hpp +++ b/logging.hpp @@ -59,6 +59,11 @@ class log { return logger_impl.init(); } + static void linebreak() { + const auto val = std::string_view{"\r\n"}; + return logger->log(val); + } + template static void error(Args&&... args) { if (!log_impl_begin(LogLevel::ERROR)) { diff --git a/main.cpp b/main.cpp index 366c545..b743c88 100644 --- a/main.cpp +++ b/main.cpp @@ -72,9 +72,13 @@ class cmd_holder { }; static bool updated_main_buf = false; -static buffer main_buffer; +static bool print_main_buf = false; +static buffer main_buffer{}; +static gps_data gps; void print_buf(void) { main_buffer.print(); } +void print_gps(void) { gps.print(); } +void print_buf_toggle(void) { print_main_buf = !print_main_buf; } class cmd_handler { public: @@ -135,12 +139,15 @@ class cmd_handler { void print_help_() const { commands.print_help(); } private: - static constexpr cmd_holder<6> commands{ + static constexpr cmd_holder<8> 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("buf", "Prints gps uart buffer", &print_buf), + std::make_tuple("gps", "Prints captured gps data", &print_gps), + std::make_tuple("buf", "Prints uart2 buffer", &print_buf), + std::make_tuple("buft", "toggles continous printing of uart2 buffer", + &print_buf_toggle), std::make_tuple("help", "Prints available commands", &print_help)}; array_t symbols; @@ -178,7 +185,6 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { if (value == '\r') { cmd_handler::get().queue_execution(); - HAL_GPIO_TogglePin(LED_PORT, LED2_PIN); return; } @@ -195,7 +201,6 @@ void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size) { HAL_GPIO_WritePin(LED_PORT, LED1_PIN, GPIO_PIN_SET); if (main_buffer.copy_from(gps_interface::new_rx_buf, Size)) { - log::debug("Copied to main buff"); updated_main_buf = true; } HAL_GPIO_WritePin(LED_PORT, LED1_PIN, GPIO_PIN_RESET); @@ -282,6 +287,11 @@ int main(void) { if (!MX_SPI1_Init()) { // toggle status led or something } + 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_Delay(10); + + log::debug("SPI1 Initialized."); if (!gps_interface::init()) { log::error("UART2 Initialization failed, needed for GPS"); @@ -289,11 +299,6 @@ int main(void) { log::debug("Uart2 Initialized"); } - 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_Delay(1000); - - log::debug("SPI1 Initialized."); log::debug("Initialization done."); char OP_Mode = 0x01; @@ -308,6 +313,8 @@ int main(void) { // RF95_Init(); + HAL_GPIO_WritePin(LED_PORT, LED2_PIN, GPIO_PIN_RESET); + while (1) { cmd_handler::get().run(); @@ -317,12 +324,20 @@ int main(void) { // RF95_receive(LoRa_buff); // HAL_Delay(100); // HAL_GPIO_WritePin(LED_PORT, LED1_PIN, GPIO_PIN_SET); - HAL_Delay(100); + // HAL_Delay(100); - // if (updated_main_buf) { - // log::debug("Printing main buf"); - // updated_main_buf = !main_buffer.print(); - // } + if (gps.is_valid()) { + HAL_GPIO_WritePin(LED_PORT, LED2_PIN, GPIO_PIN_SET); + } + + if (updated_main_buf) { + updated_main_buf = false; + gps.extract_gps_data(main_buffer); + + if (print_main_buf) { + updated_main_buf = !main_buffer.print(); + } + } // std::string_view msg{reinterpret_cast(LoRa_buff)}; // log::info("Received Message"); diff --git a/uart_handler.cpp b/uart_handler.cpp index 70b30f1..3accf2a 100644 --- a/uart_handler.cpp +++ b/uart_handler.cpp @@ -9,4 +9,4 @@ std::array uart_interface::new_rx_buf{}; template <> uint8_t gps_interface::rx_buff = 0; template <> -std::array gps_interface::new_rx_buf{}; +std::array gps_interface::new_rx_buf{}; diff --git a/uart_handler.hpp b/uart_handler.hpp index 96870c7..0dfd863 100644 --- a/uart_handler.hpp +++ b/uart_handler.hpp @@ -239,4 +239,4 @@ using uart_interface = uart_handler; using gps_interface = uart_handler; + USART2_BASE, UartMode::DMA, 256>;