12 #include <mrpt/config.h> 14 #ifdef MRPT_OS_WINDOWS 18 #if !defined(__GNUC__) 19 #pragma comment(lib, "WINMM.LIB") 23 #if defined(MRPT_OS_LINUX) || defined(MRPT_OS_APPLE) 25 #include <sys/ioctl.h> 27 #include <sys/types.h> 37 #if defined(MRPT_OS_LINUX) && defined(HAVE_LINUX_INPUT_H) 38 #include <linux/input.h> 39 #include <linux/joystick.h> 51 #if defined(MRPT_OS_LINUX) && defined(HAVE_LINUX_INPUT_H) 52 : m_joy_fd(-1), m_joy_index(-1)
63 #if defined(MRPT_OS_LINUX) && defined(HAVE_LINUX_INPUT_H) 80 #ifdef MRPT_OS_WINDOWS 81 return joyGetNumDevs();
82 #elif defined(MRPT_OS_LINUX) && defined(HAVE_LINUX_INPUT_H) 90 (joy_fd = open(
format(
"/dev/input/js%i", nJoys).c_str(), O_RDONLY)))
95 }
while (joy_fd != -1);
112 int nJoy,
float&
x,
float&
y,
float&
z, std::vector<bool>& buttons,
113 int* raw_x_pos,
int* raw_y_pos,
int* raw_z_pos)
116 #ifdef MRPT_OS_WINDOWS 119 int ID = JOYSTICKID1 + nJoy;
122 if (JOYERR_NOERROR != joyGetPos(ID, &jinfo))
return false;
135 buttons[0] = 0 != (jinfo.wButtons & JOY_BUTTON1);
136 buttons[1] = 0 != (jinfo.wButtons & JOY_BUTTON2);
137 buttons[2] = 0 != (jinfo.wButtons & JOY_BUTTON3);
138 buttons[3] = 0 != (jinfo.wButtons & JOY_BUTTON4);
140 if (raw_x_pos) *raw_x_pos = jinfo.wXpos;
141 if (raw_y_pos) *raw_y_pos = jinfo.wYpos;
142 if (raw_z_pos) *raw_z_pos = jinfo.wZpos;
145 #elif defined(MRPT_OS_LINUX) && defined(HAVE_LINUX_INPUT_H) 147 if (m_joy_index == nJoy && m_joy_fd != -1)
154 if (m_joy_fd != -1) close(m_joy_fd);
158 open(
format(
"/dev/input/js%i", nJoy).c_str(), O_RDONLY)) < 0)
165 fcntl(m_joy_fd, F_SETFL, O_NONBLOCK);
170 while (read(m_joy_fd, &js,
sizeof(
struct js_event)) ==
171 sizeof(
struct js_event))
174 if (js.type & JS_EVENT_BUTTON)
177 if (m_joystate_btns.size() < (size_t)js.number + 1)
178 m_joystate_btns.resize(js.number + 1);
179 m_joystate_btns[js.number] = js.value != 0;
183 if (js.type & JS_EVENT_AXIS)
186 if (m_joystate_axes.size() < (size_t)js.number + 1)
187 m_joystate_axes.resize(js.number + 1);
188 m_joystate_axes[js.number] = js.value;
201 const size_t nAxis = m_joystate_axes.size();
206 if (raw_x_pos) *raw_x_pos = m_joystate_axes[0];
213 if (raw_y_pos) *raw_y_pos = m_joystate_axes[1];
220 if (raw_z_pos) *raw_z_pos = m_joystate_axes[2];
227 buttons = m_joystate_btns;
243 int x_min,
int x_max,
int y_min,
int y_max,
int z_min,
int z_max)
Contains classes for various device interfaces.
int m_x_min
The axis limits:
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
static int getJoysticksCount()
Returns the number of Joysticks in the computer.
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.
void setLimits(int x_min=-32767, int x_max=32767, int y_min=-32767, int y_max=32767, int z_min=-32767, int z_max=32767)
Set the axis limit values, for computing a [-1,1] position index easily (Only required to calibrate a...
virtual ~CJoystick()
Destructor.