Commit 8999151e authored by Bryant's avatar Bryant

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
......@@ -35,8 +35,8 @@ JoystickInput::JoystickInput() :
yawAxis(-1),
throttleAxis(-1),
joystickName(""),
joystickNumButtons(0),
joystickID(-1)
joystickID(-1),
joystickNumButtons(0)
{
loadSettings();
......@@ -150,7 +150,6 @@ void JoystickInput::init()
SDL_Joystick* x = SDL_JoystickOpen(i);
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 Balls: %1").arg(QString::number(SDL_JoystickNumBalls(x)));
SDL_JoystickClose(x);
}
......@@ -199,9 +198,12 @@ void JoystickInput::run()
// Bound rounding errors
if (axisValue > 1.0f) axisValue = 1.0f;
if (axisValue < -1.0f) axisValue = -1.0f;
if (joystickAxes[i] != axisValue)
{
joystickAxes[i] = axisValue;
emit axisValueChanged(i, axisValue);
}
}
// Build up vectors describing the hat position
int hatPosition = SDL_JoystickGetHat(joystick, 0);
......@@ -215,8 +217,8 @@ void JoystickInput::run()
{
xHat = newXHat;
yHat = newYHat;
}
emit hatDirectionChanged(newXHat, newYHat);
}
// Emit signals for each button individually
for (int i = 0; i < joystickNumButtons; i++)
......@@ -258,20 +260,31 @@ void JoystickInput::setActiveJoystick(int id)
joystickID = id;
joystick = SDL_JoystickOpen(joystickID);
if (joystick)
if (joystick && SDL_JoystickOpened(joystickID))
{
SDL_JoystickUpdate();
// Update joystick configuration.
joystickName = QString(SDL_JoystickName(joystickID));
joystickNumButtons = SDL_JoystickNumButtons(joystick);
joystickNumAxes = SDL_JoystickNumAxes(joystick);
// Reset cached joystick values
// Update cached joystick values
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;
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));
}
else
......@@ -283,9 +296,9 @@ void JoystickInput::setActiveJoystick(int id)
float JoystickInput::getCurrentValueForAxis(int axisID)
{
if (joystick && axisID < joystickNumAxes)
if (axisID < joystickAxes.size())
{
return SDL_JoystickGetAxis(joystick, axisID);
return joystickAxes[axisID];
}
return 0.0f;
}
......@@ -215,7 +215,7 @@ signals:
public slots:
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 setMappingRollAxis(int axis)
......
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