From 56997f4d3cee7bedba59d4e37afb9d984d68747a Mon Sep 17 00:00:00 2001 From: Raphael Coeffic Date: Mon, 13 Dec 2021 09:59:40 +0100 Subject: [PATCH] Use definitions in Boards for pots/sliders config --- .../src/firmwares/edgetx/yaml_modeldata.cpp | 2 + .../src/firmwares/edgetx/yaml_rawswitch.cpp | 2 + .../firmwares/edgetx/yaml_switchconfig.cpp | 106 +++++++++++++++--- .../src/firmwares/edgetx/yaml_switchconfig.h | 87 ++++++++------ 4 files changed, 148 insertions(+), 49 deletions(-) diff --git a/companion/src/firmwares/edgetx/yaml_modeldata.cpp b/companion/src/firmwares/edgetx/yaml_modeldata.cpp index 4ebb6952d..ee983ccbc 100644 --- a/companion/src/firmwares/edgetx/yaml_modeldata.cpp +++ b/companion/src/firmwares/edgetx/yaml_modeldata.cpp @@ -403,8 +403,10 @@ Node convert::encode(const ModelData& rhs) node["toplcdTimer"] = rhs.toplcdTimer; + // B&W only // customScreens // topBarData + node["view"] = rhs.view; node["modelRegistrationID"] = rhs.registrationId; diff --git a/companion/src/firmwares/edgetx/yaml_rawswitch.cpp b/companion/src/firmwares/edgetx/yaml_rawswitch.cpp index 0b9762009..b215f2787 100644 --- a/companion/src/firmwares/edgetx/yaml_rawswitch.cpp +++ b/companion/src/firmwares/edgetx/yaml_rawswitch.cpp @@ -58,6 +58,7 @@ std::string YamlRawSwitchEncode(const RawSwitch& rhs) switch (rhs.type) { case SWITCH_TYPE_SWITCH: sw_str += 'S'; + // TODO: use lookup of switches in Boards ??? sw_str += 'A' + (sval - 1) / 3; sw_str += std::to_string((sval - 1) % 3); break; @@ -129,6 +130,7 @@ RawSwitch YamlRawSwitchDecode(const std::string& sw_str) } else if (val_len >= 3 && val[0] == 'S' && (val[1] >= 'A' && val[1] <= 'Z') && (val[2] >= '0' && val[2] <= '2')) { + // TODO: use lookup of switches in Boards ??? rhs = RawSwitch(SWITCH_TYPE_SWITCH, (val[1] - 'A') * 3 + val[2] - '0'); } else { // TODO: check if we have a match? diff --git a/companion/src/firmwares/edgetx/yaml_switchconfig.cpp b/companion/src/firmwares/edgetx/yaml_switchconfig.cpp index f10b21a80..70218dc03 100644 --- a/companion/src/firmwares/edgetx/yaml_switchconfig.cpp +++ b/companion/src/firmwares/edgetx/yaml_switchconfig.cpp @@ -19,19 +19,9 @@ */ #include "yaml_switchconfig.h" + #include "boards.h" - -const YamlLookupTable sticksLut = { - {0, "0"}, - {1, "1"}, - {2, "2"}, - {3, "3"}, -}; - -const YamlLookupTable switchesLut = { - {0, "SA"}, {1, "SB"}, {2, "SC"}, {3, "SD"}, {4, "SE"}, - {5, "SF"}, {6, "SG"}, {7, "SH"}, {8, "SI"}, {9, "SJ"}, -}; +#include "eeprominterface.h" const YamlLookupTable switchConfigLut = { {Board::SWITCH_NOT_AVAILABLE, "none"}, @@ -40,10 +30,6 @@ const YamlLookupTable switchConfigLut = { {Board::SWITCH_3POS, "3pos"}, }; -const YamlLookupTable potsLut = { - {0, "S1"}, {1, "6POS"}, {2, "S2"}, - {3, "EXT1"}, {4, "EXT2"}, {5, "EXT3"}, {6, "EXT4"}, -}; const YamlLookupTable potConfigLut = { {Board::POT_NONE, "none"}, @@ -62,3 +48,91 @@ const YamlLookupTable sliderConfigLut = { {Board::SLIDER_WITH_DETENT, "with_detent"}, }; +int YamlStickLookup::name2idx(const std::string& name) +{ + int idx = std::stoi(name); + if (idx < 4) { + return idx; + } + + return -1; +} + +std::string YamlStickLookup::idx2name(unsigned int idx) +{ + if (idx < 4) { + return std::to_string(idx); + } + + return std::string(); +} + +int YamlSwitchLookup::name2idx(const std::string& name) +{ + // TODO: replace this with a real lookup of available switches + if (name.size() == 2 + && name[0] == 'S' + && (name[1] >= 'A' && name[1] <= 'J')) { + + return name[1] - 'A'; + } + + return -1; +} + +std::string YamlSwitchLookup::idx2name(unsigned int idx) +{ + // TODO: replace this with a real lookup of available switches + if (idx < 10) { + return std::string("S") + (char)('A' + idx); + } + + return std::string(); +} + +int YamlPotLookup::name2idx(const std::string& name) +{ + auto fw = getCurrentFirmware(); + int idx = fw->getAnalogInputIndex(name.c_str()); + if (idx < 0) return idx; + + int pots = Boards::getCapability(fw->getBoard(), Board::Pots); + if (idx >= pots) return -1; + + return idx; +} + +std::string YamlPotLookup::idx2name(unsigned int idx) +{ + auto fw = getCurrentFirmware(); + int pots = Boards::getCapability(fw->getBoard(), Board::Pots); + if (idx >= pots) return std::string(); + + return fw->getAnalogInputTag(idx); +} + +int YamlSliderLookup::name2idx(const std::string& name) +{ + auto fw = getCurrentFirmware(); + int idx = fw->getAnalogInputIndex(name.c_str()); + if (idx < 0) return idx; + + int pots = Boards::getCapability(fw->getBoard(), Board::Pots); + if (idx < pots) return -1; + idx -= pots; + + int sliders = Boards::getCapability(fw->getBoard(), Board::Sliders); + if (idx >= sliders) return -1; + + return idx; +} + +std::string YamlSliderLookup::idx2name(unsigned int idx) +{ + auto fw = getCurrentFirmware(); + int pots = Boards::getCapability(fw->getBoard(), Board::Pots); + int sliders = Boards::getCapability(fw->getBoard(), Board::Sliders); + if (idx >= sliders) return std::string(); + + return fw->getAnalogInputTag(idx + pots); +} diff --git a/companion/src/firmwares/edgetx/yaml_switchconfig.h b/companion/src/firmwares/edgetx/yaml_switchconfig.h index fbdd5b8b6..37e3e5fd3 100644 --- a/companion/src/firmwares/edgetx/yaml_switchconfig.h +++ b/companion/src/firmwares/edgetx/yaml_switchconfig.h @@ -24,7 +24,12 @@ #include "generalsettings.h" #include -template +struct YamlStickLookup { + static int name2idx(const std::string& name); + static std::string idx2name(unsigned int idx); +}; + +template struct YamlNameConfig { char name[N][HARDWARE_NAME_LEN + 1]; @@ -41,19 +46,17 @@ struct YamlNameConfig { } }; -extern const YamlLookupTable sticksLut; -typedef YamlNameConfig YamlStickConfig; +typedef YamlNameConfig YamlStickConfig; -template -struct YamlKnobConfig : public YamlNameConfig { +template +struct YamlKnobConfig : public YamlNameConfig { unsigned int config[N]; YamlKnobConfig() { memset(config, 0, sizeof(config)); } YamlKnobConfig(const char in_names[N][HARDWARE_NAME_LEN + 1], const unsigned int in_configs[N]) : - YamlNameConfig(in_names) + YamlNameConfig(in_names) { memcpy(config, in_configs, sizeof(config)); } @@ -61,7 +64,7 @@ struct YamlKnobConfig : public YamlNameConfig { void copy(char out_names[N][HARDWARE_NAME_LEN + 1], unsigned int out_configs[N]) { - YamlNameConfig::copy(out_names); + YamlNameConfig::copy(out_names); memcpy(out_configs, config, sizeof(config)); } }; @@ -69,71 +72,89 @@ struct YamlKnobConfig : public YamlNameConfig { // SA: // type: 3pos // name: -extern const YamlLookupTable switchesLut; +struct YamlSwitchLookup { + static int name2idx(const std::string& name); + static std::string idx2name(unsigned int idx); +}; extern const YamlLookupTable switchConfigLut; -typedef YamlKnobConfig +typedef YamlKnobConfig YamlSwitchConfig; // S1: // type: with_detent // name: -extern const YamlLookupTable potsLut; +struct YamlPotLookup { + static int name2idx(const std::string& name); + static std::string idx2name(unsigned int idx); +}; extern const YamlLookupTable potConfigLut; -typedef YamlKnobConfig YamlPotConfig; +typedef YamlKnobConfig YamlPotConfig; // S1: // type: with_detent // name: -extern const YamlLookupTable slidersLut; +struct YamlSliderLookup { + static int name2idx(const std::string& name); + static std::string idx2name(unsigned int idx); +}; extern const YamlLookupTable sliderConfigLut; -typedef YamlKnobConfig +typedef YamlKnobConfig YamlSliderConfig; namespace YAML { -template -struct convert > { - static Node encode(const YamlNameConfig& rhs) +template +struct convert > { + static Node encode(const YamlNameConfig& rhs) { Node node; - for (const auto& kv : name_lut) { - node[kv.second]["name"] = rhs.name[kv.first]; + for (unsigned int i=0; i& rhs) + static bool decode(const Node& node, YamlNameConfig& rhs) { if (!node.IsMap()) return false; int idx = 0; for (const auto& kv : node) { - kv.first >> name_lut >> idx; - kv.second["name"] >> rhs.name[idx]; + idx = name_lookup::name2idx(kv.first.Scalar()); + if (idx >= 0) { + kv.second["name"] >> rhs.name[idx]; + } } return true; } }; -template -struct convert > { - static Node encode(const YamlKnobConfig& rhs) +template +struct convert > { + static Node encode(const YamlKnobConfig& rhs) { Node node; - for (const auto& kv : name_lut) { - node[kv.second]["type"] = cfg_lut << rhs.config[kv.first]; - node[kv.second]["name"] = rhs.name[kv.first]; + for (unsigned int i=0; i& rhs) + YamlKnobConfig& rhs) { if (!node.IsMap()) return false; int idx = 0; for (const auto& kv : node) { - kv.first >> name_lut >> idx; - kv.second["type"] >> cfg_lut >> rhs.config[idx]; - kv.second["name"] >> rhs.name[idx]; + idx = name_lookup::name2idx(kv.first.Scalar()); + if (idx >= 0) { + kv.second["type"] >> cfg_lut >> rhs.config[idx]; + kv.second["name"] >> rhs.name[idx]; + } } return true; }