feat(radio): refactor of switch handling to better support customisable switches (#6095)

Co-authored-by: philmoz <phil.a.mitchell@gmail.com>
Co-authored-by: 3djc <3djc@gh.com>
Co-authored-by: elecpower <elecpower@users.noreply.github.com>
This commit is contained in:
philmoz
2025-07-28 12:59:22 +10:00
committed by GitHub
co-authored by philmoz 3djc elecpower
parent 28833f42e6
commit 0c8c39b134
155 changed files with 4538 additions and 2306 deletions
+26 -3
View File
@@ -50,7 +50,6 @@ static const StringTagMappingTable switchTypesLookupTable = {
{std::to_string(Board::SWITCH_TOGGLE), "TOGGLE"},
{std::to_string(Board::SWITCH_2POS), "2POS"},
{std::to_string(Board::SWITCH_3POS), "3POS"},
{std::to_string(Board::SWITCH_FUNC), "FSWITCH"},
{std::to_string(Board::SWITCH_ADC), "ADC"},
};
@@ -443,6 +442,24 @@ const int BoardJson::getSwitchIndex(const QString val, Board::LookupValueType lv
return getSwitchIndex(m_switches, val, lvt);
}
const int BoardJson::getCFSIndexForSwitch(int sw) const
{
if (sw < (int)m_switches->size() && m_switches->at(sw).isCustomSwitch)
return m_switches->at(sw).customSwitchIdx;
return -1;
}
const int BoardJson::getSwitchIndexForCFS(int cfsIdx) const
{
for (int i = 0; i < (int)m_switches->size(); i++) {
if (m_switches->at(i).isCustomSwitch && m_switches->at(i).customSwitchIdx == cfsIdx)
return i;
}
return -1;
}
// static
int BoardJson::getSwitchIndex(const SwitchesTable * switches, QString val, Board::LookupValueType lvt)
{
@@ -772,7 +789,7 @@ const bool BoardJson::isSwitchConfigurable(int index) const
{
if (index >= 0 && index < getCapability(Board::Switches)) {
SwitchDefn &defn = m_switches->at(index);
if (isSwitchStd(defn))
if (isSwitchStd(defn) || isSwitchFunc(defn))
return true;
if (isSwitchFlex(defn)) {
@@ -813,7 +830,7 @@ const bool BoardJson::isSwitchFunc(int index) const
// static
bool BoardJson::isSwitchFunc(const SwitchDefn & defn)
{
return defn.type == Board::SWITCH_FUNC;
return defn.customSwitchIdx >= 0;
}
bool BoardJson::loadDefinition()
@@ -996,6 +1013,12 @@ bool BoardJson::loadFile(Board::Type board, QString hwdefn, InputsTable * inputs
sw.display.y = (unsigned int)d.at(1).toInt(0);
}
if (!o.value("is_cfs").isUndefined())
sw.isCustomSwitch = o.value("is_cfs").toBool();
if (!o.value("cfs_idx").isUndefined())
sw.customSwitchIdx = o.value("cfs_idx").toInt();
// special handing for ADC
if (sw.type == Board::SWITCH_ADC) {
if (sw.dflt == Board::SWITCH_TOGGLE) {
+5 -1
View File
@@ -61,13 +61,15 @@ class BoardJson
};
struct SwitchDefn {
Board::SwitchType type = Board::SWITCH_NOT_AVAILABLE;
std::string tag = "";
Board::SwitchType type = Board::SWITCH_NOT_AVAILABLE;
std::string name = "";
int flags = 0;
bool inverted = false;
Board::SwitchType dflt = Board::SWITCH_NOT_AVAILABLE;
Display display;
bool isCustomSwitch = false;
int customSwitchIdx = -1;
Board::LookupValueType cfgYaml = Board::LVT_TAG;
Board::LookupValueType refYaml = Board::LVT_NAME;
@@ -137,6 +139,8 @@ class BoardJson
const int getKeyIndex(const QString key) const;
const int getSwitchIndex(const QString val, Board::LookupValueType lvt) const;
const int getCFSIndexForSwitch(int sw) const;
const int getSwitchIndexForCFS(int customSwitchIdx) const;
const Board::SwitchInfo getSwitchInfo(int index) const;
const QString getSwitchName(int index) const;
const QString getSwitchTag(int index) const;
+20 -2
View File
@@ -315,6 +315,12 @@ int Boards::getCapability(Board::Type board, Board::Capability capability)
else
return getCapability(board, Board::Switches);
case FunctionSwitchGroups:
if (getCapability(board, FunctionSwitches)) {
return IS_RADIOMASTER_GX12(board) ? 4 : 3;
}
return 0;
case HasAudioMuteGPIO:
// All color lcd (including NV14 and EL18) except Horus X12S
// TX12, TX12MK2, ZORRO, BOXER, T8, TLITE, TPRO, LR3PRO, COMMANDO8
@@ -406,7 +412,7 @@ int Boards::getCapability(Board::Type board, Board::Capability capability)
return IS_RADIOMASTER_MT12(board);
case FunctionSwitchColors:
return IS_RADIOMASTER_GX12(board);
return IS_RADIOMASTER_GX12(board) || IS_FLYSKY_ST16(board);
default:
return getBoardJson(board)->getCapability(capability);
@@ -679,6 +685,8 @@ QString Boards::switchTypeToString(int value)
return tr("2 Positions");
case SWITCH_3POS:
return tr("3 Positions");
case SWITCH_GLOBAL:
return tr("Global");
case SWITCH_FUNC:
return tr("Function");
default:
@@ -692,7 +700,7 @@ AbstractStaticItemModel * Boards::switchTypeItemModel()
AbstractStaticItemModel * mdl = new AbstractStaticItemModel();
mdl->setName(AIM_BOARDS_SWITCH_TYPE);
for (int i = 0; i < SWITCH_FUNC; i++) { // Function not required in lists
for (int i = 0; i < SWITCH_GLOBAL; i++) { // Function not required in lists
mdl->appendToItemList(switchTypeToString(i), i, true, 0,
(i == SWITCH_NOT_AVAILABLE ? SwitchTypeFlagNone : (i < SWITCH_3POS ? SwitchTypeFlag2Pos : SwitchTypeFlag3Pos)));
}
@@ -1046,6 +1054,16 @@ int Boards::getSwitchIndex(QString val, Board::LookupValueType lvt, Board::Type
return getBoardJson(board)->getSwitchIndex(val, lvt);
}
int Boards::getCFSIndexForSwitch(int swIdx, Board::Type board)
{
return getBoardJson(board)->getCFSIndexForSwitch(swIdx);
}
int Boards::getSwitchIndexForCFS(int cfsIdx, Board::Type board)
{
return getBoardJson(board)->getSwitchIndexForCFS(cfsIdx);
}
QString Boards::getSwitchName(int index, Board::Type board)
{
return getBoardJson(board)->getSwitchName(index);
+4
View File
@@ -119,6 +119,7 @@ namespace Board {
SWITCH_TOGGLE,
SWITCH_2POS,
SWITCH_3POS,
SWITCH_GLOBAL,
SWITCH_FUNC,
SWITCH_ADC,
SWITCH_TYPE_COUNT
@@ -212,6 +213,7 @@ namespace Board {
FlexSwitches,
FunctionSwitches,
FunctionSwitchColors,
FunctionSwitchGroups,
Gyros,
GyroAxes,
HasAudioMuteGPIO,
@@ -426,6 +428,8 @@ class Boards
static Board::SwitchInfo getSwitchInfo(int index, Board::Type board = Board::BOARD_UNKNOWN);
static int getSwitchIndex(QString val, Board::LookupValueType lvt, Board::Type board = Board::BOARD_UNKNOWN);
static int getCFSIndexForSwitch(int swIdx, Board::Type board = Board::BOARD_UNKNOWN);
static int getSwitchIndexForCFS(int cfsIdx, Board::Type board = Board::BOARD_UNKNOWN);
static QString getSwitchName(int index, Board::Type board = Board::BOARD_UNKNOWN);
static QString getSwitchTag(int index, Board::Type board = Board::BOARD_UNKNOWN);
static int getSwitchTagNum(int index, Board::Type board = Board::BOARD_UNKNOWN);
@@ -262,7 +262,7 @@ bool CustomFunctionData::isFuncAvailable(const int index, const ModelData * mode
((index >= FuncAdjustGV1 && index <= FuncAdjustGVLast) && !fw->getCapability(Gvars)) ||
((index == FuncDisableTouch) && !IS_HORUS_OR_TARANIS(fw->getBoard())) ||
((index == FuncDisableAudioAmp && !Boards::getCapability(fw->getBoard(), Board::HasAudioMuteGPIO))) ||
((index == FuncRGBLed && !Boards::getCapability(fw->getBoard(), Board::HasLedStripGPIO))) ||
((index == FuncRGBLed && !(Boards::getCapability(fw->getBoard(), Board::HasLedStripGPIO) || Boards::getCapability(fw->getBoard(), Board::FunctionSwitchColors)))) ||
((index == FuncLCDtoVideo && !IS_FATFISH_F16(fw->getBoard()))) ||
((index >= FuncPushCustomSwitch1 && index <= FuncPushCustomSwitchLast) && !Boards::getCapability(fw->getBoard(), Board::FunctionSwitches))
);
@@ -602,7 +602,8 @@ bool convert<GeneralSettings>::decode(const Node& node, GeneralSettings& rhs)
// however when parsing saved settings set all switches to None and override with parsed values
// thus any switches not parsed will be None rather than the default
for (int i = 0; i < CPN_MAX_SWITCHES; i++) {
rhs.switchConfig[i].type = Board::SWITCH_NOT_AVAILABLE;
if (Boards::getCFSIndexForSwitch(i) < 0)
rhs.switchConfig[i].type = Board::SWITCH_NOT_AVAILABLE;
}
if (node["switchConfig"]) {
+153 -65
View File
@@ -31,7 +31,9 @@
#include "yaml_sensordata.h"
#include "yaml_screendata.h"
#include "yaml_usbjoystickdata.h"
#include "yaml_switchconfig.h"
#include "boardjson.h"
#include "modeldata.h"
#include "output_data.h"
#include "eeprominterface.h"
@@ -73,7 +75,7 @@ void YamlValidateLabelsNames(ModelData& model, Board::Type board)
}
for (int i = 0; i < CPN_MAX_SWITCHES_FUNCTION; i++) {
YamlValidateName(model.functionSwitchNames[i], board);
YamlValidateName(model.customSwitches[i].name, board);
}
for (int i = 0; i < CPN_MAX_INPUTS; i++) {
@@ -149,6 +151,25 @@ static const YamlLookupTable hatsModeLut = {
{ GeneralSettings::HATSMODE_GLOBAL, "GLOBAL" },
};
static const YamlLookupTable cfsSwitchConfig = {
{ 0, "NONE" },
{ 1, "TOGGLE" },
{ 2, "2POS" },
{ 3, "3POS" },
{ 4, "GLOBAL" },
};
static const YamlLookupTable cfsSwitchStart = {
{ 0, "START_OFF" },
{ 1, "START_ON" },
{ 2, "START_PREVIOUS" },
};
static const YamlLookupTable cfsSwitchLuaOverride = {
{ 0, "OFF" },
{ 1, "ON" },
};
struct YamlTrim {
int mode = 0;
int ref = 0;
@@ -283,27 +304,24 @@ struct YamlSwitchWarning {
static constexpr size_t MASK_LEN = 2;
static constexpr size_t MASK = (1 << MASK_LEN) - 1;
unsigned int enabled;
YamlSwitchWarning() = default;
YamlSwitchWarning(YAML::Node& node, uint64_t cpn_value, unsigned int switchWarningEnable)
: enabled(~switchWarningEnable)
YamlSwitchWarning(YAML::Node& node, uint64_t cpn_value)
{
uint64_t states = cpn_value;
for (int i = 0; i < Boards::getCapability(getCurrentBoard(), Board::Switches); i++) {
if (!Boards::isSwitchFunc(i) && (enabled & (1 << i))) {
if (states & MASK) {
std::string posn;
switch(states & MASK) {
case 0:
case 1:
posn = "up";
break;
case 1:
case 2:
posn = "mid";
break;
case 2:
case 3:
posn = "down";
break;
}
@@ -318,7 +336,6 @@ struct YamlSwitchWarning {
uint64_t toCpn(const YAML::Node &warn)
{
uint64_t states = 0;
enabled = 0;
if (warn.IsMap()) {
for (const auto& sw : warn) {
@@ -336,14 +353,13 @@ struct YamlSwitchWarning {
int value = 0;
if (posn == "up")
value = 0;
else if (posn == "mid")
value = 1;
else if (posn == "down")
else if (posn == "mid")
value = 2;
else if (posn == "down")
value = 3;
states |= ((uint64_t)value << (index * MASK_LEN));
enabled |= (1 << index);
}
}
@@ -360,32 +376,30 @@ struct YamlSwitchWarningState {
static constexpr size_t MASK = (1 << MASK_LEN) - 1;
std::string src_str;
unsigned int enabled;
YamlSwitchWarningState() = default;
YamlSwitchWarningState(uint64_t cpn_value, unsigned int switchWarningEnable)
: enabled(~switchWarningEnable)
YamlSwitchWarningState(uint64_t cpn_value)
{
uint64_t states = cpn_value;
std::stringstream ss;
for (int i = 0; i < Boards::getCapability(getCurrentBoard(), Board::Switches); i++) {
//TODO: exclude 2-pos toggle from switch warnings
if (enabled & (1 << i)) {
if (states & MASK) {
std::string tag = Boards::getSwitchTag(i).toStdString();
const char *sw = tag.data();
if (tag.size() >= 2 && sw[0] == 'S') {
ss << sw[1];
switch(states & MASK) {
case 0:
case 1:
ss << 'u';
break;
case 1:
case 2:
ss << '-';
break;
case 2:
case 3:
ss << 'd';
break;
}
@@ -400,7 +414,6 @@ struct YamlSwitchWarningState {
uint64_t toCpn()
{
uint64_t states = 0;
enabled = 0;
std::stringstream ss(src_str);
while (!ss.eof()) {
@@ -422,20 +435,19 @@ struct YamlSwitchWarningState {
int value = 0;
switch(s) {
case 'u':
value = 0;
break;
case '-':
value = 1;
break;
case 'd':
case '-':
value = 2;
break;
case 'd':
value = 3;
break;
default:
continue;
}
states |= ((uint64_t)value << (index * MASK_LEN));
enabled |= (1 << index);
}
return states;
@@ -955,6 +967,47 @@ struct convert<FrSkyScreenData> {
}
};
template <>
struct convert<customSwitch> {
static Node encode(const customSwitch& rhs);
static bool decode(const Node& node, customSwitch& rhs);
};
Node convert<customSwitch>::encode(const customSwitch& rhs)
{
Node node;
node["type"] = cfsSwitchConfig << rhs.type;
node["group"] = rhs.group;
node["start"] = cfsSwitchStart << rhs.start;
node["state"] = rhs.state;
node["name"] = rhs.name;
if (Boards::getCapability(getCurrentBoard(), Board::FunctionSwitchColors)) {
node["onColorLuaOverride"] = cfsSwitchLuaOverride << rhs.onColorLuaOverride;
node["offColorLuaOverride"] = cfsSwitchLuaOverride << rhs.offColorLuaOverride;
node["onColor"] = rhs.onColor;
node["offColor"] = rhs.offColor;
}
return node;
}
bool convert<customSwitch>::decode(const Node& node, customSwitch& rhs)
{
if (!node.IsMap()) return false;
unsigned type;
node["type"] >> cfsSwitchConfig >> type;
rhs.type = (Board::SwitchType)type;
node["group"] >> rhs.group;
node["start"] >> cfsSwitchStart >> rhs.start;
node["state"] >> rhs.state;
node["name"] >> rhs.name;
node["onColorLuaOverride"] >> cfsSwitchLuaOverride >> rhs.onColorLuaOverride;
node["offColorLuaOverride"] >> cfsSwitchLuaOverride >> rhs.offColorLuaOverride;
node["onColor"] >> rhs.onColor;
node["offColor"] >> rhs.offColor;
return true;
}
Node convert<ModelData>::encode(const ModelData& rhs)
{
modelSettingsVersion = SemanticVersion(VERSION);
@@ -1076,7 +1129,7 @@ Node convert<ModelData>::encode(const ModelData& rhs)
node["thrTraceSrc"] = thrTrace.src;
Node sw_warn;
YamlSwitchWarning switchWarning(sw_warn, rhs.switchWarningStates, rhs.switchWarningEnable);
YamlSwitchWarning switchWarning(sw_warn, rhs.switchWarningStates);
if (sw_warn && sw_warn.IsMap()) {
node["switchWarning"] = sw_warn;
}
@@ -1194,29 +1247,16 @@ Node convert<ModelData>::encode(const ModelData& rhs)
node["modelRegistrationID"] = rhs.registrationId;
node["hatsMode"] = hatsModeLut << rhs.hatsMode;
if (Boards::getCapability(board, Board::FunctionSwitches)) {
node["functionSwitchConfig"] = rhs.functionSwitchConfig;
node["functionSwitchGroup"] = rhs.functionSwitchGroup;
node["functionSwitchStartConfig"] = rhs.functionSwitchStartConfig;
node["functionSwitchLogicalState"] = rhs.functionSwitchLogicalState;
for (int i = 0; i < CPN_MAX_SWITCHES_FUNCTION; i++) {
if (strlen(rhs.functionSwitchNames[i]) > 0) {
node["switchNames"][std::to_string(i)]["val"] = rhs.functionSwitchNames[i];
}
int funcSwCnt = Boards::getCapability(board, Board::FunctionSwitches);
if (funcSwCnt) {
for (int i = 0; i < funcSwCnt; i += 1) {
int sw = Boards::getSwitchIndexForCFS(i);
std::string tag = Boards::getSwitchYamlName(sw, BoardJson::YLT_CONFIG).toStdString();
node["customSwitches"][tag] = rhs.customSwitches[i];
}
if (Boards::getCapability(board, Board::FunctionSwitchColors)) {
for (int i = 0; i < CPN_MAX_SWITCHES_FUNCTION; i += 1) {
node["functionSwitchLedONColor"][std::to_string(i)]["r"] = rhs.functionSwitchLedONColor[i].r;
node["functionSwitchLedONColor"][std::to_string(i)]["g"] = rhs.functionSwitchLedONColor[i].g;
node["functionSwitchLedONColor"][std::to_string(i)]["b"] = rhs.functionSwitchLedONColor[i].b;
}
for (int i = 0; i < CPN_MAX_SWITCHES_FUNCTION; i += 1) {
node["functionSwitchLedOFFColor"][std::to_string(i)]["r"] = rhs.functionSwitchLedOFFColor[i].r;
node["functionSwitchLedOFFColor"][std::to_string(i)]["g"] = rhs.functionSwitchLedOFFColor[i].g;
node["functionSwitchLedOFFColor"][std::to_string(i)]["b"] = rhs.functionSwitchLedOFFColor[i].b;
}
for (int i = 1; i < 4; i += 1) {
node["cfsGroupOn"][std::to_string(i)]["v"] = rhs.cfsGroupOn[i];
}
}
@@ -1351,16 +1391,13 @@ bool convert<ModelData>::decode(const Node& node, ModelData& rhs)
if (node["switchWarning"]) {
YamlSwitchWarning switchWarning;
rhs.switchWarningStates = switchWarning.toCpn(node["switchWarning"]);
rhs.switchWarningEnable = ~switchWarning.enabled;
}
else if (node["switchWarningState"]) { // depreciated
YamlSwitchWarningState switchWarningState;
node["switchWarningState"] >> switchWarningState.src_str;
rhs.switchWarningStates = switchWarningState.toCpn();
rhs.switchWarningEnable = ~switchWarningState.enabled;
} else {
rhs.switchWarningStates = 0;
rhs.switchWarningEnable = ~0;
}
node["thrTrimSw"] >> rhs.thrTrimSwitch;
@@ -1481,26 +1518,77 @@ bool convert<ModelData>::decode(const Node& node, ModelData& rhs)
node["modelRegistrationID"] >> rhs.registrationId;
node["hatsMode"] >> hatsModeLut >> rhs.hatsMode;
node["functionSwitchConfig"] >> rhs.functionSwitchConfig;
node["functionSwitchGroup"] >> rhs.functionSwitchGroup;
node["functionSwitchStartConfig"] >> rhs.functionSwitchStartConfig;
node["functionSwitchLogicalState"] >> rhs.functionSwitchLogicalState;
node["switchNames"] >> rhs.functionSwitchNames;
rhs.setDefaultFunctionSwitches(Boards::getCapability(board, Board::FunctionSwitches));
if (node["functionSwitchConfig"]) {
uint16_t v;
node["functionSwitchConfig"] >> v;
for (int i = 0; i < 6; i += 1) {
rhs.customSwitches[i].type = (Board::SwitchType)(v & 3);
v >>= 2;
}
}
if (node["functionSwitchGroup"]) {
uint16_t v;
node["functionSwitchGroup"] >> v;
for (int i = 0; i < 6; i += 1) {
rhs.customSwitches[i].group = v & 3;
v >>= 2;
}
for (int i = 0; i < 4; i += 1) {
rhs.cfsGroupOn[i] = v & 1;
v >>= 1;
}
}
if (node["functionSwitchStartConfig"]) {
uint16_t v;
node["functionSwitchStartConfig"] >> v;
for (int i = 0; i < 6; i += 1) {
rhs.customSwitches[i].start = v & 3;
v >>= 2;
}
}
if (node["functionSwitchLogicalState"]) {
uint16_t v;
node["functionSwitchLogicalState"] >> v;
for (int i = 0; i < 6; i += 1) {
rhs.customSwitches[i].state = v & 1;
v >>= 1;
}
}
if (node["switchNames"]) {
for (int i = 0; i < 6; i += 1) {
if (node["switchNames"][std::to_string(i)]) {
node["switchNames"][std::to_string(i)]["val"] >> rhs.customSwitches[i].name;
}
}
}
if (node["functionSwitchLedONColor"]) {
for (int i = 0; i < CPN_MAX_SWITCHES_FUNCTION; i += 1) {
for (int i = 0; i < 6; i += 1) {
if (node["functionSwitchLedONColor"][std::to_string(i)]) {
node["functionSwitchLedONColor"][std::to_string(i)]["r"] >> rhs.functionSwitchLedONColor[i].r;
node["functionSwitchLedONColor"][std::to_string(i)]["g"] >> rhs.functionSwitchLedONColor[i].g;
node["functionSwitchLedONColor"][std::to_string(i)]["b"] >> rhs.functionSwitchLedONColor[i].b;
node["functionSwitchLedONColor"][std::to_string(i)] >> rhs.customSwitches[i].onColor;
}
}
}
if (node["functionSwitchLedOFFColor"]) {
for (int i = 0; i < CPN_MAX_SWITCHES_FUNCTION; i += 1) {
for (int i = 0; i < 6; i += 1) {
if (node["functionSwitchLedOFFColor"][std::to_string(i)]) {
node["functionSwitchLedOFFColor"][std::to_string(i)]["r"] >> rhs.functionSwitchLedOFFColor[i].r;
node["functionSwitchLedOFFColor"][std::to_string(i)]["g"] >> rhs.functionSwitchLedOFFColor[i].g;
node["functionSwitchLedOFFColor"][std::to_string(i)]["b"] >> rhs.functionSwitchLedOFFColor[i].b;
node["functionSwitchLedOFFColor"][std::to_string(i)] >> rhs.customSwitches[i].offColor;
}
}
}
int funcSwCnt = Boards::getCapability(board, Board::FunctionSwitches);
if (node["customSwitches"]) {
for (int i = 0; i < funcSwCnt; i += 1) {
int sw = Boards::getSwitchIndexForCFS(i);
std::string tag = Boards::getSwitchYamlName(sw, BoardJson::YLT_CONFIG).toStdString();
node["customSwitches"][tag] >> rhs.customSwitches[i];
}
}
if (node["cfsGroupOn"]) {
for (int i = 1; i < 4; i += 1) {
if (node["cfsGroupOn"][std::to_string(i)]) {
node["cfsGroupOn"][std::to_string(i)]["v"] >> rhs.cfsGroupOn[i];
}
}
}
@@ -27,6 +27,11 @@
#include "boardjson.h"
const YamlLookupTable switchConfigLut = {
{Board::SWITCH_NOT_AVAILABLE, "NONE"},
{Board::SWITCH_TOGGLE, "TOGGLE"},
{Board::SWITCH_2POS, "2POS"},
{Board::SWITCH_3POS, "3POS"},
{Board::SWITCH_GLOBAL, "GLOBAL"},
{Board::SWITCH_NOT_AVAILABLE, "none"},
{Board::SWITCH_TOGGLE, "toggle"},
{Board::SWITCH_2POS, "2pos"},
@@ -117,8 +122,13 @@ YamlSwitchConfig::YamlSwitchConfig(const GeneralSettings::SwitchConfig* rhs)
for (int i = 0; i < Boards::getCapability(getCurrentBoard(), Board::Switches); i++) {
config[i].tag = Boards::getSwitchYamlName(i, BoardJson::YLT_CONFIG).toStdString();
config[i].type = rhs[i].type;
config[i].start = rhs[i].start;
memcpy(config[i].name, rhs[i].name, sizeof(HARDWARE_NAME_LEN));
// config[i].inverted = rhs[i].inverted;
config[i].onColorLuaOverride = rhs[i].onColorLuaOverride;
config[i].offColorLuaOverride = rhs[i].offColorLuaOverride;
config[i].onColor = rhs[i].onColor;
config[i].offColor = rhs[i].offColor;
}
}
@@ -128,7 +138,12 @@ void YamlSwitchConfig::copy(GeneralSettings::SwitchConfig* rhs) const
if (config[i].type != (unsigned int)Board::SWITCH_NOT_AVAILABLE) {
memcpy(rhs[i].name, config[i].name, sizeof(HARDWARE_NAME_LEN));
rhs[i].type = (Board::SwitchType)config[i].type;
rhs[i].start = config[i].start;
// rhs[i].inverted = config[i].inverted;
rhs[i].onColorLuaOverride = config[i].onColorLuaOverride;
rhs[i].offColorLuaOverride = config[i].offColorLuaOverride;
rhs[i].onColor = config[i].onColor;
rhs[i].offColor = config[i].offColor;
}
}
}
@@ -191,11 +206,32 @@ bool convert<InputConfig>::decode(const Node& node, InputConfig& rhs)
return true;
}
static const YamlLookupTable cfsSwitchStart = {
{ 0, "START_OFF" },
{ 1, "START_ON" },
{ 2, "START_PREVIOUS" },
};
static const YamlLookupTable cfsSwitchLuaOverride = {
{ 0, "OFF" },
{ 1, "ON" },
};
Node convert<SwitchConfig>::encode(const SwitchConfig& rhs)
{
Node node;
node["type"] = switchConfigLut << rhs.type;
node["name"] = rhs.name;
if (Boards::getCapability(getCurrentBoard(), Board::FunctionSwitches)) {
int idx = Boards::getSwitchYamlIndex(rhs.tag.c_str(), BoardJson::YLT_REF);
if (Boards::isSwitchFunc(idx, getCurrentBoard())) {
node["start"] = cfsSwitchStart << rhs.start;
node["onColorLuaOverride"] = cfsSwitchLuaOverride << rhs.onColorLuaOverride;
node["offColorLuaOverride"] = cfsSwitchLuaOverride << rhs.offColorLuaOverride;
node["onColor"] = rhs.onColor;
node["offColor"] = rhs.offColor;
}
}
// node["inv"] = (int)rhs.inverted; in hwdef json but not implemented in radio yaml
return node;
}
@@ -206,7 +242,12 @@ bool convert<SwitchConfig>::decode(const Node& node, SwitchConfig& rhs)
node["type"] >> switchConfigLut >> rhs.type;
node["name"] >> rhs.name;
node["start"] >> cfsSwitchStart >> rhs.start;
// node["inv"] >> rhs.inverted; in hwdef json but not implemented in radio yaml
node["onColorLuaOverride"] >> cfsSwitchLuaOverride >> rhs.onColorLuaOverride;
node["offColorLuaOverride"] >> cfsSwitchLuaOverride >> rhs.offColorLuaOverride;
node["onColor"] >> rhs.onColor;
node["offColor"] >> rhs.offColor;
if (radioSettingsVersion < SemanticVersion(QString(CPN_ADC_REFACTOR_VERSION))) {
int idx = Boards::getSwitchYamlIndex(rhs.tag.c_str(), BoardJson::YLT_CONFIG);
@@ -420,4 +461,21 @@ bool convert<YamlSwitchesFlex>::decode(const Node& node, YamlSwitchesFlex& rhs)
return true;
}
Node convert<RGBLedColor>::encode(const RGBLedColor& rhs)
{
Node node;
node["r"] = rhs.r;
node["g"] = rhs.g;
node["b"] = rhs.b;
return node;
}
bool convert<RGBLedColor>::decode(const Node& node, RGBLedColor& rhs)
{
node["r"] >> rhs.r;
node["g"] >> rhs.g;
node["b"] >> rhs.b;
return true;
}
} // namespace YAML
@@ -69,7 +69,12 @@ struct SwitchConfig {
std::string tag = std::string();
char name[HARDWARE_NAME_LEN + 1] = {'\0'};
unsigned int type = 0;
int start = 0;
bool inverted = false;
unsigned int onColorLuaOverride = false;
unsigned int offColorLuaOverride = false;
RGBLedColor onColor;
RGBLedColor offColor;
};
ENCODE_DECODE_CONFIG(SwitchConfig)
@@ -87,3 +92,12 @@ INPUT_SWITCH_CONFIG(YamlSliderConfig, CPN_MAX_INPUTS, InputConfig, InputConfig)
INPUT_SWITCH_CONFIG(YamlStickConfig, CPN_MAX_INPUTS, InputConfig, InputConfig)
INPUT_SWITCH_CONFIG(YamlSwitchConfig, CPN_MAX_SWITCHES, SwitchConfig, SwitchConfig)
INPUT_SWITCH_CONFIG(YamlSwitchesFlex, CPN_MAX_SWITCHES_FLEX, SwitchFlex, SwitchConfig)
namespace YAML
{
template <>
struct convert<RGBLedColor> {
static Node encode(const RGBLedColor& rhs);
static bool decode(const Node& node, RGBLedColor& rhs);
};
}
@@ -170,6 +170,11 @@ bool GeneralSettings::isSwitchFlex(int index) const
return Boards::isSwitchFlex(index);
}
bool GeneralSettings::isSwitchFunc(int index) const
{
return Boards::isSwitchFunc(index);
}
bool GeneralSettings::unassignedInputFlexSwitches() const
{
Board::Type board = getCurrentBoard();
@@ -372,6 +377,10 @@ void GeneralSettings::setDefaultControlTypes(Board::Type board)
switchConfig[i].type = info.dflt;
switchConfig[i].inverted = info.inverted;
switchConfig[i].inputIdx = SWITCH_INPUTINDEX_NONE;
if (Boards::isSwitchFunc(i)) {
switchConfig[i].start = ModelData::FUNC_SWITCH_START_PREVIOUS;
switchConfig[i].onColor.setColor(255, 255, 255);
}
}
for (uint8_t i = 0; i < 4; i++) {
+18
View File
@@ -61,6 +61,17 @@ enum UartModes {
UART_MODE_DEBUG
};
class RGBLedColor {
public:
RGBLedColor() { clear(); }
int r;
int g;
int b;
void clear() { memset(reinterpret_cast<void *>(this), 0, sizeof(RGBLedColor)); }
void setColor(int red, int green, int blue) { r = red; g = green; b = blue; }
QColor getQColor() { return QColor(r, g, b); }
};
class TrainerMix {
Q_DECLARE_TR_FUNCTIONS(TrainerMix)
@@ -362,6 +373,12 @@ class GeneralSettings {
Board::SwitchType type;
bool inverted;
int inputIdx; // used if switch tag = FLn, value -1 = none selected
// CFS settings
unsigned int start;
unsigned int onColorLuaOverride;
unsigned int offColorLuaOverride;
RGBLedColor onColor;
RGBLedColor offColor;
};
SwitchConfig switchConfig[CPN_MAX_SWITCHES];
@@ -377,6 +394,7 @@ class GeneralSettings {
bool isInputFlexSwitchAvailable(int index) const;
bool isSwitchAvailable(int index) const;
bool isSwitchFlex(int index) const;
bool isSwitchFunc(int index) const;
bool unassignedInputFlexSwitches() const;
QString antennaModeToString() const;
+51 -35
View File
@@ -223,8 +223,13 @@ void ModelData::setDefaultFunctionSwitches(int functionSwitchCount)
return;
for (int i = 0; i < functionSwitchCount; i++) {
setFuncSwitchConfig(i, Board::SWITCH_2POS);
setFuncSwitchGroup(i, 1);
customSwitches[i].type = Board::SWITCH_GLOBAL;
customSwitches[i].group = 0;
customSwitches[i].start = ModelData::FUNC_SWITCH_START_PREVIOUS;
customSwitches[i].state = 0;
customSwitches[i].name[0] = 0;
customSwitches[i].onColor.setColor(255, 255, 255);
customSwitches[i].offColor.setColor(0, 0, 0);
}
}
@@ -389,20 +394,18 @@ int ModelData::getChannelsMax(bool forceExtendedLimits) const
return 100;
}
bool ModelData::isFunctionSwitchPositionAvailable(int index) const
bool ModelData::isFunctionSwitchPositionAvailable(int swIndex, int swPos, const GeneralSettings * const gs) const
{
if (index == 0)
return true;
div_t qr = div(abs(index) - 1, 3);
int fs = getFuncSwitchConfig(qr.quot);
if (qr.rem == 1) {
if (swPos == 1)
return false;
}
else {
return fs != Board::SWITCH_NOT_AVAILABLE;
}
int fsindex = Boards::getCFSIndexForSwitch(swIndex);
int fs = getFuncSwitchConfig(fsindex);
if (fs == Board::SWITCH_GLOBAL)
return gs->switchConfig[swIndex].type != Board::SWITCH_NOT_AVAILABLE;
return true;
}
bool ModelData::isFunctionSwitchSourceAllowed(int index) const
@@ -1747,30 +1750,32 @@ AbstractStaticItemModel * ModelData::trainerModeItemModel(const GeneralSettings
return mdl;
}
unsigned int ModelData::getFuncSwitchConfig(unsigned int index) const
Board::SwitchType ModelData::getFuncSwitchConfig(unsigned int index) const
{
if (index < CPN_MAX_SWITCHES_FUNCTION)
return Helpers::getBitmappedValue(functionSwitchConfig, index, 2);
return customSwitches[index].type;
else
return FUNC_SWITCH_CONFIG_NONE;
return Board::SWITCH_NOT_AVAILABLE;
}
void ModelData::setFuncSwitchConfig(unsigned int index, unsigned int value)
void ModelData::setFuncSwitchConfig(unsigned int index, Board::SwitchType value)
{
if (index < CPN_MAX_SWITCHES_FUNCTION)
Helpers::setBitmappedValue(functionSwitchConfig, value, index, 2);
customSwitches[index].type = value;
}
// static
QString ModelData::funcSwitchConfigToString(unsigned int value)
{
switch (value) {
case FUNC_SWITCH_CONFIG_NONE:
case Board::SWITCH_NOT_AVAILABLE:
return tr("NONE");
case FUNC_SWITCH_CONFIG_TOGGLE:
case Board::SWITCH_TOGGLE:
return tr("TOGGLE");
case FUNC_SWITCH_CONFIG_2POS:
case Board::SWITCH_2POS:
return tr("2POS");
case Board::SWITCH_GLOBAL:
return tr("Global");
default:
return CPN_STR_UNKNOWN_ITEM;
}
@@ -1781,7 +1786,7 @@ AbstractStaticItemModel * ModelData::funcSwitchConfigItemModel()
{
AbstractStaticItemModel * mdl = new AbstractStaticItemModel();
mdl->setName(AIM_MODELDATA_FUNCSWITCHCONFIG);
for (unsigned int i = FUNC_SWITCH_CONFIG_FIRST; i <= FUNC_SWITCH_CONFIG_LAST; i++) {
for (unsigned int i = Board::SWITCH_NOT_AVAILABLE; i <= Board::SWITCH_GLOBAL; i++) {
mdl->appendToItemList(funcSwitchConfigToString(i), i);
}
mdl->loadItemList();
@@ -1809,7 +1814,7 @@ AbstractStaticItemModel * ModelData::funcSwitchGroupsModel()
mdl->setName(AIM_MODELDATA_FUNCSWITCHGROUPS);
mdl->appendToItemList(tr("---"), 0);
for (unsigned int i = 1; i <= 3; i += 1) {
for (unsigned int i = 1; i <= Boards::getCapability(getCurrentBoard(), Board::FunctionSwitchGroups); i += 1) {
mdl->appendToItemList(tr("Group ") + QString::number(i), i);
}
@@ -1820,7 +1825,7 @@ AbstractStaticItemModel * ModelData::funcSwitchGroupsModel()
unsigned int ModelData::getFuncSwitchGroup(unsigned int index) const
{
if (index < CPN_MAX_SWITCHES_FUNCTION)
return Helpers::getBitmappedValue(functionSwitchGroup, index, 2);
return customSwitches[index].group;
else
return 0;
}
@@ -1828,14 +1833,13 @@ unsigned int ModelData::getFuncSwitchGroup(unsigned int index) const
void ModelData::setFuncSwitchGroup(unsigned int index, unsigned int value)
{
if (index < CPN_MAX_SWITCHES_FUNCTION)
Helpers::setBitmappedValue(functionSwitchGroup, value, index, 2);
customSwitches[index].group = value;
}
unsigned int ModelData::getFuncSwitchAlwaysOnGroup(unsigned int group) const
{
if (group > 0 && group < 4) {
unsigned int switchcnt = Boards::getCapability(getCurrentFirmware()->getBoard(), Board::FunctionSwitches);
return Helpers::getBitmappedValue(functionSwitchGroup, group, 1, 2 * switchcnt);
if (group > 0 && group <= Boards::getCapability(getCurrentBoard(), Board::FunctionSwitchGroups)) {
return cfsGroupOn[group];
}
else
return 0;
@@ -1851,16 +1855,15 @@ unsigned int ModelData::getFuncSwitchAlwaysOnGroupForSwitch(unsigned int index)
void ModelData::setFuncSwitchAlwaysOnGroup(unsigned int group, unsigned int value)
{
if (group > 0 && group < 4) {
unsigned int switchcnt = Boards::getCapability(getCurrentFirmware()->getBoard(), Board::FunctionSwitches);
Helpers::setBitmappedValue(functionSwitchGroup, value, group, 1, 2 * switchcnt);
if (group > 0 && group <= Boards::getCapability(getCurrentBoard(), Board::FunctionSwitchGroups)) {
cfsGroupOn[group] = value;
}
}
unsigned int ModelData::getFuncSwitchStart(unsigned int index) const
{
if (index < CPN_MAX_SWITCHES_FUNCTION)
return Helpers::getBitmappedValue(functionSwitchStartConfig, index, 2);
return customSwitches[index].start;
else
return FUNC_SWITCH_START_OFF;
}
@@ -1868,7 +1871,7 @@ unsigned int ModelData::getFuncSwitchStart(unsigned int index) const
void ModelData::setFuncSwitchStart(unsigned int index, unsigned int value)
{
if (index < CPN_MAX_SWITCHES_FUNCTION)
Helpers::setBitmappedValue(functionSwitchStartConfig, value, index, 2);
customSwitches[index].start = value;
}
int ModelData::getFuncGroupSwitchCount(unsigned int group, int switchcnt) const
@@ -1916,7 +1919,7 @@ void ModelData::setGroupSwitchState(uint8_t group, int switchcnt)
if (getFuncSwitchAlwaysOnGroup(group)) {
for (int j = 0; j < switchcnt; j += 1) {
if (getFuncSwitchGroup(j) == group) {
setFuncSwitchConfig(j, FUNC_SWITCH_CONFIG_2POS); // Toggle not valid
setFuncSwitchConfig(j, Board::SWITCH_2POS); // Toggle not valid
}
}
if (getFuncGroupSwitchStart(group, switchcnt) == (unsigned int)switchcnt + 1) {
@@ -2026,3 +2029,16 @@ int ModelData::getInputLine(int index) const
return cnt;
}
const Board::SwitchType ModelData::getSwitchType(int sw, const GeneralSettings & gs) const
{
if (sw < 0 || sw >= Boards::getCapability(getCurrentBoard(), Board::Switches))
return Board::SWITCH_NOT_AVAILABLE;
if (Boards::isSwitchFunc(sw)) {
int fsIndex = Boards::getCFSIndexForSwitch(sw);
if (customSwitches[fsIndex].type != Board::SWITCH_GLOBAL)
return customSwitches[fsIndex].type;
}
return gs.switchConfig[sw].type;
}
+21 -27
View File
@@ -104,13 +104,19 @@ class USBJoystickChData {
void clear() { memset(reinterpret_cast<void *>(this), 0, sizeof(USBJoystickChData)); }
};
class RGBLedColor {
class customSwitch {
public:
RGBLedColor() { clear(); }
int r;
int g;
int b;
void clear() { memset(reinterpret_cast<void *>(this), 0, sizeof(RGBLedColor)); }
customSwitch() { clear(); }
Board::SwitchType type;
unsigned int group;
unsigned int start;
unsigned int state;
char name[HARDWARE_NAME_LEN + 1];
unsigned int onColorLuaOverride;
unsigned int offColorLuaOverride;
RGBLedColor onColor;
RGBLedColor offColor;
void clear() { memset(reinterpret_cast<void *>(this), 0, sizeof(customSwitch)); }
};
class ModelData {
@@ -171,7 +177,6 @@ class ModelData {
SwashRingData swashRingData;
unsigned int thrTraceSrc;
uint64_t switchWarningStates;
unsigned int switchWarningEnable;
unsigned int thrTrimSwitch;
unsigned int potsWarningMode;
bool potsWarnEnabled[CPN_MAX_INPUTS];
@@ -220,30 +225,17 @@ class ModelData {
unsigned int modelCustomScriptsDisabled;
unsigned int modelTelemetryDisabled;
enum FunctionSwitchConfig {
FUNC_SWITCH_CONFIG_NONE,
FUNC_SWITCH_CONFIG_FIRST = FUNC_SWITCH_CONFIG_NONE,
FUNC_SWITCH_CONFIG_TOGGLE,
FUNC_SWITCH_CONFIG_2POS,
FUNC_SWITCH_CONFIG_LAST = FUNC_SWITCH_CONFIG_2POS
};
enum FunctionSwitchStart {
FUNC_SWITCH_START_ON,
FUNC_SWITCH_START_FIRST = FUNC_SWITCH_START_ON,
FUNC_SWITCH_START_OFF,
FUNC_SWITCH_START_FIRST = FUNC_SWITCH_START_OFF,
FUNC_SWITCH_START_ON,
FUNC_SWITCH_START_PREVIOUS,
FUNC_SWITCH_START_LAST = FUNC_SWITCH_START_PREVIOUS
};
// Function switches
unsigned int functionSwitchConfig;
unsigned int functionSwitchGroup;
unsigned int functionSwitchStartConfig;
unsigned int functionSwitchLogicalState;
char functionSwitchNames[CPN_MAX_SWITCHES_FUNCTION][HARDWARE_NAME_LEN + 1];
RGBLedColor functionSwitchLedONColor[CPN_MAX_SWITCHES_FUNCTION];
RGBLedColor functionSwitchLedOFFColor[CPN_MAX_SWITCHES_FUNCTION];
customSwitch customSwitches[CPN_MAX_SWITCHES_FUNCTION];
unsigned int cfsGroupOn[4];
// Custom USB joytsick mapping
unsigned int usbJoystickExtMode;
@@ -287,9 +279,11 @@ class ModelData {
int getChannelsMax(bool forceExtendedLimits=false) const;
bool isAvailable(const RawSwitch & swtch) const;
bool isFunctionSwitchPositionAvailable(int index) const;
bool isFunctionSwitchPositionAvailable(int swIndex, int swPos, const GeneralSettings * const gs) const;
bool isFunctionSwitchSourceAllowed(int index) const;
const Board::SwitchType getSwitchType(int sw, const GeneralSettings & gs) const;
enum ReferenceUpdateAction {
REF_UPD_ACT_CLEAR,
REF_UPD_ACT_SHIFT,
@@ -344,8 +338,8 @@ class ModelData {
static QString trainerModeToString(const int value);
bool isTrainerModeAvailable(const GeneralSettings & generalSettings, const Firmware * firmware, const int value);
AbstractStaticItemModel * trainerModeItemModel(const GeneralSettings & generalSettings, const Firmware * firmware);
unsigned int getFuncSwitchConfig(unsigned int index) const;
void setFuncSwitchConfig(unsigned int index, unsigned int value);
Board::SwitchType getFuncSwitchConfig(unsigned int index) const;
void setFuncSwitchConfig(unsigned int index, Board::SwitchType value);
static QString funcSwitchConfigToString(unsigned int value);
static AbstractStaticItemModel * funcSwitchConfigItemModel();
static AbstractStaticItemModel * funcSwitchGroupStartSwitchModel(int switchcnt);
+1 -1
View File
@@ -203,7 +203,7 @@ QString RawSource::toString(const ModelData * model, const GeneralSettings * con
if (model) {
int fsindex = Boards::getSwitchTagNum(index - 1, board) - 1;
if (fsindex >= 0 && fsindex < CPN_MAX_SWITCHES_FUNCTION)
custName = QString(model->functionSwitchNames[fsindex]).trimmed();
custName = QString(model->customSwitches[fsindex].name).trimmed();
}
}
else {
+2 -3
View File
@@ -93,7 +93,7 @@ QString RawSwitch::toString(Board::Type board, const GeneralSettings * const gen
if (Boards::isSwitchFunc(qr.quot, board)) {
if (modelData) {
int fsindex = Boards::getSwitchTagNum(qr.quot, board) - 1;
custName = QString(modelData->functionSwitchNames[fsindex]).trimmed();
custName = QString(modelData->customSwitches[fsindex].name).trimmed();
}
}
else {
@@ -205,8 +205,7 @@ bool RawSwitch::isAvailable(const ModelData * const model, const GeneralSettings
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);
return model->isFunctionSwitchPositionAvailable(sw.quot, sw.rem, gs);
}
else
return model->isAvailable(*this);