From 12b100ff8d888a3a68bd7df555b52a786e6f7093 Mon Sep 17 00:00:00 2001 From: kalipso Date: Thu, 13 Aug 2026 09:24:08 +0200 Subject: [PATCH] WIP --- commons.hpp | 137 +++++++++++++++++++++++----------------------------- main.cpp | 9 ++-- shell.nix | 1 + 3 files changed, 66 insertions(+), 81 deletions(-) diff --git a/commons.hpp b/commons.hpp index 081917d..65fc178 100644 --- a/commons.hpp +++ b/commons.hpp @@ -1,41 +1,39 @@ #pragma once +// TODO: create bettter structure for gpio +#define LED_PORT GPIOC +#define LED2_PIN GPIO_PIN_9 + #include -#include #include +#include #include "logging.hpp" namespace commons { -template -class lock -{ -public: +template +class lock { + public: ~lock() { locker.unlock(); } - static std::optional get(LockType& l) - { - if(!l.lock()) - { + static std::optional get(LockType& l) { + if (!l.lock()) { log::debug("Could not acquire lock."); return std::nullopt; } return lock{l}; } -private: + private: explicit lock(LockType& l) : locker{l} {} LockType& locker; }; -class mutex -{ -public: - bool lock() - { - if(locked) - { +class mutex { + public: + bool lock() { + if (locked) { return false; } @@ -43,36 +41,28 @@ public: return true; } - void unlock() - { - locked = false; - } + void unlock() { locked = false; } - bool is_locked() const { - return locked; - } + bool is_locked() const { return locked; } -protected: + protected: bool locked = false; }; -} +} // namespace commons template struct buffer { constexpr buffer() {} constexpr auto size() { return S; } - bool copy_from(const std::span& input, uint16_t amount) - { + bool copy_from(const std::span& input, uint16_t amount) { const auto lk = commons::lock::get(m); - if(!lk) - { + if (!lk) { return false; } - if(amount > max_size) - { + if (amount > max_size) { return false; } @@ -80,18 +70,16 @@ struct buffer { if (old_pos + amount > max_size) { const auto size_to_copy = max_size - old_pos; - std::copy(input.begin(), - std::next(input.begin(), size_to_copy), + std::copy(input.begin(), std::next(input.begin(), size_to_copy), std::next(buf.begin(), old_pos)); old_pos = 0; - //TODO: this only works if amount is not double the size of main_buf - std::copy(std::next(input.begin(), size_to_copy), - input.begin() + amount, buf.begin()); + // TODO: this only works if amount is not double the size of main_buf + std::copy(std::next(input.begin(), size_to_copy), input.begin() + amount, + buf.begin()); new_pos = amount - size_to_copy; } else { - std::copy(input.begin(), - input.begin() + amount, + std::copy(input.begin(), input.begin() + amount, std::next(buf.begin(), old_pos)); new_pos = old_pos + amount; } @@ -99,32 +87,37 @@ struct buffer { return true; } - template - bool execute(const Func_t& executor) - { + template + bool execute(const Func_t& executor) { const auto lk = commons::lock::get(m); - if(!lk) - { + if (!lk) { return false; } executor(buf); } - bool print() const - { + bool print() const { const auto lk = commons::lock::get(m); - if(!lk) - { + if (!lk) { return false; } - uart_interface::write( - {reinterpret_cast(buf.data()), new_pos}); + uart_interface::write({reinterpret_cast(buf.data()), new_pos}); return true; } -private: + void reset() { + const auto lk = commons::lock::get(m); + if (!lk) { + return; + } + + old_pos = 0; + new_pos = 0; + } + + private: static constexpr auto max_size = S; std::array buf{}; size_t old_pos = 0; @@ -132,62 +125,54 @@ private: mutable commons::mutex m; }; -class gps_data -{ -public: - void extract_gps_data(auto& buf) - { +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()}; + 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) - { + 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) - { + if (it_end == view.npos) { return; } view.remove_suffix(view.size() - it_end); - if(view.size() > max_size) - { + if (view.size() > max_size) { return; } + HAL_GPIO_TogglePin(LED_PORT, LED2_PIN); std::copy(view.begin(), view.end(), current_line.begin()); current_size = std::distance(view.begin(), view.end()); - valid = true; + valid = current_size > 50; + HAL_GPIO_TogglePin(LED_PORT, LED2_PIN); } - bool is_valid() const - { - return valid; - } + bool is_valid() const { return valid; } - void print() const - { + void print() const { log::info("Current GPS Data:"); uart_interface::write( - {reinterpret_cast(current_line.data()), current_size}); + {reinterpret_cast(current_line.data()), current_size}); log::linebreak(); } -private: - static constexpr uint8_t max_size = 82; //defined by GPS NMEA 0183 protocol + 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/main.cpp b/main.cpp index b743c88..450871d 100644 --- a/main.cpp +++ b/main.cpp @@ -3,10 +3,6 @@ #include -#include "commons.hpp" -#include "logging.hpp" -#include "spi.hpp" - /* * reserve PB11-PB15 for SPI2 */ @@ -24,12 +20,15 @@ #define LoRa_CS_Pin GPIO_PIN_5 #define LoRa_CS_GPIO_Port GPIOC +#include "commons.hpp" +#include "logging.hpp" #include "rfm95.hpp" +#include "spi.hpp" extern uint8_t LoRa_buff[RH_RF95_FIFO_SIZE]; extern SPI_HandleTypeDef hspi1; -void print_version(void) { log::info("running PentaTrack v0.1.3"); } +void print_version(void) { log::info("running PentaTrack v0.2.0"); } void print_help(void); void loglevel_error(void) { log::set_loglevel(LogLevel::ERROR); } diff --git a/shell.nix b/shell.nix index 5bd4555..de410d2 100644 --- a/shell.nix +++ b/shell.nix @@ -13,5 +13,6 @@ mkShell { gcc-arm-embedded-10 libstm32cube libstm32stdperiph + esptool ]; }