Skip to content

Commit d3255ea

Browse files
committed
joystick: Refactor axis read logic
The calculation to apply joystick deadzone was rather unwieldy and hard to read. Break all of it out into a separate method so that the logic can be simplified. Signed-off-by: Grant Likely <grant.likely@secretlab.ca>
1 parent bdeab04 commit d3255ea

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

src/ptz-controls.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -310,21 +310,24 @@ void PTZControls::setJoystickDeadzone(double deadzone)
310310
joystickAxesChanged(jd, 0b11111111);
311311
}
312312

313+
double PTZControls::readAxis(const QJoystickDevice *jd, int axis)
314+
{
315+
if (axis < 0 || !jd || axis >= jd->axes.size())
316+
return 0.0;
317+
double v = jd->axes.at(axis);
318+
if (abs(v) < m_joystick_deadzone)
319+
return 0.0;
320+
return std::copysign((abs(v) - m_joystick_deadzone) / (1.0 - m_joystick_deadzone), v);
321+
}
322+
313323
void PTZControls::joystickAxesChanged(const QJoystickDevice *jd, uint32_t updated)
314324
{
315325
if (isLocked() || !m_joystick_enable || !jd || jd->id != m_joystick_id)
316326
return;
317-
auto filter_axis = [=](double val) -> double {
318-
val = abs(val) > m_joystick_deadzone
319-
? std::copysign((abs(val) - m_joystick_deadzone) / (1.0 - m_joystick_deadzone), val)
320-
: 0.0;
321-
return val * m_joystick_speed;
322-
};
323-
324-
if (updated & 0b0011 && jd->axes.size() > 1)
325-
setPanTilt(filter_axis(jd->axes[0]), -filter_axis(jd->axes[1]));
326-
if (updated & 0b1000 && jd->axes.size() > 3)
327-
setZoom(-filter_axis(jd->axes[3]));
327+
if (updated & 0b0011)
328+
setPanTilt(readAxis(jd, 0), -readAxis(jd, 1));
329+
if (updated & 0b1000)
330+
setZoom(-readAxis(jd, 3));
328331
}
329332

330333
void PTZControls::joystickAxisEvent(const QJoystickAxisEvent evt)

src/ptz-controls.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ private slots:
136136
void setJoystickDeadzone(double deadzone);
137137
int joystickId() { return m_joystick_id; };
138138
void setJoystickId(int id) { m_joystick_id = id; };
139+
double readAxis(const QJoystickDevice *jd, int axis);
139140
protected slots:
140141
void joystickAxesChanged(const QJoystickDevice *jd, uint32_t updated);
141142
void joystickAxisEvent(const QJoystickAxisEvent evt);

0 commit comments

Comments
 (0)