Serial port field is now drop-down list

This commit is contained in:
Dmitry K
2025-07-16 10:29:16 +04:00
committed by Adam Honse
parent 25d45fd9db
commit 46a79c6bb5
6 changed files with 158 additions and 26 deletions

View File

@@ -10,11 +10,35 @@
#include "DMXSettingsEntry.h"
#include "ui_DMXSettingsEntry.h"
#include "serial_port.h"
#include <QStandardItemModel>
DMXSettingsEntry::DMXSettingsEntry(QWidget *parent) :
BaseManualDeviceEntry(parent),
ui(new Ui::DMXSettingsEntry)
{
ui->setupUi(this);
std::vector<std::string> serialPorts = serial_port::getSerialPorts();
for(size_t i = 0; i < serialPorts.size(); ++i)
{
ui->PortComboBox->addItem(QString::fromStdString(serialPorts[i]));
}
if(serialPorts.empty())
{
/*---------------------------------------------------*\
| When no ports were found, add an unselectable entry |
| denoting this fact istead |
\*---------------------------------------------------*/
QStandardItemModel* comboBoxModel = qobject_cast<QStandardItemModel *>(ui->PortComboBox->model());
if(comboBoxModel != nullptr)
{
ui->PortComboBox->addItem(tr("No serial ports found"));
QStandardItem *item = comboBoxModel->item(0);
item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
}
}
ui->PortComboBox->clearEditText();
}
DMXSettingsEntry::~DMXSettingsEntry()
@@ -39,7 +63,7 @@ void DMXSettingsEntry::loadFromSettings(const json& data)
if(data.contains("port"))
{
ui->PortEdit->setText(QString::fromStdString(data["port"]));
ui->PortComboBox->setCurrentText(QString::fromStdString(data["port"]));
}
if(data.contains("red_channel"))
@@ -75,7 +99,7 @@ json DMXSettingsEntry::saveSettings()
| Required parameters |
\*-------------------------------------------------*/
result["name"] = ui->NameEdit->text().toStdString();
result["port"] = ui->PortEdit->text().toStdString();
result["port"] = ui->PortComboBox->currentText().toStdString();
result["red_channel"] = ui->RedEdit->text().toUInt();
result["green_channel"] = ui->GreenEdit->text().toUInt();
result["blue_channel"] = ui->BlueEdit->text().toUInt();

View File

@@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>531</width>
<height>199</height>
<height>206</height>
</rect>
</property>
<property name="sizePolicy">
@@ -94,7 +94,14 @@
</widget>
</item>
<item row="1" column="5">
<widget class="QLineEdit" name="PortEdit"/>
<widget class="QComboBox" name="PortComboBox">
<property name="editable">
<bool>true</bool>
</property>
<property name="insertPolicy">
<enum>QComboBox::NoInsert</enum>
</property>
</widget>
</item>
</layout>
</widget>

View File

@@ -10,6 +10,9 @@
#include "SerialSettingsEntry.h"
#include "ui_SerialSettingsEntry.h"
#include "serial_port.h"
#include <QStandardItemModel>
SerialSettingsEntry::SerialSettingsEntry(QWidget *parent) :
BaseManualDeviceEntry(parent),
ui(new Ui::SerialSettingsEntry)
@@ -20,6 +23,27 @@ SerialSettingsEntry::SerialSettingsEntry(QWidget *parent) :
ui->ProtocolComboBox->addItem("Adalight");
ui->ProtocolComboBox->addItem("TPM2");
ui->ProtocolComboBox->addItem("Basic I2C");
std::vector<std::string> serialPorts = serial_port::getSerialPorts();
for(size_t i = 0; i < serialPorts.size(); ++i)
{
ui->PortComboBox->addItem(QString::fromStdString(serialPorts[i]));
}
if(serialPorts.empty())
{
/*---------------------------------------------------*\
| When no ports were found, add an unselectable entry |
| denoting this fact istead |
\*---------------------------------------------------*/
QStandardItemModel* comboBoxModel = qobject_cast<QStandardItemModel *>(ui->PortComboBox->model());
if(comboBoxModel != nullptr)
{
ui->PortComboBox->addItem(tr("No serial ports found"));
QStandardItem *item = comboBoxModel->item(0);
item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
}
}
ui->PortComboBox->clearEditText();
}
SerialSettingsEntry::~SerialSettingsEntry()
@@ -56,7 +80,7 @@ void SerialSettingsEntry::loadFromSettings(const json& data)
if(data.contains("port"))
{
ui->PortEdit->setText(QString::fromStdString(data["port"]));
ui->PortComboBox->setCurrentText(QString::fromStdString(data["port"]));
}
if(data.contains("baud"))
@@ -99,7 +123,7 @@ json SerialSettingsEntry::saveSettings()
| Required parameters |
\*-------------------------------------------------*/
result["name"] = ui->NameEdit->text().toStdString();
result["port"] = ui->PortEdit->text().toStdString();
result["port"] = ui->PortComboBox->currentText().toStdString();
result["num_leds"] = ui->NumLEDsEdit->text().toUInt();
result["baud"] = ui->BaudEdit->text().toUInt();

View File

