From 5e75d2d4f3b29aa704c1b55ab25ef9e25b8494c7 Mon Sep 17 00:00:00 2001 From: Gus Grubba Date: Tue, 30 Jul 2019 15:43:55 -0400 Subject: [PATCH] Handle Button Up state (Joystick) --- src/Joystick/Joystick.cc | 72 ++++++++++++++++++++++++++-------------- src/Joystick/Joystick.h | 2 +- 2 files changed, 48 insertions(+), 26 deletions(-) diff --git a/src/Joystick/Joystick.cc b/src/Joystick/Joystick.cc index 4bf624587..bae1d221f 100644 --- a/src/Joystick/Joystick.cc +++ b/src/Joystick/Joystick.cc @@ -477,9 +477,12 @@ void Joystick::run() void Joystick::_handleButtons() { + int lastBbuttonValues[256]; //-- Update button states for (int buttonIndex = 0; buttonIndex < _buttonCount; buttonIndex++) { bool newButtonValue = _getButton(buttonIndex); + if(buttonIndex < 256) + lastBbuttonValues[buttonIndex] = _rgButtonValues[buttonIndex]; if (newButtonValue && _rgButtonValues[buttonIndex] == BUTTON_UP) { _rgButtonValues[buttonIndex] = BUTTON_DOWN; emit rawButtonPressedChanged(buttonIndex, newButtonValue); @@ -505,7 +508,7 @@ void Joystick::_handleButtons() } } } - //-- Process button press + //-- Process button press/release for (int buttonIndex = 0; buttonIndex < _totalButtonCount; buttonIndex++) { if(_rgButtonValues[buttonIndex] == BUTTON_DOWN || _rgButtonValues[buttonIndex] == BUTTON_REPEAT) { if(_buttonActionArray[buttonIndex]) { @@ -517,7 +520,7 @@ void Joystick::_handleButtons() //-- This button just went down if(_rgButtonValues[buttonIndex] == BUTTON_DOWN) { qCDebug(JoystickLog) << "Single button triggered" << buttonIndex << buttonAction; - _executeButtonAction(buttonAction); + _executeButtonAction(buttonAction, true); } } else { //-- Process repeat buttons @@ -525,12 +528,25 @@ void Joystick::_handleButtons() if(_buttonActionArray[buttonIndex]->buttonTime.elapsed() > buttonDelay) { _buttonActionArray[buttonIndex]->buttonTime.start(); qCDebug(JoystickLog) << "Repeat button triggered" << buttonIndex << buttonAction; - _executeButtonAction(buttonAction); + _executeButtonAction(buttonAction, true); } } } //-- Flag it as processed _rgButtonValues[buttonIndex] = BUTTON_REPEAT; + } else if(_rgButtonValues[buttonIndex] == BUTTON_UP) { + //-- Button up transition + if(buttonIndex < 256) { + if(lastBbuttonValues[buttonIndex] == BUTTON_DOWN || lastBbuttonValues[buttonIndex] == BUTTON_REPEAT) { + if(_buttonActionArray[buttonIndex]) { + QString buttonAction = _buttonActionArray[buttonIndex]->action; + if(buttonAction.isEmpty() || buttonAction == _buttonActionNone) + continue; + qCDebug(JoystickLog) << "Button up" << buttonIndex << buttonAction; + _executeButtonAction(buttonAction, false); + } + } + } } } } @@ -941,51 +957,57 @@ void Joystick::setCalibrationMode(bool calibrating) } -void Joystick::_executeButtonAction(const QString& action) +void Joystick::_executeButtonAction(const QString& action, bool buttonDown) { if (!_activeVehicle || !_activeVehicle->joystickEnabled() || action == _buttonActionNone) { return; } if (action == _buttonActionArm) { - emit setArmed(true); + if (buttonDown) emit setArmed(true); } else if (action == _buttonActionDisarm) { - emit setArmed(false); + if (buttonDown) emit setArmed(false); } else if (action == _buttonActionToggleArm) { - emit setArmed(!_activeVehicle->armed()); + if (buttonDown) emit setArmed(!_activeVehicle->armed()); } else if (action == _buttonActionVTOLFixedWing) { - emit setVtolInFwdFlight(true); + if (buttonDown) emit setVtolInFwdFlight(true); } else if (action == _buttonActionVTOLMultiRotor) { - emit setVtolInFwdFlight(false); + if (buttonDown) emit setVtolInFwdFlight(false); } else if (_activeVehicle->flightModes().contains(action)) { - emit setFlightMode(action); + if (buttonDown) emit setFlightMode(action); } else if(action == _buttonActionContinuousZoomIn || action == _buttonActionContinuousZoomOut) { - emit startContinuousZoom(action == _buttonActionStepZoomIn ? 1 : -1); + if (buttonDown) { + emit startContinuousZoom(action == _buttonActionContinuousZoomIn ? 1 : -1); + } else { + emit stopContinuousZoom(); + } } else if(action == _buttonActionStepZoomIn || action == _buttonActionStepZoomOut) { - emit stepZoom(action == _buttonActionStepZoomIn ? 1 : -1); + if (buttonDown) emit stepZoom(action == _buttonActionStepZoomIn ? 1 : -1); } else if(action == _buttonActionNextStream || action == _buttonActionPreviousStream) { - emit stepStream(action == _buttonActionNextStream ? 1 : -1); + if (buttonDown) emit stepStream(action == _buttonActionNextStream ? 1 : -1); } else if(action == _buttonActionNextCamera || action == _buttonActionPreviousCamera) { - emit stepCamera(action == _buttonActionNextCamera ? 1 : -1); + if (buttonDown) emit stepCamera(action == _buttonActionNextCamera ? 1 : -1); } else if(action == _buttonActionTriggerCamera) { - emit triggerCamera(); + if (buttonDown) emit triggerCamera(); } else if(action == _buttonActionStartVideoRecord) { - emit startVideoRecord(); + if (buttonDown) emit startVideoRecord(); } else if(action == _buttonActionStopVideoRecord) { - emit stopVideoRecord(); + if (buttonDown) emit stopVideoRecord(); } else if(action == _buttonActionToggleVideoRecord) { - emit toggleVideoRecord(); + if (buttonDown) emit toggleVideoRecord(); } else if(action == _buttonActionGimbalUp) { - _pitchStep(1); + if (buttonDown) _pitchStep(1); } else if(action == _buttonActionGimbalDown) { - _pitchStep(-1); + if (buttonDown) _pitchStep(-1); } else if(action == _buttonActionGimbalLeft) { - _yawStep(-1); + if (buttonDown) _yawStep(-1); } else if(action == _buttonActionGimbalRight) { - _yawStep(1); + if (buttonDown) _yawStep(1); } else if(action == _buttonActionGimbalCenter) { - _localPitch = 0.0; - _localYaw = 0.0; - emit gimbalControlValue(0.0, 0.0); + if (buttonDown) { + _localPitch = 0.0; + _localYaw = 0.0; + emit gimbalControlValue(0.0, 0.0); + } } else { qCDebug(JoystickLog) << "_buttonAction unknown action:" << action; } diff --git a/src/Joystick/Joystick.h b/src/Joystick/Joystick.h index 52baa95be..85599b642 100644 --- a/src/Joystick/Joystick.h +++ b/src/Joystick/Joystick.h @@ -225,7 +225,7 @@ protected: void _saveButtonSettings (); void _loadSettings (); float _adjustRange (int value, Calibration_t calibration, bool withDeadbands); - void _executeButtonAction (const QString& action); + void _executeButtonAction (const QString& action, bool buttonDown); int _findAssignableButtonAction(const QString& action); bool _validAxis (int axis); bool _validButton (int button); -- 2.22.0