Serial port field is now drop-down list
This commit is contained in:
parent
25d45fd9db
commit
46a79c6bb5
6 changed files with 158 additions and 26 deletions
|
|
@ -10,11 +10,35 @@
|
||||||
#include "DMXSettingsEntry.h"
|
#include "DMXSettingsEntry.h"
|
||||||
#include "ui_DMXSettingsEntry.h"
|
#include "ui_DMXSettingsEntry.h"
|
||||||
|
|
||||||
|
#include "serial_port.h"
|
||||||
|
#include <QStandardItemModel>
|
||||||
|
|
||||||
DMXSettingsEntry::DMXSettingsEntry(QWidget *parent) :
|
DMXSettingsEntry::DMXSettingsEntry(QWidget *parent) :
|
||||||
BaseManualDeviceEntry(parent),
|
BaseManualDeviceEntry(parent),
|
||||||
ui(new Ui::DMXSettingsEntry)
|
ui(new Ui::DMXSettingsEntry)
|
||||||
{
|
{
|
||||||
ui->setupUi(this);
|
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()
|
DMXSettingsEntry::~DMXSettingsEntry()
|
||||||
|
|
@ -39,7 +63,7 @@ void DMXSettingsEntry::loadFromSettings(const json& data)
|
||||||
|
|
||||||
if(data.contains("port"))
|
if(data.contains("port"))
|
||||||
{
|
{
|
||||||
ui->PortEdit->setText(QString::fromStdString(data["port"]));
|
ui->PortComboBox->setCurrentText(QString::fromStdString(data["port"]));
|
||||||
}
|
}
|
||||||
|
|
||||||
if(data.contains("red_channel"))
|
if(data.contains("red_channel"))
|
||||||
|
|
@ -75,7 +99,7 @@ json DMXSettingsEntry::saveSettings()
|
||||||
| Required parameters |
|
| Required parameters |
|
||||||
\*-------------------------------------------------*/
|
\*-------------------------------------------------*/
|
||||||
result["name"] = ui->NameEdit->text().toStdString();
|
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["red_channel"] = ui->RedEdit->text().toUInt();
|
||||||
result["green_channel"] = ui->GreenEdit->text().toUInt();
|
result["green_channel"] = ui->GreenEdit->text().toUInt();
|
||||||
result["blue_channel"] = ui->BlueEdit->text().toUInt();
|
result["blue_channel"] = ui->BlueEdit->text().toUInt();
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@
|
||||||
<x>0</x>
|
<x>0</x>
|
||||||
<y>0</y>
|
<y>0</y>
|
||||||
<width>531</width>
|
<width>531</width>
|
||||||
<height>199</height>
|
<height>206</height>
|
||||||
</rect>
|
</rect>
|
||||||
</property>
|
</property>
|
||||||
<property name="sizePolicy">
|
<property name="sizePolicy">
|
||||||
|
|
@ -94,7 +94,14 @@
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="1" column="5">
|
<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>
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
</widget>
|
</widget>
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,9 @@
|
||||||
#include "SerialSettingsEntry.h"
|
#include "SerialSettingsEntry.h"
|
||||||
#include "ui_SerialSettingsEntry.h"
|
#include "ui_SerialSettingsEntry.h"
|
||||||
|
|
||||||
|
#include "serial_port.h"
|
||||||
|
#include <QStandardItemModel>
|
||||||
|
|
||||||
SerialSettingsEntry::SerialSettingsEntry(QWidget *parent) :
|
SerialSettingsEntry::SerialSettingsEntry(QWidget *parent) :
|
||||||
BaseManualDeviceEntry(parent),
|
BaseManualDeviceEntry(parent),
|
||||||
ui(new Ui::SerialSettingsEntry)
|
ui(new Ui::SerialSettingsEntry)
|
||||||
|
|
@ -20,6 +23,27 @@ SerialSettingsEntry::SerialSettingsEntry(QWidget *parent) :
|
||||||
ui->ProtocolComboBox->addItem("Adalight");
|
ui->ProtocolComboBox->addItem("Adalight");
|
||||||
ui->ProtocolComboBox->addItem("TPM2");
|
ui->ProtocolComboBox->addItem("TPM2");
|
||||||
ui->ProtocolComboBox->addItem("Basic I2C");
|
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()
|
SerialSettingsEntry::~SerialSettingsEntry()
|
||||||
|
|
@ -56,7 +80,7 @@ void SerialSettingsEntry::loadFromSettings(const json& data)
|
||||||
|
|
||||||
if(data.contains("port"))
|
if(data.contains("port"))
|
||||||
{
|
{
|
||||||
ui->PortEdit->setText(QString::fromStdString(data["port"]));
|
ui->PortComboBox->setCurrentText(QString::fromStdString(data["port"]));
|
||||||
}
|
}
|
||||||
|
|
||||||
if(data.contains("baud"))
|
if(data.contains("baud"))
|
||||||
|
|
@ -99,7 +123,7 @@ json SerialSettingsEntry::saveSettings()
|
||||||
| Required parameters |
|
| Required parameters |
|
||||||
\*-------------------------------------------------*/
|
\*-------------------------------------------------*/
|
||||||
result["name"] = ui->NameEdit->text().toStdString();
|
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["num_leds"] = ui->NumLEDsEdit->text().toUInt();
|
||||||
result["baud"] = ui->BaudEdit->text().toUInt();
|
result["baud"] = ui->BaudEdit->text().toUInt();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,11 +26,12 @@
|
||||||
<string>Serial Device</string>
|
<string>Serial Device</string>
|
||||||
</property>
|
</property>
|
||||||
<layout class="QGridLayout" name="gridLayout_2">
|
<layout class="QGridLayout" name="gridLayout_2">
|
||||||
<item row="1" column="5">
|
<item row="2" column="4">
|
||||||
<widget class="QLineEdit" name="PortEdit"/>
|
<widget class="QLabel" name="NumLEDsLabel">
|
||||||
</item>
|
<property name="text">
|
||||||
<item row="1" column="3">
|
<string>Number of LEDs:</string>
|
||||||
<widget class="QLineEdit" name="NameEdit"/>
|
</property>
|
||||||
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="2" column="0">
|
<item row="2" column="0">
|
||||||
<widget class="QLabel" name="BaudLabel">
|
<widget class="QLabel" name="BaudLabel">
|
||||||
|
|
@ -39,6 +40,9 @@
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
|
<item row="3" column="3">
|
||||||
|
<widget class="QComboBox" name="ProtocolComboBox"/>
|
||||||
|
</item>
|
||||||
<item row="1" column="0">
|
<item row="1" column="0">
|
||||||
<widget class="QLabel" name="NameLabel">
|
<widget class="QLabel" name="NameLabel">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
|
|
@ -46,17 +50,13 @@
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="2" column="4">
|
<item row="1" column="3">
|
||||||
<widget class="QLabel" name="NumLEDsLabel">
|
<widget class="QLineEdit" name="NameEdit"/>
|
||||||
<property name="text">
|
|
||||||
<string>Number of LEDs:</string>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
</item>
|
||||||
<item row="1" column="4">
|
<item row="3" column="0">
|
||||||
<widget class="QLabel" name="PortLabel">
|
<widget class="QLabel" name="ProtocolLabel">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>Port:</string>
|
<string>Protocol:</string>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
|
|
@ -66,15 +66,22 @@
|
||||||
<item row="2" column="5">
|
<item row="2" column="5">
|
||||||
<widget class="QLineEdit" name="NumLEDsEdit"/>
|
<widget class="QLineEdit" name="NumLEDsEdit"/>
|
||||||
</item>
|
</item>
|
||||||
<item row="3" column="0">
|
<item row="1" column="4">
|
||||||
<widget class="QLabel" name="ProtocolLabel">
|
<widget class="QLabel" name="PortLabel">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>Protocol:</string>
|
<string>Port:</string>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="3" column="3">
|
<item row="1" column="5">
|
||||||
<widget class="QComboBox" name="ProtocolComboBox"/>
|
<widget class="QComboBox" name="PortComboBox">
|
||||||
|
<property name="editable">
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
<property name="insertPolicy">
|
||||||
|
<enum>QComboBox::NoInsert</enum>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
</widget>
|
</widget>
|
||||||
|
|
@ -83,7 +90,6 @@
|
||||||
</widget>
|
</widget>
|
||||||
<tabstops>
|
<tabstops>
|
||||||
<tabstop>NameEdit</tabstop>
|
<tabstop>NameEdit</tabstop>
|
||||||
<tabstop>PortEdit</tabstop>
|
|
||||||
<tabstop>BaudEdit</tabstop>
|
<tabstop>BaudEdit</tabstop>
|
||||||
<tabstop>NumLEDsEdit</tabstop>
|
<tabstop>NumLEDsEdit</tabstop>
|
||||||
<tabstop>ProtocolComboBox</tabstop>
|
<tabstop>ProtocolComboBox</tabstop>
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,73 @@
|
||||||
|
|
||||||
#include "serial_port.h"
|
#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) |
|
| serial_port (constructor) |
|
||||||
| The default constructor does not initialize the serial |
|
| The default constructor does not initialize the serial |
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,8 @@
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
/*---------------------------------------------------------*\
|
/*---------------------------------------------------------*\
|
||||||
|
|
@ -103,6 +105,8 @@ enum
|
||||||
class serial_port
|
class serial_port
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
static std::vector<std::string> getSerialPorts();
|
||||||
|
|
||||||
serial_port();
|
serial_port();
|
||||||
serial_port(const char * name, unsigned int baud);
|
serial_port(const char * name, unsigned int baud);
|
||||||
serial_port(const char * name,
|
serial_port(const char * name,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue