diff --git a/hit2023v2_RMS/dialogdevices.cpp b/hit2023v2_RMS/dialogdevices.cpp index c15eb0b..81bd5fe 100644 --- a/hit2023v2_RMS/dialogdevices.cpp +++ b/hit2023v2_RMS/dialogdevices.cpp @@ -22,6 +22,84 @@ DialogDevices::~DialogDevices() //***************** Initialization ****************** + +//***************** Processing ****************** + +void DialogDevices::accept() +{ +// qInfo("Accepted!"); + + if (validateAndSave()) + QDialog::accept(); +} + + + + +void DialogDevices::applyCalibrationDataToDevice(int dev_nr, const QVector& data) +{ + // Implementation for applying the calibration data to the specific device + // For example, updating device settings or performing calculations + qDebug() << "Calibration data loaded for Device" << dev_nr << ":" << data; + + // Example: Set the calibration data in your application logic + // deviceSettings->setValue("Device%1/CalibrationData", data); // if storing in settings +} + +// Load calibration factors from a file and store them +void DialogDevices::selectCalibrationFile(int dev_nr) +{ + QString filename = QFileDialog::getOpenFileName(this, "Select Calibration File", "", "Text Files (*.txt);;All Files (*)"); + + if (filename.isEmpty()) { + return; // User canceled the dialog + } + + QVector calibrationFactors(320, 8192); // Initialize with default value 8192 + QFile file(filename); + + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { + QMessageBox::critical(this, "Error", "Could not open calibration file: " + filename); + return; + } + + QTextStream in(&file); + int index = 0; + while (!in.atEnd() && index < 320) { + QString line = in.readLine().trimmed(); + bool ok; + int value = line.toInt(&ok); + + // Validate value and ensure it is within the range of an unsigned short + if (ok && value >= 0 && value <= 65535) { + calibrationFactors[index] = value; + } else { + qWarning() << "Invalid calibration factor in file:" << line << "at index" << index; + calibrationFactors[index] = 8192; // Use default if invalid + } + index++; + } + + file.close(); + + // Store calibration factors for the device + if (dev_nr < deviceCalibrationData.size()) { + deviceCalibrationData[dev_nr] = calibrationFactors; + } else { + deviceCalibrationData.append(calibrationFactors); + } + + // Update the table display to show the file path (optional) + QTableWidgetItem* calibFileItem = new QTableWidgetItem(filename); + ui->tableDevices->setItem(dev_nr, 10, calibFileItem); +} + +// Retrieve all calibration factors for each device +QVector> DialogDevices::getAllCalibrationFactors() const +{ + return deviceCalibrationData; +} + void DialogDevices::showEvent(QShowEvent * event) { if (!event->spontaneous()) @@ -60,121 +138,49 @@ void DialogDevices::showEvent(QShowEvent * event) QDialog::showEvent(event); } -//***************** Processing ****************** - -void DialogDevices::accept() -{ -// qInfo("Accepted!"); - - if (validateAndSave()) - QDialog::accept(); -} - - -void DialogDevices::selectCalibrationFile(int dev_nr) -{ - // Open a file dialog to select a calibration file - QString filename = QFileDialog::getOpenFileName(this, "Select Calibration File", "", "Text Files (*.txt);;All Files (*)"); - - if (filename.isEmpty()) - { - return; // User canceled the dialog - } - - // Set the file name in the table for the corresponding device - QTableWidgetItem* calibItem = new QTableWidgetItem(filename); - ui->tableDevices->setItem(dev_nr, 10, calibItem); - - // Load calibration data from the file - QVector calibrationData; - QFile file(filename); - - if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) - { - QMessageBox::critical(this, "Error", "Could not open file for reading"); - return; - } - - QTextStream in(&file); - while (!in.atEnd()) - { - QString line = in.readLine(); - bool ok; - unsigned short value = line.toUShort(&ok); - - if (ok) - { - calibrationData.append(value); - } - else - { - calibrationData.append(32768); // assume calibration range is 0 = and 1 = 65536 - QMessageBox::warning(this, "Warning", "Invalid data in file. Skipping line."); - } - } - - file.close(); - - // Store the calibration data for the device - applyCalibrationDataToDevice(dev_nr, calibrationData); -} - -void DialogDevices::applyCalibrationDataToDevice(int dev_nr, const QVector& data) -{ - // Implementation for applying the calibration data to the specific device - // For example, updating device settings or performing calculations - qDebug() << "Calibration data loaded for Device" << dev_nr << ":" << data; - - // Example: Set the calibration data in your application logic - // deviceSettings->setValue("Device%1/CalibrationData", data); // if storing in settings -} - - void DialogDevices::importSettings() { int nr_devices = ui->spinNrDevices->value(); - ui->tableDevices->setRowCount(nr_devices); + // Ensure the device calibration data vector is initialized properly + deviceCalibrationData.resize(nr_devices); + for (int dev_nr = 0; dev_nr < nr_devices; dev_nr++) { - if (dev_nr >= last_nr_devices) //update only new rows! + if (dev_nr >= last_nr_devices) // Update only new rows! { top(deviceSettings); QString group_label = QString("Device%1").arg(dev_nr); deviceSettings->beginGroup(group_label); QTableWidgetItem* newItem; - newItem = new QTableWidgetItem(deviceSettings->value("IP","0.0.0.0").toString()); - ui->tableDevices->setItem(dev_nr, 0, newItem ); - newItem = new QTableWidgetItem(deviceSettings->value("HardwareVer","1").toString()); - ui->tableDevices->setItem(dev_nr, 1, newItem ); - newItem = new QTableWidgetItem(deviceSettings->value("Plane","0").toString()); - ui->tableDevices->setItem(dev_nr, 2, newItem ); - newItem = new QTableWidgetItem(deviceSettings->value("Position","0").toString()); - ui->tableDevices->setItem(dev_nr, 3, newItem ); - newItem = new QTableWidgetItem(deviceSettings->value("Sensors","2").toString()); - ui->tableDevices->setItem(dev_nr, 4, newItem ); - newItem = new QTableWidgetItem(deviceSettings->value("Master","0").toString()); - ui->tableDevices->setItem(dev_nr, 5, newItem ); - newItem = new QTableWidgetItem(deviceSettings->value("MasterDelay","22").toString()); - ui->tableDevices->setItem(dev_nr, 6, newItem ); - newItem = new QTableWidgetItem(deviceSettings->value("SlaveDelay","1").toString()); - ui->tableDevices->setItem(dev_nr, 7, newItem ); - newItem = new QTableWidgetItem(deviceSettings->value("ClusThresh","10").toString()); - ui->tableDevices->setItem(dev_nr, 8, newItem ); - newItem = new QTableWidgetItem(deviceSettings->value("ClusSize","4").toString()); - ui->tableDevices->setItem(dev_nr, 9, newItem ); - - // Add an item for the calibration file path (optional, for display) - newItem = new QTableWidgetItem(deviceSettings->value("CalibFile", "").toString()); - ui->tableDevices->setItem(dev_nr, 10, newItem); + newItem = new QTableWidgetItem(deviceSettings->value("IP", "0.0.0.0").toString()); + ui->tableDevices->setItem(dev_nr, 0, newItem); + newItem = new QTableWidgetItem(deviceSettings->value("HardwareVer", "1").toString()); + ui->tableDevices->setItem(dev_nr, 1, newItem); + newItem = new QTableWidgetItem(deviceSettings->value("Plane", "0").toString()); + ui->tableDevices->setItem(dev_nr, 2, newItem); + newItem = new QTableWidgetItem(deviceSettings->value("Position", "0").toString()); + ui->tableDevices->setItem(dev_nr, 3, newItem); + newItem = new QTableWidgetItem(deviceSettings->value("Sensors", "2").toString()); + ui->tableDevices->setItem(dev_nr, 4, newItem); + newItem = new QTableWidgetItem(deviceSettings->value("Master", "0").toString()); + ui->tableDevices->setItem(dev_nr, 5, newItem); + newItem = new QTableWidgetItem(deviceSettings->value("MasterDelay", "22").toString()); + ui->tableDevices->setItem(dev_nr, 6, newItem); + newItem = new QTableWidgetItem(deviceSettings->value("SlaveDelay", "1").toString()); + ui->tableDevices->setItem(dev_nr, 7, newItem); + newItem = new QTableWidgetItem(deviceSettings->value("ClusThresh", "10").toString()); + ui->tableDevices->setItem(dev_nr, 8, newItem); + newItem = new QTableWidgetItem(deviceSettings->value("ClusSize", "4").toString()); + ui->tableDevices->setItem(dev_nr, 9, newItem); // Add a button for selecting calibration files QPushButton* calibButton = new QPushButton("Select File"); - ui->tableDevices->setCellWidget(dev_nr, 11, calibButton); // Column index 11 for button + ui->tableDevices->setCellWidget(dev_nr, 11, calibButton); - // Connect the button to a slot with the sensor index as a parameter + // Connect the button to the slot connect(calibButton, &QPushButton::clicked, this, [this, dev_nr]() { selectCalibrationFile(dev_nr); }); } } @@ -354,22 +360,3 @@ void DialogDevices::on_spinNrDevices_valueChanged(int arg1) importSettings(); } -QVector> DialogDevices::getAllCalibrationFactors() const { - QVector> calibrationFactors; - for (int row = 0; row < ui->tableDevices->rowCount(); ++row) { - QVector factors(320, 8192); // Initialize with default value of 8192 - QTableWidgetItem* item = ui->tableDevices->item(row, 10); // Assuming column 10 is for calibration factors - if (item) { - QStringList factorStrings = item->text().split(',', Qt::SkipEmptyParts); - for (int i = 0; i < factorStrings.size() && i < 320; ++i) { - bool ok; - int factor = factorStrings[i].toInt(&ok); - if (ok && factor >= 0 && factor <= 65535) { - factors[i] = factor; - } - } - } - calibrationFactors.append(factors); - } - return calibrationFactors; -} diff --git a/hit2023v2_RMS/dialogdevices.h b/hit2023v2_RMS/dialogdevices.h index f90491a..0b80329 100644 --- a/hit2023v2_RMS/dialogdevices.h +++ b/hit2023v2_RMS/dialogdevices.h @@ -19,7 +19,7 @@ public: ~DialogDevices(); QSettings* deviceSettings = NULL; - QVector> getAllCalibrationFactors() const; // Declaration of the method + QVector> getAllCalibrationFactors() const; // Declaration of the method public slots: void showEvent(QShowEvent *event); void accept(); @@ -29,7 +29,7 @@ protected: void importSettings(); int last_nr_devices = -1; int initialized = 0; - QVector> calibrationData; // Storage for calibration data for each device + QVector> deviceCalibrationData; // Storage for calibration data for each device private slots: void on_spinNrDevices_valueChanged(int arg1); void selectCalibrationFile(int dev_nr); // Slot for handling calibration file selection diff --git a/hit2023v2_RMS/mainwindow.cpp b/hit2023v2_RMS/mainwindow.cpp index 02599f1..8b54483 100644 --- a/hit2023v2_RMS/mainwindow.cpp +++ b/hit2023v2_RMS/mainwindow.cpp @@ -118,41 +118,51 @@ void MainWindow::setupHardware() int tint_v2 = deviceSettings->value("Tint_v2").toInt(); int gain_v2 = deviceSettings->value("Gain_v2").toInt(); // Retrieve device-specific settings from DialogDevices + + + // Open the dialog to configure devices and retrieve calibration factors DialogDevices dlg; dlg.deviceSettings = deviceSettings; - QVector> allCalibrationFactors = dlg.getAllCalibrationFactors(); + if (dlg.exec() == QDialog::Accepted) { + QVector> allCalibrationFactors = dlg.getAllCalibrationFactors(); - for (int dev_nr = 0; dev_nr < nr_devices; dev_nr++) - { - top(deviceSettings); - QString group_label = QString("Device%1").arg(dev_nr); - deviceSettings->beginGroup(group_label); - dc.device_id = dev_nr; - dc.hardware_ver = deviceSettings->value("HardwareVer").toInt(); - ip2num(deviceSettings->value("IP").toString(), ip); - for (int i = 0; i < 4; i++)//4 bytes - dc.device_ip[i] = ip[i]; - dc.master = deviceSettings->value("Master").toInt(); - dc.plane = deviceSettings->value("Plane").toInt(); - dc.position = deviceSettings->value("Position").toInt(); - dc.nr_sensors = deviceSettings->value("Sensors").toInt(); - dc.master_delay = deviceSettings->value("MasterDelay").toInt(); - dc.slave_delay = deviceSettings->value("SlaveDelay").toInt(); - dc.threshold = deviceSettings->value("Threshold").toInt(); - dc.clustersize = deviceSettings->value("ClusterSize").toInt(); -/* - // Get calibration factors for this device - QVector calibFactors = allCalibrationFactors[dev_nr]; - for (int i = 0; i < 320; i++) { - if (i < calibFactors.size() && calibFactors[i] >= 0 && calibFactors[i] <= 65535) { - dc.calibrationFactor[i] = calibFactors[i]; - } else { - dc.calibrationFactor[i] = 8192; // Default value if not set or invalid + for (int dev_nr = 0; dev_nr < nr_devices; dev_nr++) { + top(deviceSettings); + QString group_label = QString("Device%1").arg(dev_nr); + deviceSettings->beginGroup(group_label); + + dc.device_id = dev_nr; + dc.hardware_ver = deviceSettings->value("HardwareVer").toInt(); + ip2num(deviceSettings->value("IP").toString(), ip); + for (int i = 0; i < 4; i++) { // 4 bytes + dc.device_ip[i] = ip[i]; } - } -*/ - switch (dc.hardware_ver) - { + dc.master = deviceSettings->value("Master").toInt(); + dc.plane = deviceSettings->value("Plane").toInt(); + dc.position = deviceSettings->value("Position").toInt(); + dc.nr_sensors = deviceSettings->value("Sensors").toInt(); + dc.master_delay = deviceSettings->value("MasterDelay").toInt(); + dc.slave_delay = deviceSettings->value("SlaveDelay").toInt(); + dc.threshold = deviceSettings->value("Threshold").toInt(); + dc.clustersize = deviceSettings->value("ClusterSize").toInt(); + + // Get calibration factors for this device + if (dev_nr < allCalibrationFactors.size()) { + QVector calibFactors = allCalibrationFactors[dev_nr]; + for (int i = 0; i < 320; i++) { + if (i < calibFactors.size() && calibFactors[i] >= 0 && calibFactors[i] <= 65535) { + dc.calibrationFactor[i] = calibFactors[i]; + } else { + dc.calibrationFactor[i] = 8192; // Default value if not set or invalid + } + } + } else { + // Default to 8192 if no calibration factors were set for this device + std::fill(std::begin(dc.calibrationFactor), std::end(dc.calibrationFactor), 8192); + } + + switch (dc.hardware_ver) + { case 1: dc.period = period_v1; dc.tint = tint_v1; @@ -165,40 +175,14 @@ void MainWindow::setupHardware() break; default: qCritical("Unsupported hardware version!"); - break; - } + break; + } - theHW->configureDevice(dev_nr, dc); //configure the device and an entry in base address table in the event builder - } - - - -/* - // Assume calibration factors are stored as a list or comma-separated string - QString calibFactorsStr = deviceSettings->value("CalibFactors").toString(); - QStringList calibFactorList = calibFactorsStr.split(',', Qt::SkipEmptyParts); - - // Clear any existing calibration data - for (int i = 0; i < 320; i++) { - dc.calibrationFactor[i] = 0; // Initialize to 0 or some default - } - - // Load calibration factors - int index = 0; - for (const QString& factor : calibFactorList) { - if (index >= 320) break; // Ensure we do not overflow the array - bool ok; - int value = factor.toInt(&ok); - if (ok) { - dc.calibrationFactor[index] = value; - index++; - } else { - qWarning() << "Invalid calibration factor:" << factor; + theHW->configureDevice(dev_nr, dc); // Configure the device and an entry in base address table in the event builder } } -*/ - //theDisplay.setup(&theHW); + } diff --git a/hit2023v2_RMS/release/hit2023v2.exe b/hit2023v2_RMS/release/hit2023v2.exe index 6e7f8a3..5a7e393 100644 Binary files a/hit2023v2_RMS/release/hit2023v2.exe and b/hit2023v2_RMS/release/hit2023v2.exe differ