Compare commits

2 Commits
Author SHA1 Message Date
kalipso 4d5b912710 wip 2026-08-14 18:01:50 +02:00
kalipso 12b100ff8d WIP 2026-08-13 09:24:08 +02:00
5 changed files with 743 additions and 401 deletions
+61 -76
View File
@@ -1,41 +1,39 @@
#pragma once
// TODO: create bettter structure for gpio
#define LED_PORT GPIOC
#define LED2_PIN GPIO_PIN_9
#include <array>
#include <span>
#include <optional>
#include <span>
#include "logging.hpp"
namespace commons {
template<typename LockType>
class lock
{
public:
template <typename LockType>
class lock {
public:
~lock() { locker.unlock(); }
static std::optional<lock> get(LockType& l)
{
if(!l.lock())
{
static std::optional<lock> get(LockType& l) {
if (!l.lock()) {
log::debug("Could not acquire lock.");
return std::nullopt;
}
return lock<LockType>{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 <typename T, size_t S>
struct buffer {
constexpr buffer() {}
constexpr auto size() { return S; }
bool copy_from(const std::span<T>& input, uint16_t amount)
{
bool copy_from(const std::span<T>& input, uint16_t amount) {
const auto lk = commons::lock<commons::mutex>::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<typename Func_t>
bool execute(const Func_t& executor)
{
template <typename Func_t>
bool execute(const Func_t& executor) {
const auto lk = commons::lock<commons::mutex>::get(m);
if(!lk)
{
if (!lk) {
return false;
}
executor(buf);
}
bool print() const
{
bool print() const {
const auto lk = commons::lock<commons::mutex>::get(m);
if(!lk)
{
if (!lk) {
return false;
}
uart_interface::write(
{reinterpret_cast<const char *>(buf.data()), new_pos});
uart_interface::write({reinterpret_cast<const char*>(buf.data()), new_pos});
return true;
}
private:
void reset() {
const auto lk = commons::lock<commons::mutex>::get(m);
if (!lk) {
return;
}
old_pos = 0;
new_pos = 0;
}
private:
static constexpr auto max_size = S;
std::array<T, S> 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<uint8_t> other)
{
auto view = std::string_view{reinterpret_cast<const char*>(other.data()), other.size()};
void copy_to_own_buf(std::span<uint8_t> other) {
auto view = std::string_view{reinterpret_cast<const char*>(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<const char *>(current_line.data()), current_size});
{reinterpret_cast<const char*>(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<uint8_t, max_size> current_line;
bool valid = false;
};
Generated
+25 -7
View File
@@ -2,11 +2,11 @@
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1670929434,
"narHash": "sha256-n5UBO6XBV4h3TB7FYu2yAuNQMEYOrQyKeODUwKe06ow=",
"lastModified": 1786514691,
"narHash": "sha256-9dkt1i5JNbEfPb+EjLcSDNs8zqEHQ/1JlSaKgj0SBAg=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "1710ed1f6f8ceb75cf7d1cf55ee0cc21760e1c7a",
"rev": "867dcbc30bafe3c862ef88620f2e7a109d7d3be5",
"type": "github"
},
"original": {
@@ -22,13 +22,31 @@
"utils": "utils"
}
},
"utils": {
"systems": {
"locked": {
"lastModified": 1667395993,
"narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=",
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
},
"utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1731533236,
"narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f",
"rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
"type": "github"
},
"original": {
+653 -317
View File
File diff suppressed because it is too large Load Diff
+2 -1
View File
@@ -10,8 +10,9 @@ mkShell {
gnumake
stlink
screen
gcc-arm-embedded-10
gcc-arm-embedded-15
libstm32cube
libstm32stdperiph
esptool
];
}
+2
View File
@@ -57,6 +57,8 @@ void PendSV_Handler(void);
void SysTick_Handler(void);
void DMA1_Channel6_IRQHandler(void);
void USART1_IRQHandler(void);
void EXTI0_IRQHandler(void);
/* USER CODE BEGIN EFP */