Commit 55efdf8d authored by Bryant's avatar Bryant

The settings for each joystick are loaded and saved on switch. The state of...

The settings for each joystick are loaded and saved on switch. The state of the enable joysticks checkbox is not saved yet, however, though it should really be tracked by JoystickInput so that it can sleep more when joysticks are disabled.
parent 860e47b6
...@@ -37,16 +37,15 @@ JoystickInput::JoystickInput() : ...@@ -37,16 +37,15 @@ JoystickInput::JoystickInput() :
throttleAxis(-1), throttleAxis(-1),
joystickName(""), joystickName(""),
joystickID(-1), joystickID(-1),
joystickNumButtons(0), joystickNumButtons(0)
throttleAxisLimited(false)
{ {
loadSettings();
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
calibrationPositive[i] = sdlJoystickMax; calibrationPositive[i] = sdlJoystickMax;
calibrationNegative[i] = sdlJoystickMin; calibrationNegative[i] = sdlJoystickMin;
} }
loadSettings();
// Start this thread. This allows the Joystick Settings window to work correctly even w/o any UASes connected. // Start this thread. This allows the Joystick Settings window to work correctly even w/o any UASes connected.
start(); start();
} }
...@@ -57,36 +56,90 @@ JoystickInput::~JoystickInput() ...@@ -57,36 +56,90 @@ JoystickInput::~JoystickInput()
done = true; done = true;
} }
/**
* @brief Restores settings for the current joystick from saved settings file.
* Assumes that both joystickName & joystickNumAxes are correct.
*/
void JoystickInput::loadSettings() void JoystickInput::loadSettings()
{ {
// // Load defaults from settings // Load defaults from settings
// QSettings settings; QSettings settings;
// settings.sync(); settings.sync();
// settings.beginGroup("QGC_JOYSTICK_INPUT"); settings.beginGroup(joystickName);
// xAxis = (settings.value("X_AXIS_MAPPING", xAxis).toInt()); rollAxis = (settings.value("ROLL_AXIS_MAPPING", -1).toInt());
// yAxis = (settings.value("Y_AXIS_MAPPING", yAxis).toInt()); pitchAxis = (settings.value("PITCH_AXIS_MAPPING", -1).toInt());
// thrustAxis = (settings.value("THRUST_AXIS_MAPPING", thrustAxis).toInt()); yawAxis = (settings.value("YAW_AXIS_MAPPING", -1).toInt());
// yawAxis = (settings.value("YAW_AXIS_MAPPING", yawAxis).toInt()); throttleAxis = (settings.value("THROTTLE_AXIS_MAPPING", -1).toInt());
// autoButtonMapping = (settings.value("AUTO_BUTTON_MAPPING", autoButtonMapping).toInt());
// stabilizeButtonMapping = (settings.value("STABILIZE_BUTTON_MAPPING", stabilizeButtonMapping).toInt()); // Read back the joystickAxesInverted QList one element at a time.
// manualButtonMapping = (settings.value("MANUAL_BUTTON_MAPPING", manualButtonMapping).toInt()); // Also, error check.
// settings.endGroup(); int axesStored = settings.beginReadArray("AXES_INVERTED");
if (axesStored != joystickNumAxes)
{
qDebug() << "Invalid number of axes for joystickAxesInverted in settings. Ignoring.";
for (int i = 0; i < joystickNumAxes; i++)
{
joystickAxesInverted.append(false);
}
}
else
{
for (int i = 0; i < joystickNumAxes; i++)
{
settings.setArrayIndex(i);
joystickAxesInverted.append(settings.value("INVERTED", false).toBool());
}
}
settings.endArray();
// Read back the joystickAxesLimited QList one element at a time.
// Also, error check.
axesStored = settings.beginReadArray("AXES_LIMITED");
if (axesStored != joystickNumAxes)
{
qDebug() << "Invalid number of axes for joystickAxesLimited in settings. Ignoring.";
for (int i = 0; i < joystickNumAxes; i++)
{
joystickAxesLimited.append(false);
}
}
else
{
for (int i = 0; i < joystickNumAxes; i++)
{
settings.setArrayIndex(i);
joystickAxesLimited.append(settings.value("LIMITED", false).toBool());
}
}
settings.endArray();
settings.endGroup();
} }
void JoystickInput::storeSettings() void JoystickInput::storeSettings()
{ {
// // Store settings // Store settings
// QSettings settings; QSettings settings;
// settings.beginGroup("QGC_JOYSTICK_INPUT"); settings.beginGroup(joystickName);
// settings.setValue("X_AXIS_MAPPING", xAxis); settings.setValue("ROLL_AXIS_MAPPING", rollAxis);
// settings.setValue("Y_AXIS_MAPPING", yAxis); settings.setValue("PITCH_AXIS_MAPPING", pitchAxis);
// settings.setValue("THRUST_AXIS_MAPPING", thrustAxis); settings.setValue("YAW_AXIS_MAPPING", yawAxis);
// settings.setValue("YAW_AXIS_MAPPING", yawAxis); settings.setValue("THROTTLE_AXIS_MAPPING", throttleAxis);
// settings.setValue("AUTO_BUTTON_MAPPING", autoButtonMapping); settings.beginWriteArray("AXES_INVERTED");
// settings.setValue("STABILIZE_BUTTON_MAPPING", stabilizeButtonMapping); for (int i = 0; i < joystickNumAxes; i++)
// settings.setValue("MANUAL_BUTTON_MAPPING", manualButtonMapping); {
// settings.endGroup(); settings.setArrayIndex(i);
// settings.sync(); settings.setValue("INVERTED", joystickAxesInverted.at(i));
}
settings.endArray();
settings.beginWriteArray("AXES_LIMITED");
for (int i = 0; i < joystickNumAxes; i++)
{
settings.setArrayIndex(i);
settings.setValue("LIMITED", joystickAxesLimited.at(i));
}
settings.endArray();
settings.endGroup();
settings.sync();
} }
...@@ -227,7 +280,6 @@ void JoystickInput::run() ...@@ -227,7 +280,6 @@ void JoystickInput::run()
{ {
joystickAxes[i] = axisValue; joystickAxes[i] = axisValue;
emit axisValueChanged(i, axisValue); emit axisValueChanged(i, axisValue);
qDebug() << "Axis " << i << ": " << axisValue;
} }
} }
...@@ -277,8 +329,10 @@ void JoystickInput::run() ...@@ -277,8 +329,10 @@ void JoystickInput::run()
void JoystickInput::setActiveJoystick(int id) void JoystickInput::setActiveJoystick(int id)
{ {
// If we already had a joystick, close that one before opening a new one.
if (joystick && SDL_JoystickOpened(joystickID)) if (joystick && SDL_JoystickOpened(joystickID))
{ {
storeSettings();
SDL_JoystickClose(joystick); SDL_JoystickClose(joystick);
joystick = NULL; joystick = NULL;
joystickID = -1; joystickID = -1;
...@@ -294,6 +348,10 @@ void JoystickInput::setActiveJoystick(int id) ...@@ -294,6 +348,10 @@ void JoystickInput::setActiveJoystick(int id)
joystickNumButtons = SDL_JoystickNumButtons(joystick); joystickNumButtons = SDL_JoystickNumButtons(joystick);
joystickNumAxes = SDL_JoystickNumAxes(joystick); joystickNumAxes = SDL_JoystickNumAxes(joystick);
// Restore saved settings for this joystick.
loadSettings();
qDebug() << "Roll: " << rollAxis << ", Pitch: " << pitchAxis << ", Yaw: " << yawAxis << ", Throttle: " << throttleAxis;
// Update cached joystick values // Update cached joystick values
joystickAxes.clear(); joystickAxes.clear();
joystickAxesInverted.clear(); joystickAxesInverted.clear();
...@@ -303,9 +361,6 @@ void JoystickInput::setActiveJoystick(int id) ...@@ -303,9 +361,6 @@ void JoystickInput::setActiveJoystick(int id)
int axisValue = SDL_JoystickGetAxis(joystick, i); int axisValue = SDL_JoystickGetAxis(joystick, i);
joystickAxes.append(axisValue); joystickAxes.append(axisValue);
emit axisValueChanged(i, axisValue); emit axisValueChanged(i, axisValue);
joystickAxesInverted.append(false);
joystickAxesLimited.append(false);
} }
joystickButtons = 0; joystickButtons = 0;
for (int i = 0; i < joystickNumButtons; i++) for (int i = 0; i < joystickNumButtons; i++)
...@@ -388,3 +443,21 @@ float JoystickInput::getCurrentValueForAxis(int axis) ...@@ -388,3 +443,21 @@ float JoystickInput::getCurrentValueForAxis(int axis)
} }
return 0.0f; return 0.0f;
} }
bool JoystickInput::getInvertedForAxis(int axis)
{
if (axis < joystickAxes.size())
{
return joystickAxesInverted[axis];
}
return false;
}
bool JoystickInput::getRangeLimitForAxis(int axis)
{
if (axis < joystickAxes.size())
{
return joystickAxesLimited[axis];
}
return false;
}
...@@ -131,6 +131,8 @@ public: ...@@ -131,6 +131,8 @@ public:
} }
float getCurrentValueForAxis(int axis); float getCurrentValueForAxis(int axis);
bool getInvertedForAxis(int axis);
bool getRangeLimitForAxis(int axis);
const double sdlJoystickMin; const double sdlJoystickMin;
const double sdlJoystickMax; const double sdlJoystickMax;
...@@ -160,7 +162,6 @@ protected: ...@@ -160,7 +162,6 @@ protected:
QList<float> joystickAxes; ///< The values of every axes during the last sample. QList<float> joystickAxes; ///< The values of every axes during the last sample.
QList<bool> joystickAxesInverted; ///< Whether each axis should be used inverted from what was reported. QList<bool> joystickAxesInverted; ///< Whether each axis should be used inverted from what was reported.
QList<bool> joystickAxesLimited; ///< Whether each axis should be limited to only the positive range. QList<bool> joystickAxesLimited; ///< Whether each axis should be limited to only the positive range.
bool throttleAxisLimited; ///< Indicates if the throttle channel should be limited to mapping from a 0:1 range instead of -1:1. Useful for controlling throttle with an axis that autocenters.
quint16 joystickButtons; ///< The state of every button. Bitfield supporting 16 buttons with 1s indicating that the button is down. quint16 joystickButtons; ///< The state of every button. Bitfield supporting 16 buttons with 1s indicating that the button is down.
int xHat, yHat; ///< The horizontal/vertical hat directions. Values are -1, 0, 1, with (-1,-1) indicating bottom-left. int xHat, yHat; ///< The horizontal/vertical hat directions. Values are -1, 0, 1, with (-1,-1) indicating bottom-left.
......
...@@ -27,6 +27,30 @@ void JoystickAxis::setValue(float value) ...@@ -27,6 +27,30 @@ void JoystickAxis::setValue(float value)
ui->progressBar->setValue(100.0f * value); ui->progressBar->setValue(100.0f * value);
} }
void JoystickAxis::setMapping(JoystickInput::JOYSTICK_INPUT_MAPPING newMapping)
{
ui->comboBox->setCurrentIndex(newMapping);
if (newMapping == JoystickInput::JOYSTICK_INPUT_MAPPING_THROTTLE)
{
ui->rangeCheckBox->show();
}
else
{
ui->rangeCheckBox->hide();
}
this->setActiveUAS(UASManager::instance()->getActiveUAS());
}
void JoystickAxis::setInverted(bool newValue)
{
ui->invertedCheckBox->setChecked(newValue);
}
void JoystickAxis::setRangeLimit(bool newValue)
{
ui->rangeCheckBox->setChecked(newValue);
}
void JoystickAxis::mappingComboBoxChanged(int newMapping) void JoystickAxis::mappingComboBoxChanged(int newMapping)
{ {
if (newMapping == JoystickInput::JOYSTICK_INPUT_MAPPING_THROTTLE) if (newMapping == JoystickInput::JOYSTICK_INPUT_MAPPING_THROTTLE)
......
...@@ -15,6 +15,9 @@ class JoystickAxis : public QWidget ...@@ -15,6 +15,9 @@ class JoystickAxis : public QWidget
public: public:
explicit JoystickAxis(int id, QWidget *parent = 0); explicit JoystickAxis(int id, QWidget *parent = 0);
~JoystickAxis(); ~JoystickAxis();
void setMapping(JoystickInput::JOYSTICK_INPUT_MAPPING newMapping);
void setInverted(bool newValue);
void setRangeLimit(bool newValue);
signals: signals:
/** @brief Signal a change in this axis' yaw/pitch/roll mapping */ /** @brief Signal a change in this axis' yaw/pitch/roll mapping */
......
...@@ -135,6 +135,10 @@ void JoystickWidget::updateUIForJoystick(int id) ...@@ -135,6 +135,10 @@ void JoystickWidget::updateUIForJoystick(int id)
} }
// Do the same for the axes supported by this joystick. // Do the same for the axes supported by this joystick.
int rollAxis = joystick->getMappingRollAxis();
int pitchAxis = joystick->getMappingPitchAxis();
int yawAxis = joystick->getMappingYawAxis();
int throttleAxis = joystick->getMappingThrottleAxis();
int newAxes = joystick->getJoystickNumAxes(); int newAxes = joystick->getJoystickNumAxes();
if (newAxes) if (newAxes)
{ {
...@@ -142,6 +146,24 @@ void JoystickWidget::updateUIForJoystick(int id) ...@@ -142,6 +146,24 @@ 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));
if (i == rollAxis)
{
axis->setMapping(JoystickInput::JOYSTICK_INPUT_MAPPING_ROLL);
}
else if (i == pitchAxis)
{
axis->setMapping(JoystickInput::JOYSTICK_INPUT_MAPPING_PITCH);
}
else if (i == yawAxis)
{
axis->setMapping(JoystickInput::JOYSTICK_INPUT_MAPPING_YAW);
}
else if (i == throttleAxis)
{
axis->setMapping(JoystickInput::JOYSTICK_INPUT_MAPPING_THROTTLE);
}
axis->setInverted(joystick->getInvertedForAxis(i));
axis->setRangeLimit(joystick->getRangeLimitForAxis(i));
connect(axis, SIGNAL(mappingChanged(int,JoystickInput::JOYSTICK_INPUT_MAPPING)), this->joystick, SLOT(setAxisMapping(int,JoystickInput::JOYSTICK_INPUT_MAPPING))); connect(axis, SIGNAL(mappingChanged(int,JoystickInput::JOYSTICK_INPUT_MAPPING)), this->joystick, SLOT(setAxisMapping(int,JoystickInput::JOYSTICK_INPUT_MAPPING)));
connect(axis, SIGNAL(inversionChanged(int,bool)), this->joystick, SLOT(setAxisInversion(int,bool))); connect(axis, SIGNAL(inversionChanged(int,bool)), this->joystick, SLOT(setAxisInversion(int,bool)));
connect(axis, SIGNAL(rangeChanged(int,bool)), this->joystick, SLOT(setAxisRangeLimit(int,bool))); connect(axis, SIGNAL(rangeChanged(int,bool)), this->joystick, SLOT(setAxisRangeLimit(int,bool)));
......
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