Add enum lookup tables
This commit is contained in:
@@ -22,8 +22,58 @@
|
|||||||
#include "generalsettings.h"
|
#include "generalsettings.h"
|
||||||
#include "yaml_trainerdata.h"
|
#include "yaml_trainerdata.h"
|
||||||
|
|
||||||
|
typedef std::pair<int, std::string> YamlLookupTableElmt;
|
||||||
|
typedef std::vector<YamlLookupTableElmt> YamlLookupTable;
|
||||||
|
|
||||||
|
const YamlLookupTable beeperModeLut = {
|
||||||
|
{ GeneralSettings::BEEPER_QUIET, "mode_quiet" },
|
||||||
|
{ GeneralSettings::BEEPER_ALARMS_ONLY, "mode_alarms" },
|
||||||
|
{ GeneralSettings::BEEPER_NOKEYS, "mode_nokeys" },
|
||||||
|
{ GeneralSettings::BEEPER_ALL, "mode_all" },
|
||||||
|
};
|
||||||
|
|
||||||
|
enum BacklightMode {
|
||||||
|
e_backlight_mode_off = 0,
|
||||||
|
e_backlight_mode_keys = 1,
|
||||||
|
e_backlight_mode_sticks = 2,
|
||||||
|
e_backlight_mode_all = e_backlight_mode_keys+e_backlight_mode_sticks,
|
||||||
|
e_backlight_mode_on
|
||||||
|
};
|
||||||
|
|
||||||
|
const YamlLookupTable backlightModeLut = {
|
||||||
|
{ e_backlight_mode_off, "backlight_mode_off" },
|
||||||
|
{ e_backlight_mode_keys, "backlight_mode_keys" },
|
||||||
|
{ e_backlight_mode_sticks, "backlight_mode_sticks" },
|
||||||
|
{ e_backlight_mode_all, "backlight_mode_all" },
|
||||||
|
{ e_backlight_mode_on, "backlight_mode_on" },
|
||||||
|
};
|
||||||
|
|
||||||
|
const YamlLookupTable bluetoothModeLut = {
|
||||||
|
{ GeneralSettings::BLUETOOTH_MODE_OFF, "OFF" },
|
||||||
|
{ GeneralSettings::BLUETOOTH_MODE_TELEMETRY, "TELEMETRY" },
|
||||||
|
{ GeneralSettings::BLUETOOTH_MODE_TRAINER, "TRAINER" },
|
||||||
|
};
|
||||||
|
|
||||||
|
const YamlLookupTable uartModeLut = {
|
||||||
|
{ GeneralSettings::AUX_SERIAL_OFF, "MODE_NONE" },
|
||||||
|
{ GeneralSettings::AUX_SERIAL_TELE_MIRROR, "MODE_TELEMETRY_MIRROR" },
|
||||||
|
{ GeneralSettings::AUX_SERIAL_TELE_IN, "MODE_TELEMETRY" },
|
||||||
|
{ GeneralSettings::AUX_SERIAL_SBUS_TRAINER, "MODE_SBUS_TRAINER" },
|
||||||
|
{ GeneralSettings::AUX_SERIAL_LUA, "MODE_LUA" },
|
||||||
|
};
|
||||||
|
|
||||||
|
const YamlLookupTable antennaModeLut = {
|
||||||
|
{ GeneralSettings::ANTENNA_MODE_INTERNAL, "MODE_INTERNAL" },
|
||||||
|
{ GeneralSettings::ANTENNA_MODE_ASK, "MODE_ASK" },
|
||||||
|
{ GeneralSettings::ANTENNA_MODE_PER_MODEL, "MODE_PER_MODEL" },
|
||||||
|
{ GeneralSettings::ANTENNA_MODE_EXTERNAL, "MODE_EXTERNAL" },
|
||||||
|
{ GeneralSettings::ANTENNA_MODE_INTERNAL_EXTERNAL, "MODE_INTERNAL_EXTERNAL" },
|
||||||
|
};
|
||||||
|
|
||||||
namespace YAML {
|
namespace YAML {
|
||||||
|
|
||||||
|
ENUM_CONVERTER(GeneralSettings::BeeperMode, beeperModeLut);
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
struct convert<GeneralSettings> {
|
struct convert<GeneralSettings> {
|
||||||
|
|
||||||
@@ -45,20 +95,17 @@ namespace YAML {
|
|||||||
node["txVoltageCalibration"] >> rhs.txVoltageCalibration;
|
node["txVoltageCalibration"] >> rhs.txVoltageCalibration;
|
||||||
node["vBatMin"] >> rhs.vBatMin;
|
node["vBatMin"] >> rhs.vBatMin;
|
||||||
node["vBatMax"] >> rhs.vBatMax;
|
node["vBatMax"] >> rhs.vBatMax;
|
||||||
|
node["backlightMode"] >> backlightModeLut >> rhs.backlightMode;
|
||||||
// TODO: lookup table
|
|
||||||
//rhs.backlightMode = node["backlightMode"].as<std::string>(); // backlight_mode_all
|
|
||||||
|
|
||||||
node["trainer"] >> rhs.trainer;
|
node["trainer"] >> rhs.trainer;
|
||||||
node["view"] >> rhs.view;
|
node["view"] >> rhs.view;
|
||||||
node["fai"] >> rhs.fai;
|
node["fai"] >> rhs.fai;
|
||||||
node["disableMemoryWarning"] >> rhs.disableMemoryWarning;
|
node["disableMemoryWarning"] >> rhs.disableMemoryWarning;
|
||||||
//node["beepMode"] >> rhs.beeperMode; // TODO: string mapping
|
node["beepMode"] >> rhs.beeperMode;
|
||||||
node["disableAlarmWarning"] >> rhs.disableAlarmWarning;
|
node["disableAlarmWarning"] >> rhs.disableAlarmWarning;
|
||||||
node["disableRssiPoweroffAlarm"] >> rhs.disableRssiPoweroffAlarm;
|
node["disableRssiPoweroffAlarm"] >> rhs.disableRssiPoweroffAlarm;
|
||||||
node["USBMode"] >> rhs.usbMode;
|
node["USBMode"] >> rhs.usbMode;
|
||||||
node["jackMode"] >> rhs.jackMode;
|
node["jackMode"] >> rhs.jackMode;
|
||||||
//node["hapticMode"] >> rhs.hapticMode; // TODO: strin mapping
|
node["hapticMode"] >> rhs.hapticMode;
|
||||||
node["stickMode"] >> rhs.stickMode;
|
node["stickMode"] >> rhs.stickMode;
|
||||||
node["timezone"] >> rhs.timezone;
|
node["timezone"] >> rhs.timezone;
|
||||||
node["adjustRTC"] >> rhs.adjustRTC;
|
node["adjustRTC"] >> rhs.adjustRTC;
|
||||||
@@ -77,7 +124,7 @@ namespace YAML {
|
|||||||
node["globalTimer"] >> rhs.globalTimer;
|
node["globalTimer"] >> rhs.globalTimer;
|
||||||
node["bluetoothName"] >> rhs.bluetoothName;
|
node["bluetoothName"] >> rhs.bluetoothName;
|
||||||
node["bluetoothBaudrate"] >> rhs.bluetoothBaudrate;
|
node["bluetoothBaudrate"] >> rhs.bluetoothBaudrate;
|
||||||
node["bluetoothMode"] >> rhs.bluetoothMode;
|
node["bluetoothMode"] >> bluetoothModeLut >> rhs.bluetoothMode;
|
||||||
node["countryCode"] >> rhs.countryCode;
|
node["countryCode"] >> rhs.countryCode;
|
||||||
node["jitterFilter"] >> rhs.jitterFilter;
|
node["jitterFilter"] >> rhs.jitterFilter;
|
||||||
node["disableRtcWarning"] >> rhs.rtcCheckDisable; // TODO: verify
|
node["disableRtcWarning"] >> rhs.rtcCheckDisable; // TODO: verify
|
||||||
@@ -91,9 +138,9 @@ namespace YAML {
|
|||||||
node["varioRange"] >> rhs.varioRange;
|
node["varioRange"] >> rhs.varioRange;
|
||||||
node["varioRepeat"] >> rhs.varioRepeat;
|
node["varioRepeat"] >> rhs.varioRepeat;
|
||||||
node["backgroundVolume"] >> rhs.backgroundVolume;
|
node["backgroundVolume"] >> rhs.backgroundVolume;
|
||||||
//node["auxSerialMode"] >> rhs.auxSerialMode;
|
node["auxSerialMode"] >> uartModeLut >> rhs.auxSerialMode;
|
||||||
//node["aux2SerialMode"] >> rhs.aux2SerialMode;
|
node["aux2SerialMode"] >> uartModeLut >> rhs.aux2SerialMode;
|
||||||
//node["antennaMode"] >> rhs.antennaMode;
|
node["antennaMode"] >> antennaModeLut >> rhs.antennaMode;
|
||||||
node["backlightColor"] >> rhs.backlightColor;
|
node["backlightColor"] >> rhs.backlightColor;
|
||||||
|
|
||||||
// if (node["customFn"]) {
|
// if (node["customFn"]) {
|
||||||
|
|||||||
@@ -22,20 +22,65 @@
|
|||||||
|
|
||||||
#include <yaml-cpp/yaml.h>
|
#include <yaml-cpp/yaml.h>
|
||||||
|
|
||||||
|
typedef std::pair<int, std::string> YamlLookupTableElmt;
|
||||||
|
typedef std::vector<YamlLookupTableElmt> YamlLookupTable;
|
||||||
|
|
||||||
|
YAML::Node operator>>(const YAML::Node& node, const YamlLookupTable& lut)
|
||||||
|
{
|
||||||
|
if (node) {
|
||||||
|
std::string str = node.as<std::string>();
|
||||||
|
const auto& it =
|
||||||
|
find_if(lut.begin(), lut.end(), [=](const YamlLookupTableElmt& elmt) {
|
||||||
|
if (elmt.second == str) return true;
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (it != lut.end()) {
|
||||||
|
return YAML::Node(it->first);
|
||||||
|
}
|
||||||
|
return YAML::Node();
|
||||||
|
}
|
||||||
|
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void operator>>(const YAML::Node& node, T& value)
|
void operator>>(const YAML::Node& node, T& value)
|
||||||
{
|
{
|
||||||
if (node) {
|
if (node && !node.IsNull()) {
|
||||||
value = node.as<T>();
|
value = node.as<T>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
void operator>>(const YAML::Node& node, bool& value)
|
||||||
|
{
|
||||||
|
if (node && node.IsScalar()) {
|
||||||
|
value = node.as<int>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
template <size_t N>
|
template <size_t N>
|
||||||
void operator>>(const YAML::Node& node, char (&value)[N])
|
void operator>>(const YAML::Node& node, char (&value)[N])
|
||||||
{
|
{
|
||||||
if (node) {
|
if (node && node.IsScalar()) {
|
||||||
auto str = node.as<std::string>();
|
auto str = node.as<std::string>();
|
||||||
str.copy(value, N-1);
|
str.copy(value, N-1);
|
||||||
value[std::min(str.size(), N-1)] = '\0';
|
value[std::min(str.size(), N-1)] = '\0';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define ENUM_CONVERTER(enum_type, lut) \
|
||||||
|
template <> \
|
||||||
|
struct convert<enum_type> { \
|
||||||
|
static bool decode(const YAML::Node& node, enum_type& rhs) \
|
||||||
|
{ \
|
||||||
|
if (node) { \
|
||||||
|
YAML::Node conv = node >> lut; \
|
||||||
|
if (conv.IsScalar()) { \
|
||||||
|
rhs = (enum_type)conv.as<int>(); \
|
||||||
|
} \
|
||||||
|
} \
|
||||||
|
return true; \
|
||||||
|
} \
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user