diff --git a/companion/src/firmwares/boards.cpp b/companion/src/firmwares/boards.cpp index 2c85e2a88..3c790fd7a 100644 --- a/companion/src/firmwares/boards.cpp +++ b/companion/src/firmwares/boards.cpp @@ -757,3 +757,30 @@ StringTagMappingTable Boards::getSwitchesLookupTable(Board::Type board) return tbl; } + +// static +StringTagMappingTable Boards::getTrimSwitchesLookupTable(Board::Type board) +{ + StringTagMappingTable tbl; + + tbl.insert(tbl.end(), { + {std::to_string(TRIM_SW_LH_DEC), "TrimRudLeft"}, + {std::to_string(TRIM_SW_LH_INC), "TrimRudRight"}, + {std::to_string(TRIM_SW_LV_DEC), "TrimEleDown"}, + {std::to_string(TRIM_SW_LV_INC), "TrimEleUp"}, + {std::to_string(TRIM_SW_RV_DEC), "TrimThrDown"}, + {std::to_string(TRIM_SW_RV_INC), "TrimThrUp"}, + {std::to_string(TRIM_SW_RH_DEC), "TrimAilLeft"}, + {std::to_string(TRIM_SW_RH_INC), "TrimAilRight"}, + }); + + if (getCapability(board, Board::NumTrims) > 4) + tbl.insert(tbl.end(), { + {std::to_string(TRIM_SW_T5_DEC), "TrimT5Down"}, + {std::to_string(TRIM_SW_T5_INC), "TrimT5Up"}, + {std::to_string(TRIM_SW_T6_DEC), "TrimT6Down"}, + {std::to_string(TRIM_SW_T6_INC), "TrimT6Up"}, + }); + + return tbl; +} diff --git a/companion/src/firmwares/boards.h b/companion/src/firmwares/boards.h index 5188cb655..1cf1fb353 100644 --- a/companion/src/firmwares/boards.h +++ b/companion/src/firmwares/boards.h @@ -214,6 +214,7 @@ class Boards static AbstractStaticItemModel * potTypeItemModel(); static AbstractStaticItemModel * sliderTypeItemModel(); static AbstractStaticItemModel * switchTypeItemModel(); + static StringTagMappingTable getTrimSwitchesLookupTable(Board::Type board); protected: diff --git a/companion/src/firmwares/edgetx/yaml_rawswitch.cpp b/companion/src/firmwares/edgetx/yaml_rawswitch.cpp index 21b78a197..61c3ca463 100644 --- a/companion/src/firmwares/edgetx/yaml_rawswitch.cpp +++ b/companion/src/firmwares/edgetx/yaml_rawswitch.cpp @@ -21,22 +21,6 @@ #include "yaml_rawswitch.h" #include "eeprominterface.h" -// TODO: use lookup of trims in Boards ??? -const YamlLookupTable trimSwitchLut = { - { 0, "TrimRudLeft" }, - { 1, "TrimRudRight" }, - { 2, "TrimEleDown" }, - { 3, "TrimEleUp" }, - { 4, "TrimThrDown" }, - { 5, "TrimThrUp" }, - { 6, "TrimAilLeft" }, - { 7, "TrimAilRight" }, - { 8, "TrimT5Down" }, - { 9, "TrimT5Up" }, - { 10, "TrimT6Down" }, - { 11, "TrimT6Up" }, -}; - const YamlLookupTable switchTypeLut = { { SWITCH_TYPE_NONE, "NONE" }, { SWITCH_TYPE_ON, "ON" }, @@ -75,8 +59,7 @@ std::string YamlRawSwitchEncode(const RawSwitch& rhs) break; case SWITCH_TYPE_TRIM: - // TODO: use lookup of trims in Boards ??? - sw_str += YAML::LookupValue(trimSwitchLut, sval); + sw_str += getCurrentFirmware()->getTrimSwitchesTag(sval); break; case SWITCH_TYPE_FLIGHT_MODE: @@ -134,10 +117,12 @@ RawSwitch YamlRawSwitchDecode(const std::string& sw_str) std::stoi(sw_str.substr(1, val_len - 1)) - 1); } - } else if (sw_str.substr(0, 3) == std::string("Trim")) { - // TODO: use lookup of trims in Boards ??? - rhs.type = SWITCH_TYPE_TRIM; - YAML::Node(sw_str) >> trimSwitchLut >> rhs.index; + } else if (sw_str.substr(0, 4) == std::string("Trim")) { + int tsw_idx = getCurrentFirmware()->getTrimSwitchesIndex(sw_str.c_str()); + if (tsw_idx >= 0) { + rhs.type = SWITCH_TYPE_TRIM; + rhs.index = tsw_idx; + } } else if (val_len >= 3 && val[0] == 'S' && (val[1] >= 'A' && val[1] <= 'Z') && diff --git a/companion/src/firmwares/eeprominterface.cpp b/companion/src/firmwares/eeprominterface.cpp index 9e87097ac..4fcef3f1d 100644 --- a/companion/src/firmwares/eeprominterface.cpp +++ b/companion/src/firmwares/eeprominterface.cpp @@ -230,3 +230,29 @@ std::string Firmware::getSwitchesTag(unsigned int index) return std::string(); } + +int Firmware::getTrimSwitchesIndex(const char * tag) +{ + const StringTagMappingTable& lut = trimSwitchesLookupTable; + + 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::getTrimSwitchesTag(unsigned int index) +{ + const StringTagMappingTable& lut = trimSwitchesLookupTable; + if (index < lut.size()) + return lut[index].tag; + + return std::string(); +} diff --git a/companion/src/firmwares/eeprominterface.h b/companion/src/firmwares/eeprominterface.h index cdd8370f6..b2bdf4c61 100644 --- a/companion/src/firmwares/eeprominterface.h +++ b/companion/src/firmwares/eeprominterface.h @@ -292,7 +292,8 @@ class Firmware base(base), eepromInterface(nullptr), analogInputNamesLookupTable(Boards::getAnalogNamesLookupTable(board)), - switchesLookupTable(Boards::getSwitchesLookupTable(board)) + switchesLookupTable(Boards::getSwitchesLookupTable(board)), + trimSwitchesLookupTable(Boards::getTrimSwitchesLookupTable(board)) { } @@ -411,10 +412,17 @@ class Firmware return &switchesLookupTable; } + const StringTagMappingTable* getTrimSwitchesLookupTable() + { + return &trimSwitchesLookupTable; + } + int getAnalogInputIndex(const char * tag); std::string getAnalogInputTag(unsigned int index); int getSwitchesIndex(const char * tag); std::string getSwitchesTag(unsigned int index); + int getTrimSwitchesIndex(const char * tag); + std::string getTrimSwitchesTag(unsigned int index); protected: QString id; @@ -425,6 +433,7 @@ class Firmware EEPROMInterface * eepromInterface; const StringTagMappingTable analogInputNamesLookupTable; const StringTagMappingTable switchesLookupTable; + const StringTagMappingTable trimSwitchesLookupTable; QList languages; //QList ttslanguages; OptionsList opts;