Skip to content

Commit 30dafd5

Browse files
committed
Multibutton channels
1 parent 49431b3 commit 30dafd5

File tree

3 files changed

+71
-24
lines changed

3 files changed

+71
-24
lines changed

src/Joystick/Joystick.cc

+36-13
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,7 @@ void Joystick::_removeButtonSettings(int button)
998998
settings.beginGroup(_name);
999999
settings.remove(QString(_buttonActionNameKey).arg(button));
10001000
settings.remove(QString(_buttonActionRepeatKey).arg(button));
1001-
if (_buttonActionArray[button]->isPwmOverrideAction()) {
1001+
if (assignableButtonActionIsPwm(button)) {
10021002
settings.remove(QString(_buttonActionHighPwmValueKey).arg(button));
10031003
settings.remove(QString(_buttonActionLowPwmValueKey).arg(button));
10041004
settings.remove(QString(_buttonActionLatchPwmValueKey).arg(button));
@@ -1016,43 +1016,53 @@ QString Joystick::getButtonAction(int button)
10161016
}
10171017

10181018
bool Joystick::assignableButtonActionIsPwm(int button) {
1019-
return (_validButton(button) && _buttonActionArray[button]) ? _buttonActionArray[button]->isPwmOverrideAction() : false;
1019+
return _validButton(button) && _buttonActionArray[button] && _buttonActionArray[button]->isPwmOverrideAction();
10201020
}
10211021

10221022
bool Joystick::assignableActionIsPwm(QString action) {
10231023
return action.contains("PWM");
10241024
}
10251025

1026-
void Joystick::setButtonPwm(int button, bool lowPwm, int value) {
1026+
int Joystick::setButtonPwm(int button, bool lowPwm, int value) {
10271027
qDebug(JoystickLog) << "setButtonPwm: " << button << (lowPwm ? "LOW " : "HIGH ") << value;
10281028
if (assignableButtonActionIsPwm(button)) {
10291029
QSettings settings;
10301030
settings.beginGroup(_settingsGroup);
10311031
settings.beginGroup(_name);
10321032
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+
}
10331042
_buttonActionArray[button]->lowPwm(value);
10341043
settings.setValue(QString(_buttonActionLowPwmValueKey).arg(button), value);
10351044
} else {
10361045
_buttonActionArray[button]->highPwm(value);
10371046
settings.setValue(QString(_buttonActionHighPwmValueKey).arg(button), value);
10381047
}
1048+
return value;
10391049
}
10401050

1051+
return -1;
10411052
}
10421053

10431054
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();
10541063
}
10551064
}
1065+
10561066
return -1;
10571067
}
10581068

@@ -1413,4 +1423,17 @@ uint16_t Joystick::_mapRcOverrideToRelease(uint8_t rcChannel, uint16_t value) {
14131423
return rcChannel < 9 ? 0 : UINT16_MAX - 1;
14141424
}
14151425
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;
14161439
}

src/Joystick/Joystick.h

+13-2
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class Joystick : public QThread
157157
Q_INVOKABLE QString getButtonAction (int button);
158158
Q_INVOKABLE bool assignableButtonActionIsPwm (int button);
159159
Q_INVOKABLE bool assignableActionIsPwm (QString action);
160-
Q_INVOKABLE void setButtonPwm (int button, bool lowPwm, int value);
160+
Q_INVOKABLE int setButtonPwm (int button, bool lowPwm, int value);
161161
Q_INVOKABLE int getButtonPwm (int button, bool lowPwm);
162162

163163
// Property accessors
@@ -294,7 +294,6 @@ class Joystick : public QThread
294294
int _mapFunctionMode(int mode, int function);
295295
void _remapAxes(int currentMode, int newMode, int (&newMapping)[maxFunction]);
296296

