13const QHash<int, QString>& portTags()
15 static const QHash<int, QString> tags = {
16 {1, QStringLiteral(
"URT1")}, {2, QStringLiteral(
"URT2")}, {3, QStringLiteral(
"URT3")},
17 {4, QStringLiteral(
"URT4")}, {5, QStringLiteral(
"URT5")}, {6, QStringLiteral(
"URT6")},
18 {7, QStringLiteral(
"URT7")}, {8, QStringLiteral(
"URT8")}, {9, QStringLiteral(
"URT9")},
19 {101, QStringLiteral(
"TEL1")}, {102, QStringLiteral(
"TEL2")}, {103, QStringLiteral(
"TEL3")},
20 {104, QStringLiteral(
"TEL4")}, {201, QStringLiteral(
"GPS1")}, {202, QStringLiteral(
"GPS2")},
21 {203, QStringLiteral(
"GPS3")}, {300, QStringLiteral(
"RC")}, {301, QStringLiteral(
"WIFI")},
22 {401, QStringLiteral(
"EXT2")},
27constexpr int kFirstNonUartPortId = 101;
28constexpr int kRcPortId = 300;
30const Function* findFunction(
const QList<Function>& functions,
const QString& paramName)
32 const auto it = std::find_if(functions.cbegin(), functions.cend(),
33 [¶mName](
const Function& function) { return function.paramName == paramName; });
34 return (it == functions.cend()) ? nullptr : &(*it);
37bool allowsPort(
const Function& function,
int portId)
39 return std::any_of(function.
ports.cbegin(), function.
ports.cend(),
40 [portId](
const Port& port) { return port.id == portId; });
44const QRegularExpression* usualFunctionsRegex(
int portId)
46 static const QRegularExpression gpsRegex(QStringLiteral(
"^(GPS_\\d_CONFIG|SEP_PORT\\d_CFG)$"));
47 static const QRegularExpression telemetryRegex(QStringLiteral(
"^(MAV_\\d_CONFIG|UXRCE_DDS_CFG)$"));
48 static const QRegularExpression rcRegex(QStringLiteral(
"^RC_"));
50 const QString tag = portTags().value(portId);
51 if (tag.startsWith(QStringLiteral(
"TEL")) || (tag == QStringLiteral(
"WIFI")) || (portId ==
kEthernetPortId)) {
52 return &telemetryRegex;
54 if (tag.startsWith(QStringLiteral(
"GPS"))) {
57 if (tag == QStringLiteral(
"RC")) {
67 bool hasDisabled =
false;
68 bool hasNonUartPort =
false;
70 for (
const QVariant& enumValue : enumValues) {
72 const int value = enumValue.toInt(&ok);
78 }
else if (portTags().contains(value)) {
80 hasNonUartPort = hasNonUartPort || (value >= kFirstNonUartPortId);
86 return hasDisabled && hasNonUartPort;
91 const QString tag = portTags().value(portId);
92 return tag.isEmpty() ? QString() : QStringLiteral(
"SER_%1_BAUD").arg(tag);
95QString
functionLabel(
const QString& paramName,
const QString& shortDescription)
97 static const QString prefix = QStringLiteral(
"Serial Configuration for ");
99 if (shortDescription.startsWith(prefix)) {
100 return shortDescription.mid(prefix.length());
102 return shortDescription.isEmpty() ? paramName : shortDescription;
105Function makeFunction(
const QString& paramName,
const QString& shortDescription,
const QVariantList& enumValues,
106 const QStringList& enumStrings,
int value)
109 .paramName = paramName,
114 for (qsizetype i = 0; i < enumValues.count(); i++) {
115 const int portId = enumValues[i].toInt();
117 function.
ports.append({.id = portId, .label = enumStrings.value(i, QString::number(portId))});
121 if ((paramName == QStringLiteral(
"RC_PORT_CONFIG")) && allowsPort(function, kRcPortId)) {
129QList<Port>
ports(
const QList<Function>& functions)
133 for (
const Function& function : functions) {
134 for (
const Port& port : function.ports) {
135 const bool known = std::any_of(result.cbegin(), result.cend(),
136 [&port](
const Port& existing) { return existing.id == port.id; });
143 std::sort(result.begin(), result.end(), [](
const Port& a,
const Port& b) { return a.id < b.id; });
154 for (
const Function& function : functions) {
155 if (function.currentPortId == portId) {
156 result.append(function.paramName);
165 QList<Function> usual;
166 QList<Function> others;
167 const QRegularExpression*
const usualRegex = usualFunctionsRegex(portId);
169 for (
const Function& function : functions) {
170 if (!allowsPort(function, portId)) {
173 if (usualRegex && usualRegex->match(function.paramName).hasMatch()) {
174 usual.append(function);
176 others.append(function);
180 return usual + others;
183QList<ParamWrite>
assignPort(
const QList<Function>& functions,
int portId,
const QString& functionParamName)
185 QList<ParamWrite> writes;
187 if (!functionParamName.isEmpty()) {
188 const Function*
const function = findFunction(functions, functionParamName);
189 if (!function || !allowsPort(*function, portId)) {
195 for (
const Function& function : functions) {
196 if ((function.currentPortId == portId) && (function.paramName != functionParamName) &&
197 (function.defaultPortId != portId)) {
198 writes.append({.paramName = function.paramName, .value =
kDisabledPortId});
203 if (!functionParamName.isEmpty() && (findFunction(functions, functionParamName)->currentPortId != portId)) {
204 writes.append({.paramName = functionParamName, .value = portId});
210QList<ParamWrite>
removeFromPort(
const QList<Function>& functions,
int portId,
const QString& functionParamName)
212 const Function*
const function = findFunction(functions, functionParamName);
227 static const QRegularExpression mavlinkRegex(QStringLiteral(
"^MAV_(\\d)_CONFIG$"));
228 static const QRegularExpression gpsRegex(QStringLiteral(
"^GPS_(\\d)_CONFIG$"));
234 QStringList suffixes;
237 if (
const QRegularExpressionMatch mavlinkMatch = mavlinkRegex.match(functionParamName); mavlinkMatch.hasMatch()) {
238 prefix = QStringLiteral(
"MAV_%1_").arg(mavlinkMatch.captured(1));
239 suffixes = {QStringLiteral(
"MODE"), QStringLiteral(
"RATE"), QStringLiteral(
"FORWARD")};
241 suffixes += {QStringLiteral(
"UDP_PRT"), QStringLiteral(
"REMOTE_PRT"), QStringLiteral(
"BROADCAST")};
243 suffixes += QStringLiteral(
"FLOW_CTRL");
245 }
else if (
const QRegularExpressionMatch gpsMatch = gpsRegex.match(functionParamName); gpsMatch.hasMatch()) {
246 prefix = QStringLiteral(
"GPS_%1_").arg(gpsMatch.captured(1));
247 suffixes = {QStringLiteral(
"PROTOCOL")};
251 for (
const QString& suffix : suffixes) {
252 names.append(prefix + suffix);
QList< ParamWrite > removeFromPort(const QList< Function > &functions, int portId, const QString &functionParamName)
Writes which take functionParamName off portId. Empty if it isn't on the port, or the port is its def...
Function makeFunction(const QString ¶mName, const QString &shortDescription, const QVariantList &enumValues, const QStringList &enumStrings, int value)
value is the function parameter's value. currentPortId is defaultPortId when it is Disabled.