From de62e39d243a7dae0dea5a84631ade946a65fce8 Mon Sep 17 00:00:00 2001 From: awe Date: Fri, 10 Apr 2026 15:01:57 +0300 Subject: [PATCH] fix channel num --- main.cpp | 59 +++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 11 deletions(-) diff --git a/main.cpp b/main.cpp index 38d67d2..f68d42b 100644 --- a/main.cpp +++ b/main.cpp @@ -636,6 +636,17 @@ Config parse_args(int argc, char** argv) { cfg.live_update_period_ms = std::max(cfg.live_update_period_ms, 1000U); } } + if (cfg.tty_path && cfg.di1_group_average) { + if (!cfg.recv_block_specified) { + cfg.recv_block_words = std::max(cfg.recv_block_words, 65536U); + } + if (!cfg.input_step_specified) { + cfg.input_step_words = std::max(cfg.input_step_words, 65536U); + } + if (!cfg.input_buffer_specified) { + cfg.input_buffer_words = std::max(cfg.input_buffer_words, 16U * 1024U * 1024U); + } + } if (cfg.recv_block_words == 0) { fail("recv_block must be > 0"); } @@ -897,6 +908,8 @@ void expect_ok(const Api& api, int32_t err, const std::string& what) { constexpr uint32_t kE502DiSyn2Mask = (static_cast(1U) << 13U) | (static_cast(1U) << 17U); constexpr uint32_t kE502Digital1Mask = (static_cast(1U) << 0U); +constexpr uint32_t kStreamInputAdcFlag = 0x80000000U; +constexpr uint32_t kStreamInputCalibratedAdcFlag = 0x40000000U; using TickMs = uint64_t; @@ -1056,6 +1069,30 @@ int16_t pack_raw_code_to_int16(double avg_raw_code) { return static_cast(clamped); } +bool try_extract_raw_adc_code(uint32_t word, double& raw_code) { + if ((word & kStreamInputAdcFlag) == 0U) { + return false; + } + + int32_t value = 0; + if ((word & kStreamInputCalibratedAdcFlag) != 0U) { + const uint32_t payload = word & 0x00FFFFFFU; + value = static_cast(payload); + if ((payload & 0x00800000U) != 0U) { + value |= static_cast(0xFF000000U); + } + } else { + const uint32_t payload = word & 0x0000FFFFU; + value = static_cast(payload); + if ((payload & 0x00008000U) != 0U) { + value |= static_cast(0xFFFF0000U); + } + } + + raw_code = static_cast(value); + return true; +} + struct ConsoleCtrlGuard { bool installed = false; @@ -1653,18 +1690,18 @@ int run(const Config& cfg) { uint32_t raw_adc_count = 0; if (tty_writer) { - raw_adc_count = static_cast(adc_raw_buffer.size()); - const int32_t raw_process_err = api.ProcessData(device.hnd, - raw.data(), - static_cast(recvd), - 0, - adc_raw_buffer.data(), - &raw_adc_count, - nullptr, - nullptr); - expect_ok(api, raw_process_err, "Process raw ADC data"); + for (std::size_t i = 0; i < static_cast(recvd); ++i) { + double raw_code = 0.0; + if (!try_extract_raw_adc_code(raw[i], raw_code)) { + continue; + } + if (raw_adc_count >= adc_raw_buffer.size()) { + fail("Raw ADC parsing overflowed the temporary buffer"); + } + adc_raw_buffer[raw_adc_count++] = raw_code; + } if (raw_adc_count != adc_count) { - fail("Raw ADC processing returned a different sample count than voltage processing"); + fail("Raw ADC parsing returned a different sample count than voltage processing"); } if (!tty_di1_group_average) {