WIP add super basic gps handler

This commit is contained in:
2023-02-28 01:34:04 +01:00
parent 9b22bc6acf
commit 8bd5604a3c
5 changed files with 108 additions and 17 deletions

View File

@@ -99,6 +99,18 @@ struct buffer {
return true;
}
template<typename Func_t>
bool execute(const Func_t& executor)
{
const auto lk = commons::lock<commons::mutex>::get(m);
if(!lk)
{
return false;
}
executor(buf);
}
bool print() const
{
const auto lk = commons::lock<commons::mutex>::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<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)
{
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<const char *>(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<uint8_t, max_size> current_line;
bool valid = false;
};