1
0
Fork 0
mirror of https://github.com/EdgeTX/edgetx.git synced 2025-07-25 17:25:10 +03:00

String tag mapping housekeeping

This commit is contained in:
elecpower 2021-12-17 16:46:40 +11:00 committed by Raphael Coeffic
parent 19a9259d96
commit 340b779c8b
5 changed files with 48 additions and 229 deletions

View file

@ -68,3 +68,26 @@ QString DataHelpers::timeToString(const int value, const unsigned int mask)
result.append(QString("%1:%2").arg(minutes, 2, 10, QLatin1Char('0')).arg(seconds, 2, 10, QLatin1Char('0')));
return result;
}
int DataHelpers::getStringTagMappingIndex(const StringTagMappingTable& lut, const char * tag)
{
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 DataHelpers::getStringTagMappingTag(const StringTagMappingTable& lut, unsigned int index)
{
if (index < lut.size())
return lut[index].tag;
return std::string();
}

View file

@ -49,6 +49,19 @@ struct StringTagMapping {
typedef std::vector<StringTagMapping> StringTagMappingTable;
#define STRINGTAGMAPPINGFUNCS_HELPER(tbl, name, index, tag) \
inline int name##index (const char * tag) \
{ \
return DataHelpers::getStringTagMappingIndex(tbl, tag); \
} \
\
inline std::string name##tag (unsigned int index) \
{ \
return DataHelpers::getStringTagMappingTag(tbl, index); \
}
#define STRINGTAGMAPPINGFUNCS(tbl, name) STRINGTAGMAPPINGFUNCS_HELPER(tbl, get##name, Index, Tag)
class FieldRange
{
public:
@ -92,5 +105,6 @@ namespace DataHelpers
QString boolToString(const bool value, const BoolFormat format);
QString getElementName(const QString & prefix, const unsigned int index, const char * name = 0, const bool padding = false);
QString timeToString(const int value, const unsigned int mask);
int getStringTagMappingIndex(const StringTagMappingTable& lut, const char * tag);
std::string getStringTagMappingTag(const StringTagMappingTable& lut, unsigned int index);
}

View file

@ -68,7 +68,6 @@ Node convert<YamlCalibData>::encode(const YamlCalibData& rhs)
{
Node node;
int idx = 0;
// TODO: for efficiency construct once at an outer level possibly add to Firmware?
const auto* calibIdxLut = getCurrentFirmware()->getAnalogIndexNamesLookupTable();
for (const auto& kv : *calibIdxLut) {
node[kv.tag] = rhs.calib[idx++];

View file

@ -178,186 +178,3 @@ void Firmware::addOptionsGroup(const OptionsGroup & options)
{
this->opts.append(options);
}
int Firmware::getAnalogInputIndex(const char * tag)
{
const StringTagMappingTable& lut = analogInputNamesLookupTable;
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::getAnalogInputTag(unsigned int index)
{
const StringTagMappingTable& lut = analogInputNamesLookupTable;
if (index < lut.size())
return lut[index].tag;
return std::string();
}
int Firmware::getSwitchesIndex(const char * tag)
{
const StringTagMappingTable& lut = switchesLookupTable;
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::getSwitchesTag(unsigned int index)
{
const StringTagMappingTable& lut = switchesLookupTable;
if (index < lut.size())
return lut[index].tag;
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();
}
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;
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::getRawSwitchTypesTag(unsigned int index)
{
const StringTagMappingTable& lut = rawSwitchTypesLookupTable;
if (index < lut.size())
return lut[index].tag;
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();
}
int Firmware::getRawSourceCyclicIndex(const char * tag)
{
const StringTagMappingTable& lut = rawSourceCyclicLookupTable;
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::getRawSourceCyclicTag(unsigned int index)
{
const StringTagMappingTable& lut = rawSourceCyclicLookupTable;
if (index < lut.size())
return lut[index].tag;
return std::string();
}

View file

@ -411,50 +411,13 @@ class Firmware
return &analogInputNamesLookupTable;
}
const StringTagMappingTable* getSwitchesLookupTable()
{
return &switchesLookupTable;
}
const StringTagMappingTable* getTrimSwitchesLookupTable()
{
return &trimSwitchesLookupTable;
}
const StringTagMappingTable* getTrimSourcesLookupTable()
{
return &trimSourcesLookupTable;
}
const StringTagMappingTable* getRawSwitchTypesLookupTable()
{
return &rawSwitchTypesLookupTable;
}
const StringTagMappingTable* getRawSourceSpecialTypesLookupTable()
{
return &rawSourceSpecialTypesLookupTable;
}
const StringTagMappingTable* getRawSourceCyclicLookupTable()
{
return &rawSourceCyclicLookupTable;
}
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);
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);
std::string getRawSourceSpecialTypesTag(unsigned int index);
int getRawSourceCyclicIndex(const char * tag);
std::string getRawSourceCyclicTag(unsigned int index);
STRINGTAGMAPPINGFUNCS(analogInputNamesLookupTable, AnalogInput);
STRINGTAGMAPPINGFUNCS(switchesLookupTable, Switches);
STRINGTAGMAPPINGFUNCS(trimSwitchesLookupTable, TrimSwitches);
STRINGTAGMAPPINGFUNCS(trimSourcesLookupTable, TrimSources);
STRINGTAGMAPPINGFUNCS(rawSwitchTypesLookupTable, RawSwitchTypes);
STRINGTAGMAPPINGFUNCS(rawSourceSpecialTypesLookupTable, RawSourceSpecialTypes);
STRINGTAGMAPPINGFUNCS(rawSourceCyclicLookupTable, RawSourceCyclic);
protected:
QString id;
@ -463,6 +426,8 @@ class Firmware
unsigned int variantBase;
Firmware * base;
EEPROMInterface * eepromInterface;
// used by YAML encode and decode
const StringTagMappingTable analogInputNamesLookupTable;
const StringTagMappingTable switchesLookupTable;
const StringTagMappingTable trimSwitchesLookupTable;
@ -470,6 +435,7 @@ class Firmware
const StringTagMappingTable rawSwitchTypesLookupTable;
const StringTagMappingTable rawSourceSpecialTypesLookupTable;
const StringTagMappingTable rawSourceCyclicLookupTable;
QList<const char *> languages;
//QList<const char *> ttslanguages;
OptionsList opts;