From 86453bc8f4ca7b41292e1e48223dc9661ce8e92e Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Sat, 31 Oct 2020 17:18:19 -0400 Subject: [PATCH] log conversion results to .csv --- gui/stmdsp.cpp | 24 ++++++++++++++++++++---- gui/wxmain.cpp | 31 ++++++++++++++++++++++++++++++- gui/wxmain.hpp | 1 + source/main.cpp | 33 ++++++++++++++++++++++++++++++--- 4 files changed, 81 insertions(+), 8 deletions(-) diff --git a/gui/stmdsp.cpp b/gui/stmdsp.cpp index 3de3324..2091d48 100644 --- a/gui/stmdsp.cpp +++ b/gui/stmdsp.cpp @@ -84,10 +84,26 @@ namespace stmdsp std::vector device::continuous_read() { if (connected()) { - m_serial.write("a"); - std::vector data (2048); - m_serial.read(reinterpret_cast(data.data()), 2048 * sizeof(adcsample_t)); - return data; + m_serial.write("s"); + unsigned char sizebytes[2]; + m_serial.read(sizebytes, 2); + unsigned int size = sizebytes[0] | (sizebytes[1] << 8); + if (size > 0) { + std::vector data (size); + unsigned int total = size * sizeof(adcsample_t); + unsigned int offset = 0; + + while (total > 512) { + m_serial.read(reinterpret_cast(&data[0]) + offset, 512); + m_serial.write("n"); + offset += 512; + total -= 512; + } + m_serial.read(reinterpret_cast(&data[0]) + offset, total); + m_serial.write("n"); + return data; + + } } return {}; diff --git a/gui/wxmain.cpp b/gui/wxmain.cpp index 412f775..e92bb00 100644 --- a/gui/wxmain.cpp +++ b/gui/wxmain.cpp @@ -143,7 +143,24 @@ void MainFrame::onCloseEvent(wxCloseEvent& event) void MainFrame::onMeasureTimer([[maybe_unused]] wxTimerEvent&) { - if (m_status_bar && m_device) { + if (m_conv_result_log != nullptr) { + static unsigned int counter = 0; + if (auto samples = m_device->continuous_read(); samples.size() > 0) { + for (auto& s : samples) { + auto str = wxString::Format("%u\n", s); + m_conv_result_log->Write(str.ToAscii(), str.Len()); + } + + counter++; + } + + //if (counter == 20) { + // m_conv_result_log->Close(); + // delete m_conv_result_log; + // m_conv_result_log = nullptr; + // counter = 0; + //} + } else if (m_status_bar && m_device) { m_status_bar->SetStatusText(wxString::Format(wxT("Execution time: %u cycles"), m_device->continuous_start_get_measurement())); } @@ -392,11 +409,18 @@ void MainFrame::onRunStart(wxCommandEvent& ce) if (!m_is_running) { if (m_device != nullptr && m_device->connected()) { + if (m_conv_result_log != nullptr) { + m_conv_result_log->Close(); + delete m_conv_result_log; + } + m_conv_result_log = new wxFileOutputStream("results.csv"); + if (m_run_measure && m_run_measure->IsChecked()) { m_device->continuous_start_measure(); m_measure_timer->StartOnce(1000); } else { m_device->continuous_start(); + m_measure_timer->Start(15); } menuItem->SetItemLabel("&Stop"); @@ -407,6 +431,11 @@ void MainFrame::onRunStart(wxCommandEvent& ce) m_status_bar->SetStatusText("Please connect."); } } else { + if (m_conv_result_log != nullptr) { + m_conv_result_log->Close(); + delete m_conv_result_log; + m_conv_result_log = nullptr; + } m_device->continuous_stop(); menuItem->SetItemLabel("&Start"); diff --git a/gui/wxmain.hpp b/gui/wxmain.hpp index c46ed95..01dfc4b 100644 --- a/gui/wxmain.hpp +++ b/gui/wxmain.hpp @@ -56,6 +56,7 @@ private: wxTimer *m_measure_timer = nullptr; wxStatusBar *m_status_bar = nullptr; wxMenuBar *m_menu_bar = nullptr; + wxFileOutputStream *m_conv_result_log = nullptr; wxString m_open_file_path; wxString m_temp_file_name; diff --git a/source/main.cpp b/source/main.cpp index 414d2f7..17b0adb 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -21,7 +21,7 @@ constexpr unsigned int MAX_ELF_FILE_SIZE = 8 * 1024; constexpr unsigned int MAX_ERROR_QUEUE_SIZE = 8; -constexpr unsigned int MAX_SAMPLE_BUFFER_SIZE = 6000; +constexpr unsigned int MAX_SAMPLE_BUFFER_SIZE = 6000; // operate on buffers size this / 2 constexpr unsigned int MAX_SIGGEN_BUFFER_SIZE = 3000; enum class RunStatus : char @@ -78,6 +78,7 @@ static std::array dac_samples; +static volatile const dacsample_t *dac_samples_new = nullptr; #if CACHE_LINE_SIZE > 0 CC_ALIGN(CACHE_LINE_SIZE) #endif @@ -265,7 +266,29 @@ void main_loop() break; case 's': - usbserial::write(dac_samples.data(), dac_sample_count * sizeof(dacsample_t)); + if (dac_samples_new != nullptr) { + unsigned char buf[2] = { + static_cast(dac_sample_count / 2 & 0xFF), + static_cast(((dac_sample_count / 2) >> 8) & 0xFF) + }; + usbserial::write(buf, 2); + unsigned int total = dac_sample_count / 2 * sizeof(dacsample_t); + unsigned int offset = 0; + unsigned char unused; + auto samps = reinterpret_cast( + const_cast(dac_samples_new)); + while (total > 512) { + usbserial::write(samps + offset, 512); + while (usbserial::read(&unused, 1) == 0); + offset += 512; + total -= 512; + } + usbserial::write(samps + offset, total); + while (usbserial::read(&unused, 1) == 0); + dac_samples_new = nullptr; + } else { + usbserial::write("\0\0", 2); + } break; case 'W': @@ -281,7 +304,7 @@ void main_loop() } } - chThdSleepMilliseconds(1); + chThdSleepMicroseconds(100); } } @@ -308,12 +331,14 @@ THD_FUNCTION(conversionThread, arg) if (!samples) samples = &adc_samples[0]; std::copy(samples, samples + halfsize, &dac_samples[0]); + dac_samples_new = &dac_samples[0]; } else if (message == MSG_CONVSECOND) { if (elf_entry) samples = elf_entry(&adc_samples[halfsize], halfsize); if (!samples) samples = &adc_samples[halfsize]; std::copy(samples, samples + halfsize, &dac_samples[dac_sample_count / 2]); + dac_samples_new = &dac_samples[dac_sample_count / 2]; } else if (message == MSG_CONVFIRST_MEASURE) { chTMStartMeasurementX(&conversion_time_measurement); if (elf_entry) @@ -322,6 +347,7 @@ THD_FUNCTION(conversionThread, arg) if (!samples) samples = &adc_samples[0]; std::copy(samples, samples + halfsize, &dac_samples[0]); + dac_samples_new = &dac_samples[0]; } else if (message == MSG_CONVSECOND_MEASURE) { chTMStartMeasurementX(&conversion_time_measurement); if (elf_entry) @@ -330,6 +356,7 @@ THD_FUNCTION(conversionThread, arg) if (!samples) samples = &adc_samples[halfsize]; std::copy(samples, samples + halfsize, &dac_samples[dac_sample_count / 2]); + dac_samples_new = &dac_samples[dac_sample_count / 2]; } } }