From 19a9259d9672caa733ca65298e6166e4e44f808f Mon Sep 17 00:00:00 2001 From: elecpower Date: Thu, 16 Dec 2021 23:04:54 +1100 Subject: [PATCH] Move trim sources to boards --- companion/src/firmwares/boards.cpp | 21 +++++++++++++++ companion/src/firmwares/boards.h | 1 + .../src/firmwares/edgetx/yaml_rawsource.cpp | 18 +++---------- companion/src/firmwares/eeprominterface.cpp | 27 +++++++++++++++++++ companion/src/firmwares/eeprominterface.h | 9 +++++++ 5 files changed, 62 insertions(+), 14 deletions(-) diff --git a/companion/src/firmwares/boards.cpp b/companion/src/firmwares/boards.cpp index 3c790fd7a..4332f32d8 100644 --- a/companion/src/firmwares/boards.cpp +++ b/companion/src/firmwares/boards.cpp @@ -784,3 +784,24 @@ StringTagMappingTable Boards::getTrimSwitchesLookupTable(Board::Type board) return tbl; } + +// static +StringTagMappingTable Boards::getTrimSourcesLookupTable(Board::Type board) +{ + StringTagMappingTable tbl; + + tbl.insert(tbl.end(), { + {std::to_string(TRIM_AXIS_LH), "TrimRud"}, + {std::to_string(TRIM_AXIS_LV), "TrimEle"}, + {std::to_string(TRIM_AXIS_RV), "TrimThr"}, + {std::to_string(TRIM_AXIS_RH), "TrimAil"}, + }); + + if (getCapability(board, Board::NumTrims) > 4) + tbl.insert(tbl.end(), { + {std::to_string(TRIM_AXIS_T5), "TrimT5"}, + {std::to_string(TRIM_AXIS_T6), "TrimT6"}, + }); + + return tbl; +} diff --git a/companion/src/firmwares/boards.h b/companion/src/firmwares/boards.h index 1cf1fb353..f3a836963 100644 --- a/companion/src/firmwares/boards.h +++ b/companion/src/firmwares/boards.h @@ -215,6 +215,7 @@ class Boards static AbstractStaticItemModel * sliderTypeItemModel(); static AbstractStaticItemModel * switchTypeItemModel(); static StringTagMappingTable getTrimSwitchesLookupTable(Board::Type board); + static StringTagMappingTable getTrimSourcesLookupTable(Board::Type board); protected: diff --git a/companion/src/firmwares/edgetx/yaml_rawsource.cpp b/companion/src/firmwares/edgetx/yaml_rawsource.cpp index 21dccdf26..c8f02d794 100644 --- a/companion/src/firmwares/edgetx/yaml_rawsource.cpp +++ b/companion/src/firmwares/edgetx/yaml_rawsource.cpp @@ -21,16 +21,6 @@ #include "yaml_rawsource.h" #include "eeprominterface.h" -// SOURCE_TYPE_TRIM -const YamlLookupTable trimSourceLut = { - { 0, "TrimRud" }, - { 1, "TrimEle" }, - { 2, "TrimThr" }, - { 3, "TrimAil" }, - { 4, "TrimT5" }, - { 5, "TrimT6" }, -}; - std::string YamlRawSourceEncode(const RawSource& rhs) { std::string src_str; @@ -49,7 +39,7 @@ std::string YamlRawSourceEncode(const RawSource& rhs) src_str = getCurrentFirmware()->getAnalogInputTag(rhs.index); break; case SOURCE_TYPE_TRIM: - src_str = YAML::LookupValue(trimSourceLut, rhs.index); + src_str = getCurrentFirmware()->getTrimSourcesTag(rhs.index); break; case SOURCE_TYPE_MAX: src_str += "MAX"; @@ -220,10 +210,10 @@ RawSource YamlRawSourceDecode(const std::string& src_str) rhs.index = ana_idx; } - YAML::Node conv = node >> trimSourceLut; - if (conv.IsScalar()) { + int trm_idx = getCurrentFirmware()->getTrimSourcesIndex(src_str.c_str()); + if (trm_idx >= 0) { rhs.type = SOURCE_TYPE_TRIM; - rhs.index = conv.as(); + rhs.index = trm_idx; } int cyc_idx = getCurrentFirmware()->getRawSourceCyclicIndex(src_str.c_str()); diff --git a/companion/src/firmwares/eeprominterface.cpp b/companion/src/firmwares/eeprominterface.cpp index 56d039bbc..4cd5f11fe 100644 --- a/companion/src/firmwares/eeprominterface.cpp +++ b/companion/src/firmwares/eeprominterface.cpp @@ -257,6 +257,33 @@ std::string Firmware::getTrimSwitchesTag(unsigned int index) return std::string(); } + +int Firmware::getTrimSourcesIndex(const char * tag) +{ + const StringTagMappingTable& lut = trimSourcesLookupTable; + + auto it = + find_if(lut.begin(), lut.end(), [=](const StringTagMapping& elmt) { + if (elmt.tag == tag) return true; + return false; + }); + + if (it != lut.end()) { + return it - lut.begin(); + } + + return -1; +} + +std::string Firmware::getTrimSourcesTag(unsigned int index) +{ + const StringTagMappingTable& lut = trimSourcesLookupTable; + if (index < lut.size()) + return lut[index].tag; + + return std::string(); +} + int Firmware::getRawSwitchTypesIndex(const char * tag) { const StringTagMappingTable& lut = rawSwitchTypesLookupTable; diff --git a/companion/src/firmwares/eeprominterface.h b/companion/src/firmwares/eeprominterface.h index c61d921a5..9c9205e96 100644 --- a/companion/src/firmwares/eeprominterface.h +++ b/companion/src/firmwares/eeprominterface.h @@ -294,6 +294,7 @@ class Firmware analogInputNamesLookupTable(Boards::getAnalogNamesLookupTable(board)), switchesLookupTable(Boards::getSwitchesLookupTable(board)), trimSwitchesLookupTable(Boards::getTrimSwitchesLookupTable(board)), + trimSourcesLookupTable(Boards::getTrimSourcesLookupTable(board)), rawSwitchTypesLookupTable(RawSwitch::getRawSwitchTypesLookupTable()), rawSourceSpecialTypesLookupTable(RawSource::getSpecialTypesLookupTable()), rawSourceCyclicLookupTable(RawSource::getCyclicLookupTable()) @@ -420,6 +421,11 @@ class Firmware return &trimSwitchesLookupTable; } + const StringTagMappingTable* getTrimSourcesLookupTable() + { + return &trimSourcesLookupTable; + } + const StringTagMappingTable* getRawSwitchTypesLookupTable() { return &rawSwitchTypesLookupTable; @@ -441,6 +447,8 @@ class Firmware std::string getSwitchesTag(unsigned int index); int getTrimSwitchesIndex(const char * tag); std::string getTrimSwitchesTag(unsigned int index); + int getTrimSourcesIndex(const char * tag); + std::string getTrimSourcesTag(unsigned int index); int getRawSwitchTypesIndex(const char * tag); std::string getRawSwitchTypesTag(unsigned int index); int getRawSourceSpecialTypesIndex(const char * tag); @@ -458,6 +466,7 @@ class Firmware const StringTagMappingTable analogInputNamesLookupTable; const StringTagMappingTable switchesLookupTable; const StringTagMappingTable trimSwitchesLookupTable; + const StringTagMappingTable trimSourcesLookupTable; const StringTagMappingTable rawSwitchTypesLookupTable; const StringTagMappingTable rawSourceSpecialTypesLookupTable; const StringTagMappingTable rawSourceCyclicLookupTable;