@@ -26,11 +26,12 @@
<string>Serial Device</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="1" column="5">
<widget class="QLineEdit" name="PortEdit"/>
</item>
<item row="1" column="3">
<widget class="QLineEdit" name="NameEdit"/>
<item row="2" column="4">
<widget class="QLabel" name="NumLEDsLabel">
<property name="text">
<string>Number of LEDs:</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="BaudLabel">
@@ -39,6 +40,9 @@
</property>
</widget>
</item>
<item row="3" column="3">
<widget class="QComboBox" name="ProtocolComboBox"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="NameLabel">
<property name="text">
@@ -46,17 +50,13 @@
</property>
</widget>
</item>
<item row="2" column="4">
<widget class="QLabel" name="NumLEDsLabel">
<property name="text">
<string>Number of LEDs:</string>
</property>
</widget>
<item row="1" column="3">
<widget class="QLineEdit" name="NameEdit"/>
</item>
<item row="1" column="4">
<widget class="QLabel" name="PortLabel">
<item row="3" column="0">
<widget class="QLabel" name="ProtocolLabel">
<property name="text">
<string>Port:</string>
<string>Protocol:</string>
</property>
</widget>
</item>
@@ -66,15 +66,22 @@
<item row="2" column="5">
<widget class="QLineEdit" name="NumLEDsEdit"/>
</item>
<item row="3" column="0">
<widget class="QLabel" name="ProtocolLabel">
<item row="1" column="4">
<widget class="QLabel" name="PortLabel">
<property name="text">
<string>Protocol:</string>
<string>Port:</string>
</property>
</widget>
</item>
<item row="3" column="3">
<widget class="QComboBox" name="ProtocolComboBox"/>
<item row="1" column="5">
<widget class="QComboBox" name="PortComboBox">
<property name="editable">
<bool>true</bool>
</property>
<property name="insertPolicy">
<enum>QComboBox::NoInsert</enum>
</property>
</widget>
</item>
</layout>
</widget>
@@ -83,7 +90,6 @@
</widget>
<tabstops>
<tabstop>NameEdit</tabstop>
<tabstop>PortEdit</tabstop>
<tabstop>BaudEdit</tabstop>
<tabstop>NumLEDsEdit</tabstop>
<tabstop>ProtocolComboBox</tabstop>

View File

@@ -13,6 +13,73 @@
#include "serial_port.h"
#include <algorithm> // For std::sort only
/*---------------------------------------------------------*\
| getSerialPorts(): returns the list of available serial |
| ports in the system |
\*---------------------------------------------------------*/
std::vector<std::string> serial_port::getSerialPorts()
{
/*-----------------------------------------------------------------------------------*\
| Ported from https://github.com/nkinar/GetComPortList/blob/master/GetComPortList.cpp |
\*-----------------------------------------------------------------------------------*/
std::vector<std::string> port_list;
#if defined (_WIN32) || defined( _WIN64)
const uint32_t CHAR_NUM = 1024;
const uint32_t MAX_PORTS = 255;
const std::string COM_STR = "COM";
char path[CHAR_NUM];
for (uint32_t k = 0; k < MAX_PORTS; k++)
{
std::string port_name = COM_STR + std::to_string(k);
DWORD test = QueryDosDevice(port_name.c_str(), path, CHAR_NUM);
if (test == 0) continue;
port_list.push_back(port_name);
}
#endif
#if defined (__linux__)
namespace fs = std::filesystem;
const std::string DEV_PATH = "/dev/serial/by-id";
try
{
fs::path p(DEV_PATH);
if (!fs::exists(DEV_PATH)) return port_list;
for (fs::directory_entry de: fs::directory_iterator(p))
{
if (fs::is_symlink(de.symlink_status()))
{
fs::path symlink_points_at = fs::read_symlink(de);
port_list.push_back(std::string("/dev/")+symlink_points_at.filename().c_str());
}
}
}
catch (const fs::filesystem_error &ex) {}
#endif
#if defined(__APPLE__)
namespace fs = std::filesystem;
const std::string DEV_PATH = "/dev";
const std::regex base_regex(R"(\/dev\/(tty|cu)\..*)");
try
{
fs::path p(DEV_PATH);
if (!fs::exists(DEV_PATH)) return port_list;
for (fs::directory_entry de: fs::directory_iterator(p)) {
fs::path canonical_path = fs::canonical(de);
std::string name = canonical_path.generic_string();
std::smatch res;
std::regex_search(name, res, base_regex);
if (res.empty()) continue;
port_list.push_back(canonical_path.generic_string());
}
}
catch (const fs::filesystem_error &ex) {}
#endif
std::sort(port_list.begin(), port_list.end());
return port_list;
}
/*---------------------------------------------------------*\
| serial_port (constructor) |
| The default constructor does not initialize the serial |

View File

@@ -16,6 +16,8 @@
#include <string.h>
#include <stdio.h>
#include <vector>
#include <string>
#ifdef _WIN32
/*---------------------------------------------------------*\
@@ -103,6 +105,8 @@ enum
class serial_port
{
public:
static std::vector<std::string> getSerialPorts();
serial_port();
serial_port(const char * name, unsigned int baud);
serial_port(const char * name,