summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2026-08-09 22:07:17 +0200
committerThomas Schmucker <ts@its1.de>2026-08-09 22:07:17 +0200
commit454407e417af2ea4b42ac9be5eddb0d4642a42f8 (patch)
treedd7b7e851a9ad0615b14e8aada2a3bd65faceccb
parent48e22c4f6fb7b46976e6fba745760316aeb22e11 (diff)
downloadgmc-300e-monitor-master.tar.gz
gmc-300e-monitor-master.tar.bz2
gmc-300e-monitor-master.zip
code etwas in kleinere Fuinktionen unterteiltHEADmaster
-rw-r--r--src/app.cpp36
1 files 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 @@
4#include <functional> 4#include <functional>
5#include <iostream> 5#include <iostream>
6#include <map> 6#include <map>
7#include <numeric>
7#include <span> 8#include <span>
8#include <string> 9#include <string>
9 10
@@ -107,6 +108,18 @@ print_hex(string_view message, span<const unsigned char> data)
107 cout << '\n'; 108 cout << '\n';
108} 109}
109 110
111uint16_t
112decode_heartbeat(span<const uint8_t> data)
113{
114 static const uint16_t mask = 0b0011'1111'1111'1111U;
115
116 auto msb = static_cast<uint16_t>(data[0]);
117 auto lsb = static_cast<uint16_t>(data[1]);
118 auto count = ((msb << 8U) | lsb) & mask;
119
120 return count;
121}
122
110bool 123bool
111sync(asio::serial_port& port, asio::io_context& context) 124sync(asio::serial_port& port, asio::io_context& context)
112{ 125{
@@ -281,7 +294,19 @@ run()
281 asio::write(port, asio::buffer("<HEARTBEAT1>>")); 294 asio::write(port, asio::buffer("<HEARTBEAT1>>"));
282 295
283 thread = jthread([&context, &port]() { 296 thread = jthread([&context, &port]() {
284 array<unsigned char, 1000> buffer{}; 297 array<uint8_t, 1000> buffer{};
298
299 uint64_t second{};
300 array<uint16_t, 60> counts{};
301
302 auto update_counts = [&counts, &second](uint16_t count) {
303 counts.at(second++ % counts.size()) = count;
304
305 if ( second >= counts.size() ) {
306 auto cpm = accumulate(counts.begin(), counts.end(), uint32_t(0));
307 cout << "CPM: " << cpm << '\n';
308 }
309 };
285 310
286 function<void()> readFromPort = [&]() { 311 function<void()> readFromPort = [&]() {
287 port.async_read_some(asio::buffer(buffer, buffer.size()), [&](std::error_code error, size_t length) { 312 port.async_read_some(asio::buffer(buffer, buffer.size()), [&](std::error_code error, size_t length) {
@@ -290,7 +315,14 @@ run()
290 return; 315 return;
291 } 316 }
292 317
293 print_hex("Daten:", { buffer.data(), length }); 318 if ( length == 2 ) {
319 const auto count = decode_heartbeat(buffer);
320
321 update_counts(count);
322 }
323 else {
324 print_hex("HB Daten:", { buffer.data(), length });
325 }
294 326
295 readFromPort(); 327 readFromPort();
296 }); 328 });