class mrpt::hwdrivers::CJoystick

Overview

Reads axis positions and button states from joysticks and gamepads.

Supports an arbitrary number of axes and buttons. Enumerates connected devices via getJoysticksCount() and reads state with getJoystickPosition().

Platform support: Linux (via /dev/input/jsX) and Windows (WinMM joystick API).

As of MRPT 2.13.0 the API was extended to support an arbitrary number of input axes (previously only x, y, z were exposed).

#include <mrpt/hwdrivers/CJoystick.h>

class CJoystick
{
public:
    // structs

    struct State;

    // construction

    CJoystick();

    // methods

    bool getJoystickPosition(int nJoy, State& output);
    void setLimits(const std::vector<int>& minPerAxis, const std::vector<int>& maxPerAxis);
    static int getJoysticksCount();
};

Construction

CJoystick()

Default constructor.

Methods

bool getJoystickPosition(int nJoy, State& output)

Reads the current state of a joystick.

Attempts to open the device if not already open, so it is safe to call while joysticks are plugged or unplugged.

Parameters:

nJoy

Zero-based index of the joystick to query.

output

Filled with normalized axis values [0,1] in output.axes, raw axis values in output.axes_raw, and button states in output.buttons.

Returns:

true on success; false if the device is absent or an error occurs.

See also:

setLimits

void setLimits(
    const std::vector<int>& minPerAxis,
    const std::vector<int>& maxPerAxis
    )

Sets the raw ADC range for each axis, used to normalize to [-1,1].

Required only when calibrating analog joysticks whose default range differs from the platform default (Windows: [0, 0xFFFF]; Linux: [-32767, 32767]).

Parameters:

minPerAxis

Vector of minimum raw values per axis.

maxPerAxis

Vector of maximum raw values per axis.

See also:

getJoystickPosition

static int getJoysticksCount()

Returns the number of joystick/gamepad devices currently connected to the system.

Returns:

Device count (>= 0).