feat(simu): cross-platform connection of host serial ports to simu radio (#5584)

This commit is contained in:
Nigel Williams
2024-12-16 20:00:42 +10:00
committed by GitHub
parent 8885016fd5
commit 9109d4f46f
22 changed files with 789 additions and 808 deletions
+2
View File
@@ -19,6 +19,8 @@ set(${PROJECT_NAME}_NAMES
widgets/lcdwidget
widgets/radiowidget
widgets/virtualjoystickwidget
serialportsdialog
hostserialconnector
)
if(SDL2_FOUND)
@@ -0,0 +1,187 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program 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.
*/
#include "hostserialconnector.h"
#include <QMessageBox>
HostSerialConnector::HostSerialConnector(QObject *parent, SimulatorInterface *simulator)
: simulator(simulator)
{
for (int i = 0; i < MAX_HOST_SERIAL; i++) {
hostAuxPorts[i] = nullptr;
hostAuxPortsEncoding[i] = SERIAL_ENCODING_8N1;
hostAuxPortsBaudRate[i] = 9600;
hostAuxPortsOpen[i] = false;
}
}
HostSerialConnector::~HostSerialConnector()
{
for (int i = 0; i < MAX_HOST_SERIAL; i++) {
if (hostAuxPorts[i] != nullptr) {
hostAuxPorts[i]->close();
hostAuxPorts[i]->deleteLater();
}
}
}
QString HostSerialConnector::getConnectedSerialPortName(int index)
{
if (index >= MAX_HOST_SERIAL)
return QString("");
QMutexLocker locker(&hostAuxPortsMutex);
QSerialPort * port = hostAuxPorts[index];
if (port == nullptr)
return QString("");
return port->portName();
}
void HostSerialConnector::connectSerialPort(int index, QString portName)
{
if (index >= MAX_HOST_SERIAL)
return;
QMutexLocker locker(&hostAuxPortsMutex);
QSerialPort * port = hostAuxPorts[index];
if (port != nullptr) {
port->close();
port->deleteLater();
}
if (portName.isEmpty()) {
hostAuxPorts[index] = nullptr;
return;
}
port = new QSerialPort(portName, this);
hostAuxPorts[index] = port;
setSerialEncoding(index, hostAuxPortsEncoding[index]);
setSerialBaudRate(index, hostAuxPortsBaudRate[index]);
connect(port, &QSerialPort::readyRead, [this, index, port]() {
QByteArray data = port->readAll();
simulator->receiveAuxSerialData(index, data);
});
if (hostAuxPortsOpen[index])
serialStart(index);
}
void HostSerialConnector::sendSerialData(const quint8 index, const QByteArray & data)
{
if (index >= MAX_HOST_SERIAL)
return;
QMutexLocker locker(&hostAuxPortsMutex);
QSerialPort * port = hostAuxPorts[index];
if (port == nullptr)
return;
port->write(data);
}
void HostSerialConnector::setSerialEncoding(const quint8 index, const quint8 encoding)
{
if (index >= MAX_HOST_SERIAL)
return;
QMutexLocker locker(&hostAuxPortsMutex);
hostAuxPortsEncoding[index] = encoding;
QSerialPort * port = hostAuxPorts[index];
if (port == nullptr)
return;
switch(encoding) {
case SERIAL_ENCODING_8N1:
port->setDataBits(QSerialPort::Data8);
port->setParity(QSerialPort::NoParity);
port->setStopBits(QSerialPort::OneStop);
break;
case SERIAL_ENCODING_8E2:
port->setDataBits(QSerialPort::Data8);
port->setParity(QSerialPort::EvenParity);
port->setStopBits(QSerialPort::TwoStop);
break;
default:
// Do nothing, QSerialPort can't do SERIAL_ENCODING_PXX1_PWM
break;
}
}
void HostSerialConnector::setSerialBaudRate(const quint8 index, const quint32 baudrate)
{
if (index >= MAX_HOST_SERIAL)
return;
QMutexLocker locker(&hostAuxPortsMutex);
hostAuxPortsBaudRate[index] = baudrate;
QSerialPort * port = hostAuxPorts[index];
if (port == nullptr)
return;
if (!port->setBaudRate(baudrate))
qDebug() << "Failed to set baudrate";
}
void HostSerialConnector::serialStart(const quint8 index)
{
if (index >= MAX_HOST_SERIAL)
return;
QMutexLocker locker(&hostAuxPortsMutex);
hostAuxPortsOpen[index] = true;
QSerialPort * port = hostAuxPorts[index];
if (port == nullptr)
return;
if (port->open(QIODevice::ReadWrite))
qDebug() << "Opened host serial " << index;
else
QMessageBox::warning(nullptr, tr("Host Serial Error"), port->errorString(), QMessageBox::Cancel, QMessageBox::Cancel);
}
void HostSerialConnector::serialStop(const quint8 index)
{
if (index >= MAX_HOST_SERIAL)
return;
QMutexLocker locker(&hostAuxPortsMutex);
hostAuxPortsOpen[index] = false;
QSerialPort * port = hostAuxPorts[index];
if (port == nullptr)
return;
port->close();
}
@@ -0,0 +1,57 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program 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.
*/
#pragma once
#include "simulatorinterface.h"
#include <QSerialPort>
#include <QMutex>
#define MAX_HOST_SERIAL 2
class HostSerialConnector : public QObject
{
Q_OBJECT
public:
explicit HostSerialConnector(QObject * parent, SimulatorInterface * simulator);
~HostSerialConnector();
QString getConnectedSerialPortName(int index);
public slots:
void connectSerialPort(int index, QString portName);
void sendSerialData(const quint8 index, const QByteArray & data);
void setSerialEncoding(const quint8 index, const quint8 encoding);
void setSerialBaudRate(const quint8 index, const quint32 baudrate);
void serialStart(const quint8 index);
void serialStop(const quint8 index);
private:
SimulatorInterface * simulator;
QRecursiveMutex hostAuxPortsMutex;
QSerialPort * hostAuxPorts[MAX_HOST_SERIAL];
quint8 hostAuxPortsEncoding[MAX_HOST_SERIAL];
quint32 hostAuxPortsBaudRate[MAX_HOST_SERIAL];
bool hostAuxPortsOpen[MAX_HOST_SERIAL];
};
@@ -0,0 +1,89 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program 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.
*/
#include "serialportsdialog.h"
#include "ui_serialportsdialog.h"
SerialPortsDialog::SerialPortsDialog(QWidget *parent, SimulatorInterface *simulator, HostSerialConnector *connector) :
QDialog(parent),
simulator(simulator),
connector(connector),
ui(new Ui::SerialPortsDialog)
{
ui->setupUi(this);
aux1 = connector->getConnectedSerialPortName(0);
aux2 = connector->getConnectedSerialPortName(1);
populateSerialPortCombo(ui->aux1Combo, aux1);
populateSerialPortCombo(ui->aux2Combo, aux2);
ui->aux1Combo->setEnabled(simulator->getCapability(SimulatorInterface::Capability::CAP_SERIAL_AUX1));
ui->aux2Combo->setEnabled(simulator->getCapability(SimulatorInterface::Capability::CAP_SERIAL_AUX2));
}
SerialPortsDialog::~SerialPortsDialog()
{
delete ui;
}
void SerialPortsDialog::populateSerialPortCombo(QComboBox * cb, QString currentPortName)
{
cb->clear();
cb->addItem(tr("Not Assigned"), "");
if (currentPortName == "") {
cb->setCurrentIndex(0);
}
const auto serialPortInfos = QSerialPortInfo::availablePorts();
for (int i = 0; i < serialPortInfos.size(); i++) {
const auto portInfo = serialPortInfos[i];
cb->addItem(portInfo.portName(), portInfo.portName());
if (portInfo.portName() == currentPortName)
cb->setCurrentIndex(i + 1);
}
}
void SerialPortsDialog::on_cancelButton_clicked()
{
this->reject();
}
void SerialPortsDialog::on_okButton_clicked()
{
this->accept();
}
void SerialPortsDialog::on_refreshButton_clicked()
{
populateSerialPortCombo(ui->aux1Combo, aux1);
populateSerialPortCombo(ui->aux2Combo, aux2);
}
void SerialPortsDialog::on_aux1Combo_currentIndexChanged(int index)
{
aux1 = ui->aux1Combo->itemData(index).toString();
}
void SerialPortsDialog::on_aux2Combo_currentIndexChanged(int index)
{
aux2 = ui->aux2Combo->itemData(index).toString();
}
@@ -0,0 +1,59 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program 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.
*/
#pragma once
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QtWidgets>
#include "simulatorinterface.h"
#include "hostserialconnector.h"
class QComboBox;
namespace Ui {
class SerialPortsDialog;
}
class SerialPortsDialog : public QDialog
{
Q_OBJECT
public:
explicit SerialPortsDialog(QWidget *parent, SimulatorInterface *simulator, HostSerialConnector *connector);
~SerialPortsDialog();
QString aux1;
QString aux2;
SimulatorInterface *simulator;
HostSerialConnector *connector;
private:
Ui::SerialPortsDialog *ui;
private slots:
void populateSerialPortCombo(QComboBox * cb, QString currentPortName);
void on_cancelButton_clicked();
void on_okButton_clicked();
void on_refreshButton_clicked();
void on_aux1Combo_currentIndexChanged(int index);
void on_aux2Combo_currentIndexChanged(int index);
};
@@ -0,0 +1,117 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>SerialPortsDialog</class>
<widget class="QDialog" name="SerialPortsDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>558</width>
<height>108</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="windowTitle">
<string>Configure Serial Ports</string>
</property>
<widget class="QWidget" name="layoutWidget">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>534</width>
<height>89</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLabel" name="aux1Label">
<property name="text">
<string>AUX1 Port</string>
</property>
</widget>
</item>
<item row="2" column="3">
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>360</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="2" column="0" colspan="2">
<widget class="QPushButton" name="cancelButton">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Cancel</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="aux2Label">
<property name="text">
<string>AUX2 Port</string>
</property>
</widget>
</item>
<item row="0" column="1" colspan="4">
<widget class="QComboBox" name="aux1Combo">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item row="1" column="1" colspan="4">
<widget class="QComboBox" name="aux2Combo">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item row="2" column="4">
<widget class="QPushButton" name="okButton">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Ok</string>
</property>
</widget>
</item>
<item row="2" column="2">
<widget class="QPushButton" name="refreshButton">
<property name="text">
<string>Search for Serial Ports</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<resources/>
<connections/>
</ui>
@@ -33,6 +33,7 @@
#include <QDir>
#include <QLibrary>
#include <QMap>
#include <QSerialPort>
#define SIMULATOR_INTERFACE_HEARTBEAT_PERIOD 1000 // ms
@@ -44,6 +45,13 @@ enum SimulatorTelemetryProtocol {
SIMU_TELEMETRY_PROTOCOL_COUNT
};
enum SimulatorSerialEncoding {
SERIAL_ENCODING_8N1 = 0,
SERIAL_ENCODING_8E2,
SERIAL_ENCODING_PXX1_PWM,
SERIAL_ENCODING_COUNT
};
class SimulatorInterface : public QObject
{
Q_OBJECT
@@ -84,6 +92,8 @@ class SimulatorInterface : public QObject
CAP_ROTARY_ENC, // ROTARY_ENCODERS
CAP_ROTARY_ENC_NAV, // ROTARY_ENCODER_NAVIGATION
CAP_TELEM_FRSKY_SPORT, // TELEMETRY_FRSKY_SPORT
CAP_SERIAL_AUX1, // Does AUX1 exist on this radio?
CAP_SERIAL_AUX2, // Does AUX2 exist on this radio?
CAP_ENUM_COUNT
};
@@ -160,6 +170,7 @@ class SimulatorInterface : public QObject
virtual void setLuaStateReloadPermanentScripts() = 0;
virtual void addTracebackDevice(QIODevice * device) = 0;
virtual void removeTracebackDevice(QIODevice * device) = 0;
virtual void receiveAuxSerialData(const quint8 port_num, const QByteArray & data) = 0;
signals:
@@ -176,6 +187,11 @@ class SimulatorInterface : public QObject
void trimRangeChange(quint8 index, qint32 min, qint16 max);
void gVarValueChange(quint8 index, qint32 value);
void outputValueChange(int type, quint8 index, qint32 value);
void auxSerialSendData(const quint8 port_num, const QByteArray & data);
void auxSerialSetEncoding(const quint8 port_num, const quint8 encoding);
void auxSerialSetBaudrate(const quint8 port_num, const quint32 baudrate);
void auxSerialStart(const quint8 port_num);
void auxSerialStop(const quint8 port_num);
};
class SimulatorFactory {
@@ -32,6 +32,7 @@
#ifdef JOYSTICKS
#include "joystickdialog.h"
#endif
#include "serialportsdialog.h"
#include <QDebug>
#include <QDir>
@@ -82,6 +83,8 @@ SimulatorMainWindow::SimulatorMainWindow(QWidget *parent, const QString & simula
m_simulator->moveToThread(&simuThread);
simuThread.start();
hostSerialConnector = new HostSerialConnector(this, m_simulator);
ui->setupUi(this);
setCorner(Qt::TopLeftCorner, Qt::LeftDockWidgetArea);
@@ -131,6 +134,7 @@ SimulatorMainWindow::SimulatorMainWindow(QWidget *parent, const QString & simula
connect(ui->actionShowKeymap, &QAction::triggered, this, &SimulatorMainWindow::showHelp);
connect(ui->actionJoystickSettings, &QAction::triggered, this, &SimulatorMainWindow::openJoystickDialog);
connect(ui->actionSerialPorts, &QAction::triggered, this, &SimulatorMainWindow::openSerialPortsDialog);
connect(ui->actionToggleMenuBar, &QAction::toggled, this, &SimulatorMainWindow::showMenuBar);
connect(ui->actionFixedRadioWidth, &QAction::toggled, this, &SimulatorMainWindow::showRadioFixedWidth);
connect(ui->actionFixedRadioHeight, &QAction::toggled, this, &SimulatorMainWindow::showRadioFixedHeight);
@@ -150,6 +154,12 @@ SimulatorMainWindow::SimulatorMainWindow(QWidget *parent, const QString & simula
connect(ui->actionScreenshot, &QAction::triggered, m_simulatorWidget, &SimulatorWidget::captureScreenshot);
connect(m_simulatorWidget, &SimulatorWidget::windowTitleChanged, this, &SimulatorMainWindow::setWindowTitle);
}
connect(m_simulator, &SimulatorInterface::auxSerialSendData, hostSerialConnector, &HostSerialConnector::sendSerialData);
connect(m_simulator, &SimulatorInterface::auxSerialSetEncoding, hostSerialConnector, &HostSerialConnector::setSerialEncoding);
connect(m_simulator, &SimulatorInterface::auxSerialSetBaudrate, hostSerialConnector, &HostSerialConnector::setSerialBaudRate);
connect(m_simulator, &SimulatorInterface::auxSerialStart, hostSerialConnector, &HostSerialConnector::serialStart);
connect(m_simulator, &SimulatorInterface::auxSerialStop, hostSerialConnector, &HostSerialConnector::serialStop);
}
SimulatorMainWindow::~SimulatorMainWindow()
@@ -171,6 +181,8 @@ SimulatorMainWindow::~SimulatorMainWindow()
}
delete m_simulator;
}
delete hostSerialConnector;
SimulatorLoader::unloadSimulator(m_simulatorId);
}
@@ -470,6 +482,16 @@ void SimulatorMainWindow::openJoystickDialog(bool)
#endif
}
void SimulatorMainWindow::openSerialPortsDialog(bool)
{
SerialPortsDialog * dialog = new SerialPortsDialog(this, m_simulator, hostSerialConnector);
if (dialog->exec() == QDialog::Accepted && m_simulator) {
hostSerialConnector->connectSerialPort(0, dialog->aux1);
hostSerialConnector->connectSerialPort(1, dialog->aux2);
}
dialog->deleteLater();
}
void SimulatorMainWindow::showHelp(bool show)
{
QString helpText = ""
@@ -22,6 +22,7 @@
#pragma once
#include "simulator.h"
#include "hostserialconnector.h"
#include <QDockWidget>
#include <QFile>
@@ -80,6 +81,7 @@ class SimulatorMainWindow : public QMainWindow
void setRadioSizePolicy(int fixType);
void toggleRadioDocked(bool dock);
void openJoystickDialog(bool);
void openSerialPortsDialog(bool);
void showHelp(bool show);
protected:
@@ -87,6 +89,7 @@ class SimulatorMainWindow : public QMainWindow
void addTool(QDockWidget * widget, Qt::DockWidgetArea area, QIcon icon = QIcon(), QKeySequence shortcut = QKeySequence());
SimulatorInterface * m_simulator;
HostSerialConnector * hostSerialConnector;
Ui::SimulatorMainWindow * ui;
SimulatorWidget * m_simulatorWidget;
@@ -72,6 +72,7 @@
<addaction name="actionScreenshot"/>
<addaction name="actionJoystickSettings"/>
<addaction name="actionShowKeymap"/>
<addaction name="actionSerialPorts"/>
</widget>
<addaction name="menuView"/>
<addaction name="menuReload"/>
@@ -216,6 +217,11 @@
<string>Set radio widget height to be a fixed size.</string>
</property>
</action>
<action name="actionSerialPorts">
<property name="text">
<string>Serial Ports</string>
</property>
</action>
</widget>
<resources>
<include location="../companion.qrc"/>
@@ -74,6 +74,7 @@ class SimulatorWidget : public QWidget
void setUiAreaStyle(const QString & style);
void captureScreenshot(bool);
void setupJoysticks();
void setupSerialPorts(QSerialPort *aux1, QSerialPort *aux2);
QString getSdPath() const { return sdCardPath; }
QString getDataPath() const { return radioDataPath; }