Skip to content
Snippets Groups Projects
Commit 8999151e authored by Bryant's avatar Bryant
Browse files

The button/axis/hat readings are now compared to the previous readings and...

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.
parent 47cd05ea
No related branches found
No related tags found
No related merge requests found
...@@ -26,17 +26,17 @@ ...@@ -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() ...@@ -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() ...@@ -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() ...@@ -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) ...@@ -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) ...@@ -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;
} }
...@@ -215,7 +215,7 @@ signals: ...@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment