Fix calibration bug + improve device shutdown

This commit is contained in:
Jan Käberich 2022-10-31 15:09:19 +01:00
parent 39a91034d9
commit 69f7c878b9
5 changed files with 18 additions and 14 deletions

View File

@ -22,7 +22,8 @@ static constexpr USBID IDs[] = {
USBInBuffer::USBInBuffer(libusb_device_handle *handle, unsigned char endpoint, int buffer_size) : USBInBuffer::USBInBuffer(libusb_device_handle *handle, unsigned char endpoint, int buffer_size) :
buffer_size(buffer_size), buffer_size(buffer_size),
received_size(0), received_size(0),
inCallback(false) inCallback(false),
cancelling(false)
{ {
buffer = new unsigned char[buffer_size]; buffer = new unsigned char[buffer_size];
memset(buffer, 0, buffer_size); memset(buffer, 0, buffer_size);
@ -34,6 +35,7 @@ USBInBuffer::USBInBuffer(libusb_device_handle *handle, unsigned char endpoint, i
USBInBuffer::~USBInBuffer() USBInBuffer::~USBInBuffer()
{ {
if(transfer) { if(transfer) {
cancelling = true;
libusb_cancel_transfer(transfer); libusb_cancel_transfer(transfer);
// wait for cancellation to complete // wait for cancellation to complete
mutex mtx; mutex mtx;
@ -67,6 +69,13 @@ int USBInBuffer::getReceived() const
void USBInBuffer::Callback(libusb_transfer *transfer) 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); // qDebug() << libusb_error_name(transfer->status);
switch(transfer->status) { switch(transfer->status) {
case LIBUSB_TRANSFER_COMPLETED: case LIBUSB_TRANSFER_COMPLETED:
@ -111,11 +120,7 @@ void USBInBuffer::Callback(libusb_transfer *transfer)
// nothing to do // nothing to do
break; break;
case LIBUSB_TRANSFER_CANCELLED: case LIBUSB_TRANSFER_CANCELLED:
// destructor called, do not resubmit // already handled before switch-case
libusb_free_transfer(transfer);
this->transfer = nullptr;
cv.notify_all();
return;
break; break;
} }
// Resubmit the transfer // Resubmit the transfer

View File

@ -40,6 +40,7 @@ private:
int buffer_size; int buffer_size;
int received_size; int received_size;
bool inCallback; bool inCallback;
bool cancelling;
std::condition_variable cv; std::condition_variable cv;
}; };

View File

@ -1160,7 +1160,7 @@ void VNA::StartCalibrationMeasurements(std::set<CalibrationMeasurement::Base*> m
} }
// Stop sweep // Stop sweep
running = false; running = false;
SettingsChanged(); ConfigureDevice();
calMeasurements = m; calMeasurements = m;
// Delete any already captured data of this measurement // Delete any already captured data of this measurement
cal.clearMeasurements(m); cal.clearMeasurements(m);
@ -1187,6 +1187,7 @@ void VNA::StartCalibrationMeasurements(std::set<CalibrationMeasurement::Base*> m
cal.clearMeasurements(calMeasurements); cal.clearMeasurements(calMeasurements);
}); });
// Trigger sweep to start from beginning // Trigger sweep to start from beginning
running = true;
ConfigureDevice(true, [=](bool){ ConfigureDevice(true, [=](bool){
// enable calibration measurement only in transmission callback (prevents accidental sampling of data which was still being processed) // enable calibration measurement only in transmission callback (prevents accidental sampling of data which was still being processed)
calMeasuring = true; calMeasuring = true;
@ -1707,15 +1708,14 @@ void VNA::ConfigureDevice(bool resetTraces, std::function<void(bool)> cb)
} else { } else {
if(window->getDevice()) { if(window->getDevice()) {
changingSettings = true; changingSettings = true;
// single sweep finished
window->getDevice()->setIdle([=](bool){ window->getDevice()->setIdle([=](bool){
emit sweepStopped();
changingSettings = false; changingSettings = false;
}); });
} else { } else {
emit sweepStopped(); emit sweepStopped();
changingSettings = false; changingSettings = false;
} }
emit sweepStopped();
} }
} }

View File

@ -502,10 +502,10 @@ void Preferences::load()
for(auto d : descr) { for(auto d : descr) {
try { try {
d.var.setValue(settings.value(d.name, d.def)); 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){ } catch (const exception& e){
d.var.setValue(d.def); d.var.setValue(d.def);
qDebug() << "Setting" << d.name << "reset to default:" << d.def; // qDebug() << "Setting" << d.name << "reset to default:" << d.def;
} }
} }
nonTrivialParsing(); nonTrivialParsing();

View File

@ -174,9 +174,7 @@ public:
private: private:
Preferences() : Preferences() :
TCPoverride(false) { TCPoverride(false) {}
qDebug() << "Pref constructor: " << &compoundDeviceJSON;
}
static Preferences instance; static Preferences instance;
const std::vector<Savable::SettingDescription> descr = {{ const std::vector<Savable::SettingDescription> descr = {{