From a7b1926045c616f5924a1c70ce6530d240f67004 Mon Sep 17 00:00:00 2001 From: Rafael Araujo Lehmkuhl Date: Wed, 13 Sep 2023 14:26:40 -0300 Subject: [PATCH] Fix MAVLink button mapping logic If there's more than one Cockpit buttons assigned to the same MAVLink button, use another MAVLink button, if available, or throw if not. With this logic change we prevent strange cases where there were two Cockpit buttons pointing to Arm, for example, and changing one of them also changed the other. --- src/views/ConfigurationJoystickView.vue | 30 +++++++++++++++---------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/views/ConfigurationJoystickView.vue b/src/views/ConfigurationJoystickView.vue index ba63b3720..871f4e304 100644 --- a/src/views/ConfigurationJoystickView.vue +++ b/src/views/ConfigurationJoystickView.vue @@ -380,22 +380,28 @@ const updateMapping = (index: number, newValue: ProtocolInput, inputType: InputT Swal.fire({ text: `Could not find MAVLink parameter ${newValue.value}.`, icon: 'error', timer: 5000 }) return } + let mavlinkButton: undefined | number = undefined - if (oldInputMapping[index].protocol === JoystickProtocol.MAVLink) { - // If the selected Cockpit button is already mapped to be used with MAVLink functions, re-use it - mavlinkButton = oldInputMapping[index].value as number - } else { - // If not, we should look for a MAVLink button that is not being used already and use it - const usedMavlinkButtons = oldInputMapping - .filter((i) => i.protocol === JoystickProtocol.MAVLink) - .map((i) => i.value) - const availableMavlinkButtons = mavlinkAvailableButtons.filter((b) => !usedMavlinkButtons.includes(b)) - if (!availableMavlinkButtons.isEmpty()) { - mavlinkButton = availableMavlinkButtons[0] + const usedMavButtons = oldInputMapping.filter((i) => i.protocol === JoystickProtocol.MAVLink).map((i) => i.value) + const availableMavButtons = mavlinkAvailableButtons.filter((b) => !usedMavButtons.includes(b)) + const oldButtonInput = oldInputMapping[index] + + if (oldButtonInput.protocol !== JoystickProtocol.MAVLink && !availableMavButtons.isEmpty()) { + mavlinkButton = availableMavButtons[0] + } else if (oldButtonInput.protocol === JoystickProtocol.MAVLink) { + // Check if there's more than one Cockpit button assigned to this same MAVLink button + const doubleMapped = usedMavButtons.filter((b) => b === oldButtonInput.value).length > 1 + if (doubleMapped && !availableMavButtons.isEmpty()) { + // In case there's a double mapping but there are MAVLink buttons still not used, pick one. + mavlinkButton = availableMavButtons[0] + } else if (!doubleMapped) { + // If there's onlyy one Cockpit button mapped to this MAVLink button, use it. + mavlinkButton = oldButtonInput.value as number } } + if (mavlinkButton === undefined) { - // If the variable is still undefined, it means we could not find an available MAVLink button, thus we cannot proceed mapping + // If the variable is still undefined, it means we could not find an available MAVLink button to be used, thus we cannot proceed mapping. const errorMessage = `None of the 16 MAVLink Manual Control buttons are available. Please assign "No function" to one already used.` console.error(errorMessage)