Skip to content

Commit

Permalink
Retain PWM channel setting when switching a serial pin function
Browse files Browse the repository at this point in the history
  • Loading branch information
CapnBry committed Dec 8, 2023
1 parent 7d08ab4 commit 9af2c0d
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions src/lib/LUA/rx_devLUA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,45 +185,38 @@ static void luaparamMappingChannelIn(struct luaPropertiesCommon *item, uint8_t a
config.SetPwmChannelRaw(ch, newPwmCh.raw);
}

static uint8_t configureSerialPin(uint8_t pin, uint8_t sibling, uint8_t oldMode, uint8_t newMode)
static void configureSerialPin(uint8_t pin, uint8_t sibling, uint8_t oldMode, uint8_t newMode)
{
for (int ch=0 ; ch<GPIO_PIN_PWM_OUTPUTS_COUNT ; ch++)
{
if (GPIO_PIN_PWM_OUTPUTS[ch] == sibling)
{
// set sibling pin channel settings based on this pins settings
rx_config_pwm_t newPin3Config;
// Retain as much of the sibling's current config as possible
rx_config_pwm_t siblingPinConfig;
siblingPinConfig.raw = config.GetPwmChannel(ch)->raw;

// If the new mode is serial, the sibling is also forced to serial
if (newMode == somSerial)
{
newPin3Config.val.mode = somSerial;
siblingPinConfig.val.mode = somSerial;
}
else
// If the new mode is not serial, and the sibling is serial, set the sibling to PWM (50Hz)
else if (siblingPinConfig.val.mode == somSerial)
{
rx_config_pwm_t oldSiblingConfig;
oldSiblingConfig.raw = config.GetPwmChannel(ch)->raw;

// Only when the old port was serial, switch it to PWM at the default 50Hz.
if (oldSiblingConfig.val.mode == somSerial)
{
newPin3Config.val.mode = som50Hz;
}
else
{
// retain the channel configuration if the sibiling is already in PWM mode
newPin3Config.raw = oldSiblingConfig.raw;
}
siblingPinConfig.val.mode = som50Hz;
}
config.SetPwmChannelRaw(ch, newPin3Config.raw);

config.SetPwmChannelRaw(ch, siblingPinConfig.raw);
break;
}
}

if (oldMode != newMode)
{
deferExecution(100, [](){
reconfigureSerial();
});
}
return newMode;
}

static void luaparamMappingOutputMode(struct luaPropertiesCommon *item, uint8_t arg)
Expand All @@ -237,11 +230,11 @@ static void luaparamMappingOutputMode(struct luaPropertiesCommon *item, uint8_t
// Check if pin == 1/3 and do other pin adjustment accordingly
if (GPIO_PIN_PWM_OUTPUTS[ch] == 1)
{
newPwmCh.val.mode = configureSerialPin(1, 3, oldMode, arg);
configureSerialPin(1, 3, oldMode, arg);
}
else if (GPIO_PIN_PWM_OUTPUTS[ch] == 3)
{
newPwmCh.val.mode = configureSerialPin(3, 1, oldMode, arg);
configureSerialPin(3, 1, oldMode, arg);
}
else if (arg == somSerial)
{
Expand Down

0 comments on commit 9af2c0d

Please sign in to comment.