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) :
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

View File

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

View File

@ -1160,7 +1160,7 @@ void VNA::StartCalibrationMeasurements(std::set<CalibrationMeasurement::Base*> 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<CalibrationMeasurement::Base*> 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<void(bool)> 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();
}
}

View File

@ -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();

View File

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