Use Boards for trim switches

This commit is contained in:
elecpower
2021-12-31 10:53:10 +01:00
committed by Raphael Coeffic
parent 609604d141
commit 1c7bece090
5 changed files with 71 additions and 23 deletions
+27
View File
@@ -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;
}
+1
View File
@@ -214,6 +214,7 @@ class Boards
static AbstractStaticItemModel * potTypeItemModel();
static AbstractStaticItemModel * sliderTypeItemModel();
static AbstractStaticItemModel * switchTypeItemModel();
static StringTagMappingTable getTrimSwitchesLookupTable(Board::Type board);
protected:
@@ -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') &&
@@ -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();
}
+10 -1
View File
@@ -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<const char *> languages;
//QList<const char *> ttslanguages;
OptionsList opts;