From 071a6ba805c46e0cb59a5a4b89c43d9d58de5220 Mon Sep 17 00:00:00 2001 From: philmoz Date: Fri, 11 Apr 2025 12:57:31 +1000 Subject: [PATCH] fix(radio): 2POS switch connected to 3POS socket may not behave as expected (#6067) --- radio/src/switches.cpp | 21 ++++++++++++++------- radio/src/tests/mixer.cpp | 11 +++++++---- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/radio/src/switches.cpp b/radio/src/switches.cpp index d3c687f76..2d06c9d3b 100644 --- a/radio/src/switches.cpp +++ b/radio/src/switches.cpp @@ -727,15 +727,22 @@ bool getSwitch(swsrc_t swtch, uint8_t flags) } else #endif { - if (flags & GETSWITCH_MIDPOS_DELAY) { - result = SWITCH_POSITION(cs_idx); - } else { - div_t qr = div(cs_idx, 3); - if (SWITCH_EXISTS(qr.quot)) { - result = switchState(cs_idx); + div_t qr = div(cs_idx, 3); + if (SWITCH_EXISTS(qr.quot)) { + auto sw_cfg = (SwitchConfig)SWITCH_CONFIG(qr.quot); + if (flags & GETSWITCH_MIDPOS_DELAY) { + result = SWITCH_POSITION(cs_idx); + // Handle 2POS switch installed in 3POS slot + if (!result && qr.rem == SWITCH_HW_DOWN && (sw_cfg == SWITCH_2POS || sw_cfg == SWITCH_TOGGLE)) + result = SWITCH_POSITION(cs_idx - 1); } else { - result = false; + result = switchState(cs_idx); + // Handle 2POS switch installed in 3POS slot + if (!result && qr.rem == SWITCH_HW_DOWN && (sw_cfg == SWITCH_2POS || sw_cfg == SWITCH_TOGGLE)) + result = switchState(cs_idx - 1); } + } else { + result = false; } } } diff --git a/radio/src/tests/mixer.cpp b/radio/src/tests/mixer.cpp index 2d541cc4f..bb2462345 100644 --- a/radio/src/tests/mixer.cpp +++ b/radio/src/tests/mixer.cpp @@ -810,11 +810,14 @@ TEST_F(MixerTest, DelayOnSwitch) evalFlightModeMixes(e_perout_mode_normal, 1); EXPECT_EQ(chans[0], CHANNEL_MAX); - simuSetSwitch(switch_index, 0); - CHECK_DELAY(0, 500); + auto sw_cfg = (SwitchConfig)SWITCH_CONFIG(switch_index); + if (sw_cfg == SWITCH_3POS) { + simuSetSwitch(switch_index, 0); + CHECK_DELAY(0, 500); - evalFlightModeMixes(e_perout_mode_normal, 1); - EXPECT_EQ(chans[0], 0); + evalFlightModeMixes(e_perout_mode_normal, 1); + EXPECT_EQ(chans[0], 0); + } } TEST_F(MixerTest, DelayOnSwitch2)