@@ -998,7 +998,7 @@ void Joystick::_removeButtonSettings(int button)
998
998
settings.beginGroup(_name);
999
999
settings.remove(QString(_buttonActionNameKey).arg(button));
1000
1000
settings.remove(QString(_buttonActionRepeatKey).arg(button));
1001
- if (_buttonActionArray[button]->isPwmOverrideAction( )) {
1001
+ if (assignableButtonActionIsPwm(button )) {
1002
1002
settings.remove(QString(_buttonActionHighPwmValueKey).arg(button));
1003
1003
settings.remove(QString(_buttonActionLowPwmValueKey).arg(button));
1004
1004
settings.remove(QString(_buttonActionLatchPwmValueKey).arg(button));
@@ -1016,43 +1016,53 @@ QString Joystick::getButtonAction(int button)
1016
1016
}
1017
1017
1018
1018
bool Joystick::assignableButtonActionIsPwm(int button) {
1019
- return ( _validButton(button) && _buttonActionArray[button]) ? _buttonActionArray[button]->isPwmOverrideAction() : false ;
1019
+ return _validButton(button) && _buttonActionArray[button] && _buttonActionArray[button]->isPwmOverrideAction();
1020
1020
}
1021
1021
1022
1022
bool Joystick::assignableActionIsPwm(QString action) {
1023
1023
return action.contains("PWM");
1024
1024
}
1025
1025
1026
- void Joystick::setButtonPwm(int button, bool lowPwm, int value) {
1026
+ int Joystick::setButtonPwm(int button, bool lowPwm, int value) {
1027
1027
qDebug(JoystickLog) << "setButtonPwm: " << button << (lowPwm ? "LOW " : "HIGH ") << value;
1028
1028
if (assignableButtonActionIsPwm(button)) {
1029
1029
QSettings settings;
1030
1030
settings.beginGroup(_settingsGroup);
1031
1031
settings.beginGroup(_name);
1032
1032
if (lowPwm) {
1033
+ /// finds first other button with same action and sets low value to same value, emits error
1034
+ int anyOtherButtonWithSameAction = _getOtherMultiButtonPWMOverrideButtonIndex(button);
1035
+ if (anyOtherButtonWithSameAction != -1) {
1036
+ if (value != _buttonActionArray[anyOtherButtonWithSameAction]->lowPwm()) {
1037
+ value = _buttonActionArray[anyOtherButtonWithSameAction]->lowPwm();
1038
+ qCDebug(JoystickLog) << "setButtonPwm: " << button << " has same action as " << anyOtherButtonWithSameAction << " setting low pwm to " << value;
1039
+ //TODO(bzd) emit error
1040
+ }
1041
+ }
1033
1042
_buttonActionArray[button]->lowPwm(value);
1034
1043
settings.setValue(QString(_buttonActionLowPwmValueKey).arg(button), value);
1035
1044
} else {
1036
1045
_buttonActionArray[button]->highPwm(value);
1037
1046
settings.setValue(QString(_buttonActionHighPwmValueKey).arg(button), value);
1038
1047
}
1048
+ return value;
1039
1049
}
1040
1050
1051
+ return -1;
1041
1052
}
1042
1053
1043
1054
int Joystick::getButtonPwm(int button, bool lowPwm) {
1044
- if (_validButton(button)) {
1045
- if (assignableButtonActionIsPwm(button)) {
1046
- QSettings settings;
1047
- settings.beginGroup(_settingsGroup);
1048
- settings.beginGroup(_name);
1049
- if (lowPwm) {
1050
- return settings.value(QString(_buttonActionLowPwmValueKey).arg(button), -1).toInt();
1051
- } else {
1052
- return settings.value(QString(_buttonActionHighPwmValueKey).arg(button), -1).toInt();
1053
- }
1055
+ if (_validButton(button) && assignableButtonActionIsPwm(button)) {
1056
+ QSettings settings;
1057
+ settings.beginGroup(_settingsGroup);
1058
+ settings.beginGroup(_name);
1059
+ if (lowPwm) {
1060
+ return settings.value(QString(_buttonActionLowPwmValueKey).arg(button), -1).toInt();
1061
+ } else {
1062
+ return settings.value(QString(_buttonActionHighPwmValueKey).arg(button), -1).toInt();
1054
1063
}
1055
1064
}
1065
+
1056
1066
return -1;
1057
1067
}
1058
1068
@@ -1413,4 +1423,17 @@ uint16_t Joystick::_mapRcOverrideToRelease(uint8_t rcChannel, uint16_t value) {
1413
1423
return rcChannel < 9 ? 0 : UINT16_MAX - 1;
1414
1424
}
1415
1425
return value;
1426
+ }
1427
+
1428
+ int Joystick::_getOtherMultiButtonPWMOverrideButtonIndex(int button) {
1429
+ if (_buttonActionArray[button] && _buttonActionArray[button]->isPwmOverrideAction()) {
1430
+ auto action = _buttonActionArray[button]->action();
1431
+ // check if there is another button with the same action
1432
+ for (int i = 0; i < _buttonActionArray.count(); i++) {
1433
+ if (i != button && _buttonActionArray[i] && _buttonActionArray[i]->action() == action) {
1434
+ return i;
1435
+ }
1436
+ }
1437
+ }
1438
+ return -1;
1416
1439
}
0 commit comments