Browse Source

Recognize joystick hats and append to the button list.

QGC4.4
Rustom Jehangir 9 years ago
parent
commit
682da79759
  1. 35
      src/Joystick/Joystick.cc
  2. 9
      src/Joystick/Joystick.h
  3. 7
      src/Joystick/JoystickManager.cc
  4. 6
      src/VehicleSetup/JoystickConfig.qml

35
src/Joystick/Joystick.cc

@ -51,13 +51,16 @@ const char* Joystick::_rgFunctionSettingsKey[Joystick::maxFunction] = {
"ThrottleAxis" "ThrottleAxis"
}; };
Joystick::Joystick(const QString& name, int axisCount, int buttonCount, int sdlIndex, MultiVehicleManager* multiVehicleManager) Joystick::Joystick(const QString& name, int axisCount, int buttonCount, int hatCount, int sdlIndex, MultiVehicleManager* multiVehicleManager)
#ifndef __mobile__ #ifndef __mobile__
: _sdlIndex(sdlIndex) : _sdlIndex(sdlIndex)
, _exitThread(false) , _exitThread(false)
, _name(name) , _name(name)
, _axisCount(axisCount) , _axisCount(axisCount)
, _buttonCount(buttonCount) , _buttonCount(buttonCount)
, _hatCount(hatCount)
, _hatButtonCount(4*hatCount)
, _totalButtonCount(_buttonCount+_hatButtonCount)
, _calibrationMode(CalibrationModeOff) , _calibrationMode(CalibrationModeOff)
, _rgAxisValues(NULL) , _rgAxisValues(NULL)
, _rgCalibration(NULL) , _rgCalibration(NULL)
@ -74,18 +77,19 @@ Joystick::Joystick(const QString& name, int axisCount, int buttonCount, int sdlI
Q_UNUSED(name) Q_UNUSED(name)
Q_UNUSED(axisCount) Q_UNUSED(axisCount)
Q_UNUSED(buttonCount) Q_UNUSED(buttonCount)
Q_UNUSED(hatCount)
Q_UNUSED(sdlIndex) Q_UNUSED(sdlIndex)
Q_UNUSED(multiVehicleManager) Q_UNUSED(multiVehicleManager)
#else #else
_rgAxisValues = new int[_axisCount]; _rgAxisValues = new int[_axisCount];
_rgCalibration = new Calibration_t[_axisCount]; _rgCalibration = new Calibration_t[_axisCount];
_rgButtonValues = new bool[_buttonCount]; _rgButtonValues = new bool[_totalButtonCount];
_rgButtonActions = new QString[_buttonCount]; _rgButtonActions = new QString[_totalButtonCount];
for (int i=0; i<_axisCount; i++) { for (int i=0; i<_axisCount; i++) {
_rgAxisValues[i] = 0; _rgAxisValues[i] = 0;
} }
for (int i=0; i<_buttonCount; i++) { for (int i=0; i<_totalButtonCount; i++) {
_rgButtonValues[i] = false; _rgButtonValues[i] = false;
} }
@ -283,6 +287,21 @@ void Joystick::run(void)
emit rawButtonPressedChanged(buttonIndex, newButtonValue); emit rawButtonPressedChanged(buttonIndex, newButtonValue);
} }
} }
// Update hat - append hat buttons to the end of the normal button list
quint8 hatButtons[] = {SDL_HAT_UP,SDL_HAT_DOWN,SDL_HAT_LEFT,SDL_HAT_RIGHT};
for (int hatIndex=0; hatIndex<_hatCount; hatIndex++) {
for (int hatButtonIndex=0; hatButtonIndex<int(sizeof(hatButtons)); hatButtonIndex++) {
// Create new index value that includes the normal button list
int rgButtonValueIndex = hatIndex*sizeof(hatButtons) + hatButtonIndex + _buttonCount;
// Get hat value from SDL - Only recognize the values in hatButtonMask
bool newButtonValue = !!(SDL_JoystickGetHat(sdlJoystick, hatIndex) & hatButtons[hatButtonIndex]);
if (newButtonValue != _rgButtonValues[rgButtonValueIndex]) {
_rgButtonValues[rgButtonValueIndex] = newButtonValue;
emit rawButtonPressedChanged(rgButtonValueIndex, newButtonValue);
}
}
}
if (_calibrationMode != CalibrationModeCalibrating) { if (_calibrationMode != CalibrationModeCalibrating) {
int axis = _rgFunctionAxis[rollFunction]; int axis = _rgFunctionAxis[rollFunction];
@ -320,13 +339,13 @@ void Joystick::run(void)
// We only send the buttons the firmwware has reserved // We only send the buttons the firmwware has reserved
int reservedButtonCount = _activeVehicle->manualControlReservedButtonCount(); int reservedButtonCount = _activeVehicle->manualControlReservedButtonCount();
if (reservedButtonCount == -1) { if (reservedButtonCount == -1) {
reservedButtonCount = _buttonCount; reservedButtonCount = _totalButtonCount;
} }
quint16 newButtonBits = 0; // New set of button which are down quint16 newButtonBits = 0; // New set of button which are down
quint16 buttonPressedBits = 0; // Buttons pressed for manualControl signal quint16 buttonPressedBits = 0; // Buttons pressed for manualControl signal
for (int buttonIndex=0; buttonIndex<_buttonCount; buttonIndex++) { for (int buttonIndex=0; buttonIndex<_totalButtonCount; buttonIndex++) {
quint16 buttonBit = 1 << buttonIndex; quint16 buttonBit = 1 << buttonIndex;
if (!_rgButtonValues[buttonIndex]) { if (!_rgButtonValues[buttonIndex]) {
@ -354,7 +373,7 @@ void Joystick::run(void)
_lastButtonBits = newButtonBits; _lastButtonBits = newButtonBits;
qCDebug(JoystickValuesLog) << "name:roll:pitch:yaw:throttle" << name() << roll << -pitch << yaw << throttle; qCDebug(JoystickValuesLog) << "name:roll:pitch:yaw:throttle" << name() << roll << -pitch << yaw << throttle;
emit manualControl(roll, -pitch, yaw, throttle, buttonPressedBits, _activeVehicle->joystickMode()); emit manualControl(roll, -pitch, yaw, throttle, buttonPressedBits, _activeVehicle->joystickMode());
} }
@ -576,7 +595,7 @@ bool Joystick::_validAxis(int axis)
bool Joystick::_validButton(int button) bool Joystick::_validButton(int button)
{ {
return button >= 0 && button < _buttonCount; return button >= 0 && button < _totalButtonCount;
} }
#endif // __mobile__ #endif // __mobile__

9
src/Joystick/Joystick.h

@ -39,7 +39,7 @@ class Joystick : public QThread
Q_OBJECT Q_OBJECT
public: public:
Joystick(const QString& name, int axisCount, int buttonCount, int sdlIndex, MultiVehicleManager* multiVehicleManager); Joystick(const QString& name, int axisCount, int buttonCount, int hatCount, int sdlIndex, MultiVehicleManager* multiVehicleManager);
~Joystick(); ~Joystick();
typedef struct { typedef struct {
@ -68,7 +68,7 @@ public:
Q_PROPERTY(bool calibrated MEMBER _calibrated NOTIFY calibratedChanged) Q_PROPERTY(bool calibrated MEMBER _calibrated NOTIFY calibratedChanged)
Q_PROPERTY(int buttonCount READ buttonCount CONSTANT) Q_PROPERTY(int totalButtonCount READ totalButtonCount CONSTANT)
Q_PROPERTY(int axisCount READ axisCount CONSTANT) Q_PROPERTY(int axisCount READ axisCount CONSTANT)
Q_PROPERTY(QStringList actions READ actions CONSTANT) Q_PROPERTY(QStringList actions READ actions CONSTANT)
@ -82,7 +82,7 @@ public:
// Property accessors // Property accessors
int axisCount(void) { return _axisCount; } int axisCount(void) { return _axisCount; }
int buttonCount(void) { return _buttonCount; } int totalButtonCount(void) { return _totalButtonCount; }
/// Start the polling thread which will in turn emit joystick signals /// Start the polling thread which will in turn emit joystick signals
void startPolling(Vehicle* vehicle); void startPolling(Vehicle* vehicle);
@ -157,6 +157,9 @@ private:
bool _calibrated; bool _calibrated;
int _axisCount; int _axisCount;
int _buttonCount; int _buttonCount;
int _hatCount;
int _hatButtonCount;
int _totalButtonCount;
CalibrationMode_t _calibrationMode; CalibrationMode_t _calibrationMode;

7
src/Joystick/JoystickManager.cc

@ -69,15 +69,16 @@ void JoystickManager::setToolbox(QGCToolbox *toolbox)
QString name = SDL_JoystickName(i); QString name = SDL_JoystickName(i);
if (!_name2JoystickMap.contains(name)) { if (!_name2JoystickMap.contains(name)) {
int axisCount, buttonCount; int axisCount, buttonCount, hatCount;
SDL_Joystick* sdlJoystick = SDL_JoystickOpen(i); SDL_Joystick* sdlJoystick = SDL_JoystickOpen(i);
axisCount = SDL_JoystickNumAxes(sdlJoystick); axisCount = SDL_JoystickNumAxes(sdlJoystick);
buttonCount = SDL_JoystickNumButtons(sdlJoystick); buttonCount = SDL_JoystickNumButtons(sdlJoystick);
hatCount = SDL_JoystickNumHats(sdlJoystick);
SDL_JoystickClose(sdlJoystick); SDL_JoystickClose(sdlJoystick);
qCDebug(JoystickManagerLog) << "\t" << name << "axes:" << axisCount << "buttons:" << buttonCount; qCDebug(JoystickManagerLog) << "\t" << name << "axes:" << axisCount << "buttons:" << buttonCount << "hats:" << hatCount;
_name2JoystickMap[name] = new Joystick(name, axisCount, buttonCount, i, _multiVehicleManager); _name2JoystickMap[name] = new Joystick(name, axisCount, buttonCount, hatCount, i, _multiVehicleManager);
} else { } else {
qCDebug(JoystickManagerLog) << "\tSkipping duplicate" << name; qCDebug(JoystickManagerLog) << "\tSkipping duplicate" << name;
} }

6
src/VehicleSetup/JoystickConfig.qml

@ -473,7 +473,7 @@ QGCView {
visible: _activeVehicle.manualControlReservedButtonCount != 0 visible: _activeVehicle.manualControlReservedButtonCount != 0
text: qsTr("Buttons 0-%1 reserved for firmware use").arg(reservedButtonCount) text: qsTr("Buttons 0-%1 reserved for firmware use").arg(reservedButtonCount)
property int reservedButtonCount: _activeVehicle.manualControlReservedButtonCount == -1 ? _activeJoystick.buttonCount : _activeVehicle.manualControlReservedButtonCount property int reservedButtonCount: _activeVehicle.manualControlReservedButtonCount == -1 ? _activeJoystick.totalButtonCount : _activeVehicle.manualControlReservedButtonCount
} }
Repeater { Repeater {
@ -612,10 +612,10 @@ QGCView {
Repeater { Repeater {
id: buttonMonitorRepeater id: buttonMonitorRepeater
model: _activeJoystick.buttonCount model: _activeJoystick.totalButtonCount
Rectangle { Rectangle {
width: ScreenTools.defaultFontPixelHeight * 1.5 width: ScreenTools.defaultFontPixelHeight * 1.2
height: width height: width
border.width: 1 border.width: 1
border.color: qgcPal.text border.color: qgcPal.text

Loading…
Cancel
Save