Skip to content

Commit

Permalink
Multibutton channels
Browse files Browse the repository at this point in the history
  • Loading branch information
zdanek committed Mar 16, 2023
1 parent 5803575 commit 4e1bc81
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 24 deletions.
49 changes: 36 additions & 13 deletions src/Joystick/Joystick.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}
15 changes: 13 additions & 2 deletions src/Joystick/Joystick.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand Down
31 changes: 22 additions & 9 deletions src/VehicleSetup/JoystickConfigButtons.qml
Original file line number Diff line number Diff line change
Expand Up @@ -119,30 +119,33 @@ ColumnLayout {
visible: _activeJoystick ? _activeJoystick.pwmVisibilities[modelData] : false

function _setButtonPwm(button, isLow, pwm) {
var pwmValue = -1;
if(_activeJoystick) {
if (pwm < 1000) {
pwm = 1000;
}
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 {
id: lowPwmLabel
text: qsTr("Low")
anchors.verticalCenter: parent.verticalCenter
}

QGCTextField {
id: lowPwmValue
width: ScreenTools.defaultFontPixelWidth * 10
Expand All @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 4e1bc81

Please sign in to comment.