Skip to content

Commit

Permalink
Bugfixes LibreCAL automatic calibration dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
jankae committed Jan 4, 2024
1 parent 10e3135 commit 8c1d2d1
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,59 @@
#include <thread>

#include <QDebug>

#include <QDateTime>
using namespace std;

static QString getLocalDateTimeWithUtcOffset()
{
QDateTime currentDateTime = QDateTime::currentDateTime();
int year = currentDateTime.date().year();
int month = currentDateTime.date().month();
int day = currentDateTime.date().day();
int hour = currentDateTime.time().hour();
int minute = currentDateTime.time().minute();
int second = currentDateTime.time().second();
int utcOffset = currentDateTime.offsetFromUtc() / 3600;
int utcOffsetMinute = (currentDateTime.offsetFromUtc() % 3600) / 60;
QString dateTimeString = QString("%1/%2/%3 %4:%5:%6 UTC%7%8:%9")
.arg(year, 4, 10, QChar('0'))
.arg(month, 2, 10, QChar('0'))
.arg(day, 2, 10, QChar('0'))
.arg(hour, 2, 10, QChar('0'))
.arg(minute, 2, 10, QChar('0'))
.arg(second, 2, 10, QChar('0'))
.arg(utcOffset > 0 ? "+" : "-") // Add a plus sign for positive offsets
.arg(qAbs(utcOffset), 2, 10, QChar('0'))
.arg(utcOffsetMinute, 2, 10, QChar('0'));
return dateTimeString;
}

CalDevice::CalDevice(QString serial) :
usb(new USBDevice(serial))
{
// Check device identification
auto id = usb->Query("*IDN?");
if(!id.startsWith("LibreCAL_")) {
if(!id.startsWith("LibreCAL,")) {
delete usb;
throw std::runtime_error("Invalid response to *IDN?: "+id.toStdString());
}

firmware = usb->Query(":FIRMWARE?");
QList<QString> fw_version = firmware.split(".");
if (fw_version.size() == 3) {
int firmware_major = fw_version[0].toInt();
int firmware_minor = fw_version[1].toInt();
this->firmware_major_minor = firmware_major + ((float)firmware_minor/10.0f);
if(this->firmware_major_minor >= 0.2)
{
/* Set Date Time UTC */
QString LocalDateTimeWithUtcOffset = getLocalDateTimeWithUtcOffset();
QString ret = usb->Query(":DATE_TIME "+LocalDateTimeWithUtcOffset);
}
} else {
// fw_version invalid
this->firmware_major_minor = 0.0;
}
QString ports = usb->Query(":PORTS?");
bool okay;
numPorts = ports.toInt(&okay);
Expand Down Expand Up @@ -119,6 +158,17 @@ bool CalDevice::enterBootloader()
return usb->Cmd(":BOOTloader");
}

QString CalDevice::getDateTimeUTC()
{
if(this->firmware_major_minor >= 0.2)
{
return usb->Query(":DATE_TIME?");
}else
{
return ""; // Not available
}
}

void CalDevice::loadCoefficientSets(QStringList names)
{
coeffSets.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class CalDevice : public QObject
QString getFirmware() const;
unsigned int getNumPorts() const;

QString getDateTimeUTC();

bool enterBootloader();

class CoefficientSet {
Expand Down Expand Up @@ -97,6 +99,8 @@ class CalDevice : public QObject
QString firmware;
int numPorts;

float firmware_major_minor;

std::vector<CoefficientSet> coeffSets;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ LibreCALDialog::LibreCALDialog(Calibration *cal) :
device = new CalDevice(text);
} catch (exception &e) {
device = nullptr;
InformationBox::ShowError("Failed to connect", e.what(), this);
}
if(device) {
createPortAssignmentUI();
connect(device, &CalDevice::updateCoefficientsPercent, ui->progressCoeff, &QProgressBar::setValue);
connect(device, &CalDevice::updateCoefficientsDone, [=](bool success){
connect(device, &CalDevice::updateCoefficientsDone, this, [=](bool success){
busy = false;
if(success) {
ui->progressCoeff->setValue(100);
Expand All @@ -48,7 +49,7 @@ LibreCALDialog::LibreCALDialog(Calibration *cal) :
ui->lCoefficientStatus->setText("Failed to load coefficients");
}
updateCalibrationStartStatus();
});
}, Qt::QueuedConnection);

ui->cbCoefficients->clear();
ui->cbCoefficients->addItem("Select...");
Expand Down Expand Up @@ -389,7 +390,7 @@ void LibreCALDialog::startCalibration()
}
setTerminationOnAllUsedPorts(CalDevice::Standard(CalDevice::Standard::Type::None));
auto m = throughMeasurements[throughIndex];
device->setStandard(m->getPort1(), CalDevice::Standard(m->getPort2()));
device->setStandard(portAssignment[m->getPort1()-1], CalDevice::Standard(portAssignment[m->getPort2()-1]));
emit cal->startMeasurements({m});
}
break;
Expand Down

0 comments on commit 8c1d2d1

Please sign in to comment.