Commit c532f413 authored by Bryant's avatar Bryant

The JoystickInput class now emits signals for both button press and button release events.

parent 7908dc7d
...@@ -299,35 +299,29 @@ void JoystickInput::run() ...@@ -299,35 +299,29 @@ void JoystickInput::run()
// Send new values to rest of groundstation // Send new values to rest of groundstation
emit hatDirectionChanged(xHat, yHat); emit hatDirectionChanged(xHat, yHat);
// Display all buttons // Emit signals for each button individually
int buttons = 0; for (int i = 0; i < SDL_JoystickNumButtons(joystick); i++)
for(int i = 0; i < SDL_JoystickNumButtons(joystick); i++)
{ {
//qDebug() << "BUTTON" << i << "is: " << SDL_JoystickGetAxis(joystick, i); // If the button was down, but now it's up, trigger a buttonPressed event
if(SDL_JoystickGetButton(joystick, i)) quint16 lastButtonState = buttonState & (1 << i);
if (SDL_JoystickGetButton(joystick, i) && !lastButtonState)
{ {
emit buttonPressed(i); emit buttonPressed(i);
buttons |= 1 << i; buttonState |= 1 << i;
// Check if button is a UAS select button }
else if (!SDL_JoystickGetButton(joystick, i) && lastButtonState)
if (uasButtonList.contains(i)) {
{ emit buttonReleased(i);
UASInterface* uas = UASManager::instance()->getUASForId(i); buttonState &= ~(1 << i);
if (uas)
{
UASManager::instance()->setActiveUAS(uas);
}
}
} }
} }
emit joystickChanged(y, x, yaw, thrust, xHat, yHat, buttons);
// Now signal an update for all UI together.
emit joystickChanged(y, x, yaw, thrust, xHat, yHat, buttonState);
// Sleep, update rate of joystick is approx. 50 Hz (1000 ms / 50 = 20 ms) // Sleep, update rate of joystick is approx. 50 Hz (1000 ms / 50 = 20 ms)
QGC::SLEEP::msleep(20); QGC::SLEEP::msleep(20);
} }
} }
void JoystickInput::setActiveJoystick(int id) void JoystickInput::setActiveJoystick(int id)
...@@ -340,6 +334,7 @@ void JoystickInput::setActiveJoystick(int id) ...@@ -340,6 +334,7 @@ void JoystickInput::setActiveJoystick(int id)
joystickButtons = SDL_JoystickNumButtons(joystick); joystickButtons = SDL_JoystickNumButtons(joystick);
qDebug() << QString("Switching to joystick '%1' with %2 buttons").arg(joystickName, QString::number(joystickButtons)); qDebug() << QString("Switching to joystick '%1' with %2 buttons").arg(joystickName, QString::number(joystickButtons));
} }
buttonState = 0;
} }
const QString& JoystickInput::getName() const QString& JoystickInput::getName()
......
...@@ -150,6 +150,7 @@ protected: ...@@ -150,6 +150,7 @@ protected:
int joystickButtons; int joystickButtons;
int joystickID; int joystickID;
int joysticksFound; int joysticksFound;
quint16 buttonState; ///< Track the state of the buttons so we can trigger on Up and Down events
void init(); void init();
...@@ -196,12 +197,18 @@ signals: ...@@ -196,12 +197,18 @@ signals:
void yawChanged(int yaw); void yawChanged(int yaw);
/** /**
* @brief Joystick button has been pressed * @brief Joystick button has changed state from unpressed to pressed.
*
* @param key index of the pressed key * @param key index of the pressed key
*/ */
void buttonPressed(int key); void buttonPressed(int key);
/**
* @brief Joystick button has changed state from pressed to unpressed.
*
* @param key index of the released key
*/
void buttonReleased(int key);
/** /**
* @brief Hat (8-way switch on the top) has changed position * @brief Hat (8-way switch on the top) has changed position
* *
......
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