From f57a1fd6c3f2c55054e37eb63632e761c5a60ffd Mon Sep 17 00:00:00 2001 From: Raphael Coeffic Date: Mon, 29 Nov 2021 11:14:50 +0100 Subject: [PATCH] Add support for radio calibration --- companion/src/firmwares/CMakeLists.txt | 3 + .../src/firmwares/edgetx/yaml_calibdata.cpp | 57 +++++++++++++++ .../src/firmwares/edgetx/yaml_calibdata.h | 64 ++++++++++++++++ .../firmwares/edgetx/yaml_generalsettings.cpp | 66 +++++++++++++++++ .../firmwares/edgetx/yaml_generalsettings.h | 73 ++++++------------- companion/src/firmwares/edgetx/yaml_ops.cpp | 55 ++++++++++++++ companion/src/firmwares/edgetx/yaml_ops.h | 36 +-------- 7 files changed, 271 insertions(+), 83 deletions(-) create mode 100644 companion/src/firmwares/edgetx/yaml_calibdata.cpp create mode 100644 companion/src/firmwares/edgetx/yaml_calibdata.h create mode 100644 companion/src/firmwares/edgetx/yaml_generalsettings.cpp create mode 100644 companion/src/firmwares/edgetx/yaml_ops.cpp diff --git a/companion/src/firmwares/CMakeLists.txt b/companion/src/firmwares/CMakeLists.txt index 2185e3676..16aa92710 100644 --- a/companion/src/firmwares/CMakeLists.txt +++ b/companion/src/firmwares/CMakeLists.txt @@ -30,6 +30,9 @@ set(firmwares_SRCS opentx/opentxeeprom.cpp opentx/opentxinterface.cpp edgetx/edgetxinterface.cpp + edgetx/yaml_ops.cpp + edgetx/yaml_generalsettings.cpp + edgetx/yaml_calibdata.cpp datahelpers.cpp customisation_data.cpp ) diff --git a/companion/src/firmwares/edgetx/yaml_calibdata.cpp b/companion/src/firmwares/edgetx/yaml_calibdata.cpp new file mode 100644 index 000000000..b5cd83a4e --- /dev/null +++ b/companion/src/firmwares/edgetx/yaml_calibdata.cpp @@ -0,0 +1,57 @@ +/* + * Copyright (C) OpenTX + * + * Based on code named + * th9x - http://code.google.com/p/th9x + * er9x - http://code.google.com/p/er9x + * gruvin9x - http://code.google.com/p/gruvin9x + * + * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include "yaml_calibdata.h" + +// TODO: we need a function that +// does the opposite of Boards::getAnalogInputName() +// +// This is only valid for X10, T16, TX16S (not X12S) +// +const YamlLookupTable calibIdxLut = { + { 0, "Rud" }, + { 1, "Ele" }, + { 2, "Thr" }, + { 3, "Ail" }, + { 4, "S1" }, + { 5, "6POS" }, + { 6, "S2" }, + { 7, "EXT1" }, + { 8, "EXT2" }, + // not yet supported in Companion: + // { 9, "EXT3" }, + // { 10, "EXT4" }, + { 9, "LS" }, + { 10, "RS" }, + { 11, "MOUSE1" }, + { 12, "MOUSE2" }, +}; + +YamlCalibData::YamlCalibData() { memset(calib, 0, sizeof(calib)); } + +void YamlCalibData::copy(int* calibMid, int* calibSpanNeg, + int* calibSpanPos) const +{ + for (int i = 0; i < CPN_MAX_ANALOGS; i++) { + calibMid[i] = calib[i].mid; + calibSpanNeg[i] = calib[i].spanNeg; + calibSpanPos[i] = calib[i].spanPos; + } +} diff --git a/companion/src/firmwares/edgetx/yaml_calibdata.h b/companion/src/firmwares/edgetx/yaml_calibdata.h new file mode 100644 index 000000000..a0de4c915 --- /dev/null +++ b/companion/src/firmwares/edgetx/yaml_calibdata.h @@ -0,0 +1,64 @@ +/* + * Copyright (C) OpenTX + * + * Based on code named + * th9x - http://code.google.com/p/th9x + * er9x - http://code.google.com/p/er9x + * gruvin9x - http://code.google.com/p/gruvin9x + * + * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include "yaml_ops.h" +#include "generalsettings.h" + +struct CalibData { + int16_t mid; + int16_t spanNeg; + int16_t spanPos; +}; + +struct YamlCalibData { + CalibData calib[CPN_MAX_ANALOGS]; + + YamlCalibData(); + void copy(int* calibMid, int* calibSpanNeg, int* calibSpanPos) const; +}; + +extern const YamlLookupTable calibIdxLut; + +namespace YAML { + + template<> + struct convert { + static bool decode(const Node& node, CalibData& rhs) { + if (!node.IsMap()) return false; + node["mid"] >> rhs.mid; + node["spanNeg"] >> rhs.spanNeg; + node["spanPos"] >> rhs.spanPos; + return true; + } + }; + + template<> + struct convert { + static bool decode(const Node& node, YamlCalibData& rhs) { + if (!node.IsMap()) return false; + int idx = 0; + for (const auto& kv : node) { + kv.first >> calibIdxLut >> idx; + kv.second >> rhs.calib[idx]; + } + return true; + } + }; +} diff --git a/companion/src/firmwares/edgetx/yaml_generalsettings.cpp b/companion/src/firmwares/edgetx/yaml_generalsettings.cpp new file mode 100644 index 000000000..5c4c1e3dd --- /dev/null +++ b/companion/src/firmwares/edgetx/yaml_generalsettings.cpp @@ -0,0 +1,66 @@ +/* + * Copyright (C) OpenTX + * + * Based on code named + * th9x - http://code.google.com/p/th9x + * er9x - http://code.google.com/p/er9x + * gruvin9x - http://code.google.com/p/gruvin9x + * + * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include "yaml_generalsettings.h" + +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" }, +}; diff --git a/companion/src/firmwares/edgetx/yaml_generalsettings.h b/companion/src/firmwares/edgetx/yaml_generalsettings.h index 19bbc971e..da3d0ada0 100644 --- a/companion/src/firmwares/edgetx/yaml_generalsettings.h +++ b/companion/src/firmwares/edgetx/yaml_generalsettings.h @@ -21,54 +21,13 @@ #include "yaml_ops.h" #include "generalsettings.h" #include "yaml_trainerdata.h" +#include "yaml_calibdata.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" }, -}; +extern const YamlLookupTable beeperModeLut; +extern const YamlLookupTable backlightModeLut; +extern const YamlLookupTable bluetoothModeLut; +extern const YamlLookupTable uartModeLut; +extern const YamlLookupTable antennaModeLut; namespace YAML { @@ -85,11 +44,18 @@ namespace YAML { static bool decode(const Node& node, GeneralSettings& rhs) { + // TODO: check board string and fetch Board instance + std::string board; + node["board"] >> board; node["version"] >> rhs.version; - node["versionBla"] >> rhs.version; - // // calib + + // Radio calib + YamlCalibData calib; + node["calib"] >> calib; + calib.copy(rhs.calibMid, rhs.calibSpanNeg, rhs.calibSpanPos); + node["currModel"] >> rhs.currModelIndex; - // // filename + node["currModelFilename"] >> rhs.currModelFilename; node["contrast"] >> rhs.contrast; node["vBatWarn"] >> rhs.vBatWarn; node["txVoltageCalibration"] >> rhs.txVoltageCalibration; @@ -153,13 +119,18 @@ namespace YAML { // } // TODO: + // char stickName[CPN_MAX_STICKS][HARDWARE_NAME_LEN + 1]; + // char switchName[CPN_MAX_SWITCHES][HARDWARE_NAME_LEN + 1]; // unsigned int switchConfig[CPN_MAX_SWITCHES]; - // char stickName[CPN_MAX_STICKS][HARDWARE_NAME_LEN + 1]; + // char potName[CPN_MAX_KNOBS][HARDWARE_NAME_LEN + 1]; // unsigned int potConfig[CPN_MAX_KNOBS]; + // char sliderName[CPN_MAX_SLIDERS][HARDWARE_NAME_LEN + 1]; // unsigned int sliderConfig[CPN_MAX_SLIDERS]; + + // Color lcd theme settings are probably obsolete // RadioTheme::ThemeData themeData; node["ownerRegistrationID"] >> rhs.registrationId; diff --git a/companion/src/firmwares/edgetx/yaml_ops.cpp b/companion/src/firmwares/edgetx/yaml_ops.cpp new file mode 100644 index 000000000..6c148dd94 --- /dev/null +++ b/companion/src/firmwares/edgetx/yaml_ops.cpp @@ -0,0 +1,55 @@ +/* + * Copyright (C) OpenTX + * + * Based on code named + * th9x - http://code.google.com/p/th9x + * er9x - http://code.google.com/p/er9x + * gruvin9x - http://code.google.com/p/gruvin9x + * + * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include "yaml_ops.h" + +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; +} + +void operator >> (const YAML::Node& node, const ioffset_int& value) +{ + if (node && node.IsScalar()) { + value.assign(node.as()); + } +} + +template <> +void operator >> (const YAML::Node& node, bool& value) +{ + if (node && node.IsScalar()) { + value = node.as(); + } +} diff --git a/companion/src/firmwares/edgetx/yaml_ops.h b/companion/src/firmwares/edgetx/yaml_ops.h index e2c71a836..06d974872 100644 --- a/companion/src/firmwares/edgetx/yaml_ops.h +++ b/companion/src/firmwares/edgetx/yaml_ops.h @@ -42,31 +42,8 @@ private: }; -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; -} - -void operator >> (const YAML::Node& node, const ioffset_int& value) -{ - if (node && node.IsScalar()) { - value.assign(node.as()); - } -} +YAML::Node operator >> (const YAML::Node& node, const YamlLookupTable& lut); +void operator >> (const YAML::Node& node, const ioffset_int& value); template void operator >> (const YAML::Node& node, T& value) @@ -76,13 +53,8 @@ void operator >> (const YAML::Node& node, T& value) } } -template <> -void operator >> (const YAML::Node& node, bool& value) -{ - if (node && node.IsScalar()) { - value = node.as(); - } -} +// type specialisation +template <> void operator >> (const YAML::Node& node, bool& value); template void operator >> (const YAML::Node& node, char (&value)[N])