From 454407e417af2ea4b42ac9be5eddb0d4642a42f8 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Sun, 9 Aug 2026 22:07:17 +0200 Subject: code etwas in kleinere Fuinktionen unterteilt --- src/app.cpp | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/app.cpp b/src/app.cpp index fb7659d..8d89b05 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -107,6 +108,18 @@ print_hex(string_view message, span data) cout << '\n'; } +uint16_t +decode_heartbeat(span data) +{ + static const uint16_t mask = 0b0011'1111'1111'1111U; + + auto msb = static_cast(data[0]); + auto lsb = static_cast(data[1]); + auto count = ((msb << 8U) | lsb) & mask; + + return count; +} + bool sync(asio::serial_port& port, asio::io_context& context) { @@ -281,7 +294,19 @@ run() asio::write(port, asio::buffer(">")); thread = jthread([&context, &port]() { - array buffer{}; + array buffer{}; + + uint64_t second{}; + array counts{}; + + auto update_counts = [&counts, &second](uint16_t count) { + counts.at(second++ % counts.size()) = count; + + if ( second >= counts.size() ) { + auto cpm = accumulate(counts.begin(), counts.end(), uint32_t(0)); + cout << "CPM: " << cpm << '\n'; + } + }; function readFromPort = [&]() { port.async_read_some(asio::buffer(buffer, buffer.size()), [&](std::error_code error, size_t length) { @@ -290,7 +315,14 @@ run() return; } - print_hex("Daten:", { buffer.data(), length }); + if ( length == 2 ) { + const auto count = decode_heartbeat(buffer); + + update_counts(count); + } + else { + print_hex("HB Daten:", { buffer.data(), length }); + } readFromPort(); }); -- cgit v1.3