Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Software/src/LedDeviceAdalight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions Software/src/LedDeviceAdalight.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<QRgb> & /*colors*/);
void switchOffLeds();
Expand Down
7 changes: 7 additions & 0 deletions Software/src/LedDeviceArdulight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions Software/src/LedDeviceArdulight.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<QRgb> & /*colors*/);
void switchOffLeds();
Expand Down
34 changes: 34 additions & 0 deletions Software/src/LedDeviceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions Software/src/LedDeviceManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<QRgb> & colors);
Expand All @@ -101,6 +104,7 @@ public slots:
void onSessionChange(SystemSession::Status status);

private slots:
void applySerialPortSettings();
void ledDeviceCommandCompleted(bool ok);
void ledDeviceCommandTimedOut();
void ledDeviceOpenTimedOut();
Expand Down Expand Up @@ -140,6 +144,7 @@ private slots:
QHash<AbstractLedDevice *, int> 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 };
Expand Down
4 changes: 4 additions & 0 deletions Software/src/LightpackApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
110 changes: 110 additions & 0 deletions Software/src/SerialPortComboHelper.cpp
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*
*/

#include "SerialPortComboHelper.hpp"

#include <QCollator>
#include <QComboBox>
#include <QSerialPortInfo>
#include <QSignalBlocker>
#include <QStringList>
#include <algorithm>

namespace SerialPortComboHelper
{

void populate(QComboBox *comboBox, const QString &preferredPort, const bool preferredIsDefault)
{
QList<QSerialPortInfo> ports = QSerialPortInfo::availablePorts();

Comment on lines +33 to +36

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider extracting an overload that accepts a list of lightweight port-info structs (instead of calling QSerialPortInfo::availablePorts() directly) and adding unit tests in Software/tests/ covering the sorting, fallback selection, and Windows (COMx) vs. Unix (/dev/tty*) path matching.

// 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;
Comment on lines +66 to +70

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Linux and macOS, QSerialPortInfo::portName() returns the bare device name without /dev/ (e.g. "ttyUSB0", "ttyACM0", "cu.usbserial-..."), whereas port.systemLocation() is "/dev/ttyUSB0". However, SERIAL_PORT_DEFAULT on Unix is "/dev/ttyUSB0" (SettingsDefaults.hpp) and existing main.conf files / manual entries typically store /dev/... paths.

Because populate() and selectPortName() (line 101) only store and compare port.portName(), any configured /dev/... path on Linux/macOS will fail to match (preferredIdx == -1), causing populate() to treat the configured port as disconnected and silently fall back to firstUsbIdx (which may be a different USB serial device).

Please match preferredPort / portName against both port.portName() and port.systemLocation(), and consider storing port.systemLocation() on Unix (or normalizing /dev/ prefixes consistently) so existing Unix configs keep matching.

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;
}
Comment on lines +88 to +97

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the combo box is editable and items use "COM5 - USB-SERIAL CH340" as their Qt::DisplayRole text, Qt populates the QLineEdit with the full "port - description" label. If a user edits the port name in place (e.g. changing 5 to 6 so the text becomes "COM6 - USB-SERIAL CH340"), comboBox->findText(text) returns -1 and selectedPortName() returns the entire "COM6 - USB-SERIAL CH340" string as the port name, which QSerialPort cannot open.

Either put only the bare port name into the QLineEdit when an item is selected (rendering descriptions in the popup via a delegate/model or separate hint) or otherwise avoid treating an edited "<port> - <description>" label as a literal port name.


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
43 changes: 43 additions & 0 deletions Software/src/SerialPortComboHelper.hpp
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*
*/

#pragma once

#include <QString>

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);
}
82 changes: 82 additions & 0 deletions Software/src/SettingsWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "ui_SettingsWindow.h"

#include "Settings.hpp"
#include "SerialPortComboHelper.hpp"
#include "ColorButton.hpp"
#include "LedDeviceManager.hpp"
#include "enums.hpp"
Expand Down Expand Up @@ -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<int>(&QComboBox::activated), this, &SettingsWindow::onSerialPort_changed);
connect(ui->comboBox_SerialPort->lineEdit(), &QLineEdit::editingFinished, this, &SettingsWindow::onSerialPort_changed);
connect(ui->comboBox_SerialBaudRate, qOverload<int>(&QComboBox::activated), this, &SettingsWindow::onSerialBaudRate_changed);

// Open Settings file
connect(ui->commandLinkButton_OpenSettings, &QCommandLinkButton::clicked, this, &SettingsWindow::openCurrentProfile);
Expand Down Expand Up @@ -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)
Expand Down
Loading