297-
//TODO(bzd) change private members to use _ prefix
298297
void _removeButtonSettings(int button);
299298
void _saveButtonSettings(int button);
300299
/// if repeat is available for action under button, sets passed repeat flag
@@ -303,6 +302,18 @@ class Joystick : public QThread
303302
bool _executeRcOverrideButtonAction(int buttonIndex, bool buttonDown);
304303
void _clearRcOverrideButtonActions();
305304
uint16_t _mapRcOverrideToRelease(uint8_t rcChannel, uint16_t value);
305+
// bool _isButtonOfMultiButtonPWMOverride(int button);
306+
/**
307+
* @brief Checks if the button is a multi-button PWM override, returns any other button if it is
308+
*
309+
* 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.
310+
* If the passed button is not part of a multi-button PWM override, returns -1.
311+
*
312+
* @param button
313+
* @return
314+
*/
315+
int _getOtherMultiButtonPWMOverrideButtonIndex(int button);
316+
bool _isActionMultiButtonPWMOverride(const QString& action);
306317

307318
// Override from QThread
308319
virtual void run();

src/VehicleSetup/JoystickConfigButtons.qml

+22-9
Original file line numberDiff line numberDiff line change
@@ -119,30 +119,33 @@ ColumnLayout {
119119
visible: _activeJoystick ? _activeJoystick.pwmVisibilities[modelData] : false
120120

121121
function _setButtonPwm(button, isLow, pwm) {
122+
var pwmValue = -1;
122123
if(_activeJoystick) {
123124
if (pwm < 1000) {
124125
pwm = 1000;
125126
}
126127
if (pwm > 2000) {
127128
pwm = 2000;
128129
}
129-
_activeJoystick.setButtonPwm(modelData, isLow, pwm)
130+
pwmValue = _activeJoystick.setButtonPwm(modelData, isLow, pwm)
130131
}
132+
return pwmValue == -1 ? "" : pwmValue;
131133
}
132134

133135
function _getButtonPwm(button, isLow) {
134-
var pwm = -1;
136+
var pwmValue = -1;
135137
if(_activeJoystick) {
136-
pwm = _activeJoystick.getButtonPwm(modelData, isLow)
138+
pwmValue = _activeJoystick.getButtonPwm(modelData, isLow)
137139
}
138-
return pwm == -1 ? "" : pwm;
140+
return pwmValue == -1 ? "" : pwmValue;
139141
}
140142

141143
QGCLabel {
142144
id: lowPwmLabel
143145
text: qsTr("Low")
144146
anchors.verticalCenter: parent.verticalCenter
145147
}
148+
146149
QGCTextField {
147150
id: lowPwmValue
148151
width: ScreenTools.defaultFontPixelWidth * 10
@@ -153,7 +156,7 @@ ColumnLayout {
153156
Connections {
154157
target: buttonActionCombo
155158
onCurrentIndexChanged: {
156-
if(_activeJoystick) {
159+
if (_activeJoystick) {
157160
console.log("index changed, ", buttonActionCombo.currentIndex)
158161
console.log("index changed, ", modelData)
159162
console.log("index changed, ", target)
@@ -166,10 +169,14 @@ ColumnLayout {
166169

167170
Component.onCompleted: {
168171
if(_activeJoystick) {
169-
text = parent._getButtonPwm(modelData, true)
172+
text = pwmSettings._getButtonPwm(modelData, true)
170173
}
171174
}
172-
onEditingFinished: parent._setButtonPwm(modelData, true, text)
175+
onEditingFinished: {
176+
// setButtonPwm calculates proper value and we set it back
177+
var pwm = pwmSettings._setButtonPwm(modelData, true, text)
178+
lowPwmValue.text = pwm;
179+
}
173180

174181
}
175182
QGCLabel {
@@ -201,15 +208,21 @@ ColumnLayout {
201208

202209
Component.onCompleted: {
203210
if(_activeJoystick) {
204-
text = parent._getButtonPwm(modelData, false)
211+
text = pwmSettings._getButtonPwm(modelData, false)
205212
}
206213
}
207-
onEditingFinished: parent._setButtonPwm(modelData, false, text)
214+
onEditingFinished: {
215+
// setButtonPwm calculates proper value and we set it back
216+
var pwm = pwmSettings._setButtonPwm(modelData, false, text)
217+
highPwmValue.text = pwm;
218+
}
208219
}
220+
209221
QGCCheckBox {
210222
id: latchCheck
211223
text: qsTr("Latch")
212224
anchors.verticalCenter: parent.verticalCenter
225+
enabled: pwmSettings._latchEnabled(modelData)
213226

214227
onClicked: {
215228
_activeJoystick.setButtonPwmLatch(modelData, checked)

0 commit comments

Comments
 (0)