diff --git a/src/Joystick/Joystick.cc b/src/Joystick/Joystick.cc index 93a1f01541be..d4d446bc13d2 100644 --- a/src/Joystick/Joystick.cc +++ b/src/Joystick/Joystick.cc @@ -998,7 +998,7 @@ void Joystick::_removeButtonSettings(int button) settings.beginGroup(_name); settings.remove(QString(_buttonActionNameKey).arg(button)); settings.remove(QString(_buttonActionRepeatKey).arg(button)); - if (_buttonActionArray[button]->isPwmOverrideAction()) { + if (assignableButtonActionIsPwm(button)) { settings.remove(QString(_buttonActionHighPwmValueKey).arg(button)); settings.remove(QString(_buttonActionLowPwmValueKey).arg(button)); settings.remove(QString(_buttonActionLatchPwmValueKey).arg(button)); @@ -1016,43 +1016,53 @@ QString Joystick::getButtonAction(int button) } bool Joystick::assignableButtonActionIsPwm(int button) { - return (_validButton(button) && _buttonActionArray[button]) ? _buttonActionArray[button]->isPwmOverrideAction() : false; + return _validButton(button) && _buttonActionArray[button] && _buttonActionArray[button]->isPwmOverrideAction(); } bool Joystick::assignableActionIsPwm(QString action) { return action.contains("PWM"); } -void Joystick::setButtonPwm(int button, bool lowPwm, int value) { +int Joystick::setButtonPwm(int button, bool lowPwm, int value) { qDebug(JoystickLog) << "setButtonPwm: " << button << (lowPwm ? "LOW " : "HIGH ") << value; if (assignableButtonActionIsPwm(button)) { QSettings settings; settings.beginGroup(_settingsGroup); settings.beginGroup(_name); if (lowPwm) { + /// finds first other button with same action and sets low value to same value, emits error + int anyOtherButtonWithSameAction = _getOtherMultiButtonPWMOverrideButtonIndex(button); + if (anyOtherButtonWithSameAction != -1) { + if (value != _buttonActionArray[anyOtherButtonWithSameAction]->lowPwm()) { + value = _buttonActionArray[anyOtherButtonWithSameAction]->lowPwm(); + qCDebug(JoystickLog) << "setButtonPwm: " << button << " has same action as " << anyOtherButtonWithSameAction << " setting low pwm to " << value; + //TODO(bzd) emit error + } + } _buttonActionArray[button]->lowPwm(value); settings.setValue(QString(_buttonActionLowPwmValueKey).arg(button), value); } else { _buttonActionArray[button]->highPwm(value); settings.setValue(QString(_buttonActionHighPwmValueKey).arg(button), value); } + return value; } + return -1; } int Joystick::getButtonPwm(int button, bool lowPwm) { - if (_validButton(button)) { - if (assignableButtonActionIsPwm(button)) { - QSettings settings; - settings.beginGroup(_settingsGroup); - settings.beginGroup(_name); - if (lowPwm) { - return settings.value(QString(_buttonActionLowPwmValueKey).arg(button), -1).toInt(); - } else { - return settings.value(QString(_buttonActionHighPwmValueKey).arg(button), -1).toInt(); - } + if (_validButton(button) && assignableButtonActionIsPwm(button)) { + QSettings settings; + settings.beginGroup(_settingsGroup); + settings.beginGroup(_name); + if (lowPwm) { + return settings.value(QString(_buttonActionLowPwmValueKey).arg(button), -1).toInt(); + } else { + return settings.value(QString(_buttonActionHighPwmValueKey).arg(button), -1).toInt(); } } + return -1; } @@ -1413,4 +1423,17 @@ uint16_t Joystick::_mapRcOverrideToRelease(uint8_t rcChannel, uint16_t value) { return rcChannel < 9 ? 0 : UINT16_MAX - 1; } return value; +} + +int Joystick::_getOtherMultiButtonPWMOverrideButtonIndex(int button) { + if (_buttonActionArray[button] && _buttonActionArray[button]->isPwmOverrideAction()) { + auto action = _buttonActionArray[button]->action(); + // check if there is another button with the same action + for (int i = 0; i < _buttonActionArray.count(); i++) { + if (i != button && _buttonActionArray[i] && _buttonActionArray[i]->action() == action) { + return i; + } + } + } + return -1; } \ No newline at end of file diff --git a/src/Joystick/Joystick.h b/src/Joystick/Joystick.h index ecd433983c15..f32dbd3b28be 100644 --- a/src/Joystick/Joystick.h +++ b/src/Joystick/Joystick.h @@ -157,7 +157,7 @@ class Joystick : public QThread Q_INVOKABLE QString getButtonAction (int button); Q_INVOKABLE bool assignableButtonActionIsPwm (int button); Q_INVOKABLE bool assignableActionIsPwm (QString action); - Q_INVOKABLE void setButtonPwm (int button, bool lowPwm, int value); + Q_INVOKABLE int setButtonPwm (int button, bool lowPwm, int value); Q_INVOKABLE int getButtonPwm (int button, bool lowPwm); // Property accessors @@ -294,7 +294,6 @@ class Joystick : public QThread int _mapFunctionMode(int mode, int function); void _remapAxes(int currentMode, int newMode, int (&newMapping)[maxFunction]); - //TODO(bzd) change private members to use _ prefix void _removeButtonSettings(int button); void _saveButtonSettings(int button); /// if repeat is available for action under button, sets passed repeat flag @@ -303,6 +302,18 @@ class Joystick : public QThread bool _executeRcOverrideButtonAction(int buttonIndex, bool buttonDown); void _clearRcOverrideButtonActions(); uint16_t _mapRcOverrideToRelease(uint8_t rcChannel, uint16_t value); +// bool _isButtonOfMultiButtonPWMOverride(int button); + /** + * @brief Checks if the button is a multi-button PWM override, returns any other button if it is + * + * Searches for other buttons that are part of the same multi-button PWM override Action and returns the first one found that is different from the passed button. + * If the passed button is not part of a multi-button PWM override, returns -1. + * + * @param button + * @return + */ + int _getOtherMultiButtonPWMOverrideButtonIndex(int button); + bool _isActionMultiButtonPWMOverride(const QString& action); // Override from QThread virtual void run(); diff --git a/src/VehicleSetup/JoystickConfigButtons.qml b/src/VehicleSetup/JoystickConfigButtons.qml index 4900a5d260ef..e26141785a03 100644 --- a/src/VehicleSetup/JoystickConfigButtons.qml +++ b/src/VehicleSetup/JoystickConfigButtons.qml @@ -119,6 +119,7 @@ ColumnLayout { visible: _activeJoystick ? _activeJoystick.pwmVisibilities[modelData] : false function _setButtonPwm(button, isLow, pwm) { + var pwmValue = -1; if(_activeJoystick) { if (pwm < 1000) { pwm = 1000; @@ -126,16 +127,17 @@ ColumnLayout { if (pwm > 2000) { pwm = 2000; } - _activeJoystick.setButtonPwm(modelData, isLow, pwm) + pwmValue = _activeJoystick.setButtonPwm(modelData, isLow, pwm) } + return pwmValue == -1 ? "" : pwmValue; } function _getButtonPwm(button, isLow) { - var pwm = -1; + var pwmValue = -1; if(_activeJoystick) { - pwm = _activeJoystick.getButtonPwm(modelData, isLow) + pwmValue = _activeJoystick.getButtonPwm(modelData, isLow) } - return pwm == -1 ? "" : pwm; + return pwmValue == -1 ? "" : pwmValue; } QGCLabel { @@ -143,6 +145,7 @@ ColumnLayout { text: qsTr("Low") anchors.verticalCenter: parent.verticalCenter } + QGCTextField { id: lowPwmValue width: ScreenTools.defaultFontPixelWidth * 10 @@ -153,7 +156,7 @@ ColumnLayout { Connections { target: buttonActionCombo onCurrentIndexChanged: { - if(_activeJoystick) { + if (_activeJoystick) { console.log("index changed, ", buttonActionCombo.currentIndex) console.log("index changed, ", modelData) console.log("index changed, ", target) @@ -166,10 +169,14 @@ ColumnLayout { Component.onCompleted: { if(_activeJoystick) { - text = parent._getButtonPwm(modelData, true) + text = pwmSettings._getButtonPwm(modelData, true) } } - onEditingFinished: parent._setButtonPwm(modelData, true, text) + onEditingFinished: { + // setButtonPwm calculates proper value and we set it back + var pwm = pwmSettings._setButtonPwm(modelData, true, text) + lowPwmValue.text = pwm; + } } QGCLabel { @@ -201,15 +208,21 @@ ColumnLayout { Component.onCompleted: { if(_activeJoystick) { - text = parent._getButtonPwm(modelData, false) + text = pwmSettings._getButtonPwm(modelData, false) } } - onEditingFinished: parent._setButtonPwm(modelData, false, text) + onEditingFinished: { + // setButtonPwm calculates proper value and we set it back + var pwm = pwmSettings._setButtonPwm(modelData, false, text) + highPwmValue.text = pwm; + } } + QGCCheckBox { id: latchCheck text: qsTr("Latch") anchors.verticalCenter: parent.verticalCenter + enabled: pwmSettings._latchEnabled(modelData) onClicked: { _activeJoystick.setButtonPwmLatch(modelData, checked)