Commit 682da797 authored by Rustom Jehangir's avatar Rustom Jehangir

Recognize joystick hats and append to the button list.

parent 276b19e4
......@@ -51,13 +51,16 @@ const char* Joystick::_rgFunctionSettingsKey[Joystick::maxFunction] = {
"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__
: _sdlIndex(sdlIndex)
, _exitThread(false)
, _name(name)
, _axisCount(axisCount)
, _buttonCount(buttonCount)
, _hatCount(hatCount)
, _hatButtonCount(4*hatCount)
, _totalButtonCount(_buttonCount+_hatButtonCount)
, _calibrationMode(CalibrationModeOff)
, _rgAxisValues(NULL)
, _rgCalibration(NULL)
......@@ -74,18 +77,19 @@ Joystick::Joystick(const QString& name, int axisCount, int buttonCount, int sdlI
Q_UNUSED(name)
Q_UNUSED(axisCount)
Q_UNUSED(buttonCount)
Q_UNUSED(hatCount)
Q_UNUSED(sdlIndex)
Q_UNUSED(multiVehicleManager)
#else
_rgAxisValues = new int[_axisCount];
_rgCalibration = new Calibration_t[_axisCount];
_rgButtonValues = new bool[_buttonCount];
_rgButtonActions = new QString[_buttonCount];
_rgButtonValues = new bool[_totalButtonCount];
_rgButtonActions = new QString[_totalButtonCount];
for (int i=0; i<_axisCount; i++) {
_rgAxisValues[i] = 0;
}
for (int i=0; i<_buttonCount; i++) {
for (int i=0; i<_totalButtonCount; i++) {
_rgButtonValues[i] = false;
}
......@@ -283,6 +287,21 @@ void Joystick::run(void)
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) {
int axis = _rgFunctionAxis[rollFunction];
......@@ -320,13 +339,13 @@ void Joystick::run(void)
// We only send the buttons the firmwware has reserved
int reservedButtonCount = _activeVehicle->manualControlReservedButtonCount();
if (reservedButtonCount == -1) {
reservedButtonCount = _buttonCount;
reservedButtonCount = _totalButtonCount;
}
quint16 newButtonBits = 0; // New set of button which are down
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;
if (!_rgButtonValues[buttonIndex]) {
......@@ -354,7 +373,7 @@ void Joystick::run(void)
_lastButtonBits = newButtonBits;
qCDebug(JoystickValuesLog) << "name:roll:pitch:yaw:throttle" << name() << roll << -pitch << yaw << throttle;
emit manualControl(roll, -pitch, yaw, throttle, buttonPressedBits, _activeVehicle->joystickMode());
}
......@@ -576,7 +595,7 @@ bool Joystick::_validAxis(int axis)
bool Joystick::_validButton(int button)
{
return button >= 0 && button < _buttonCount;
return button >= 0 && button < _totalButtonCount;
}
#endif // __mobile__
......@@ -39,7 +39,7 @@ class Joystick : public QThread
Q_OBJECT
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();
typedef struct {
......@@ -68,7 +68,7 @@ public:
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(QStringList actions READ actions CONSTANT)
......@@ -82,7 +82,7 @@ public:
// Property accessors
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
void startPolling(Vehicle* vehicle);
......@@ -157,6 +157,9 @@ private:
bool _calibrated;
int _axisCount;
int _buttonCount;
int _hatCount;
int _hatButtonCount;
int _totalButtonCount;
CalibrationMode_t _calibrationMode;
......
......@@ -69,15 +69,16 @@ void JoystickManager::setToolbox(QGCToolbox *toolbox)
QString name = SDL_JoystickName(i);
if (!_name2JoystickMap.contains(name)) {
int axisCount, buttonCount;
int axisCount, buttonCount, hatCount;
SDL_Joystick* sdlJoystick = SDL_JoystickOpen(i);
axisCount = SDL_JoystickNumAxes(sdlJoystick);
buttonCount = SDL_JoystickNumButtons(sdlJoystick);
hatCount = SDL_JoystickNumHats(sdlJoystick);
SDL_JoystickClose(sdlJoystick);
qCDebug(JoystickManagerLog) << "\t" << name << "axes:" << axisCount << "buttons:" << buttonCount;
_name2JoystickMap[name] = new Joystick(name, axisCount, buttonCount, i, _multiVehicleManager);
qCDebug(JoystickManagerLog) << "\t" << name << "axes:" << axisCount << "buttons:" << buttonCount << "hats:" << hatCount;
_name2JoystickMap[name] = new Joystick(name, axisCount, buttonCount, hatCount, i, _multiVehicleManager);
} else {
qCDebug(JoystickManagerLog) << "\tSkipping duplicate" << name;
}
......
......@@ -473,7 +473,7 @@ QGCView {
visible: _activeVehicle.manualControlReservedButtonCount != 0
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 {
......@@ -612,10 +612,10 @@ QGCView {
Repeater {
id: buttonMonitorRepeater
model: _activeJoystick.buttonCount
model: _activeJoystick.totalButtonCount
Rectangle {
width: ScreenTools.defaultFontPixelHeight * 1.5
width: ScreenTools.defaultFontPixelHeight * 1.2
height: width
border.width: 1
border.color: qgcPal.text
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment