13 #include <mrpt/config.h> 15 #ifdef MRPT_OS_WINDOWS 19 #if !defined(__GNUC__) 20 # pragma comment (lib,"WINMM.LIB") 24 #if defined(MRPT_OS_LINUX) || defined(MRPT_OS_APPLE) 26 #include <sys/ioctl.h> 28 #include <sys/types.h> 38 #if defined(MRPT_OS_LINUX) && defined(HAVE_LINUX_INPUT_H) 39 #include <linux/input.h> 40 #include <linux/joystick.h> 52 #if defined(MRPT_OS_LINUX) && defined(HAVE_LINUX_INPUT_H) 66 #if defined(MRPT_OS_LINUX) && defined(HAVE_LINUX_INPUT_H) 83 #ifdef MRPT_OS_WINDOWS 84 return joyGetNumDevs();
85 #elif defined(MRPT_OS_LINUX) && defined(HAVE_LINUX_INPUT_H) 92 if ( -1 != (joy_fd=open(
format(
"/dev/input/js%i",nJoys).c_str(),O_RDONLY)) )
118 std::vector<bool> &buttons,
124 #ifdef MRPT_OS_WINDOWS 127 int ID = JOYSTICKID1 + nJoy;
130 if (JOYERR_NOERROR != joyGetPos( ID, &jinfo ) )
144 buttons[0] = 0 != (jinfo.wButtons & JOY_BUTTON1);
145 buttons[1] = 0 != (jinfo.wButtons & JOY_BUTTON2);
146 buttons[2] = 0 != (jinfo.wButtons & JOY_BUTTON3);
147 buttons[3] = 0 != (jinfo.wButtons & JOY_BUTTON4);
149 if (raw_x_pos) *raw_x_pos=jinfo.wXpos;
150 if (raw_y_pos) *raw_y_pos=jinfo.wYpos;
151 if (raw_z_pos) *raw_z_pos=jinfo.wZpos;
154 #elif defined(MRPT_OS_LINUX) && defined(HAVE_LINUX_INPUT_H) 156 if ( m_joy_index == nJoy && m_joy_fd!=-1)
167 if ((m_joy_fd = open(
format(
"/dev/input/js%i",nJoy).c_str() , O_RDONLY)) < 0)
174 fcntl(m_joy_fd, F_SETFL, O_NONBLOCK);
179 while (read(m_joy_fd, &js,
sizeof(
struct js_event)) ==
sizeof(
struct js_event))
182 if (js.type & JS_EVENT_BUTTON)
185 if (m_joystate_btns.size() < (size_t)js.number+1) m_joystate_btns.resize( js.number+1 );
186 m_joystate_btns[js.number] = js.value!=0;
190 if (js.type & JS_EVENT_AXIS)
193 if (m_joystate_axes.size()<(size_t)js.number+1) m_joystate_axes.resize( js.number+1 );
194 m_joystate_axes[js.number] = js.value;
208 const size_t nAxis = m_joystate_axes.size();
212 if (raw_x_pos) *raw_x_pos = m_joystate_axes[0];
218 if (raw_y_pos) *raw_y_pos = m_joystate_axes[1];
224 if (raw_z_pos) *raw_z_pos = m_joystate_axes[2];
231 buttons = m_joystate_btns;
Contains classes for various device interfaces.
int m_x_min
The axis limits:
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
bool getJoystickPosition(int nJoy, float &x, float &y, float &z, std::vector< bool > &buttons, int *raw_x_pos=NULL, int *raw_y_pos=NULL, int *raw_z_pos=NULL)
Gets joystick information.
static int getJoysticksCount()
Returns the number of Joysticks in the computer.
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.