From 4c92f9dbc95cdb0173f72acdd0f39330f9c2b017 Mon Sep 17 00:00:00 2001 From: elecpower Date: Thu, 16 Dec 2021 22:31:58 +1100 Subject: [PATCH] Move special source lookup to firmware --- .../src/firmwares/edgetx/yaml_rawsource.cpp | 21 +++------------ companion/src/firmwares/eeprominterface.cpp | 26 +++++++++++++++++++ companion/src/firmwares/eeprominterface.h | 11 +++++++- companion/src/firmwares/rawsource.cpp | 21 +++++++++++++++ companion/src/firmwares/rawsource.h | 2 ++ 5 files changed, 63 insertions(+), 18 deletions(-) diff --git a/companion/src/firmwares/edgetx/yaml_rawsource.cpp b/companion/src/firmwares/edgetx/yaml_rawsource.cpp index df72ec4fd..ba7ca7d10 100644 --- a/companion/src/firmwares/edgetx/yaml_rawsource.cpp +++ b/companion/src/firmwares/edgetx/yaml_rawsource.cpp @@ -41,19 +41,6 @@ const YamlLookupTable cycSourceLut = { // SOURCE_TYPE_MAX // { MIXSRC_MAX, "MAX" }, -const YamlLookupTable specialSourceLut = { - { 0, "TX_VOLTAGE" }, - { 1, "TX_TIME" }, - { 2, "TX_GPS" }, - { 3, "RESERVED1" }, - { 4, "RESERVED2" }, - { 5, "RESERVED3" }, - { 6, "RESERVED4" }, - { 7, "TIMER1" }, - { 8, "TIMER2" }, - { 9, "TIMER3" }, -}; - std::string YamlRawSourceEncode(const RawSource& rhs) { std::string src_str; @@ -105,7 +92,7 @@ std::string YamlRawSourceEncode(const RawSource& rhs) src_str += ")"; break; case SOURCE_TYPE_SPECIAL: - src_str = YAML::LookupValue(specialSourceLut, rhs.index); + src_str = getCurrentFirmware()->getRawSourceSpecialTypesTag(rhs.index); break; case SOURCE_TYPE_TELEMETRY: src_str = "tele("; @@ -255,10 +242,10 @@ RawSource YamlRawSourceDecode(const std::string& src_str) rhs.index = conv.as(); } - conv = node >> specialSourceLut; - if (conv.IsScalar()) { + int sp_idx = getCurrentFirmware()->getRawSourceSpecialTypesIndex(src_str.c_str()); + if (sp_idx >= 0) { rhs.type = SOURCE_TYPE_SPECIAL; - rhs.index = conv.as(); + rhs.index = sp_idx; } if (node.IsScalar() && node.as() == "MAX") { diff --git a/companion/src/firmwares/eeprominterface.cpp b/companion/src/firmwares/eeprominterface.cpp index e7f36875b..458a69dff 100644 --- a/companion/src/firmwares/eeprominterface.cpp +++ b/companion/src/firmwares/eeprominterface.cpp @@ -282,3 +282,29 @@ std::string Firmware::getRawSwitchTypesTag(unsigned int index) return std::string(); } + +int Firmware::getRawSourceSpecialTypesIndex(const char * tag) +{ + const StringTagMappingTable& lut = rawSourceSpecialTypesLookupTable; + + 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::getRawSourceSpecialTypesTag(unsigned int index) +{ + const StringTagMappingTable& lut = rawSourceSpecialTypesLookupTable; + 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 2632224d9..049e0ba12 100644 --- a/companion/src/firmwares/eeprominterface.h +++ b/companion/src/firmwares/eeprominterface.h @@ -294,7 +294,8 @@ class Firmware analogInputNamesLookupTable(Boards::getAnalogNamesLookupTable(board)), switchesLookupTable(Boards::getSwitchesLookupTable(board)), trimSwitchesLookupTable(Boards::getTrimSwitchesLookupTable(board)), - rawSwitchTypesLookupTable(RawSwitch::getRawSwitchTypesLookupTable()) + rawSwitchTypesLookupTable(RawSwitch::getRawSwitchTypesLookupTable()), + rawSourceSpecialTypesLookupTable(RawSource::getSpecialTypesLookupTable()) { } @@ -423,6 +424,11 @@ class Firmware return &rawSwitchTypesLookupTable; } + const StringTagMappingTable* getRawSourceSpecialTypesLookupTable() + { + return &rawSourceSpecialTypesLookupTable; + } + int getAnalogInputIndex(const char * tag); std::string getAnalogInputTag(unsigned int index); int getSwitchesIndex(const char * tag); @@ -431,6 +437,8 @@ class Firmware std::string getTrimSwitchesTag(unsigned int index); int getRawSwitchTypesIndex(const char * tag); std::string getRawSwitchTypesTag(unsigned int index); + int getRawSourceSpecialTypesIndex(const char * tag); + std::string getRawSourceSpecialTypesTag(unsigned int index); protected: QString id; @@ -443,6 +451,7 @@ class Firmware const StringTagMappingTable switchesLookupTable; const StringTagMappingTable trimSwitchesLookupTable; const StringTagMappingTable rawSwitchTypesLookupTable; + const StringTagMappingTable rawSourceSpecialTypesLookupTable; QList languages; //QList ttslanguages; OptionsList opts; diff --git a/companion/src/firmwares/rawsource.cpp b/companion/src/firmwares/rawsource.cpp index 36844fab0..030ec3753 100644 --- a/companion/src/firmwares/rawsource.cpp +++ b/companion/src/firmwares/rawsource.cpp @@ -418,3 +418,24 @@ QStringList RawSource::getSwitchList(Boards board) const } return ret; } + +// static +StringTagMappingTable RawSource::getSpecialTypesLookupTable() +{ + StringTagMappingTable tbl; + +tbl.insert(tbl.end(), { + {std::to_string(SOURCE_TYPE_SPECIAL_TX_BATT), "TX_VOLTAGE"}, + {std::to_string(SOURCE_TYPE_SPECIAL_TX_TIME), "TX_TIME"}, + {std::to_string(SOURCE_TYPE_SPECIAL_TX_GPS), "TX_GPS"}, + {std::to_string(SOURCE_TYPE_SPECIAL_RESERVED1), "RESERVED1"}, + {std::to_string(SOURCE_TYPE_SPECIAL_RESERVED2), "RESERVED2"}, + {std::to_string(SOURCE_TYPE_SPECIAL_RESERVED3), "RESERVED3"}, + {std::to_string(SOURCE_TYPE_SPECIAL_RESERVED4), "RESERVED4"}, + {std::to_string(SOURCE_TYPE_SPECIAL_TIMER1), "TIMER1"}, + {std::to_string(SOURCE_TYPE_SPECIAL_TIMER2), "TIMER2"}, + {std::to_string(SOURCE_TYPE_SPECIAL_TIMER3), "TIMER3"}, + }); + + return tbl; +} diff --git a/companion/src/firmwares/rawsource.h b/companion/src/firmwares/rawsource.h index f5fbfbcd5..88339d514 100644 --- a/companion/src/firmwares/rawsource.h +++ b/companion/src/firmwares/rawsource.h @@ -23,6 +23,7 @@ #include "boards.h" #include "constants.h" +#include "datahelpers.h" #include @@ -274,6 +275,7 @@ class RawSource { void clear() { type = SOURCE_TYPE_NONE; index = 0; } QStringList getStickList(Boards board) const; QStringList getSwitchList(Boards board) const; + static StringTagMappingTable getSpecialTypesLookupTable(); bool operator == ( const RawSource & other) const { return (this->type == other.type) && (this->index == other.index);