From 69f7c878b90f9269d538d4a8955983af46c6fdc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20K=C3=A4berich?= Date: Mon, 31 Oct 2022 15:09:19 +0100 Subject: [PATCH] Fix calibration bug + improve device shutdown --- .../LibreVNA-GUI/Device/device.cpp | 17 +++++++++++------ .../PC_Application/LibreVNA-GUI/Device/device.h | 1 + .../PC_Application/LibreVNA-GUI/VNA/vna.cpp | 6 +++--- .../PC_Application/LibreVNA-GUI/preferences.cpp | 4 ++-- .../PC_Application/LibreVNA-GUI/preferences.h | 4 +--- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/Software/PC_Application/LibreVNA-GUI/Device/device.cpp b/Software/PC_Application/LibreVNA-GUI/Device/device.cpp index 1f9e288..1706c9f 100644 --- a/Software/PC_Application/LibreVNA-GUI/Device/device.cpp +++ b/Software/PC_Application/LibreVNA-GUI/Device/device.cpp @@ -22,7 +22,8 @@ static constexpr USBID IDs[] = { USBInBuffer::USBInBuffer(libusb_device_handle *handle, unsigned char endpoint, int buffer_size) : buffer_size(buffer_size), received_size(0), - inCallback(false) + inCallback(false), + cancelling(false) { buffer = new unsigned char[buffer_size]; memset(buffer, 0, buffer_size); @@ -34,6 +35,7 @@ USBInBuffer::USBInBuffer(libusb_device_handle *handle, unsigned char endpoint, i USBInBuffer::~USBInBuffer() { if(transfer) { + cancelling = true; libusb_cancel_transfer(transfer); // wait for cancellation to complete mutex mtx; @@ -67,6 +69,13 @@ int USBInBuffer::getReceived() const void USBInBuffer::Callback(libusb_transfer *transfer) { + if(cancelling || (transfer->status == LIBUSB_TRANSFER_CANCELLED)) { + // destructor called, do not resubmit + libusb_free_transfer(transfer); + this->transfer = nullptr; + cv.notify_all(); + return; + } // qDebug() << libusb_error_name(transfer->status); switch(transfer->status) { case LIBUSB_TRANSFER_COMPLETED: @@ -111,11 +120,7 @@ void USBInBuffer::Callback(libusb_transfer *transfer) // nothing to do break; case LIBUSB_TRANSFER_CANCELLED: - // destructor called, do not resubmit - libusb_free_transfer(transfer); - this->transfer = nullptr; - cv.notify_all(); - return; + // already handled before switch-case break; } // Resubmit the transfer diff --git a/Software/PC_Application/LibreVNA-GUI/Device/device.h b/Software/PC_Application/LibreVNA-GUI/Device/device.h index aa5a9be..4241f43 100644 --- a/Software/PC_Application/LibreVNA-GUI/Device/device.h +++ b/Software/PC_Application/LibreVNA-GUI/Device/device.h @@ -40,6 +40,7 @@ private: int buffer_size; int received_size; bool inCallback; + bool cancelling; std::condition_variable cv; }; diff --git a/Software/PC_Application/LibreVNA-GUI/VNA/vna.cpp b/Software/PC_Application/LibreVNA-GUI/VNA/vna.cpp index ecfc816..a74ca01 100644 --- a/Software/PC_Application/LibreVNA-GUI/VNA/vna.cpp +++ b/Software/PC_Application/LibreVNA-GUI/VNA/vna.cpp @@ -1160,7 +1160,7 @@ void VNA::StartCalibrationMeasurements(std::set m } // Stop sweep running = false; - SettingsChanged(); + ConfigureDevice(); calMeasurements = m; // Delete any already captured data of this measurement cal.clearMeasurements(m); @@ -1187,6 +1187,7 @@ void VNA::StartCalibrationMeasurements(std::set m cal.clearMeasurements(calMeasurements); }); // Trigger sweep to start from beginning + running = true; ConfigureDevice(true, [=](bool){ // enable calibration measurement only in transmission callback (prevents accidental sampling of data which was still being processed) calMeasuring = true; @@ -1707,15 +1708,14 @@ void VNA::ConfigureDevice(bool resetTraces, std::function cb) } else { if(window->getDevice()) { changingSettings = true; - // single sweep finished window->getDevice()->setIdle([=](bool){ - emit sweepStopped(); changingSettings = false; }); } else { emit sweepStopped(); changingSettings = false; } + emit sweepStopped(); } } diff --git a/Software/PC_Application/LibreVNA-GUI/preferences.cpp b/Software/PC_Application/LibreVNA-GUI/preferences.cpp index 9afda95..c71751c 100644 --- a/Software/PC_Application/LibreVNA-GUI/preferences.cpp +++ b/Software/PC_Application/LibreVNA-GUI/preferences.cpp @@ -502,10 +502,10 @@ void Preferences::load() for(auto d : descr) { try { d.var.setValue(settings.value(d.name, d.def)); - qDebug() << "Setting" << d.name << "is set to" << d.var.value(); +// qDebug() << "Setting" << d.name << "is set to" << d.var.value(); } catch (const exception& e){ d.var.setValue(d.def); - qDebug() << "Setting" << d.name << "reset to default:" << d.def; +// qDebug() << "Setting" << d.name << "reset to default:" << d.def; } } nonTrivialParsing(); diff --git a/Software/PC_Application/LibreVNA-GUI/preferences.h b/Software/PC_Application/LibreVNA-GUI/preferences.h index f4ee7a4..521ffe3 100644 --- a/Software/PC_Application/LibreVNA-GUI/preferences.h +++ b/Software/PC_Application/LibreVNA-GUI/preferences.h @@ -174,9 +174,7 @@ public: private: Preferences() : - TCPoverride(false) { - qDebug() << "Pref constructor: " << &compoundDeviceJSON; - } + TCPoverride(false) {} static Preferences instance; const std::vector descr = {{