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() :
throttleAxis(-1),
joystickName(""),
joystickID(-1),
joystickNumButtons(0),
throttleAxisLimited(false)
joystickNumButtons(0)
{
loadSettings();
for (int i = 0; i < 10; i++) {
calibrationPositive[i] = sdlJoystickMax;
calibrationNegative[i] = sdlJoystickMin;
}
loadSettings();
// Start this thread. This allows the Joystick Settings window to work correctly even w/o any UASes connected.
start();
}
......@@ -57,36 +56,90 @@ JoystickInput::~JoystickInput()
done = true;
}
/**
* @brief Restores settings for the current joystick from saved settings file.
* Assumes that both joystickName & joystickNumAxes are correct.
*/
void JoystickInput::loadSettings()
{
// // Load defaults from settings
// QSettings settings;
// settings.sync();
// settings.beginGroup("QGC_JOYSTICK_INPUT");
// xAxis = (settings.value("X_AXIS_MAPPING", xAxis).toInt());
// yAxis = (settings.value("Y_AXIS_MAPPING", yAxis).toInt());
// thrustAxis = (settings.value("THRUST_AXIS_MAPPING", thrustAxis).toInt());
// yawAxis = (settings.value("YAW_AXIS_MAPPING", yawAxis).toInt());
// autoButtonMapping = (settings.value("AUTO_BUTTON_MAPPING", autoButtonMapping).toInt());
// stabilizeButtonMapping = (settings.value("STABILIZE_BUTTON_MAPPING", stabilizeButtonMapping).toInt());
// manualButtonMapping = (settings.value("MANUAL_BUTTON_MAPPING", manualButtonMapping).toInt());
// settings.endGroup();
// Load defaults from settings
QSettings settings;
settings.sync();
settings.beginGroup(joystickName);
rollAxis = (settings.value("ROLL_AXIS_MAPPING", -1).toInt());
pitchAxis = (settings.value("PITCH_AXIS_MAPPING", -1).toInt());
yawAxis = (settings.value("YAW_AXIS_MAPPING", -1).toInt());
throttleAxis = (settings.value("THROTTLE_AXIS_MAPPING", -1).toInt());
// Read back the joystickAxesInverted QList one element at a time.
// Also, error check.
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()
{
// // Store settings
// QSettings settings;
// settings.beginGroup("QGC_JOYSTICK_INPUT");
// settings.setValue("X_AXIS_MAPPING", xAxis);
// settings.setValue("Y_AXIS_MAPPING", yAxis);
// settings.setValue("THRUST_AXIS_MAPPING", thrustAxis);
// settings.setValue("YAW_AXIS_MAPPING", yawAxis);
// settings.setValue("AUTO_BUTTON_MAPPING", autoButtonMapping);
// settings.setValue("STABILIZE_BUTTON_MAPPING", stabilizeButtonMapping);
// settings.setValue("MANUAL_BUTTON_MAPPING", manualButtonMapping);
// settings.endGroup();
// settings.sync();
// Store settings
QSettings settings;
settings.beginGroup(joystickName);
settings.setValue("ROLL_AXIS_MAPPING", rollAxis);
settings.setValue("PITCH_AXIS_MAPPING", pitchAxis);
settings.setValue("YAW_AXIS_MAPPING", yawAxis);
settings.setValue("THROTTLE_AXIS_MAPPING", throttleAxis);
settings.beginWriteArray("AXES_INVERTED");
for (int i = 0; i < joystickNumAxes; i++)
{
settings.setArrayIndex(i);
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()
{
joystickAxes[i] = axisValue;
emit axisValueChanged(i, axisValue);
qDebug() << "Axis " << i << ": " << axisValue;
}
}
......@@ -277,8 +329,10 @@ void JoystickInput::run()
void JoystickInput::setActiveJoystick(int id)
{
// If we already had a joystick, close that one before opening a new one.
if (joystick && SDL_JoystickOpened(joystickID))
{
storeSettings();
SDL_JoystickClose(joystick);
joystick = NULL;
joystickID = -1;
......@@ -294,6 +348,10 @@ void JoystickInput::setActiveJoystick(int id)
joystickNumButtons = SDL_JoystickNumButtons(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
joystickAxes.clear();
joystickAxesInverted.clear();
......@@ -303,9 +361,6 @@ void JoystickInput::setActiveJoystick(int id)
int axisValue = SDL_JoystickGetAxis(joystick, i);
joystickAxes.append(axisValue);
emit axisValueChanged(i, axisValue);
joystickAxesInverted.append(false);
joystickAxesLimited.append(false);
}
joystickButtons = 0;
for (int i = 0; i < joystickNumButtons; i++)
......@@ -388,3 +443,21 @@ float JoystickInput::getCurrentValueForAxis(int axis)
}
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:
}
float getCurrentValueForAxis(int axis);
bool getInvertedForAxis(int axis);
bool getRangeLimitForAxis(int axis);
const double sdlJoystickMin;
const double sdlJoystickMax;
......@@ -160,7 +162,6 @@ protected:
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> 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.
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)
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)
{
if (newMapping == JoystickInput::JOYSTICK_INPUT_MAPPING_THROTTLE)
......
......@@ -15,6 +15,9 @@ class JoystickAxis : public QWidget
public:
explicit JoystickAxis(int id, QWidget *parent = 0);
~JoystickAxis();
void setMapping(JoystickInput::JOYSTICK_INPUT_MAPPING newMapping);
void setInverted(bool newValue);
void setRangeLimit(bool newValue);
signals:
/** @brief Signal a change in this axis' yaw/pitch/roll mapping */
......
......@@ -135,6 +135,10 @@ void JoystickWidget::updateUIForJoystick(int id)
}
// 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();
if (newAxes)
{
......@@ -142,6 +146,24 @@ void JoystickWidget::updateUIForJoystick(int id)
{
JoystickAxis* axis = new JoystickAxis(i, m_ui->axesBox);
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(inversionChanged(int,bool)), this->joystick, SLOT(setAxisInversion(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