refactor(cpn): Support JSON radio hardware definitions (#4406)

This commit is contained in:
Neil Horne
2024-02-04 15:59:04 +10:00
committed by GitHub
parent c6ae44f523
commit 83294613f6
63 changed files with 4166 additions and 2295 deletions
+57 -93
View File
@@ -24,7 +24,7 @@
#include "radiodata.h"
#include "radiodataconversionstate.h"
QString RawSwitch::toString(Board::Type board, const GeneralSettings * const generalSettings, const ModelData * const modelData) const
QString RawSwitch::toString(Board::Type board, const GeneralSettings * const generalSettings, const ModelData * const modelData, bool prefixCustomName) const
{
if (board == Board::BOARD_UNKNOWN) {
board = getCurrentBoard();
@@ -37,14 +37,14 @@ QString RawSwitch::toString(Board::Type board, const GeneralSettings * const gen
};
static const QString trimsSwitches[] = {
tr("RudTrim Left"), tr("RudTrim Right"),
tr("EleTrim Down"), tr("EleTrim Up"),
tr("ThrTrim Down"), tr("ThrTrim Up"),
tr("AilTrim Left"), tr("AilTrim Right"),
tr("Trim 5 Down"), tr("Trim 5 Up"),
tr("Trim 6 Down"), tr("Trim 6 Up"),
tr("Trim 7 Down"), tr("Trim 7 Up"),
tr("Trim 8 Down"), tr("Trim 8 Up")
tr("Rud-"), tr("Rud+"),
tr("Ele-"), tr("Ele+"),
tr("Thr-"), tr("Thr+"),
tr("Ail-"), tr("Ail+"),
tr("T5-"), tr("T5+"),
tr("T6-"), tr("T6+"),
tr("T7-"), tr("T7+"),
tr("T8-"), tr("T8+")
};
static const QString trimsSwitches2[] = {
@@ -71,16 +71,26 @@ QString RawSwitch::toString(Board::Type board, const GeneralSettings * const gen
}
else {
QString swName;
QString custName;
div_t qr;
switch(type) {
case SWITCH_TYPE_SWITCH:
if (IS_HORUS_OR_TARANIS(board)) {
qr = div(index - 1, 3);
if (generalSettings)
swName = QString(generalSettings->switchName[qr.quot]).trimmed();
if (swName.isEmpty())
swName = Boards::getSwitchInfo(board, qr.quot).name;
return swName + directionIndicators.at(qr.rem > -1 && qr.rem < directionIndicators.size() ? qr.rem : 1);
swName = Boards::getSwitchInfo(qr.quot, board).name.c_str();
if (Boards::isSwitchFunc(qr.quot, board)) {
if (modelData) {
int fsindex = Boards::getSwitchTagNum(qr.quot, board) - 1;
custName = QString(modelData->functionSwitchNames[fsindex]).trimmed();
}
}
else {
if (generalSettings) {
custName = QString(generalSettings->switchConfig[qr.quot].name).trimmed();
}
}
return DataHelpers::getCompositeName(swName, custName, prefixCustomName) +
directionIndicators.at(qr.rem > -1 && qr.rem < directionIndicators.size() ? qr.rem : 1);
}
else {
return CHECK_IN_ARRAY(switches9X, index - 1);
@@ -88,35 +98,26 @@ QString RawSwitch::toString(Board::Type board, const GeneralSettings * const gen
case SWITCH_TYPE_VIRTUAL:
if (modelData)
return modelData->logicalSw[index].nameToString(index-1);
return modelData->logicalSw[index].nameToString(index - 1);
else
return LogicalSwitchData().nameToString(index-1);
case SWITCH_TYPE_FUNCTIONSWITCH:
if (!Boards::getCapability(board, Board::FunctionSwitches))
return CPN_STR_UNKNOWN_ITEM;
qr = div(index - 1, 3);
if (modelData)
swName = QString(modelData->functionSwitchNames[qr.quot]).trimmed();
if (swName.isEmpty())
swName = tr("SW%1").arg(qr.quot + 1);
return swName + directionIndicators.at(qr.rem > -1 && qr.rem < directionIndicators.size() ? qr.rem : 1);
return LogicalSwitchData().nameToString(index - 1);
case SWITCH_TYPE_MULTIPOS_POT:
if (!Boards::getCapability(board, Board::MultiposPotsPositions))
return CPN_STR_UNKNOWN_ITEM;
qr = div(index - 1, Boards::getCapability(board, Board::MultiposPotsPositions));
if (generalSettings && qr.quot < (int)DIM(generalSettings->potConfig))
swName = QString(generalSettings->potName[qr.quot]);
if (generalSettings && qr.quot < (int)DIM(generalSettings->inputConfig))
swName = QString(generalSettings->inputConfig[qr.quot].name);
if (swName.isEmpty())
swName = Boards::getAnalogInputName(board, qr.quot + Boards::getCapability(board, Board::Sticks));
swName = Boards::getInputName(qr.quot, board);
return swName + "_" + QString::number(qr.rem + 1);
case SWITCH_TYPE_TRIM:
return (Boards::getCapability(board, Board::NumTrims) == 2 ? CHECK_IN_ARRAY(trimsSwitches2, index-1) : CHECK_IN_ARRAY(trimsSwitches, index-1));
return (Boards::getCapability(board, Board::NumTrims) == 2 ?
CHECK_IN_ARRAY(trimsSwitches2, index - 1) : CHECK_IN_ARRAY(trimsSwitches, index - 1));
case SWITCH_TYPE_ROTARY_ENCODER:
return CHECK_IN_ARRAY(rotaryEncoders, index-1);
return CHECK_IN_ARRAY(rotaryEncoders, index - 1);
case SWITCH_TYPE_ON:
return tr("ON");
@@ -132,7 +133,7 @@ QString RawSwitch::toString(Board::Type board, const GeneralSettings * const gen
case SWITCH_TYPE_FLIGHT_MODE:
if (modelData)
return modelData->flightModeData[index-1].nameToString(index - 1);
return modelData->flightModeData[index - 1].nameToString(index - 1);
else
return FlightModeData().nameToString(index - 1);
@@ -144,9 +145,9 @@ QString RawSwitch::toString(Board::Type board, const GeneralSettings * const gen
case SWITCH_TYPE_SENSOR:
if (modelData)
return modelData->sensorData[index-1].nameToString(index-1);
return modelData->sensorData[index - 1].nameToString(index - 1);
else
return SensorData().nameToString(index-1);
return SensorData().nameToString(index - 1);
case SWITCH_TYPE_TELEMETRY:
return tr("Telemetry");
@@ -166,33 +167,36 @@ bool RawSwitch::isAvailable(const ModelData * const model, const GeneralSettings
board = getCurrentBoard();
Boards b(board);
div_t sw;
if (type == SWITCH_TYPE_SWITCH && abs(index) > b.getCapability(Board::SwitchPositions))
if (type == SWITCH_TYPE_SWITCH && abs(index) > b.getCapability(Board::SwitchesPositions))
return false;
if (type == SWITCH_TYPE_FUNCTIONSWITCH) {
if (!model || abs(index) > b.getCapability(Board::NumFunctionSwitchesPositions))
return false;
else if (!model->isFunctionSwitchPositionAvailable(abs(index)))
return false;
}
if (type == SWITCH_TYPE_TRIM && abs(index) > b.getCapability(Board::NumTrimSwitches))
return false;
if (type == SWITCH_TYPE_SWITCH)
sw = div(abs(index) - 1, 3);
if (gs) {
if (type == SWITCH_TYPE_SWITCH && IS_HORUS_OR_TARANIS(board) && !gs->switchPositionAllowedTaranis(abs(index)))
if (type == SWITCH_TYPE_SWITCH && !b.isSwitchFunc(sw.quot, board) && !gs->switchPositionAllowed(abs(index)))
return false;
if (type == SWITCH_TYPE_MULTIPOS_POT) {
int pot = div(abs(index) - 1, b.getCapability(Board::MultiposPotsPositions)).quot;
if (!gs->isPotAvailable(pot) || gs->potConfig[pot] != Board::POT_MULTIPOS_SWITCH)
int idx = div(abs(index) - 1, b.getCapability(Board::MultiposPotsPositions)).quot;
if (!gs->isInputAvailable(idx) || gs->inputConfig[idx].flexType != Board::FLEX_MULTIPOS)
return false;
}
}
if (model && !model->isAvailable(*this))
return false;
if (model) {
if (type == SWITCH_TYPE_SWITCH && b.isSwitchFunc(sw.quot, board)) {
int fsindex = (((Boards::getSwitchTagNum(sw.quot, board) - 1) * 3) + sw.rem + 1) * ( index < 0 ? -1 : 1);
return model->isFunctionSwitchPositionAvailable(fsindex);
}
else
return model->isAvailable(*this);
}
return true;
}
@@ -203,67 +207,27 @@ RawSwitch RawSwitch::convert(RadioDataConversionState & cstate)
return *this;
cstate.setItemType(tr("SW"), 2);
RadioDataConversionState::EventType evt = RadioDataConversionState::EVT_NONE;
RadioDataConversionState::LogField oldData(index, toString(cstate.fromType, cstate.fromGS(), cstate.fromModel()));
int newIdx = 0;
if (type == SWITCH_TYPE_SWITCH) {
div_t swtch = div(abs(index) - 1, 3); // rawsource index
QStringList fromSwitchList(getSwitchList(cstate.fromBoard));
QStringList toSwitchList(getSwitchList(cstate.toBoard));
// set to -1 if no match found
if (swtch.quot < fromSwitchList.count())
newIdx = toSwitchList.indexOf(fromSwitchList.at(swtch.quot));
else
newIdx = -1;
// perform forced mapping
if (newIdx < 0) {
if (IS_TARANIS_X7(cstate.toType) && (IS_TARANIS_X9(cstate.fromType) || IS_FAMILY_HORUS_OR_T16(cstate.fromType))) {
// No SE and SG on X7 board
newIdx = toSwitchList.indexOf("SD");
if (newIdx >= 0)
evt = RadioDataConversionState::EVT_CVRT;
}
else if (IS_FAMILY_T12(cstate.toType) && (IS_TARANIS_X9(cstate.fromType) || IS_FAMILY_HORUS_OR_T16(cstate.fromType))) {
// No SE and SG on T12 board
newIdx = toSwitchList.indexOf("SD");
if (newIdx >= 0)
evt = RadioDataConversionState::EVT_CVRT;
}
}
div_t swtch = div(abs(index) - 1, 3);
newIdx = Boards::getSwitchIndex(Boards::getSwitchTag(swtch.quot, cstate.fromType), Board::LVT_TAG, cstate.toType);
if (newIdx >= 0)
index = (newIdx * 3 + 1 + swtch.rem) * (index < 0 ? -1 : 1);
} // SWITCH_TYPE_SWITCH
}
// final validation (we do not pass model to isAvailable() because we don't know what has or hasn't been converted)
if (newIdx < 0 || !isAvailable(NULL, cstate.toGS(), cstate.toType)) {
if (newIdx < 0 || !isAvailable(nullptr, cstate.toGS(), cstate.toType)) {
cstate.setInvalid(oldData);
// no switch is safer than an invalid one
clear();
}
else if (evt == RadioDataConversionState::EVT_CVRT) {
cstate.setConverted(oldData, RadioDataConversionState::LogField(index, toString(cstate.toType, cstate.toGS(), cstate.toModel())));
}
else if (oldData.id != index) {
// provide info by default if anything changed
cstate.setMoved(oldData, RadioDataConversionState::LogField(index, toString(cstate.toType, cstate.toGS(), cstate.toModel())));
clear(); // no switch is safer than an invalid one
}
return *this;
}
QStringList RawSwitch::getSwitchList(Boards board) const
{
QStringList ret;
for (int i = 0; i < board.getCapability(Board::Switches); i++) {
ret.append(board.getSwitchInfo(i).name);
}
return ret;
}
// static
StringTagMappingTable RawSwitch::getRawSwitchTypesLookupTable()
{
@@ -272,7 +236,7 @@ StringTagMappingTable RawSwitch::getRawSwitchTypesLookupTable()
tbl.insert(tbl.end(), {
{std::to_string(SWITCH_TYPE_NONE), "NONE"},
{std::to_string(SWITCH_TYPE_SWITCH), "Sxn"},
{std::to_string(SWITCH_TYPE_FUNCTIONSWITCH), "Sxn"},
{std::to_string(SWITCH_TYPE_FUNCTIONSWITCH), "FSn"},
{std::to_string(SWITCH_TYPE_VIRTUAL), "Ln"},
{std::to_string(SWITCH_TYPE_MULTIPOS_POT), "6Pn"},
{std::to_string(SWITCH_TYPE_TRIM), "Trim"},