stopped device dialogue opening on start. calibFile in device_config.ini

This commit is contained in:
Blake Leverington 2024-08-07 11:59:13 +02:00
parent a4a3b34247
commit dfcc2a7394
2 changed files with 45 additions and 14 deletions

View File

@ -120,11 +120,7 @@ void MainWindow::setupHardware()
// Retrieve device-specific settings from DialogDevices // Retrieve device-specific settings from DialogDevices
// Open the dialog to configure devices and retrieve calibration factors
DialogDevices dlg;
dlg.deviceSettings = deviceSettings;
if (dlg.exec() == QDialog::Accepted) {
QVector<QVector<unsigned short>> allCalibrationFactors = dlg.getAllCalibrationFactors();
for (int dev_nr = 0; dev_nr < nr_devices; dev_nr++) { for (int dev_nr = 0; dev_nr < nr_devices; dev_nr++) {
top(deviceSettings); top(deviceSettings);
@ -147,15 +143,11 @@ void MainWindow::setupHardware()
dc.clustersize = deviceSettings->value("ClusterSize").toInt(); dc.clustersize = deviceSettings->value("ClusterSize").toInt();
// Get calibration factors for this device // Get calibration factors for this device
if (dev_nr < allCalibrationFactors.size()) { // Get calibration factors from the settings
QVector<unsigned short> calibFactors = allCalibrationFactors[dev_nr]; QString calibFilePath = deviceSettings->value("CalibFile").toString();
for (int i = 0; i < 320; i++) { if (!calibFilePath.isEmpty()) {
if (i < calibFactors.size() && calibFactors[i] >= 0 && calibFactors[i] <= 65535) { // Load calibration factors from the file
dc.calibrationFactor[i] = calibFactors[i]; loadCalibrationFactors(calibFilePath, dc.calibrationFactor);
} else {
dc.calibrationFactor[i] = 8192; // Default value if not set or invalid
}
}
} else { } else {
// Default to 8192 if no calibration factors were set for this device // Default to 8192 if no calibration factors were set for this device
std::fill(std::begin(dc.calibrationFactor), std::end(dc.calibrationFactor), 8192); std::fill(std::begin(dc.calibrationFactor), std::end(dc.calibrationFactor), 8192);
@ -180,9 +172,47 @@ void MainWindow::setupHardware()
theHW->configureDevice(dev_nr, dc); // Configure the device and an entry in base address table in the event builder theHW->configureDevice(dev_nr, dc); // Configure the device and an entry in base address table in the event builder
} }
}
// Provide a function to load calibration factors from a file
bool MainWindow::loadCalibrationFactors(const QString& filePath, int* calibrationFactors)
{
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qWarning() << "Could not open calibration file:" << filePath;
return false;
} }
QTextStream in(&file);
int index = 0;
bool allValid = true;
while (!in.atEnd() && index < 320) {
QString line = in.readLine().trimmed();
bool ok;
int value = line.toInt(&ok);
// Validate that value is within the range of an unsigned short
if (ok && value >= 0 && value <= 65535) {
calibrationFactors[index] = value;
} else {
qWarning() << "Invalid calibration factor:" << line << "at index" << index;
calibrationFactors[index] = 8192; // Default to 8192 if invalid
allValid = false;
}
index++;
}
// If file has fewer than 320 lines, fill the rest with default values
while (index < 320) {
calibrationFactors[index++] = 8192;
}
file.close();
return allValid;
} }

View File

@ -57,6 +57,7 @@ protected:
QTimer timer; QTimer timer;
QLineEdit status1; QLineEdit status1;
QLineEdit statusKeithley; QLineEdit statusKeithley;
bool loadCalibrationFactors(const QString& filePath, int* calibrationFactors); // Loads calibration factors from a file
private slots: private slots: