Browse Source

The hat readings from the joystick now works correctly. Also moved some signals over to using an enum from an int type.

QGC4.4
Bryant 12 years ago
parent
commit
47cd05eaac
  1. 17
      src/input/JoystickInput.cc
  2. 3
      src/ui/JoystickAxis.cc
  3. 3
      src/ui/JoystickAxis.h
  4. 17
      src/ui/JoystickWidget.cc
  5. 23
      src/ui/JoystickWidget.h

17
src/input/JoystickInput.cc

@ -205,11 +205,18 @@ void JoystickInput::run()
// Build up vectors describing the hat position // Build up vectors describing the hat position
int hatPosition = SDL_JoystickGetHat(joystick, 0); int hatPosition = SDL_JoystickGetHat(joystick, 0);
if ((SDL_HAT_UP & hatPosition) > 0) yHat = 1; int newYHat = 0;
if ((SDL_HAT_DOWN & hatPosition) > 0) yHat = -1; if ((SDL_HAT_UP & hatPosition) > 0) newYHat = 1;
if ((SDL_HAT_LEFT & hatPosition) > 0) xHat = -1; if ((SDL_HAT_DOWN & hatPosition) > 0) newYHat = -1;
if ((SDL_HAT_RIGHT & hatPosition) > 0) xHat = 1; int newXHat = 0;
emit hatDirectionChanged(xHat, yHat); if ((SDL_HAT_LEFT & hatPosition) > 0) newXHat = -1;
if ((SDL_HAT_RIGHT & hatPosition) > 0) newXHat = 1;
if (newYHat != yHat || newXHat != xHat)
{
xHat = newXHat;
yHat = 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++)

3
src/ui/JoystickAxis.cc

@ -1,4 +1,5 @@
#include "JoystickAxis.h" #include "JoystickAxis.h"
#include "JoystickInput.h"
#include "ui_JoystickAxis.h" #include "ui_JoystickAxis.h"
#include <QString> #include <QString>
@ -24,5 +25,5 @@ void JoystickAxis::setValue(float value)
void JoystickAxis::mappingComboBoxChanged(int newMapping) void JoystickAxis::mappingComboBoxChanged(int newMapping)
{ {
emit mappingChanged(id, newMapping); emit mappingChanged(id, (JoystickInput::JOYSTICK_INPUT_MAPPING)newMapping);
} }

3
src/ui/JoystickAxis.h

