MRPT  1.9.9
CJoystick.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <vector>
12 
13 /*---------------------------------------------------------------
14  Class
15  ---------------------------------------------------------------*/
16 namespace mrpt::hwdrivers
17 {
18 /** Access to joysticks and gamepads (read buttons and position), and request
19  * number of joysticks in the system.
20  * \ingroup mrpt_hwdrivers_grp
21  */
22 class CJoystick
23 {
24  private:
25  /** The axis limits:
26  */
28 
29 #if defined(MRPT_OS_LINUX)
30  /** File FD for the joystick, or -1 if not open (Linux only) */
31  int m_joy_fd{-1};
32  /** The index of the joystick open in m_joy_fd (Linux only) */
33  int m_joy_index{-1};
34  /** Using an event system we only have deltas, need to keep the whole
35  * joystick state (Linux only) */
36  std::vector<bool> m_joystate_btns;
37  /** Using an event system we only have deltas, need to keep the whole
38  * joystick state (Linux only) */
39  std::vector<int> m_joystate_axes;
40 #endif
41 
42  public:
43  /** Constructor
44  */
45  CJoystick();
46 
47  /** Destructor
48  */
49  virtual ~CJoystick();
50 
51  /** Returns the number of Joysticks in the computer.
52  */
53  static int getJoysticksCount();
54 
55  /** Gets joystick information.
56  *
57  * This method will try first to open the joystick, so you can safely call
58  * it while the joystick is plugged and removed arbitrarly.
59  *
60  * \param nJoy The index of the joystick to query: The first one is 0, the
61  * second 1, etc... See CJoystick::getJoysticksCount to discover the number
62  * of joysticks in the system.
63  * \param x The x axis position, range [-1,1]
64  * \param y The y axis position, range [-1,1]
65  * \param z The z axis position, range [-1,1]
66  * \param buttons Each element will hold true if buttons are pressed. The
67  * size of the vector will be set automatically to the number of buttons.
68  * \param raw_x_pos If it is desired the raw integer measurement from
69  * JoyStick, set this pointer to a desired placeholder.
70  * \param raw_y_pos If it is desired the raw integer measurement from
71  * JoyStick, set this pointer to a desired placeholder.
72  * \param raw_z_pos If it is desired the raw integer measurement from
73  * JoyStick, set this pointer to a desired placeholder.
74  *
75  * \return Returns true if successfull, false on error, for example, if
76  * joystick is not present.
77  *
78  * \sa setLimits
79  */
81  int nJoy, float& x, float& y, float& z, std::vector<bool>& buttons,
82  int* raw_x_pos = nullptr, int* raw_y_pos = nullptr,
83  int* raw_z_pos = nullptr);
84 
85 /** Set the axis limit values, for computing a [-1,1] position index easily
86  * (Only required to calibrate analog joystick).
87  * It seems that these values must been calibrated for each joystick model.
88  *
89  * \sa getJoystickPosition
90  */
91 #ifdef _WIN32
92  void setLimits(
93  int x_min = 0, int x_max = 0xFFFF, int y_min = 0, int y_max = 0xFFFF,
94  int z_min = 0, int z_max = 0xFFFF);
95 #else
96  void setLimits(
97  int x_min = -32767, int x_max = 32767, int y_min = -32767,
98  int y_max = 32767, int z_min = -32767, int z_max = 32767);
99 #endif
100 }; // End of class def.
101 
102 } // namespace mrpt::hwdrivers
Contains classes for various device interfaces.
int m_x_min
The axis limits:
Definition: CJoystick.h:27
void setLimits(int x_min=0, int x_max=0xFFFF, int y_min=0, int y_max=0xFFFF, int z_min=0, int z_max=0xFFFF)
Set the axis limit values, for computing a [-1,1] position index easily (Only required to calibrate a...
Definition: CJoystick.cpp:237
static int getJoysticksCount()
Returns the number of Joysticks in the computer.
Definition: CJoystick.cpp:72
bool getJoystickPosition(int nJoy, float &x, float &y, float &z, std::vector< bool > &buttons, int *raw_x_pos=nullptr, int *raw_y_pos=nullptr, int *raw_z_pos=nullptr)
Gets joystick information.
Definition: CJoystick.cpp:106
CJoystick()
Constructor.
Definition: CJoystick.cpp:52
Access to joysticks and gamepads (read buttons and position), and request number of joysticks in the ...
Definition: CJoystick.h:22
virtual ~CJoystick()
Destructor.
Definition: CJoystick.cpp:56



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: c7a3bec24 Sun Mar 29 18:33:13 2020 +0200 at dom mar 29 18:50:38 CEST 2020