diff --git a/hit2023v2_RMS/datareceiver.cpp b/hit2023v2_RMS/datareceiver.cpp index e89876d..f472219 100644 --- a/hit2023v2_RMS/datareceiver.cpp +++ b/hit2023v2_RMS/datareceiver.cpp @@ -79,6 +79,9 @@ void DataReceiver::readData() rms.sigma = BYTES2SHORT(tmpBuffer+baseaddr+DATA_PACKET_HEADER_SIZE+DATA_SYNC_HEADER_SIZE+DATA_BLOCK_SIZE + 2); rms.max = BYTES2SHORT(tmpBuffer+baseaddr+DATA_PACKET_HEADER_SIZE+DATA_SYNC_HEADER_SIZE+DATA_BLOCK_SIZE + 4); rms.status = BYTES2SHORT(tmpBuffer+baseaddr+DATA_PACKET_HEADER_SIZE+DATA_SYNC_HEADER_SIZE+DATA_BLOCK_SIZE + 6); + for (unsigned int ii = 0; ii < 4; ii++) + rms.registers[ii] = BYTES2SHORT(tmpBuffer+baseaddr+DATA_PACKET_HEADER_SIZE+DATA_SYNC_HEADER_SIZE+DATA_BLOCK_SIZE + 8+ii*2); + data_to_push.rms_frame = rms; dataBuffer.push(data_to_push); diff --git a/hit2023v2_RMS/dev_commands.h b/hit2023v2_RMS/dev_commands.h index 4154215..2f5249b 100644 --- a/hit2023v2_RMS/dev_commands.h +++ b/hit2023v2_RMS/dev_commands.h @@ -111,10 +111,18 @@ //Slow control snapshot - read all channels of ADC #define COMMAND_SET_CLUSTER_THRESHOLD 0x4001 - //L: ? + //L: 1 //D: [] //Send 8 bit beam cluster selection threshold +#define COMMAND_SET_CLUSTER_SIZE 0x4002 + //L: 1 + + +#define COMMAND_SET_CALIBRATION_FACTOR 0x4003 + //L: 320*16bit + //D: [] + // ***DATA TRANSFER - SOCKET 1!!!!*** #define COMMAND_DATA_TRANSFER 0x8000 diff --git a/hit2023v2_RMS/device.cpp b/hit2023v2_RMS/device.cpp index cff1fe7..8d6169e 100644 --- a/hit2023v2_RMS/device.cpp +++ b/hit2023v2_RMS/device.cpp @@ -494,4 +494,38 @@ int Device::ctrlSetClusterThreshold(int threshold) return queryCtrl(COMMAND_SET_CLUSTER_THRESHOLD, txdata, rxdata); } +int Device::ctrlSetClusterSize(int size) +{ + QVector rxdata; + QVector txdata; + txdata.append((unsigned short)size); + + if (size) + return queryCtrl(COMMAND_SET_CLUSTER_SIZE, txdata, rxdata); + else + return queryCtrl(COMMAND_SET_CLUSTER_SIZE, txdata, rxdata); +} + +int Device::ctrlSetCalibrationFactor(int calibrationFactor[320]) +{ + // Check for null pointer to ensure valid input + if (calibrationFactor == nullptr) + { + // Handle the error case: log it, return an error code, or throw an exception + return -1; // Or handle as appropriate + } + + QVector rxdata; + QVector txdata; + + // Append each calibration factor to txdata + for (int i = 0; i < 320; ++i) + { + txdata.append(static_cast(calibrationFactor[i])); + } + + // Perform the queryCtrl operation with the populated txdata + return queryCtrl(COMMAND_SET_CALIBRATION_FACTOR, txdata, rxdata); +} + diff --git a/hit2023v2_RMS/device.h b/hit2023v2_RMS/device.h index 15717e6..9742dc9 100644 --- a/hit2023v2_RMS/device.h +++ b/hit2023v2_RMS/device.h @@ -34,22 +34,32 @@ public: int gain; //0 (low) or 1 (high); TO BE DONE int dma_bunch; int eth_bunch; - int threshold; - int max_sensors() + int threshold; //cluster amplitude threshold + int clustersize; //clustering channel size lower limit + int calibrationFactor[320]; + - { - switch(hardware_ver) - { - case 1: return 2; break; - case 2: return 5; break; - default: qCritical("Unsupported hardware version!"); return 0; break; - } - } - int max_channels() {return 64*max_sensors();} - int nr_channels() {return 64*nr_sensors;} DeviceConfig() : own_ip{10,0,7,1}, device_ip{10,0,7,2}, device_id{0}, hardware_ver{0}, master{1}, plane{0}, position{0}, nr_sensors{0}, - period{65535}, tint{1}, master_delay{1}, slave_delay{1}, gain{0}, dma_bunch{1}, eth_bunch{1}, threshold{10} {} + period{65535}, tint{1}, master_delay{1}, slave_delay{1}, gain{0}, dma_bunch{1}, eth_bunch{1}, threshold{10}, clustersize{4} + { + // Initialize calibrationFactor array with default value 8192 + for (int i = 0; i < 320; i++) { + calibrationFactor[i] = 8192; + } + } + + int max_sensors() + { + switch (hardware_ver) { + case 1: return 2; break; + case 2: return 5; break; + default: qCritical("Unsupported hardware version!"); return 0; break; + } + } + + int max_channels() { return 64 * max_sensors(); } + int nr_channels() { return 64 * nr_sensors; } }; @@ -103,6 +113,8 @@ protected: int ctrlSetMasterDelay(int tint); int ctrlSetSlaveDelay(int tint); int ctrlSetClusterThreshold(int threshold); + int ctrlSetClusterSize(int clustersize); + int ctrlSetCalibrationFactor(int calibrationFactor[320]); protected slots: void onConnected(); diff --git a/hit2023v2_RMS/device_config.ini b/hit2023v2_RMS/device_config.ini index b079857..20833f5 100644 --- a/hit2023v2_RMS/device_config.ini +++ b/hit2023v2_RMS/device_config.ini @@ -11,6 +11,9 @@ Sensors=5 Master=1 MasterDelay=61 SlaveDelay=34 +Threshold=10 +Size=4 +CalibFile= [Device1] IP=10.0.7.18 @@ -21,6 +24,9 @@ Sensors=5 Master=0 MasterDelay=7 SlaveDelay=1 +Threshold=10 +Size=4 +CalibFile= [Trigger] Period=9000 diff --git a/hit2023v2_RMS/dialogdevices.cpp b/hit2023v2_RMS/dialogdevices.cpp index ef9eeab..62b3075 100644 --- a/hit2023v2_RMS/dialogdevices.cpp +++ b/hit2023v2_RMS/dialogdevices.cpp @@ -1,6 +1,12 @@ #include "dialogdevices.h" #include "ui_dialogdevices.h" #include "helpers.h" +#include +#include +#include +#include +#include +#include // Include QPushButton header for button creation DialogDevices::DialogDevices(QWidget *parent) : QDialog(parent), @@ -28,7 +34,7 @@ void DialogDevices::showEvent(QShowEvent * event) ui->spinNrDevices->setValue(deviceSettings->value("NrDevices", int(1)).toInt()); - ui->tableDevices->setColumnCount(9); + ui->tableDevices->setColumnCount(12); QStringList h_header; h_header.append("IP Address"); h_header.append("Hardware ver."); @@ -39,6 +45,11 @@ void DialogDevices::showEvent(QShowEvent * event) h_header.append("Master dly"); h_header.append("Slave dly"); h_header.append("Threshold"); + h_header.append("ClusterSize"); + h_header.append("CalibFile"); // Column for showing file path + h_header.append("Select Calib File"); // New column for the button + + ui->tableDevices->setHorizontalHeaderLabels(h_header); importSettings(); @@ -59,6 +70,66 @@ void DialogDevices::accept() 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(); @@ -92,6 +163,19 @@ void DialogDevices::importSettings() 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); + + // 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 + + // Connect the button to a slot with the sensor index as a parameter + connect(calibButton, &QPushButton::clicked, this, [this, dev_nr]() { selectCalibrationFile(dev_nr); }); } } @@ -218,6 +302,19 @@ int DialogDevices::validateAndSave() data_ok = 0; ui->tableDevices->item(dev_nr,8)->setText(generated); + //now cluster size + user_data = ui->tableDevices->item(dev_nr,9)->text(); + num_value = user_data.toInt(); + if (num_value < 1) + num_value = 1; + if (num_value > 255) + num_value = 255; + generated = QString("%1").arg(num_value); + //if the strings are identical, save the value and return 1 + if (generated.compare(user_data) != 0) + data_ok = 0; + ui->tableDevices->item(dev_nr,9)->setText(generated); + } //now store the data @@ -243,9 +340,9 @@ int DialogDevices::validateAndSave() deviceSettings->setValue("MasterDelay", ui->tableDevices->item(dev_nr,6)->text()); deviceSettings->setValue("SlaveDelay", ui->tableDevices->item(dev_nr,7)->text()); deviceSettings->setValue("Threshold", ui->tableDevices->item(dev_nr,8)->text()); - - -} + deviceSettings->setValue("Size", ui->tableDevices->item(dev_nr,9)->text()); + deviceSettings->setValue("CalibFile", ui->tableDevices->item(dev_nr, 10)->text()); + } return 1; } @@ -256,3 +353,23 @@ void DialogDevices::on_spinNrDevices_valueChanged(int arg1) if (initialized) 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 f79fd35..f90491a 100644 --- a/hit2023v2_RMS/dialogdevices.h +++ b/hit2023v2_RMS/dialogdevices.h @@ -1,9 +1,10 @@ -#ifndef DIALOGDEVICES_H + #ifndef DIALOGDEVICES_H #define DIALOGDEVICES_H #include #include #include +#include // Include QVector for calibration data storage namespace Ui { class DialogDevices; @@ -18,19 +19,23 @@ public: ~DialogDevices(); QSettings* deviceSettings = NULL; + QVector> getAllCalibrationFactors() const; // Declaration of the method public slots: void showEvent(QShowEvent *event); void accept(); protected: int validateAndSave(); + void applyCalibrationDataToDevice(int dev_nr, const QVector& data); // Function to apply calibration data to a device void importSettings(); int last_nr_devices = -1; int initialized = 0; + QVector> calibrationData; // 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 private: Ui::DialogDevices *ui; + }; #endif // DIALOGDEVICES_H diff --git a/hit2023v2_RMS/dialogdevices.ui b/hit2023v2_RMS/dialogdevices.ui index 0b30468..0677de0 100644 --- a/hit2023v2_RMS/dialogdevices.ui +++ b/hit2023v2_RMS/dialogdevices.ui @@ -6,39 +6,51 @@ 0 0 - 573 + 1085 233 + + + 0 + 0 + + Devices + + true + - 210 + 720 190 341 32 - Qt::Horizontal + Qt::Orientation::Horizontal - QDialogButtonBox::Cancel|QDialogButtonBox::Ok + QDialogButtonBox::StandardButton::Cancel|QDialogButtonBox::StandardButton::Ok - 20 + 10 10 - 531 + 1061 171 + + QLayout::SizeConstraint::SetNoConstraint + @@ -58,7 +70,11 @@ - + + + true + + diff --git a/hit2023v2_RMS/eventbuilder.cpp b/hit2023v2_RMS/eventbuilder.cpp index d819224..85f901b 100644 --- a/hit2023v2_RMS/eventbuilder.cpp +++ b/hit2023v2_RMS/eventbuilder.cpp @@ -195,8 +195,9 @@ void EventBuilder::logDataToFile() * - number of boards: N = 1 x unsigned short * - number of channels per each board Cn: N x unsigned short * - N times the following sequence: - * - SyncFrame S = 1 x SyncFrame (== 16 bytes) + * - SyncFrame S = 1 x SyncFrame (== 16 bytes 4 unsigned shorts and 2 int) * - Data D = Cn x unsigned short + * - RMSframe R = 1 x RMSframe(== 8 unsigned shorts == 16 bytes) */ logFile.write((const char*)&totalBoards, sizeof(unsigned short)); diff --git a/hit2023v2_RMS/main.cpp b/hit2023v2_RMS/main.cpp index 66c046b..f6ae59f 100644 --- a/hit2023v2_RMS/main.cpp +++ b/hit2023v2_RMS/main.cpp @@ -14,7 +14,7 @@ int main(int argc, char *argv[]) QFile styleFile(stylesheetPath); if (styleFile.open(QFile::ReadOnly | QFile::Text)) { QString style = QTextStream(&styleFile).readAll(); - qApp->setStyleSheet(style); + //qApp->setStyleSheet(style); styleFile.close(); } else { qWarning("Failed to open stylesheet file: %s", qPrintable(stylesheetPath)); diff --git a/hit2023v2_RMS/mainwindow.cpp b/hit2023v2_RMS/mainwindow.cpp index 50fc109..5719682 100644 --- a/hit2023v2_RMS/mainwindow.cpp +++ b/hit2023v2_RMS/mainwindow.cpp @@ -117,6 +117,10 @@ void MainWindow::setupHardware() int period_v2 = deviceSettings->value("Period_v2").toInt(); int tint_v2 = deviceSettings->value("Tint_v2").toInt(); int gain_v2 = deviceSettings->value("Gain_v2").toInt(); + // Retrieve device-specific settings from DialogDevices + DialogDevices dlg; + dlg.deviceSettings = deviceSettings; + QVector> allCalibrationFactors = dlg.getAllCalibrationFactors(); for (int dev_nr = 0; dev_nr < nr_devices; dev_nr++) { @@ -126,7 +130,7 @@ void MainWindow::setupHardware() 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++) + 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(); @@ -134,7 +138,19 @@ void MainWindow::setupHardware() 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 + } + } +*/ switch (dc.hardware_ver) { case 1: @@ -155,6 +171,34 @@ void MainWindow::setupHardware() theHW->configureDevice(dev_nr, dc); //configure the device and an entry in base address table in the event builder } + + dc.threshold = deviceSettings->value("Threshold").toInt(); + dc.clustersize = deviceSettings->value("ClusterSize").toInt(); +/* + // 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; + } + } +*/ + //theDisplay.setup(&theHW); } diff --git a/hit2023v2_RMS/release/hit2023v2.exe b/hit2023v2_RMS/release/hit2023v2.exe index 780a7fe..58b5d0d 100644 Binary files a/hit2023v2_RMS/release/hit2023v2.exe and b/hit2023v2_RMS/release/hit2023v2.exe differ