Reference control added

This commit is contained in:
Jan Käberich 2020-09-12 12:17:35 +02:00
parent 6960498fcb
commit e266d37b96
13 changed files with 272 additions and 143 deletions

Binary file not shown.

View file

@ -82,14 +82,11 @@ Device::~Device()
}
}
bool Device::Configure(Protocol::SweepSettings settings)
bool Device::SendPacket(Protocol::PacketInfo packet)
{
if(m_connected) {
unsigned char buffer[128];
Protocol::PacketInfo p;
p.type = Protocol::PacketType::SweepSettings;
p.settings = settings;
unsigned int length = Protocol::EncodePacket(p, buffer, sizeof(buffer));
unsigned char buffer[1024];
unsigned int length = Protocol::EncodePacket(packet, buffer, sizeof(buffer));
if(!length) {
qCritical() << "Failed to encode packet";
return false;
@ -107,78 +104,35 @@ bool Device::Configure(Protocol::SweepSettings settings)
}
}
bool Device::Configure(Protocol::SweepSettings settings)
{
Protocol::PacketInfo p;
p.type = Protocol::PacketType::SweepSettings;
p.settings = settings;
return SendPacket(p);
}
bool Device::SetManual(Protocol::ManualControl manual)
{
if(m_connected) {
unsigned char buffer[128];
Protocol::PacketInfo p;
p.type = Protocol::PacketType::ManualControl;
p.manual = manual;
unsigned int length = Protocol::EncodePacket(p, buffer, sizeof(buffer));
if(!length) {
qCritical() << "Failed to encode packet";
return false;
}
int actual_length;
auto ret = libusb_bulk_transfer(m_handle, EP_Data_Out_Addr, buffer, length, &actual_length, 0);
if(ret < 0) {
qCritical() << "Error sending data: "
<< libusb_strerror((libusb_error) ret);
return false;
}
return true;
} else {
return false;
}
Protocol::PacketInfo p;
p.type = Protocol::PacketType::ManualControl;
p.manual = manual;
return SendPacket(p);
}
bool Device::SendFirmwareChunk(Protocol::FirmwarePacket &fw)
{
if(m_connected) {
unsigned char buffer[Protocol::FirmwareChunkSize + 4 + 8];
Protocol::PacketInfo p;
p.type = Protocol::PacketType::FirmwarePacket;
p.firmware = fw;
unsigned int length = Protocol::EncodePacket(p, buffer, sizeof(buffer));
if(!length) {
qCritical() << "Failed to encode packet";
return false;
}
int actual_length;
auto ret = libusb_bulk_transfer(m_handle, EP_Data_Out_Addr, buffer, length, &actual_length, 0);
if(ret < 0) {
qCritical() << "Error sending data: "
<< libusb_strerror((libusb_error) ret);
return false;
}
return true;
} else {
return false;
}
Protocol::PacketInfo p;
p.type = Protocol::PacketType::FirmwarePacket;
p.firmware = fw;
return SendPacket(p);
}
bool Device::SendCommandWithoutPayload(Protocol::PacketType type)
{
if(m_connected) {
unsigned char buffer[32];
Protocol::PacketInfo p;
p.type = type;
unsigned int length = Protocol::EncodePacket(p, buffer, sizeof(buffer));
if(!length) {
qCritical() << "Failed to encode packet";
return false;
}
int actual_length;
auto ret = libusb_bulk_transfer(m_handle, EP_Data_Out_Addr, buffer, length, &actual_length, 0);
if(ret < 0) {
qCritical() << "Error sending data: "
<< libusb_strerror((libusb_error) ret);
return false;
}
return true;
} else {
return false;
}
Protocol::PacketInfo p;
p.type = type;
return SendPacket(p);
}
std::set<QString> Device::GetDevices()

View file

@ -46,6 +46,7 @@ public:
// connect to a VNA device. If serial is specified only connecting to this device, otherwise to the first one found
Device(QString serial = QString());
~Device();
bool SendPacket(Protocol::PacketInfo packet);
bool Configure(Protocol::SweepSettings settings);
bool SetManual(Protocol::ManualControl manual);
bool SendFirmwareChunk(Protocol::FirmwarePacket &fw);

View file

@ -403,7 +403,6 @@ void VNA::ConnectToDevice(QString serial)
lConnectionStatus.setText("Connected to " + device->serial());
qInfo() << "Connected to " << device->serial();
lDeviceInfo.setText(device->getLastDeviceInfoString());
device->Configure(settings);
connect(device, &Device::DatapointReceived, this, &VNA::NewDatapoint);
connect(device, &Device::LogLineReceived, &deviceLog, &DeviceLog::addLine);
connect(device, &Device::ConnectionLost, this, &VNA::DeviceConnectionLost);
@ -414,21 +413,26 @@ void VNA::ConnectToDevice(QString serial)
ui->actionManual_Control->setEnabled(true);
ui->menuDefault_Calibration->setEnabled(true);
ui->actionFirmware_Update->setEnabled(true);
// Check if default calibration exists and attempt to load it
QSettings settings;
auto key = "DefaultCalibration"+device->serial();
if (settings.contains(key)) {
auto filename = settings.value(key).toString();
qDebug() << "Attempting to load default calibration file \"" << filename << "\"";
if(QFile::exists(filename)) {
cal.openFromFile(filename);
ApplyCalibration(cal.getType());
{
// Check if default calibration exists and attempt to load it
QSettings settings;
auto key = "DefaultCalibration"+device->serial();
if (settings.contains(key)) {
auto filename = settings.value(key).toString();
qDebug() << "Attempting to load default calibration file \"" << filename << "\"";
if(QFile::exists(filename)) {
cal.openFromFile(filename);
ApplyCalibration(cal.getType());
}
ui->actionRemoveDefaultCal->setEnabled(true);
} else {
qDebug() << "No default calibration file set for this device";
ui->actionRemoveDefaultCal->setEnabled(false);
}
ui->actionRemoveDefaultCal->setEnabled(true);
} else {
qDebug() << "No default calibration file set for this device";
ui->actionRemoveDefaultCal->setEnabled(false);
}
// Configure initial state of device
device->Configure(settings);
UpdateReference();
} catch (const runtime_error e) {
DisconnectDevice();
UpdateDeviceList();
@ -553,26 +557,28 @@ void VNA::CreateToolbars()
// Reference toolbar
auto tb_reference = new QToolBar("Reference", this);
tb_reference->addWidget(new QLabel("Ref:"));
toolbars.referenceType = new QComboBox();
toolbars.referenceType->addItem("Int");
toolbars.referenceType->addItem("Ext");
auto refInAuto = new QCheckBox("Auto");
refInAuto->setChecked(true);
toolbars.referenceType->setEnabled(false);
connect(refInAuto, &QCheckBox::clicked, [this](bool checked) {
// TODO change device settings
toolbars.referenceType->setEnabled(!checked);
toolbars.reference.type = new QComboBox();
toolbars.reference.type->addItem("Int");
toolbars.reference.type->addItem("Ext");
toolbars.reference.automatic = new QCheckBox("Auto");
connect(toolbars.reference.automatic, &QCheckBox::clicked, [this](bool checked) {
toolbars.reference.type->setEnabled(!checked);
UpdateReference();
});
tb_reference->addWidget(toolbars.referenceType);
tb_reference->addWidget(refInAuto);
// toolbars.reference.automatic->setChecked(true);
tb_reference->addWidget(toolbars.reference.type);
tb_reference->addWidget(toolbars.reference.automatic);
tb_reference->addSeparator();
tb_reference->addWidget(new QLabel("Ref out:"));
auto refOutEnabled = new QCheckBox();
auto refOutFreq = new QComboBox();
refOutFreq->addItem("10 MHz");
refOutFreq->addItem("100 MHz");
tb_reference->addWidget(refOutEnabled);
tb_reference->addWidget(refOutFreq);
toolbars.reference.outputEnabled = new QCheckBox();
toolbars.reference.outFreq = new QComboBox();
toolbars.reference.outFreq->addItem("10 MHz");
toolbars.reference.outFreq->addItem("100 MHz");
tb_reference->addWidget(toolbars.reference.outputEnabled);
tb_reference->addWidget(toolbars.reference.outFreq);
connect(toolbars.reference.type, qOverload<int>(&QComboBox::currentIndexChanged), this, &VNA::UpdateReference);
connect(toolbars.reference.outFreq, qOverload<int>(&QComboBox::currentIndexChanged), this, &VNA::UpdateReference);
connect(toolbars.reference.outputEnabled, &QCheckBox::clicked, this, &VNA::UpdateReference);
addToolBar(tb_reference);
@ -860,6 +866,35 @@ void VNA::StartCalibrationMeasurement(Calibration::Measurement m)
});
}
void VNA::UpdateReference()
{
if(!device) {
// can't update without a device connected
return;
}
Protocol::ReferenceSettings s = {};
if(toolbars.reference.automatic->isChecked()) {
s.AutomaticSwitch = 1;
}
if(toolbars.reference.type->currentText()=="Ext") {
s.UseExternalRef = 1;
}
if(toolbars.reference.outputEnabled->isChecked()) {
switch(toolbars.reference.outFreq->currentIndex()) {
case 0:
s.ExtRefOuputFreq = 10000000;
break;
case 1:
s.ExtRefOuputFreq = 100000000;
break;
}
}
Protocol::PacketInfo p;
p.type = Protocol::PacketType::Reference;
p.reference = s;
device->SendPacket(p);
}
void VNA::ConstrainAndUpdateFrequencies()
{
if(settings.f_stop > maxFreq) {

View file

@ -17,6 +17,7 @@
#include "Device/devicelog.h"
#include "preferences.h"
#include <QButtonGroup>
#include <QCheckBox>
namespace Ui {
class MainWindow;
@ -63,6 +64,7 @@ private slots:
void DisableCalibration(bool force = false);
void ApplyCalibration(Calibration::Type type);
void StartCalibrationMeasurement(Calibration::Measurement m);
void UpdateReference();
signals:
void CalibrationMeasurementComplete(Calibration::Measurement m);
@ -110,7 +112,12 @@ private:
QStackedWidget *central;
struct {
QComboBox *referenceType;
struct {
QComboBox *type;
QCheckBox *automatic;
QCheckBox *outputEnabled;
QComboBox *outFreq;
} reference;
} toolbars;
Preferences pref;