From 162ffdb148c2442161dba643ab2b7024a9a2bb5d Mon Sep 17 00:00:00 2001 From: Raphael Coeffic Date: Sun, 6 Jun 2021 11:45:56 +0200 Subject: [PATCH] Added auto-switch / auto-source to colorlcd Resolves #118 --- .clang-format | 1 - radio/src/gui/colorlcd/sourcechoice.cpp | 15 ++++++++ radio/src/gui/colorlcd/switchchoice.cpp | 31 +++++++++++---- radio/src/gui/colorlcd/switchchoice.h | 51 +++++++++++++------------ radio/src/opentx.cpp | 2 +- radio/src/opentx.h | 3 +- 6 files changed, 67 insertions(+), 36 deletions(-) diff --git a/.clang-format b/.clang-format index dac5c39ef9..eb4ad3e10b 100644 --- a/.clang-format +++ b/.clang-format @@ -3,6 +3,5 @@ Language: Cpp BasedOnStyle: Google BreakBeforeBraces: Linux BreakConstructorInitializers: AfterColon -AccessModifierOffset: 0 ... diff --git a/radio/src/gui/colorlcd/sourcechoice.cpp b/radio/src/gui/colorlcd/sourcechoice.cpp index d2e8bf2701..69513b8908 100644 --- a/radio/src/gui/colorlcd/sourcechoice.cpp +++ b/radio/src/gui/colorlcd/sourcechoice.cpp @@ -93,6 +93,21 @@ void SourceChoice::fillMenu(Menu * menu, const std::function & fi if (current >= 0) { menu->select(current); } + +#if defined(AUTOSOURCE) + menu->setWaitHandler([=]() { + int8_t val = getMovedSource(0); + if (val) { + if (filter && filter(val)) { + return; + } + if (setValue) { + setValue(val); + } + this->fillMenu(menu); + } + }); +#endif } void SourceChoice::openMenu() diff --git a/radio/src/gui/colorlcd/switchchoice.cpp b/radio/src/gui/colorlcd/switchchoice.cpp index 804c2b0c6c..d10c38b11f 100644 --- a/radio/src/gui/colorlcd/switchchoice.cpp +++ b/radio/src/gui/colorlcd/switchchoice.cpp @@ -65,13 +65,9 @@ void SwitchChoice::fillMenu(Menu * menu, std::function filter) menu->removeLines(); for (int i = vmin; i <= vmax; ++i) { - if (filter && !filter(i)) - continue; - if (isValueAvailable && !isValueAvailable(i)) - continue; - menu->addLine(getSwitchPositionName(i), [=]() { - setValue(i); - }); + if (filter && !filter(i)) continue; + if (isValueAvailable && !isValueAvailable(i)) continue; + menu->addLine(getSwitchPositionName(i), [=]() { setValue(i); }); if (value == i) { current = count; } @@ -93,6 +89,27 @@ void SwitchChoice::openMenu() editMode = false; setFocus(SET_FOCUS_DEFAULT); }); + +#if defined(AUTOSWITCH) + menu->setWaitHandler([=]() { + swsrc_t val = 0; + swsrc_t swtch = getMovedSwitch(); + if (swtch) { + div_t info = switchInfo(swtch); + if (IS_CONFIG_TOGGLE(info.quot)) { + if (info.rem != 0) { + val = (val == swtch ? swtch - 2 : swtch); + } + } else { + val = swtch; + } + if (val && (!isValueAvailable || isValueAvailable(val))) { + if (setValue) setValue(val); + this->fillMenu(menu); + } + } + }); +#endif } #if defined(HARDWARE_KEYS) diff --git a/radio/src/gui/colorlcd/switchchoice.h b/radio/src/gui/colorlcd/switchchoice.h index 7aaaa769b1..65c9156ef3 100644 --- a/radio/src/gui/colorlcd/switchchoice.h +++ b/radio/src/gui/colorlcd/switchchoice.h @@ -26,49 +26,50 @@ class Menu; bool isSwitchAvailableInMixes(int swtch); -class SwitchChoice : public FormField { - template friend class MenuToolbar; +class SwitchChoice : public FormField +{ + template + friend class MenuToolbar; - public: - SwitchChoice(Window * parent, const rect_t & rect, int vmin, int vmax, std::function getValue, std::function setValue): + public: + SwitchChoice(Window* parent, const rect_t& rect, int vmin, int vmax, + std::function getValue, + std::function setValue) : FormField(parent, rect), vmin(vmin), vmax(vmax), getValue(std::move(getValue)), setValue(std::move(setValue)) - { - } + { + } #if defined(DEBUG_WINDOWS) - std::string getName() const override - { - return "SwitchChoice"; - } + std::string getName() const override { return "SwitchChoice"; } #endif - void paint(BitmapBuffer * dc) override; + void paint(BitmapBuffer* dc) override; #if defined(HARDWARE_KEYS) - void onEvent(event_t event) override; + void onEvent(event_t event) override; #endif #if defined(HARDWARE_TOUCH) - bool onTouchEnd(coord_t x, coord_t y) override ; + bool onTouchEnd(coord_t x, coord_t y) override; #endif - void setAvailableHandler(std::function handler) - { - isValueAvailable = std::move(handler); - } + void setAvailableHandler(std::function handler) + { + isValueAvailable = std::move(handler); + } - protected: - int16_t vmin; - int16_t vmax; - std::function getValue; - std::function setValue; - std::function isValueAvailable = isSwitchAvailableInMixes; - void fillMenu(Menu * menu, std::function condition=nullptr); - void openMenu(); + protected: + int16_t vmin; + int16_t vmax; + std::function getValue; + std::function setValue; + std::function isValueAvailable = isSwitchAvailableInMixes; + void fillMenu(Menu* menu, std::function condition = nullptr); + void openMenu(); }; #endif // _SWITCHCHOICE_H_ diff --git a/radio/src/opentx.cpp b/radio/src/opentx.cpp index 3645b10a4f..4c421e52e3 100644 --- a/radio/src/opentx.cpp +++ b/radio/src/opentx.cpp @@ -383,7 +383,7 @@ bool isInputRecursive(int index) #if defined(AUTOSOURCE) constexpr int MULTIPOS_STEP_SIZE = (2 * RESX) / XPOTS_MULTIPOS_COUNT; -int8_t getMovedSource(GET_MOVED_SOURCE_PARAMS) +int8_t getMovedSource(uint8_t min) { int8_t result = 0; static tmr10ms_t s_move_last_time = 0; diff --git a/radio/src/opentx.h b/radio/src/opentx.h index 75057c6c88..033a7c339a 100644 --- a/radio/src/opentx.h +++ b/radio/src/opentx.h @@ -549,8 +549,7 @@ void logicalSwitchesCopyState(uint8_t src, uint8_t dst); extern swarnstate_t switches_states; swsrc_t getMovedSwitch(); -#define GET_MOVED_SOURCE_PARAMS uint8_t min -int8_t getMovedSource(GET_MOVED_SOURCE_PARAMS); +int8_t getMovedSource(uint8_t min); #define GET_MOVED_SOURCE(min, max) getMovedSource(min) #if defined(FLIGHT_MODES)