From 0b2af266b85f8193f15441c808810cd353dbb984 Mon Sep 17 00:00:00 2001 From: Jacob Walser Date: Tue, 24 Jan 2017 18:16:17 -0500 Subject: [PATCH] Setup gamecontrollers with default calibration --- src/Joystick/Joystick.cc | 32 ++++++++++++++++++++++++++++- src/Joystick/Joystick.h | 26 +++++++++++++++-------- src/Joystick/JoystickSDL.cc | 1 + src/Joystick/JoystickSDL.h | 5 ++++- src/VehicleSetup/JoystickConfig.qml | 1 + 5 files changed, 55 insertions(+), 10 deletions(-) diff --git a/src/Joystick/Joystick.cc b/src/Joystick/Joystick.cc index 19700e9fa..2b217ae48 100644 --- a/src/Joystick/Joystick.cc +++ b/src/Joystick/Joystick.cc @@ -79,6 +79,36 @@ Joystick::~Joystick() delete _rgButtonValues; } +void Joystick::_setDefaultCalibration(void) { + QSettings settings; + settings.beginGroup(_settingsGroup); + settings.beginGroup(_name); + _calibrated = settings.value(_calibratedSettingsKey, false).toBool(); + + // Only set default calibrations if we do not have a calibration for this gamecontroller + if(_calibrated) return; + + for(int axis = 0; axis < _axisCount; axis++) { + Joystick::Calibration_t calibration; + _rgCalibration[axis] = calibration; + } + + _rgCalibration[1].reversed = true; + _rgCalibration[3].reversed = true; + + for(int function = 0; function < maxFunction; function++) { + _rgFunctionAxis[function] = function; + } + + _exponential = false; + _accumulator = false; + _deadband = false; + _throttleMode = ThrottleModeCenterZero; + _calibrated = true; + + _saveSettings(); +} + void Joystick::_loadSettings(void) { QSettings settings; @@ -102,7 +132,7 @@ void Joystick::_loadSettings(void) _throttleMode = (ThrottleMode_t)settings.value(_throttleModeSettingsKey, ThrottleModeCenterZero).toInt(&convertOk); badSettings |= !convertOk; - qCDebug(JoystickLog) << "_loadSettings calibrated:throttlemode:exponential:deadband:badsettings" << _calibrated << _throttleMode << _exponential << _deadband << badSettings; + qCDebug(JoystickLog) << "_loadSettings calibrated:txmode:throttlemode:exponential:deadband:badsettings" << _calibrated << _transmitterMode << _throttleMode << _exponential << _deadband << badSettings; QString minTpl ("Axis%1Min"); QString maxTpl ("Axis%1Max"); diff --git a/src/Joystick/Joystick.h b/src/Joystick/Joystick.h index bef89146f..521f8866a 100644 --- a/src/Joystick/Joystick.h +++ b/src/Joystick/Joystick.h @@ -30,12 +30,18 @@ public: ~Joystick(); - typedef struct { + typedef struct Calibration_t { int min; int max; int center; int deadband; bool reversed; + Calibration_t() + : min(-32767) + , max(32767) + , center(0) + , deadband(0) + , reversed(false) {} } Calibration_t; typedef enum { @@ -68,7 +74,8 @@ public: Q_PROPERTY(int throttleMode READ throttleMode WRITE setThrottleMode NOTIFY throttleModeChanged) Q_PROPERTY(bool exponential READ exponential WRITE setExponential NOTIFY exponentialChanged) Q_PROPERTY(bool accumulator READ accumulator WRITE setAccumulator NOTIFY accumulatorChanged) - + Q_PROPERTY(bool requiresCalibration READ requiresCalibration CONSTANT) + // Property accessors int axisCount(void) { return _axisCount; } @@ -89,6 +96,8 @@ public: QString name(void) { return _name; } + virtual bool requiresCalibration(void) { return true; } + int throttleMode(void); void setThrottleMode(int mode); @@ -144,12 +153,13 @@ signals: void buttonActionTriggered(int action); protected: - void _saveSettings(void); - void _loadSettings(void); - float _adjustRange(int value, Calibration_t calibration, bool withDeadbands); - void _buttonAction(const QString& action); - bool _validAxis(int axis); - bool _validButton(int button); + void _setDefaultCalibration(void); + void _saveSettings(void); + void _loadSettings(void); + float _adjustRange(int value, Calibration_t calibration, bool withDeadbands); + void _buttonAction(const QString& action); + bool _validAxis(int axis); + bool _validButton(int button); private: virtual bool _open() = 0; diff --git a/src/Joystick/JoystickSDL.cc b/src/Joystick/JoystickSDL.cc index 303c3005c..59798e268 100644 --- a/src/Joystick/JoystickSDL.cc +++ b/src/Joystick/JoystickSDL.cc @@ -10,6 +10,7 @@ JoystickSDL::JoystickSDL(const QString& name, int axisCount, int buttonCount, in , _isGameController(isGameController) , _index(index) { + if(_isGameController) _setDefaultCalibration(); } QMap JoystickSDL::discover(MultiVehicleManager* _multiVehicleManager) { diff --git a/src/Joystick/JoystickSDL.h b/src/Joystick/JoystickSDL.h index 2c4a9ca6a..36d966e28 100644 --- a/src/Joystick/JoystickSDL.h +++ b/src/Joystick/JoystickSDL.h @@ -12,7 +12,10 @@ class JoystickSDL : public Joystick public: JoystickSDL(const QString& name, int axisCount, int buttonCount, int hatCount, int index, bool isGameController, MultiVehicleManager* multiVehicleManager); - static QMap discover(MultiVehicleManager* _multiVehicleManager); + static QMap discover(MultiVehicleManager* _multiVehicleManager); + + // This can be uncommented to hide the calibration buttons for gamecontrollers in the future + // bool requiresCalibration(void) final { return !_isGameController; } private: static void _loadGameControllerMappings(); diff --git a/src/VehicleSetup/JoystickConfig.qml b/src/VehicleSetup/JoystickConfig.qml index ce68595db..a204c16c1 100644 --- a/src/VehicleSetup/JoystickConfig.qml +++ b/src/VehicleSetup/JoystickConfig.qml @@ -302,6 +302,7 @@ SetupPage { // Command Buttons Row { spacing: 10 + visible: _activeJoystick.requiresCalibration QGCButton { id: skipButton -- 2.22.0