From 88ea3f8b20cd8085ad7e05328d64ef3eb38aaae1 Mon Sep 17 00:00:00 2001 From: Raphael Coeffic Date: Sun, 28 Nov 2021 16:20:49 +0100 Subject: [PATCH] Add enum lookup tables --- .../firmwares/edgetx/yaml_generalsettings.h | 69 ++++++++++++++++--- companion/src/firmwares/edgetx/yaml_ops.h | 49 ++++++++++++- 2 files changed, 105 insertions(+), 13 deletions(-) diff --git a/companion/src/firmwares/edgetx/yaml_generalsettings.h b/companion/src/firmwares/edgetx/yaml_generalsettings.h index 1cda57076..3c76cabb3 100644 --- a/companion/src/firmwares/edgetx/yaml_generalsettings.h +++ b/companion/src/firmwares/edgetx/yaml_generalsettings.h @@ -22,8 +22,58 @@ #include "generalsettings.h" #include "yaml_trainerdata.h" +typedef std::pair YamlLookupTableElmt; +typedef std::vector 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 { - + + ENUM_CONVERTER(GeneralSettings::BeeperMode, beeperModeLut); + template<> struct convert { @@ -45,20 +95,17 @@ namespace YAML { node["txVoltageCalibration"] >> rhs.txVoltageCalibration; node["vBatMin"] >> rhs.vBatMin; node["vBatMax"] >> rhs.vBatMax; - - // TODO: lookup table - //rhs.backlightMode = node["backlightMode"].as(); // backlight_mode_all - + node["backlightMode"] >> backlightModeLut >> rhs.backlightMode; node["trainer"] >> rhs.trainer; node["view"] >> rhs.view; node["fai"] >> rhs.fai; node["disableMemoryWarning"] >> rhs.disableMemoryWarning; - //node["beepMode"] >> rhs.beeperMode; // TODO: string mapping + node["beepMode"] >> rhs.beeperMode; node["disableAlarmWarning"] >> rhs.disableAlarmWarning; node["disableRssiPoweroffAlarm"] >> rhs.disableRssiPoweroffAlarm; node["USBMode"] >> rhs.usbMode; node["jackMode"] >> rhs.jackMode; - //node["hapticMode"] >> rhs.hapticMode; // TODO: strin mapping + node["hapticMode"] >> rhs.hapticMode; node["stickMode"] >> rhs.stickMode; node["timezone"] >> rhs.timezone; node["adjustRTC"] >> rhs.adjustRTC; @@ -77,7 +124,7 @@ namespace YAML { node["globalTimer"] >> rhs.globalTimer; node["bluetoothName"] >> rhs.bluetoothName; node["bluetoothBaudrate"] >> rhs.bluetoothBaudrate; - node["bluetoothMode"] >> rhs.bluetoothMode; + node["bluetoothMode"] >> bluetoothModeLut >> rhs.bluetoothMode; node["countryCode"] >> rhs.countryCode; node["jitterFilter"] >> rhs.jitterFilter; node["disableRtcWarning"] >> rhs.rtcCheckDisable; // TODO: verify @@ -91,9 +138,9 @@ namespace YAML { node["varioRange"] >> rhs.varioRange; node["varioRepeat"] >> rhs.varioRepeat; node["backgroundVolume"] >> rhs.backgroundVolume; - //node["auxSerialMode"] >> rhs.auxSerialMode; - //node["aux2SerialMode"] >> rhs.aux2SerialMode; - //node["antennaMode"] >> rhs.antennaMode; + node["auxSerialMode"] >> uartModeLut >> rhs.auxSerialMode; + node["aux2SerialMode"] >> uartModeLut >> rhs.aux2SerialMode; + node["antennaMode"] >> antennaModeLut >> rhs.antennaMode; node["backlightColor"] >> rhs.backlightColor; // if (node["customFn"]) { diff --git a/companion/src/firmwares/edgetx/yaml_ops.h b/companion/src/firmwares/edgetx/yaml_ops.h index 4dc4d6608..e84adca3d 100644 --- a/companion/src/firmwares/edgetx/yaml_ops.h +++ b/companion/src/firmwares/edgetx/yaml_ops.h @@ -22,20 +22,65 @@ #include +typedef std::pair YamlLookupTableElmt; +typedef std::vector YamlLookupTable; + +YAML::Node operator>>(const YAML::Node& node, const YamlLookupTable& lut) +{ + if (node) { + std::string str = node.as(); + 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 void operator>>(const YAML::Node& node, T& value) { - if (node) { + if (node && !node.IsNull()) { value = node.as(); } } +template <> +void operator>>(const YAML::Node& node, bool& value) +{ + if (node && node.IsScalar()) { + value = node.as(); + } +} + template void operator>>(const YAML::Node& node, char (&value)[N]) { - if (node) { + if (node && node.IsScalar()) { auto str = node.as(); str.copy(value, N-1); value[std::min(str.size(), N-1)] = '\0'; } } + +#define ENUM_CONVERTER(enum_type, lut) \ + template <> \ + struct convert { \ + 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(); \ + } \ + } \ + return true; \ + } \ + }