Move switch types lookup to rawswitch and firmware

This commit is contained in:
elecpower
2021-12-31 10:53:10 +01:00
committed by Raphael Coeffic
parent 245d64f521
commit 924942ca0d
5 changed files with 79 additions and 15 deletions
@@ -21,15 +21,6 @@
#include "yaml_rawswitch.h"
#include "eeprominterface.h"
const YamlLookupTable switchTypeLut = {
{ SWITCH_TYPE_NONE, "NONE" },
{ SWITCH_TYPE_ON, "ON" },
{ SWITCH_TYPE_ONE, "ONE" },
{ SWITCH_TYPE_TELEMETRY, "TELEMETRY_STREAMING" },
{ SWITCH_TYPE_ACT, "RADIO_ACTIVITY" },
{ SWITCH_TYPE_OFF, "OFF" },
};
std::string YamlRawSwitchEncode(const RawSwitch& rhs)
{
std::string sw_str;
@@ -73,7 +64,7 @@ std::string YamlRawSwitchEncode(const RawSwitch& rhs)
break;
default:
sw_str += YAML::LookupValue(switchTypeLut, rhs.type);
sw_str += getCurrentFirmware()->getRawSwitchTypesTag(sval);
break;
}
return sw_str;
@@ -95,6 +86,7 @@ RawSwitch YamlRawSwitchDecode(const std::string& sw_str)
int multiposcnt = Boards::getCapability(getCurrentBoard(), Board::MultiposPotsPositions);
if (val_len >= 2 && val[0] == 'L' && (val[1] >= '0' && val[1] <= '9')) {
if (std::stoi(sw_str.substr(1, val_len - 1)) < CPN_MAX_LOGICAL_SWITCHES) {
rhs = RawSwitch(SWITCH_TYPE_VIRTUAL,
std::stoi(sw_str.substr(1, val_len - 1)) - 1);
@@ -103,21 +95,25 @@ RawSwitch YamlRawSwitchDecode(const std::string& sw_str)
} else if (val_len > 3 && val[0] == '6' && val[1] == 'P' &&
(val[2] >= '0' && val[2] <= '9') &&
(val[3] >= '0' && val[3] < (multiposcnt + '0'))) {
rhs = RawSwitch(SWITCH_TYPE_MULTIPOS_POT,
(val[2] - '0') * multiposcnt + (val[3] - '0'));
} else if (val_len == 3 && val[0] == 'F' && val[1] == 'M' &&
(val[2] >= '0' && val[2] <= '9')) {
rhs = RawSwitch(SWITCH_TYPE_FLIGHT_MODE, val[2] - '0');
} else if (val_len >= 2 && val[0] == 'T' &&
(val[1] >= '0' && val[1] <= '9')) {
if (std::stoi(sw_str.substr(1, val_len - 1)) < CPN_MAX_SENSORS) {
rhs = RawSwitch(SWITCH_TYPE_SENSOR,
std::stoi(sw_str.substr(1, val_len - 1)) - 1);
}
} 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;
@@ -127,15 +123,23 @@ RawSwitch YamlRawSwitchDecode(const std::string& sw_str)
} else if (val_len >= 3 && val[0] == 'S' &&
(val[1] >= 'A' && val[1] <= 'Z') &&
(val[2] >= '0' && val[2] <= '2')) {
int sw_idx = getCurrentFirmware()->getSwitchesIndex(sw_str.substr(0, 2).c_str());
if (sw_idx >= 0) {
rhs.type = SWITCH_TYPE_SWITCH;
rhs.index = sw_idx * 3 + val[2] - '0';
}
// TODO: SWITCH_TYPE_TIMER_MODE
//} else if (sw_str.substr(0, ?) == std::string("????")) {
} else {
YAML::Node(sw_str) >> rhs.type;
rhs.index = 0;
// types which do not use index
int sw_type = getCurrentFirmware()->getRawSwitchTypesIndex(sw_str.c_str());
if (sw_type >= 0) {
rhs.type = (RawSwitchType)sw_type;
rhs.index = 0;
}
}
if (neg) {
@@ -146,8 +150,6 @@ RawSwitch YamlRawSwitchDecode(const std::string& sw_str)
namespace YAML {
ENUM_CONVERTER(RawSwitchType, switchTypeLut);
Node convert<RawSwitch>::encode(const RawSwitch& rhs)
{
return Node(YamlRawSwitchEncode(rhs));
@@ -256,3 +256,29 @@ std::string Firmware::getTrimSwitchesTag(unsigned int index)
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();
}
+10 -1
View File
@@ -293,7 +293,8 @@ class Firmware
eepromInterface(nullptr),
analogInputNamesLookupTable(Boards::getAnalogNamesLookupTable(board)),
switchesLookupTable(Boards::getSwitchesLookupTable(board)),
trimSwitchesLookupTable(Boards::getTrimSwitchesLookupTable(board))
trimSwitchesLookupTable(Boards::getTrimSwitchesLookupTable(board)),
rawSwitchTypesLookupTable(RawSwitch::getRawSwitchTypesLookupTable())
{
}
@@ -417,12 +418,19 @@ class Firmware
return &trimSwitchesLookupTable;
}
const StringTagMappingTable* getRawSwitchTypesLookupTable()
{
return &rawSwitchTypesLookupTable;
}
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 getRawSwitchTypesIndex(const char * tag);
std::string getRawSwitchTypesTag(unsigned int index);
protected:
QString id;
@@ -434,6 +442,7 @@ class Firmware
const StringTagMappingTable analogInputNamesLookupTable;
const StringTagMappingTable switchesLookupTable;
const StringTagMappingTable trimSwitchesLookupTable;
const StringTagMappingTable rawSwitchTypesLookupTable;
QList<const char *> languages;
//QList<const char *> ttslanguages;
OptionsList opts;
+25
View File
@@ -241,3 +241,28 @@ QStringList RawSwitch::getSwitchList(Boards board) const
}
return ret;
}
// static
StringTagMappingTable RawSwitch::getRawSwitchTypesLookupTable()
{
StringTagMappingTable tbl;
tbl.insert(tbl.end(), {
{std::to_string(SWITCH_TYPE_NONE), "NONE"},
{std::to_string(SWITCH_TYPE_SWITCH), "Sxn"},
{std::to_string(SWITCH_TYPE_VIRTUAL), "Ln"},
{std::to_string(SWITCH_TYPE_MULTIPOS_POT), "6Pn"},
{std::to_string(SWITCH_TYPE_TRIM), "Trim"},
{std::to_string(SWITCH_TYPE_ROTARY_ENCODER), "RE"},
{std::to_string(SWITCH_TYPE_ON), "ON"},
{std::to_string(SWITCH_TYPE_OFF), "OFF"},
{std::to_string(SWITCH_TYPE_ONE), "ONE"},
{std::to_string(SWITCH_TYPE_FLIGHT_MODE), "FMn"},
{std::to_string(SWITCH_TYPE_TIMER_MODE), "Timer"},
{std::to_string(SWITCH_TYPE_TELEMETRY), "TELEMETRY_STREAMING"},
{std::to_string(SWITCH_TYPE_SENSOR), "Tnn"},
{std::to_string(SWITCH_TYPE_ACT), "RADIO_ACTIVITY"},
});
return tbl;
}
+2
View File
@@ -22,6 +22,7 @@
#include "boards.h"
#include "constants.h"
#include "datahelpers.h"
#include <QtCore>
@@ -89,6 +90,7 @@ class RawSwitch {
bool isSet() const { return type != SWITCH_TYPE_NONE || index != 0; }
void clear() { type = SWITCH_TYPE_NONE; index = 0; }
QStringList getSwitchList(Boards board) const;
static StringTagMappingTable getRawSwitchTypesLookupTable();
bool operator== ( const RawSwitch& other) const {
return (this->type == other.type) && (this->index == other.index);