From b3eccbc1b9f0194d9f71345113acdc4518b991c1 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Sat, 22 Aug 2020 10:31:45 -0400 Subject: [PATCH] working: continuous signal passthrough --- gui/stmdsp.cpp | 31 ++++++++++++++++++++++++++++--- gui/stmdsp.hpp | 6 +++++- gui/wxmain.hpp | 9 ++++++--- source/adc.cpp | 11 ++++------- source/main.cpp | 24 +++++++++++++++++++++--- 5 files changed, 64 insertions(+), 17 deletions(-) diff --git a/gui/stmdsp.cpp b/gui/stmdsp.cpp index 837d09c..ed152f7 100644 --- a/gui/stmdsp.cpp +++ b/gui/stmdsp.cpp @@ -16,7 +16,14 @@ namespace stmdsp } device::device(const std::string& file) : - m_serial(file, 230400, serial::Timeout::simpleTimeout(50)) {} + m_serial(file, 230400, serial::Timeout::simpleTimeout(50)) + { + if (m_serial.isOpen()) { + m_serial.write("i"); + if (m_serial.read(6) != "stmdsp") + m_serial.close(); + } + } std::vector device::sample(unsigned long int count) { if (connected()) { @@ -27,11 +34,29 @@ namespace stmdsp }; m_serial.write(request, 3); std::vector data (count); - m_serial.read(reinterpret_cast(data.data()), - data.size() * sizeof(adcsample_t)); + m_serial.read(reinterpret_cast(data.data()), data.size() * sizeof(adcsample_t)); return data; } else { return {}; } } + + void device::continuous_start() { + if (connected()) + m_serial.write("R"); + } + + std::vector device::continuous_read() { + if (connected()) { + m_serial.write("s"); + std::vector data (2048); + m_serial.read(reinterpret_cast(data.data()), 2048 * sizeof(adcsample_t)); + return data; + } + } + + void device::continuous_stop() { + if (connected()) + m_serial.write("S"); + } } diff --git a/gui/stmdsp.hpp b/gui/stmdsp.hpp index 2148fa1..c179955 100644 --- a/gui/stmdsp.hpp +++ b/gui/stmdsp.hpp @@ -35,11 +35,15 @@ namespace stmdsp } bool connected() { - return m_serial.isOpen() && (m_serial.write("i"), m_serial.read(6) == "stmdsp"); + return m_serial.isOpen(); } std::vector sample(unsigned long int count = 1); + void continuous_start(); + std::vector continuous_read(); + void continuous_stop(); + private: serial::Serial m_serial; }; diff --git a/gui/wxmain.hpp b/gui/wxmain.hpp index 97baae3..1fc74bd 100644 --- a/gui/wxmain.hpp +++ b/gui/wxmain.hpp @@ -78,7 +78,7 @@ public: void doSingle() { m_device_samples_future = std::async(std::launch::async, - [this]() { return m_device->sample(250); }); + [this]() { return m_device->continuous_read(); }); } void onSinglePressed(wxCommandEvent& ce) { @@ -87,8 +87,10 @@ public: if (!m_render_timer->IsRunning()) { m_device = new stmdsp::device(m_device_combo->GetStringSelection().ToStdString()); if (m_device->connected()) { - doSingle(); - m_render_timer->Start(100); + m_device->continuous_start(); + m_device_samples_future = std::async(std::launch::async, + []() { return decltype(m_device_samples)(); }); + m_render_timer->Start(1000); button->SetLabel("Stop"); } else { delete m_device; @@ -96,6 +98,7 @@ public: } } else { m_render_timer->Stop(); + m_device->continuous_stop(); button->SetLabel("Single"); delete m_device; diff --git a/source/adc.cpp b/source/adc.cpp index f2903c3..9e0491e 100644 --- a/source/adc.cpp +++ b/source/adc.cpp @@ -77,7 +77,7 @@ namespace adc adc_operation_func = operation_func; adc_group_config.circular = true; adcStartConversion(adcd, &adc_group_config, buffer, count); - gptStartContinuous(gptd, 100); // 10kHz + gptStartContinuous(gptd, 1000); // 10kHz } void read_stop() @@ -131,13 +131,10 @@ void adc_read_callback(ADCDriver *driver) adc_is_read_finished = true; } else if (adc_operation_func != nullptr) { auto half_size = adc_current_buffer_size / 2; - if (driver->state == ADC_ACTIVE) { - // Half full - adc_operation_func(adc_current_buffer, half_size); - } else if (driver->state == ADC_COMPLETE) { - // Second half full + if (adcIsBufferComplete(driver)) adc_operation_func(adc_current_buffer + half_size, half_size); - } + else + adc_operation_func(adc_current_buffer, half_size); } } diff --git a/source/main.cpp b/source/main.cpp index f1170a6..162771a 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -19,13 +19,19 @@ #include static_assert(sizeof(adcsample_t) == sizeof(uint16_t)); +static_assert(sizeof(dacsample_t) == sizeof(uint16_t)); #if CACHE_LINE_SIZE > 0 CC_ALIGN(CACHE_LINE_SIZE) #endif static std::array adc_samples; +#if CACHE_LINE_SIZE > 0 +CC_ALIGN(CACHE_LINE_SIZE) +#endif static std::array dac_samples; +static volatile bool signal_operate_done = false; + static void signal_operate(adcsample_t *buffer, size_t count); int main() @@ -54,10 +60,20 @@ int main() } break; case 'R': - adc::read_start(signal_operate, &adc_samples[0], adc_samples.size() * sizeof(adcsample_t)); + dac_samples.fill(0); + adc::read_start(signal_operate, &adc_samples[0], adc_samples.size()); + dac::write_start(&dac_samples[0], dac_samples.size()); + break; + case 's': + while (!signal_operate_done); + usbserial::write(dac_samples.data(), dac_samples.size() * sizeof(adcsample_t)); break; case 'S': + dac::write_stop(); adc::read_stop(); + break; + case 'e': + break; case 'W': if (usbserial::read(&cmd[1], 2) < 2) @@ -85,8 +101,10 @@ int main() } } -void signal_operate([[maybe_unused]] adcsample_t *buffer, [[maybe_unused]] size_t count) +void signal_operate(adcsample_t *buffer, size_t count) { - + auto dac_buffer = &dac_samples[buffer == &adc_samples[0] ? 0 : 1024]; + std::copy(buffer, buffer + count, dac_buffer); + signal_operate_done = buffer == &adc_samples[1024]; }