Skip to content

Commit

Permalink
joystick: Add a speed control slider
Browse files Browse the repository at this point in the history
Adds a speed setting to the joystick controls so that sensitivity of the
joystick can be adjusted.

Signed-off-by: Grant Likely <grant.likely@secretlab.ca>
  • Loading branch information
glikely committed Feb 20, 2023
1 parent 2686eea commit 2c0e12e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/ptz-controls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,14 @@ void PTZControls::setJoystickEnabled(bool enable)
m_joystick_enable = enable;
};

void PTZControls::setJoystickSpeed(double speed)
{
m_joystick_speed = speed;
/* Immediatly apply the deadzone */
auto jd = QJoysticks::getInstance()->getInputDevice(m_joystick_id);
joystickAxesChanged(jd, 0b11111111);
};

void PTZControls::setJoystickDeadzone(double deadzone)
{
m_joystick_deadzone = deadzone;
Expand All @@ -343,7 +351,8 @@ void PTZControls::joystickAxesChanged(const QJoystickDevice *jd,
if (!m_joystick_enable || !jd || jd->id != m_joystick_id)
return;
auto filter_axis = [=](double val) -> double {
return abs(val) > m_joystick_deadzone ? val : 0.0;
val = abs(val) > m_joystick_deadzone ? val : 0.0;
return val * m_joystick_speed;
};

if (updated & 0b0011)
Expand Down Expand Up @@ -431,6 +440,7 @@ void PTZControls::SaveConfig()
obs_data_set_string(savedata, "target_mode", target_mode);
obs_data_set_bool(savedata, "joystick_enable", m_joystick_enable);
obs_data_set_int(savedata, "joystick_id", m_joystick_id);
obs_data_set_double(savedata, "joystick_speed", m_joystick_speed);
obs_data_set_double(savedata, "joystick_deadzone", m_joystick_deadzone);

OBSDataArray camera_array = ptz_devices_get_config();
Expand Down Expand Up @@ -475,6 +485,7 @@ void PTZControls::LoadConfig()
obs_data_set_default_string(loaddata, "target_mode", "preview");
obs_data_set_default_bool(loaddata, "joystick_enable", false);
obs_data_set_default_int(loaddata, "joystick_id", -1);
obs_data_set_default_double(loaddata, "joystick_speed", 1.0);
obs_data_set_default_double(loaddata, "joystick_deadzone", 0.0);

ptz_debug_level = (int)obs_data_get_int(loaddata, "debug_log_level");
Expand All @@ -485,6 +496,7 @@ void PTZControls::LoadConfig()
ui->actionFollowProgram->setChecked(target_mode == "program");
m_joystick_enable = obs_data_get_bool(loaddata, "joystick_enable");
m_joystick_id = (int)obs_data_get_int(loaddata, "joystick_id");
m_joystick_speed = obs_data_get_double(loaddata, "joystick_speed");
m_joystick_deadzone =
obs_data_get_double(loaddata, "joystick_deadzone");

Expand Down
3 changes: 3 additions & 0 deletions src/ptz-controls.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,15 @@ private slots:
bool m_joystick_enable = false;
int m_joystick_id = -1;
double m_joystick_deadzone = 0.0;
double m_joystick_speed = 1.0;
#if defined(ENABLE_JOYSTICK)
public:
void joystickSetup();
bool joystickEnabled() { return m_joystick_enable; };
double joystickDeadzone() { return m_joystick_deadzone; };
double joystickSpeed() { return m_joystick_speed; };
void setJoystickEnabled(bool enable);
void setJoystickSpeed(double speed);
void setJoystickDeadzone(double deadzone);
int joystickId() { return m_joystick_id; };
void setJoystickId(int id) { m_joystick_id = id; };
Expand Down
7 changes: 7 additions & 0 deletions src/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ void PTZSettings::joystickSetup()
auto controls = PTZControls::getInstance();
ui->joystickNamesListView->setModel(&m_joystickNamesModel);
ui->joystickGroupBox->setChecked(controls->joystickEnabled());
ui->joystickSpeedSlider->setDoubleConstraints(
0.25, 1.75, 0.05, controls->joystickSpeed());
ui->joystickDeadzoneSlider->setDoubleConstraints(
0.01, 0.15, 0.01, controls->joystickDeadzone());

Expand All @@ -181,6 +183,11 @@ void PTZSettings::joystickSetup()
joystickUpdate();
}

void PTZSettings::on_joystickSpeedSlider_doubleValChanged(double val)
{
PTZControls::getInstance()->setJoystickSpeed(val);
}

void PTZSettings::on_joystickDeadzoneSlider_doubleValChanged(double val)
{
PTZControls::getInstance()->setJoystickDeadzone(val);
Expand Down
1 change: 1 addition & 0 deletions src/settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class PTZSettings : public QWidget {
QStringListModel m_joystickNamesModel;
protected slots:
void on_joystickGroupBox_toggled(bool checked);
void on_joystickSpeedSlider_doubleValChanged(double val);
void on_joystickDeadzoneSlider_doubleValChanged(double val);
void joystickUpdate();
void joystickCurrentChanged(QModelIndex, QModelIndex);
Expand Down
16 changes: 15 additions & 1 deletion src/settings.ui
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,27 @@
<widget class="QListView" name="joystickNamesListView"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="joystickSpeedLabel">
<property name="text">
<string>Speed</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="DoubleSlider" name="joystickSpeedSlider">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="joystickDeadzoneLabel">
<property name="text">
<string>Deadzone size</string>
</property>
</widget>
</item>
<item row="1" column="1">
<item row="2" column="1">
<widget class="DoubleSlider" name="joystickDeadzoneSlider">
<property name="toolTip">
<string>Set this value higher if the camera position drifts when the joysticks are centered</string>
Expand Down

0 comments on commit 2c0e12e

Please sign in to comment.