@ -2,6 +2,7 @@
#define JOYSTICKAXIS_H #define JOYSTICKAXIS_H
#include <QWidget> #include <QWidget>
#include "JoystickInput.h"
namespace Ui { namespace Ui {
class JoystickAxis; class JoystickAxis;
@ -17,7 +18,7 @@ public:
signals: signals:
/** @brief Signal a change in this axis' yaw/pitch/roll mapping */ /** @brief Signal a change in this axis' yaw/pitch/roll mapping */
void mappingChanged(int id, int newMapping); void mappingChanged(int id, JoystickInput::JOYSTICK_INPUT_MAPPING newMapping);
public slots: public slots:
/** @brief Update the displayed value of the included progressbar. /** @brief Update the displayed value of the included progressbar.

17
src/ui/JoystickWidget.cc

@ -21,10 +21,11 @@ JoystickWidget::JoystickWidget(JoystickInput* joystick, QWidget *parent) :
// Initialize the UI based on the current joystick // Initialize the UI based on the current joystick
initUI(); initUI();
// Watch for button and hat input events from the joystick. // Watch for button, axis, and hat input events from the joystick.
connect(this->joystick, SIGNAL(buttonPressed(int)), this, SLOT(joystickButtonPressed(int))); connect(this->joystick, SIGNAL(buttonPressed(int)), this, SLOT(joystickButtonPressed(int)));
connect(this->joystick, SIGNAL(buttonReleased(int)), this, SLOT(joystickButtonReleased(int))); connect(this->joystick, SIGNAL(buttonReleased(int)), this, SLOT(joystickButtonReleased(int)));
connect(this->joystick, SIGNAL(axisValueChanged(int,float)), this, SLOT(updateAxisValue(int,float))); connect(this->joystick, SIGNAL(axisValueChanged(int,float)), this, SLOT(updateAxisValue(int,float)));
connect(this->joystick, SIGNAL(hatDirectionChanged(int,int)), this, SLOT(setHat(int,int)));
// Update the UI if the joystick changes. // Update the UI if the joystick changes.
connect(m_ui->joystickNameComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUIForJoystick(int))); connect(m_ui->joystickNameComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUIForJoystick(int)));
@ -131,7 +132,7 @@ void JoystickWidget::updateUIForJoystick(int id)
{ {
JoystickAxis* axis = new JoystickAxis(i, m_ui->axesBox); JoystickAxis* axis = new JoystickAxis(i, m_ui->axesBox);
axis->setValue(joystick->getCurrentValueForAxis(i)); axis->setValue(joystick->getCurrentValueForAxis(i));
connect(axis, SIGNAL(mappingChanged(int,int)), this, SLOT(setMappingAxis(int,int))); connect(axis, SIGNAL(mappingChanged(int,JoystickInput::JOYSTICK_INPUT_MAPPING)), this, SLOT(setMappingAxis(int,JoystickInput::JOYSTICK_INPUT_MAPPING)));
// And make sure we insert BEFORE the vertical spacer. // And make sure we insert BEFORE the vertical spacer.
m_ui->axesLayout->insertWidget(i, axis); m_ui->axesLayout->insertWidget(i, axis);
axes.append(axis); axes.append(axis);
@ -146,9 +147,9 @@ void JoystickWidget::updateAxisValue(int axis, float value)
} }
} }
void JoystickWidget::setHat(float x, float y) void JoystickWidget::setHat(int x, int y)
{ {
updateStatus(tr("Hat position: x: %1, y: %2").arg(x).arg(y)); m_ui->statusLabel->setText(tr("Hat position: x: %1, y: %2").arg(x).arg(y));
} }
void JoystickWidget::setMappingAxis(int axisID, JoystickInput::JOYSTICK_INPUT_MAPPING newMapping) void JoystickWidget::setMappingAxis(int axisID, JoystickInput::JOYSTICK_INPUT_MAPPING newMapping)
@ -167,6 +168,9 @@ void JoystickWidget::setMappingAxis(int axisID, JoystickInput::JOYSTICK_INPUT_MA
case JoystickInput::JOYSTICK_INPUT_MAPPING_THROTTLE: case JoystickInput::JOYSTICK_INPUT_MAPPING_THROTTLE:
joystick->setMappingThrottleAxis(axisID); joystick->setMappingThrottleAxis(axisID);
break; break;
case JoystickInput::JOYSTICK_INPUT_MAPPING_NONE:
default:
break;
} }
} }
@ -180,8 +184,3 @@ void JoystickWidget::joystickButtonReleased(int key)
{ {
buttons.at(key)->setStyleSheet(""); buttons.at(key)->setStyleSheet("");
} }
void JoystickWidget::updateStatus(const QString& status)
{
m_ui->statusLabel->setText(status);
}

23
src/ui/JoystickWidget.h

@ -53,14 +53,25 @@ public:
virtual ~JoystickWidget(); virtual ~JoystickWidget();
public slots: public slots:
/** @brief Update the UI for a new joystick based on SDL ID. */
void updateUIForJoystick(int id);
/** @brief Change the stored mapping for a given axis. */
void setMappingAxis(int axisID, JoystickInput::JOYSTICK_INPUT_MAPPING newMapping);
/**
* @brief Update a given axis with a new value
* @param axis The index of the axis to update.
* @param value The new value for the axis, [-1.0:1.0].
* @see JoystickInput:axisValueChanged
*/
void updateAxisValue(int axis, float value); void updateAxisValue(int axis, float value);
void setHat(float x, float y); /** @brief Update the UI with new values for the hat.
* @see JoystickInput::hatDirectionChanged
*/
void setHat(int x, int y);
/** @brief Trigger a UI change based on a button being pressed */ /** @brief Trigger a UI change based on a button being pressed */
void joystickButtonPressed(int key); void joystickButtonPressed(int key);
/** @brief Trigger a UI change based on a button being released */ /** @brief Trigger a UI change based on a button being released */
void joystickButtonReleased(int key); void joystickButtonReleased(int key);
/** @brief Update status string */
void updateStatus(const QString& status);
/** @brief Update the UI color scheme when the MainWindow theme changes. */ /** @brief Update the UI color scheme when the MainWindow theme changes. */
void styleChanged(MainWindow::QGC_MAINWINDOW_STYLE); void styleChanged(MainWindow::QGC_MAINWINDOW_STYLE);
@ -77,12 +88,6 @@ protected:
/** @brief The color to use for button labels when their corresponding button is pressed */ /** @brief The color to use for button labels when their corresponding button is pressed */
QColor buttonLabelColor; QColor buttonLabelColor;
protected slots:
/** @brief Update the UI for a new joystick based on SDL ID. */
void updateUIForJoystick(int id);
/** @brief Change the stored mapping for a given axis. */
void setMappingAxis(int axisID, JoystickInput::JOYSTICK_INPUT_MAPPING newMapping);
private: private:
Ui::JoystickWidget *m_ui; Ui::JoystickWidget *m_ui;
/** @brief Initialize all dynamic UI elements (button list, joystick names, etc.) */ /** @brief Initialize all dynamic UI elements (button list, joystick names, etc.) */

Loading…
Cancel
Save