Commit 935e1ca9 authored by Don Gagne's avatar Don Gagne Committed by GitHub

Merge pull request #3511 from bluerobotics/windowsJoystickHat

Recognize joystick hats and append to the button list (rev 2)
parents 23bf5b00 5d552835
......@@ -30,11 +30,14 @@ const char* Joystick::_rgFunctionSettingsKey[Joystick::maxFunction] = {
"ThrottleAxis"
};
Joystick::Joystick(const QString& name, int axisCount, int buttonCount, MultiVehicleManager* multiVehicleManager)
Joystick::Joystick(const QString& name, int axisCount, int buttonCount, int hatCount, MultiVehicleManager* multiVehicleManager)
: _exitThread(false)
, _name(name)
, _axisCount(axisCount)
, _buttonCount(buttonCount)
, _hatCount(hatCount)
, _hatButtonCount(4*hatCount)
, _totalButtonCount(_buttonCount+_hatButtonCount)
, _calibrationMode(CalibrationModeOff)
, _rgAxisValues(NULL)
, _rgCalibration(NULL)
......@@ -46,15 +49,16 @@ Joystick::Joystick(const QString& name, int axisCount, int buttonCount, MultiVeh
, _pollingStartedForCalibration(false)
, _multiVehicleManager(multiVehicleManager)
{
_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;
}
......@@ -242,6 +246,21 @@ void Joystick::run(void)
emit rawButtonPressedChanged(buttonIndex, newButtonValue);
}
}
// Update hat - append hat buttons to the end of the normal button list
int numHatButtons = 4;
for (int hatIndex=0; hatIndex<_hatCount; hatIndex++) {
for (int hatButtonIndex=0; hatButtonIndex<numHatButtons; hatButtonIndex++) {
// Create new index value that includes the normal button list
int rgButtonValueIndex = hatIndex*numHatButtons + hatButtonIndex + _buttonCount;
// Get hat value from joystick
bool newButtonValue = _getHat(hatIndex,hatButtonIndex);
if (newButtonValue != _rgButtonValues[rgButtonValueIndex]) {
_rgButtonValues[rgButtonValueIndex] = newButtonValue;
emit rawButtonPressedChanged(rgButtonValueIndex, newButtonValue);
}
}
}
if (_calibrationMode != CalibrationModeCalibrating) {
int axis = _rgFunctionAxis[rollFunction];
......@@ -279,13 +298,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]) {
......@@ -313,7 +332,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());
}
......@@ -535,6 +554,6 @@ bool Joystick::_validAxis(int axis)
bool Joystick::_validButton(int button)
{
return button >= 0 && button < _buttonCount;
return button >= 0 && button < _totalButtonCount;
}
......@@ -26,7 +26,8 @@ class Joystick : public QThread
Q_OBJECT
public:
Joystick(const QString& name, int axisCount, int buttonCount, MultiVehicleManager* multiVehicleManager);
Joystick(const QString& name, int axisCount, int buttonCount, int hatCount, MultiVehicleManager* multiVehicleManager);
~Joystick();
typedef struct {
......@@ -54,7 +55,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)
......@@ -68,7 +69,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);
......@@ -138,6 +139,7 @@ private:
virtual bool _getButton(int i) = 0;
virtual int _getAxis(int i) = 0;
virtual uint8_t _getHat(int hat,int i) = 0;
// Override from QThread
virtual void run(void);
......@@ -150,6 +152,9 @@ protected:
bool _calibrated;
int _axisCount;
int _buttonCount;
int _hatCount;
int _hatButtonCount;
int _totalButtonCount;
CalibrationMode_t _calibrationMode;
......
......@@ -4,8 +4,8 @@
#include <QQmlEngine>
JoystickSDL::JoystickSDL(const QString& name, int axisCount, int buttonCount, int index, MultiVehicleManager* multiVehicleManager)
: Joystick(name,axisCount,buttonCount,multiVehicleManager)
JoystickSDL::JoystickSDL(const QString& name, int axisCount, int buttonCount, int hatCount, int index, MultiVehicleManager* multiVehicleManager)
: Joystick(name,axisCount,buttonCount,hatCount,multiVehicleManager)
, _index(index)
{
}
......@@ -26,15 +26,16 @@ QMap<QString, Joystick*> JoystickSDL::discover(MultiVehicleManager* _multiVehicl
QString name = SDL_JoystickName(i);
if (!ret.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(JoystickLog) << "\t" << name << "axes:" << axisCount << "buttons:" << buttonCount;
ret[name] = new JoystickSDL(name, axisCount, buttonCount, i, _multiVehicleManager);
qCDebug(JoystickLog) << "\t" << name << "axes:" << axisCount << "buttons:" << buttonCount << "hats:" << hatCount;
ret[name] = new JoystickSDL(name, axisCount, buttonCount, hatCount, i, _multiVehicleManager);
} else {
qCDebug(JoystickLog) << "\tSkipping duplicate" << name;
}
......@@ -71,3 +72,12 @@ int JoystickSDL::_getAxis(int i) {
return SDL_JoystickGetAxis(sdlJoystick, i);
}
uint8_t JoystickSDL::_getHat(int hat,int i) {
uint8_t hatButtons[] = {SDL_HAT_UP,SDL_HAT_DOWN,SDL_HAT_LEFT,SDL_HAT_RIGHT};
if ( i < int(sizeof(hatButtons)) ) {
return !!(SDL_JoystickGetHat(sdlJoystick, hat) & hatButtons[i]);
}
return 0;
}
......@@ -15,7 +15,7 @@
class JoystickSDL : public Joystick
{
public:
JoystickSDL(const QString& name, int axisCount, int buttonCount, int index, MultiVehicleManager* multiVehicleManager);
JoystickSDL(const QString& name, int axisCount, int buttonCount, int hatCount, int index, MultiVehicleManager* multiVehicleManager);
static QMap<QString, Joystick*> discover(MultiVehicleManager* _multiVehicleManager);
......@@ -26,6 +26,7 @@ private:
bool _getButton(int i) final;
int _getAxis(int i) final;
uint8_t _getHat(int hat,int i) final;
SDL_Joystick *sdlJoystick;
int _index; ///< Index for SDL_JoystickOpen
......
......@@ -460,7 +460,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 {
......@@ -599,10 +599,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