From 99485a0d094da472375c918ae8c9407ca3701a3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aar=C3=B3n=20Rodr=C3=ADguez?= Date: Sat, 28 Nov 2026 01:20:10 -0600 Subject: [PATCH 1/2] Wizard: list detected serial ports in device configuration Replace the serial port text field with an editable combo box listing the ports reported by QSerialPortInfo, labelled with their description (e.g. "COM5 - USB-SERIAL CH340"), plus a Refresh button to rescan. USB serial adapters are listed first and pre-selected when the configured port is missing (e.g. the device was re-plugged and got a new number) or is just the built-in default. A configured port that is present stays selected, and a port name can still be typed manually. The wizard field still stores only the port name. Also guard QTEST_DISABLE_KEYPAD_NAVIGATION in the test runner, since it was removed in Qt 6.12. Co-Authored-By: Claude Opus 5.5 --- Software/src/wizard/ConfigureDevicePage.cpp | 98 ++++++++++++++++++++- Software/src/wizard/ConfigureDevicePage.hpp | 13 +++ Software/src/wizard/ConfigureDevicePage.ui | 32 ++++++- Software/tests/TestsMain.cpp | 2 + 4 files changed, 141 insertions(+), 4 deletions(-) diff --git a/Software/src/wizard/ConfigureDevicePage.cpp b/Software/src/wizard/ConfigureDevicePage.cpp index e92c575e0..7bc7764e2 100644 --- a/Software/src/wizard/ConfigureDevicePage.cpp +++ b/Software/src/wizard/ConfigureDevicePage.cpp @@ -25,6 +25,9 @@ */ #include +#include +#include +#include #include "ConfigureDevicePage.hpp" #include "ui_ConfigureDevicePage.h" @@ -42,6 +45,96 @@ ConfigureDevicePage::ConfigureDevicePage(bool isInitFromSettings, TransientSetti ui(new Ui::ConfigureDevicePage) { ui->setupUi(this); + + connect(ui->tbRefreshSerialPorts, &QToolButton::clicked, this, &ConfigureDevicePage::refreshSerialPorts); + connect(ui->cbSerialPortName, &QComboBox::currentTextChanged, this, &ConfigureDevicePage::serialPortNameChanged); +} + +QString ConfigureDevicePage::serialPortName() const +{ + const QComboBox *cb = ui->cbSerialPortName; + const QString text = cb->currentText().trimmed(); + // A detected port is shown as "COM5 - USB-SERIAL CH340"; its port name is in the item data. + // Anything else was typed by hand and is used as-is. + const int idx = cb->findText(text); + if (idx >= 0) + return cb->itemData(idx).toString(); + return text; +} + +void ConfigureDevicePage::setSerialPortName(const QString &portName) +{ + QComboBox *cb = ui->cbSerialPortName; + const int idx = cb->findData(portName); + if (idx >= 0) { + cb->setCurrentIndex(idx); + } else { + cb->setCurrentIndex(-1); + cb->setEditText(portName); + } +} + +void ConfigureDevicePage::refreshSerialPorts() +{ + populateSerialPorts(serialPortName(), false); +} + +void ConfigureDevicePage::populateSerialPorts(const QString &preferredPort, const bool preferredIsDefault) +{ + QList ports = QSerialPortInfo::availablePorts(); + + // USB serial adapters (Arduino, CH340, FTDI, CP210x...) first, then by port name in natural order (COM2 < COM10) + QCollator collator; + collator.setNumericMode(true); + collator.setCaseSensitivity(Qt::CaseInsensitive); + std::sort(ports.begin(), ports.end(), [&collator](const QSerialPortInfo &a, const QSerialPortInfo &b) { + if (a.hasVendorIdentifier() != b.hasVendorIdentifier()) + return a.hasVendorIdentifier(); + return collator.compare(a.portName(), b.portName()) < 0; + }); + + QComboBox *cb = ui->cbSerialPortName; + const QSignalBlocker blocker(cb); + cb->clear(); + + int preferredIdx = -1; + int firstUsbIdx = -1; + for (const QSerialPortInfo &port : std::as_const(ports)) { + const QString description = port.description().isEmpty() ? port.manufacturer() : port.description(); + const QString label = description.isEmpty() ? port.portName() : port.portName() + QStringLiteral(" - ") + description; + + QStringList tooltip; + tooltip << port.systemLocation(); + if (!port.manufacturer().isEmpty()) + tooltip << port.manufacturer(); + if (port.hasVendorIdentifier() && port.hasProductIdentifier()) + tooltip << QStringLiteral("VID:PID %1:%2") + .arg(port.vendorIdentifier(), 4, 16, QLatin1Char('0')) + .arg(port.productIdentifier(), 4, 16, QLatin1Char('0')).toUpper(); + + const int idx = cb->count(); + cb->addItem(label, port.portName()); + cb->setItemData(idx, tooltip.join(QLatin1Char('\n')), Qt::ToolTipRole); + + if (preferredIdx < 0 && port.portName().compare(preferredPort, Qt::CaseInsensitive) == 0) + preferredIdx = idx; + if (firstUsbIdx < 0 && port.hasVendorIdentifier()) + firstUsbIdx = idx; + } + + // Keep the configured port if it is connected, unless it is just the built-in default + // and a USB serial adapter is present. If the configured port is gone (e.g. the device + // was re-plugged and got a new number), pick the first USB serial adapter instead. + if (preferredIdx >= 0 && !(preferredIsDefault && firstUsbIdx >= 0)) + cb->setCurrentIndex(preferredIdx); + else if (firstUsbIdx >= 0) + cb->setCurrentIndex(firstUsbIdx); + else if (!preferredPort.isEmpty()) + setSerialPortName(preferredPort); + else if (cb->count() > 0) + cb->setCurrentIndex(0); + + emit serialPortNameChanged(); } void ConfigureDevicePage::initializePage() @@ -67,12 +160,11 @@ void ConfigureDevicePage::initializePage() if (currentBaudRate > 0) ui->cbBaudRate->setCurrentText(QString::number(currentBaudRate)); - if (!currentSerialPort.isEmpty()) - ui->leSerialPortName->setText(currentSerialPort); + populateSerialPorts(currentSerialPort, currentSerialPort == QStringLiteral(SERIAL_PORT_DEFAULT)); if (!currentColorSequence.isEmpty()) ui->cbColorFormat->setCurrentText(currentColorSequence); - registerField(QStringLiteral("serialPort"), ui->leSerialPortName); + registerField(QStringLiteral("serialPort"), this, "serialPortName", SIGNAL(serialPortNameChanged())); registerField(QStringLiteral("baudRate"), ui->cbBaudRate, "currentText"); registerField(QStringLiteral("colorFormat"), ui->cbColorFormat, "currentText"); diff --git a/Software/src/wizard/ConfigureDevicePage.hpp b/Software/src/wizard/ConfigureDevicePage.hpp index 66b2ee1c4..3683bd442 100644 --- a/Software/src/wizard/ConfigureDevicePage.hpp +++ b/Software/src/wizard/ConfigureDevicePage.hpp @@ -37,17 +37,30 @@ class ConfigureDevicePage; class ConfigureDevicePage : public QWizardPage, SettingsAwareTrait { Q_OBJECT + Q_PROPERTY(QString serialPortName READ serialPortName WRITE setSerialPortName NOTIFY serialPortNameChanged) public: explicit ConfigureDevicePage(bool isInitFromSettings, TransientSettings *ts, QWidget *parent = 0); ~ConfigureDevicePage(); int nextId() const; + + QString serialPortName() const; + void setSerialPortName(const QString &portName); + +signals: + void serialPortNameChanged(); + protected: void initializePage(); void cleanupPage(); bool validatePage(); +private slots: + void refreshSerialPorts(); + private: + void populateSerialPorts(const QString &preferredPort, bool preferredIsDefault); + Ui::ConfigureDevicePage *ui; }; diff --git a/Software/src/wizard/ConfigureDevicePage.ui b/Software/src/wizard/ConfigureDevicePage.ui index 344c760ac..60be5534b 100644 --- a/Software/src/wizard/ConfigureDevicePage.ui +++ b/Software/src/wizard/ConfigureDevicePage.ui @@ -71,7 +71,37 @@ - + + + + + + 0 + 0 + + + + true + + + QComboBox::NoInsert + + + Detected serial ports. You can also type a port name manually. + + + + + + + Rescan serial ports + + + Refresh + + + + diff --git a/Software/tests/TestsMain.cpp b/Software/tests/TestsMain.cpp index bfedb6bb2..65abfc4af 100644 --- a/Software/tests/TestsMain.cpp +++ b/Software/tests/TestsMain.cpp @@ -20,7 +20,9 @@ unsigned g_debugLevel = Debug::LowLevel; int main(int argc, char *argv[]) { +#ifdef QTEST_DISABLE_KEYPAD_NAVIGATION // removed in Qt 6.12 QTEST_DISABLE_KEYPAD_NAVIGATION +#endif QApplication app(argc, argv); QList tests; From b0e5844192788d3385a1db3f7c3734bea0a6ddea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aar=C3=B3n=20Rodr=C3=ADguez?= Date: Sat, 28 Nov 2026 01:48:39 -0600 Subject: [PATCH 2/2] Add serial port and baud rate settings to the Device tab Adalight/Ardulight users previously had to rerun the whole wizard (or edit main.conf and restart) whenever the device's COM port changed. Add a "Serial device" group to the Device tab, shown for serial devices, with the detected-port combo box, a Refresh button and the baud rate. Changes apply live: the Settings change signals now reach LedDeviceManager, which pushes the new port/baud rate to the cached serial device instances (they only got them at construction) and reconnects. Back-to-back changes are coalesced into one reconnect; while sleeping, the new settings are used on wake-up. The port listing is moved into SerialPortComboHelper and shared with the wizard's device configuration page. Co-Authored-By: Claude Opus 5.5 --- Software/src/LedDeviceAdalight.cpp | 7 ++ Software/src/LedDeviceAdalight.hpp | 2 + Software/src/LedDeviceArdulight.cpp | 7 ++ Software/src/LedDeviceArdulight.hpp | 2 + Software/src/LedDeviceManager.cpp | 34 ++++++ Software/src/LedDeviceManager.hpp | 5 + Software/src/LightpackApplication.cpp | 4 + Software/src/SerialPortComboHelper.cpp | 110 ++++++++++++++++++++ Software/src/SerialPortComboHelper.hpp | 43 ++++++++ Software/src/SettingsWindow.cpp | 82 +++++++++++++++ Software/src/SettingsWindow.hpp | 4 + Software/src/SettingsWindow.ui | 59 +++++++++++ Software/src/src.pro | 2 + Software/src/wizard/ConfigureDevicePage.cpp | 76 +------------- 14 files changed, 365 insertions(+), 72 deletions(-) create mode 100644 Software/src/SerialPortComboHelper.cpp create mode 100644 Software/src/SerialPortComboHelper.hpp diff --git a/Software/src/LedDeviceAdalight.cpp b/Software/src/LedDeviceAdalight.cpp index ea3fa17c4..0fb53a813 100644 --- a/Software/src/LedDeviceAdalight.cpp +++ b/Software/src/LedDeviceAdalight.cpp @@ -202,6 +202,13 @@ void LedDeviceAdalight::updateDeviceSettings() AbstractLedDevice::updateDeviceSettings(); } +void LedDeviceAdalight::setSerialPortSettings(const QString &portName, int baudRate) +{ + DEBUG_LOW_LEVEL << Q_FUNC_INFO << portName << baudRate; + m_portName = portName; + m_baudRate = baudRate; +} + void LedDeviceAdalight::open() { DEBUG_LOW_LEVEL << Q_FUNC_INFO << sender(); diff --git a/Software/src/LedDeviceAdalight.hpp b/Software/src/LedDeviceAdalight.hpp index 6ecb6d723..0faf1b443 100644 --- a/Software/src/LedDeviceAdalight.hpp +++ b/Software/src/LedDeviceAdalight.hpp @@ -42,6 +42,8 @@ class LedDeviceAdalight : public AbstractLedDevice public slots: void open(); + // Takes effect on the next open() + void setSerialPortSettings(const QString &portName, int baudRate); void close(); void setColors(const QList & /*colors*/); void switchOffLeds(); diff --git a/Software/src/LedDeviceArdulight.cpp b/Software/src/LedDeviceArdulight.cpp index bf480e020..c00f9b48f 100644 --- a/Software/src/LedDeviceArdulight.cpp +++ b/Software/src/LedDeviceArdulight.cpp @@ -207,6 +207,13 @@ void LedDeviceArdulight::updateDeviceSettings() AbstractLedDevice::updateDeviceSettings(); } +void LedDeviceArdulight::setSerialPortSettings(const QString &portName, int baudRate) +{ + DEBUG_LOW_LEVEL << Q_FUNC_INFO << portName << baudRate; + m_portName = portName; + m_baudRate = baudRate; +} + void LedDeviceArdulight::open() { DEBUG_LOW_LEVEL << Q_FUNC_INFO; diff --git a/Software/src/LedDeviceArdulight.hpp b/Software/src/LedDeviceArdulight.hpp index a7f0e5a8a..e8f5997f7 100644 --- a/Software/src/LedDeviceArdulight.hpp +++ b/Software/src/LedDeviceArdulight.hpp @@ -42,6 +42,8 @@ class LedDeviceArdulight : public AbstractLedDevice public slots: void open(); + // Takes effect on the next open() + void setSerialPortSettings(const QString &portName, int baudRate); void close(); void setColors(const QList & /*colors*/); void switchOffLeds(); diff --git a/Software/src/LedDeviceManager.cpp b/Software/src/LedDeviceManager.cpp index ef1f922c4..734ed8f4d 100644 --- a/Software/src/LedDeviceManager.cpp +++ b/Software/src/LedDeviceManager.cpp @@ -132,6 +132,40 @@ void LedDeviceManager::recreateLedDevice() initLedDevice(); } +void LedDeviceManager::updateSerialPortSettings() +{ + DEBUG_LOW_LEVEL << Q_FUNC_INFO; + + if (m_isSerialPortUpdatePending) + return; + m_isSerialPortUpdatePending = true; + QMetaObject::invokeMethod(this, "applySerialPortSettings", Qt::QueuedConnection); +} + +void LedDeviceManager::applySerialPortSettings() +{ + DEBUG_LOW_LEVEL << Q_FUNC_INFO; + + m_isSerialPortUpdatePending = false; + + // Devices are cached per type and get their port when created, so update the + // cached instances. Queued to the device thread ahead of the close/open below. + if (AbstractLedDevice *adalight = m_ledDevices[SupportedDevices::DeviceTypeAdalight]) + QMetaObject::invokeMethod(adalight, "setSerialPortSettings", Qt::QueuedConnection, + Q_ARG(QString, Settings::getAdalightSerialPortName()), + Q_ARG(int, Settings::getAdalightSerialPortBaudRate())); + if (AbstractLedDevice *ardulight = m_ledDevices[SupportedDevices::DeviceTypeArdulight]) + QMetaObject::invokeMethod(ardulight, "setSerialPortSettings", Qt::QueuedConnection, + Q_ARG(QString, Settings::getArdulightSerialPortName()), + Q_ARG(int, Settings::getArdulightSerialPortBaudRate())); + + // While sleeping the device is reopened with the new settings on wake-up + if (Settings::isConnectedDeviceUsesSerialPort() && !m_isSleeping) { + m_failedCreationAttempts = 0; + recreateLedDevice(); + } +} + void LedDeviceManager::switchOnLeds() { DEBUG_LOW_LEVEL << Q_FUNC_INFO; diff --git a/Software/src/LedDeviceManager.hpp b/Software/src/LedDeviceManager.hpp index fafcb01fd..9847c7dd8 100644 --- a/Software/src/LedDeviceManager.hpp +++ b/Software/src/LedDeviceManager.hpp @@ -79,6 +79,9 @@ public slots: void init(); void recreateLedDevice(); + // Pushes the configured serial port / baud rate to the serial devices and reconnects. + // Several calls in a row are coalesced into one reconnect. + void updateSerialPortSettings(); // This slots are protected from the overflow of queries void setColors(const QList & colors); @@ -101,6 +104,7 @@ public slots: void onSessionChange(SystemSession::Status status); private slots: + void applySerialPortSettings(); void ledDeviceCommandCompleted(bool ok); void ledDeviceCommandTimedOut(); void ledDeviceOpenTimedOut(); @@ -140,6 +144,7 @@ private slots: QHash m_pendingOpens; int m_consecutiveOpenTimeouts{ 0 }; bool m_isSleeping{ false }; + bool m_isSerialPortUpdatePending{ false }; bool m_isLastCommandCompleted{ true }; bool m_isColorsSaved{ false }; bool m_firmwareVersionRequested{ false }; diff --git a/Software/src/LightpackApplication.cpp b/Software/src/LightpackApplication.cpp index a76bc6a83..00c1a6dbc 100644 --- a/Software/src/LightpackApplication.cpp +++ b/Software/src/LightpackApplication.cpp @@ -735,6 +735,10 @@ void LightpackApplication::startLedDeviceManager() connect(settings(), &Settings::ledCoefBlueChanged, m_ledDeviceManager, &LedDeviceManager::updateWBAdjustments, Qt::QueuedConnection); connect(settings(), &Settings::ledCoefRedChanged, m_ledDeviceManager, &LedDeviceManager::updateWBAdjustments, Qt::QueuedConnection); connect(settings(), &Settings::ledCoefGreenChanged, m_ledDeviceManager, &LedDeviceManager::updateWBAdjustments, Qt::QueuedConnection); + connect(settings(), &Settings::adalightSerialPortNameChanged, m_ledDeviceManager, &LedDeviceManager::updateSerialPortSettings, Qt::QueuedConnection); + connect(settings(), &Settings::adalightSerialPortBaudRateChanged, m_ledDeviceManager, &LedDeviceManager::updateSerialPortSettings, Qt::QueuedConnection); + connect(settings(), &Settings::ardulightSerialPortNameChanged, m_ledDeviceManager, &LedDeviceManager::updateSerialPortSettings, Qt::QueuedConnection); + connect(settings(), &Settings::ardulightSerialPortBaudRateChanged, m_ledDeviceManager, &LedDeviceManager::updateSerialPortSettings, Qt::QueuedConnection); if (!m_noGui) diff --git a/Software/src/SerialPortComboHelper.cpp b/Software/src/SerialPortComboHelper.cpp new file mode 100644 index 000000000..8e94d9087 --- /dev/null +++ b/Software/src/SerialPortComboHelper.cpp @@ -0,0 +1,110 @@ +/* + * SerialPortComboHelper.cpp + * + * Project: Prismatik + * + * Prismatik is a free, open-source software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Prismatik and Lightpack files is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include "SerialPortComboHelper.hpp" + +#include +#include +#include +#include +#include +#include + +namespace SerialPortComboHelper +{ + +void populate(QComboBox *comboBox, const QString &preferredPort, const bool preferredIsDefault) +{ + QList ports = QSerialPortInfo::availablePorts(); + + // USB serial adapters (Arduino, CH340, FTDI, CP210x...) first, then by port name in natural order (COM2 < COM10) + QCollator collator; + collator.setNumericMode(true); + collator.setCaseSensitivity(Qt::CaseInsensitive); + std::sort(ports.begin(), ports.end(), [&collator](const QSerialPortInfo &a, const QSerialPortInfo &b) { + if (a.hasVendorIdentifier() != b.hasVendorIdentifier()) + return a.hasVendorIdentifier(); + return collator.compare(a.portName(), b.portName()) < 0; + }); + + const QSignalBlocker blocker(comboBox); + comboBox->clear(); + + int preferredIdx = -1; + int firstUsbIdx = -1; + for (const QSerialPortInfo &port : std::as_const(ports)) { + const QString description = port.description().isEmpty() ? port.manufacturer() : port.description(); + const QString label = description.isEmpty() ? port.portName() : port.portName() + QStringLiteral(" - ") + description; + + QStringList tooltip; + tooltip << port.systemLocation(); + if (!port.manufacturer().isEmpty()) + tooltip << port.manufacturer(); + if (port.hasVendorIdentifier() && port.hasProductIdentifier()) + tooltip << QStringLiteral("VID:PID %1:%2") + .arg(port.vendorIdentifier(), 4, 16, QLatin1Char('0')) + .arg(port.productIdentifier(), 4, 16, QLatin1Char('0')).toUpper(); + + const int idx = comboBox->count(); + comboBox->addItem(label, port.portName()); + comboBox->setItemData(idx, tooltip.join(QLatin1Char('\n')), Qt::ToolTipRole); + + if (preferredIdx < 0 && port.portName().compare(preferredPort, Qt::CaseInsensitive) == 0) + preferredIdx = idx; + if (firstUsbIdx < 0 && port.hasVendorIdentifier()) + firstUsbIdx = idx; + } + + // Keep the configured port if it is connected, unless it is just the built-in default + // and a USB serial adapter is present. If the configured port is gone (e.g. the device + // was re-plugged and got a new number), pick the first USB serial adapter instead. + if (preferredIdx >= 0 && !(preferredIsDefault && firstUsbIdx >= 0)) + comboBox->setCurrentIndex(preferredIdx); + else if (firstUsbIdx >= 0) + comboBox->setCurrentIndex(firstUsbIdx); + else if (!preferredPort.isEmpty()) + selectPortName(comboBox, preferredPort); + else if (comboBox->count() > 0) + comboBox->setCurrentIndex(0); +} + +QString selectedPortName(const QComboBox *comboBox) +{ + const QString text = comboBox->currentText().trimmed(); + // A detected port is shown as "COM5 - USB-SERIAL CH340"; its port name is in the item data. + // Anything else was typed by hand and is used as-is. + const int idx = comboBox->findText(text); + if (idx >= 0) + return comboBox->itemData(idx).toString(); + return text; +} + +void selectPortName(QComboBox *comboBox, const QString &portName) +{ + const int idx = comboBox->findData(portName); + if (idx >= 0) { + comboBox->setCurrentIndex(idx); + } else { + comboBox->setCurrentIndex(-1); + comboBox->setEditText(portName); + } +} + +} // namespace SerialPortComboHelper diff --git a/Software/src/SerialPortComboHelper.hpp b/Software/src/SerialPortComboHelper.hpp new file mode 100644 index 000000000..e650a8caa --- /dev/null +++ b/Software/src/SerialPortComboHelper.hpp @@ -0,0 +1,43 @@ +/* + * SerialPortComboHelper.hpp + * + * Project: Prismatik + * + * Prismatik is a free, open-source software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License as published + * by the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Prismatik and Lightpack files is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#pragma once + +#include + +class QComboBox; + +// Fills an (editable) QComboBox with the detected serial ports, labelled with +// their description (e.g. "COM5 - USB-SERIAL CH340"), keeping the bare port +// name in the item data. A port name typed by hand is also accepted. +namespace SerialPortComboHelper +{ + // Rescans the ports and selects one: the preferred port if it is connected + // (unless it is only the built-in default and a USB serial adapter is present), + // otherwise the first USB serial adapter, otherwise the preferred port as text. + // Emits no signals from the combo box. + void populate(QComboBox *comboBox, const QString &preferredPort, bool preferredIsDefault); + + // The port name for the current selection or typed text. + QString selectedPortName(const QComboBox *comboBox); + + // Selects the item for portName, or shows it as typed text if not detected. + void selectPortName(QComboBox *comboBox, const QString &portName); +} diff --git a/Software/src/SettingsWindow.cpp b/Software/src/SettingsWindow.cpp index 84473eab7..d6f95df74 100644 --- a/Software/src/SettingsWindow.cpp +++ b/Software/src/SettingsWindow.cpp @@ -33,6 +33,7 @@ #include "ui_SettingsWindow.h" #include "Settings.hpp" +#include "SerialPortComboHelper.hpp" #include "ColorButton.hpp" #include "LedDeviceManager.hpp" #include "enums.hpp" @@ -276,6 +277,10 @@ void SettingsWindow::connectSignalsSlots() connect(ui->checkBox_SendDataOnlyIfColorsChanges, &QCheckBox::toggled, this, &SettingsWindow::onDeviceSendDataOnlyIfColorsChanged_toggled); connect(ui->pbRunConfigurationWizard, &QPushButton::clicked, this, &SettingsWindow::onRunConfigurationWizard_clicked); + connect(ui->toolButton_RefreshSerialPorts, &QToolButton::clicked, this, &SettingsWindow::onRefreshSerialPorts_clicked); + connect(ui->comboBox_SerialPort, qOverload(&QComboBox::activated), this, &SettingsWindow::onSerialPort_changed); + connect(ui->comboBox_SerialPort->lineEdit(), &QLineEdit::editingFinished, this, &SettingsWindow::onSerialPort_changed); + connect(ui->comboBox_SerialBaudRate, qOverload(&QComboBox::activated), this, &SettingsWindow::onSerialBaudRate_changed); // Open Settings file connect(ui->commandLinkButton_OpenSettings, &QCommandLinkButton::clicked, this, &SettingsWindow::openCurrentProfile); @@ -463,7 +468,84 @@ void SettingsWindow::updateDeviceTabWidgetsVisibility() break; } setDeviceTabWidgetsVisibility(DeviceTab::Lightpack); + updateSerialPortWidgets(); +} +void SettingsWindow::updateSerialPortWidgets() +{ + DEBUG_LOW_LEVEL << Q_FUNC_INFO; + + const SupportedDevices::DeviceType connectedDevice = Settings::getConnectedDevice(); + const bool usesSerialPort = Settings::isConnectedDeviceUsesSerialPort(); + ui->groupBox_SerialPort->setVisible(usesSerialPort); + if (!usesSerialPort) + return; + + const bool isAdalight = connectedDevice == SupportedDevices::DeviceTypeAdalight; + const QString portName = isAdalight ? Settings::getAdalightSerialPortName() : Settings::getArdulightSerialPortName(); + const QString baudRate = QString::number(isAdalight ? Settings::getAdalightSerialPortBaudRate() : Settings::getArdulightSerialPortBaudRate()); + + // Show what is configured; don't auto-pick another port here, the user decides + SerialPortComboHelper::populate(ui->comboBox_SerialPort, portName, false); + if (SerialPortComboHelper::selectedPortName(ui->comboBox_SerialPort) != portName) + SerialPortComboHelper::selectPortName(ui->comboBox_SerialPort, portName); + + const QSignalBlocker blocker(ui->comboBox_SerialBaudRate); + ui->comboBox_SerialBaudRate->clear(); + ui->comboBox_SerialBaudRate->addItems(Settings::getSupportedSerialPortBaudRates()); + if (ui->comboBox_SerialBaudRate->findText(baudRate) < 0) + ui->comboBox_SerialBaudRate->addItem(baudRate); + ui->comboBox_SerialBaudRate->setCurrentText(baudRate); +} + +void SettingsWindow::onRefreshSerialPorts_clicked() +{ + DEBUG_LOW_LEVEL << Q_FUNC_INFO; + + // Keep the current choice if it is still there; otherwise this may pick a USB adapter, + // which is applied right away like a manual selection + SerialPortComboHelper::populate(ui->comboBox_SerialPort, SerialPortComboHelper::selectedPortName(ui->comboBox_SerialPort), false); + onSerialPort_changed(); +} + +void SettingsWindow::onSerialPort_changed() +{ + const QString portName = SerialPortComboHelper::selectedPortName(ui->comboBox_SerialPort); + if (portName.isEmpty()) + return; + + switch (Settings::getConnectedDevice()) { + case SupportedDevices::DeviceTypeAdalight: + if (portName != Settings::getAdalightSerialPortName()) + Settings::setAdalightSerialPortName(portName); + break; + case SupportedDevices::DeviceTypeArdulight: + if (portName != Settings::getArdulightSerialPortName()) + Settings::setArdulightSerialPortName(portName); + break; + default: + break; + } +} + +void SettingsWindow::onSerialBaudRate_changed(int index) +{ + const QString baudRate = ui->comboBox_SerialBaudRate->itemText(index); + if (baudRate.toInt() <= 0) + return; + + switch (Settings::getConnectedDevice()) { + case SupportedDevices::DeviceTypeAdalight: + if (baudRate.toInt() != Settings::getAdalightSerialPortBaudRate()) + Settings::setAdalightSerialPortBaudRate(baudRate); + break; + case SupportedDevices::DeviceTypeArdulight: + if (baudRate.toInt() != Settings::getArdulightSerialPortBaudRate()) + Settings::setArdulightSerialPortBaudRate(baudRate); + break; + default: + break; + } } void SettingsWindow::setDeviceTabWidgetsVisibility(DeviceTab::Options options) diff --git a/Software/src/SettingsWindow.hpp b/Software/src/SettingsWindow.hpp index a90236695..b8b5840b7 100644 --- a/Software/src/SettingsWindow.hpp +++ b/Software/src/SettingsWindow.hpp @@ -239,6 +239,9 @@ private slots: void onKeepLightsAfterScreenOff_Toggled(bool isEnabled); void onRunConfigurationWizard_clicked(); + void onRefreshSerialPorts_clicked(); + void onSerialPort_changed(); + void onSerialBaudRate_changed(int index); void onCheckBox_checkForUpdates_Toggled(bool isEnabled); void onCheckBox_installUpdates_Toggled(bool isEnabled); @@ -247,6 +250,7 @@ private slots: private: void updateDeviceTabWidgetsVisibility(); + void updateSerialPortWidgets(); void setDeviceTabWidgetsVisibility(DeviceTab::Options options); void syncLedDeviceWithSettingsWindow(); int getLigtpackFirmwareVersionMajor(); diff --git a/Software/src/SettingsWindow.ui b/Software/src/SettingsWindow.ui index 49cd9e6ec..1f5d2dc64 100644 --- a/Software/src/SettingsWindow.ui +++ b/Software/src/SettingsWindow.ui @@ -1494,6 +1494,65 @@ Internally emulates the effects of f.lux, redshift, Night Light, Night Shift...< + + + + Serial device + + + + + + Serial port: + + + + + + + + 0 + 0 + + + + Detected serial ports. You can also type a port name manually. Changes apply immediately. + + + true + + + QComboBox::NoInsert + + + + + + + Rescan serial ports + + + Refresh + + + + + + + Baud rate: + + + + + + + Must match the baud rate of your device's firmware. + + + + + + diff --git a/Software/src/src.pro b/Software/src/src.pro index 6f323aab0..a0b1b2e52 100644 --- a/Software/src/src.pro +++ b/Software/src/src.pro @@ -265,6 +265,7 @@ SOURCES += \ LightpackApplication.cpp main.cpp SettingsWindow.cpp Settings.cpp \ GrabWidget.cpp GrabConfigWidget.cpp \ LogWriter.cpp \ + SerialPortComboHelper.cpp \ LedDeviceLightpack.cpp \ LedDeviceAdalight.cpp \ LedDeviceArdulight.cpp \ @@ -309,6 +310,7 @@ HEADERS += \ SettingsWindow.hpp \ Settings.hpp \ SettingsDefaults.hpp \ + SerialPortComboHelper.hpp \ version.h \ TimeEvaluations.hpp \ GrabManager.hpp \ diff --git a/Software/src/wizard/ConfigureDevicePage.cpp b/Software/src/wizard/ConfigureDevicePage.cpp index 7bc7764e2..cde13daed 100644 --- a/Software/src/wizard/ConfigureDevicePage.cpp +++ b/Software/src/wizard/ConfigureDevicePage.cpp @@ -25,13 +25,11 @@ */ #include -#include -#include -#include #include "ConfigureDevicePage.hpp" #include "ui_ConfigureDevicePage.h" #include "Settings.hpp" +#include "SerialPortComboHelper.hpp" #include "LedDeviceArdulight.hpp" #include "LedDeviceAdalight.hpp" #include "LedDeviceVirtual.hpp" @@ -52,26 +50,12 @@ ConfigureDevicePage::ConfigureDevicePage(bool isInitFromSettings, TransientSetti QString ConfigureDevicePage::serialPortName() const { - const QComboBox *cb = ui->cbSerialPortName; - const QString text = cb->currentText().trimmed(); - // A detected port is shown as "COM5 - USB-SERIAL CH340"; its port name is in the item data. - // Anything else was typed by hand and is used as-is. - const int idx = cb->findText(text); - if (idx >= 0) - return cb->itemData(idx).toString(); - return text; + return SerialPortComboHelper::selectedPortName(ui->cbSerialPortName); } void ConfigureDevicePage::setSerialPortName(const QString &portName) { - QComboBox *cb = ui->cbSerialPortName; - const int idx = cb->findData(portName); - if (idx >= 0) { - cb->setCurrentIndex(idx); - } else { - cb->setCurrentIndex(-1); - cb->setEditText(portName); - } + SerialPortComboHelper::selectPortName(ui->cbSerialPortName, portName); } void ConfigureDevicePage::refreshSerialPorts() @@ -81,59 +65,7 @@ void ConfigureDevicePage::refreshSerialPorts() void ConfigureDevicePage::populateSerialPorts(const QString &preferredPort, const bool preferredIsDefault) { - QList ports = QSerialPortInfo::availablePorts(); - - // USB serial adapters (Arduino, CH340, FTDI, CP210x...) first, then by port name in natural order (COM2 < COM10) - QCollator collator; - collator.setNumericMode(true); - collator.setCaseSensitivity(Qt::CaseInsensitive); - std::sort(ports.begin(), ports.end(), [&collator](const QSerialPortInfo &a, const QSerialPortInfo &b) { - if (a.hasVendorIdentifier() != b.hasVendorIdentifier()) - return a.hasVendorIdentifier(); - return collator.compare(a.portName(), b.portName()) < 0; - }); - - QComboBox *cb = ui->cbSerialPortName; - const QSignalBlocker blocker(cb); - cb->clear(); - - int preferredIdx = -1; - int firstUsbIdx = -1; - for (const QSerialPortInfo &port : std::as_const(ports)) { - const QString description = port.description().isEmpty() ? port.manufacturer() : port.description(); - const QString label = description.isEmpty() ? port.portName() : port.portName() + QStringLiteral(" - ") + description; - - QStringList tooltip; - tooltip << port.systemLocation(); - if (!port.manufacturer().isEmpty()) - tooltip << port.manufacturer(); - if (port.hasVendorIdentifier() && port.hasProductIdentifier()) - tooltip << QStringLiteral("VID:PID %1:%2") - .arg(port.vendorIdentifier(), 4, 16, QLatin1Char('0')) - .arg(port.productIdentifier(), 4, 16, QLatin1Char('0')).toUpper(); - - const int idx = cb->count(); - cb->addItem(label, port.portName()); - cb->setItemData(idx, tooltip.join(QLatin1Char('\n')), Qt::ToolTipRole); - - if (preferredIdx < 0 && port.portName().compare(preferredPort, Qt::CaseInsensitive) == 0) - preferredIdx = idx; - if (firstUsbIdx < 0 && port.hasVendorIdentifier()) - firstUsbIdx = idx; - } - - // Keep the configured port if it is connected, unless it is just the built-in default - // and a USB serial adapter is present. If the configured port is gone (e.g. the device - // was re-plugged and got a new number), pick the first USB serial adapter instead. - if (preferredIdx >= 0 && !(preferredIsDefault && firstUsbIdx >= 0)) - cb->setCurrentIndex(preferredIdx); - else if (firstUsbIdx >= 0) - cb->setCurrentIndex(firstUsbIdx); - else if (!preferredPort.isEmpty()) - setSerialPortName(preferredPort); - else if (cb->count() > 0) - cb->setCurrentIndex(0); - + SerialPortComboHelper::populate(ui->cbSerialPortName, preferredPort, preferredIsDefault); emit serialPortNameChanged(); }