Browse Source

The button/axis/hat readings are now compared to the previous readings and signals are only emitted if they changed. The joystickChanged signal is still emitted every sample period.

QGC4.4
Bryant 12 years ago
parent
commit
7b5b09a27d
  1. 55
      src/input/JoystickInput.cc
  2. 2
      src/input/JoystickInput.h

55
src/input/JoystickInput.cc

@ -26,17 +26,17 @@
* @image html http://pixhawk.ethz.ch/wiki/_media/standards/body-frame.png Aeronautical frame * @image html http://pixhawk.ethz.ch/wiki/_media/standards/body-frame.png Aeronautical frame
*/ */
JoystickInput::JoystickInput() : JoystickInput::JoystickInput() :
sdlJoystickMin(-32768.0f), sdlJoystickMin(-32768.0f),
sdlJoystickMax(32767.0f), sdlJoystickMax(32767.0f),
uas(NULL), uas(NULL),
done(false), done(false),
rollAxis(-1), rollAxis(-1),
pitchAxis(-1), pitchAxis(-1),
yawAxis(-1), yawAxis(-1),
throttleAxis(-1), throttleAxis(-1),
joystickName(""), joystickName(""),
joystickNumButtons(0), joystickID(-1),
joystickID(-1) joystickNumButtons(0)
{ {
loadSettings(); loadSettings();
@ -150,7 +150,6 @@ void JoystickInput::init()
SDL_Joystick* x = SDL_JoystickOpen(i); SDL_Joystick* x = SDL_JoystickOpen(i);
qDebug() << QString("Number of Axes: %1").arg(QString::number(SDL_JoystickNumAxes(x))); qDebug() << QString("Number of Axes: %1").arg(QString::number(SDL_JoystickNumAxes(x)));
qDebug() << QString("Number of Buttons: %1").arg(QString::number(SDL_JoystickNumButtons(x))); qDebug() << QString("Number of Buttons: %1").arg(QString::number(SDL_JoystickNumButtons(x)));
qDebug() << QString("Number of Balls: %1").arg(QString::number(SDL_JoystickNumBalls(x)));
SDL_JoystickClose(x); SDL_JoystickClose(x);
} }
@ -199,8 +198,11 @@ void JoystickInput::run()
// Bound rounding errors // Bound rounding errors
if (axisValue > 1.0f) axisValue = 1.0f; if (axisValue > 1.0f) axisValue = 1.0f;
if (axisValue < -1.0f) axisValue = -1.0f; if (axisValue < -1.0f) axisValue = -1.0f;
joystickAxes[i] = axisValue; if (joystickAxes[i] != axisValue)
emit axisValueChanged(i, axisValue); {
joystickAxes[i] = axisValue;
emit axisValueChanged(i, axisValue);
}
} }
// Build up vectors describing the hat position // Build up vectors describing the hat position
@ -215,8 +217,8 @@ void JoystickInput::run()
{ {
xHat = newXHat; xHat = newXHat;
yHat = newYHat; yHat = newYHat;
emit hatDirectionChanged(newXHat, newYHat);
} }
emit hatDirectionChanged(newXHat, newYHat);
// Emit signals for each button individually // Emit signals for each button individually
for (int i = 0; i < joystickNumButtons; i++) for (int i = 0; i < joystickNumButtons; i++)
@ -258,20 +260,31 @@ void JoystickInput::setActiveJoystick(int id)
joystickID = id; joystickID = id;
joystick = SDL_JoystickOpen(joystickID); joystick = SDL_JoystickOpen(joystickID);
if (joystick) if (joystick && SDL_JoystickOpened(joystickID))
{ {
SDL_JoystickUpdate();
// Update joystick configuration. // Update joystick configuration.
joystickName = QString(SDL_JoystickName(joystickID)); joystickName = QString(SDL_JoystickName(joystickID));
joystickNumButtons = SDL_JoystickNumButtons(joystick); joystickNumButtons = SDL_JoystickNumButtons(joystick);
joystickNumAxes = SDL_JoystickNumAxes(joystick); joystickNumAxes = SDL_JoystickNumAxes(joystick);
// Reset cached joystick values // Update cached joystick values
joystickAxes.clear(); joystickAxes.clear();
while (joystickAxes.size() < joystickNumAxes) for (int i = 0; i < joystickNumAxes; i++)
{ {
joystickAxes.append(0); int axisValue = SDL_JoystickGetAxis(joystick, i);
joystickAxes.append(axisValue);
emit axisValueChanged(i, axisValue);
} }
joystickButtons = 0; joystickButtons = 0;
for (int i = 0; i < joystickNumButtons; i++)
{
if (SDL_JoystickGetButton(joystick, i))
{
emit buttonPressed(i);
joystickButtons |= 1 << i;
}
}
qDebug() << QString("Switching to joystick '%1' with %2 buttons/%3 axes").arg(joystickName, QString::number(joystickNumButtons), QString::number(joystickNumAxes)); qDebug() << QString("Switching to joystick '%1' with %2 buttons/%3 axes").arg(joystickName, QString::number(joystickNumButtons), QString::number(joystickNumAxes));
} }
else else
@ -283,9 +296,9 @@ void JoystickInput::setActiveJoystick(int id)
float JoystickInput::getCurrentValueForAxis(int axisID) float JoystickInput::getCurrentValueForAxis(int axisID)
{ {
if (joystick && axisID < joystickNumAxes) if (axisID < joystickAxes.size())
{ {
return SDL_JoystickGetAxis(joystick, axisID); return joystickAxes[axisID];
} }
return 0.0f; return 0.0f;
} }

2
src/input/JoystickInput.h

@ -215,7 +215,7 @@ signals:
public slots: public slots:
void setActiveUAS(UASInterface* uas); void setActiveUAS(UASInterface* uas);
/** @brief Switch to a new joystick by ID number. */ /** @brief Switch to a new joystick by ID number. Both buttons and axes are updated with the proper signals emitted. */
void setActiveJoystick(int id); void setActiveJoystick(int id);
void setMappingRollAxis(int axis) void setMappingRollAxis(int axis)

Loading…
Cancel
Save