peripherals  1.4.2
RoadNarrows Robotics Hardware Peripherals Package
HIDXbox360.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: RoadNarrows Robotics Peripherals
4 //
5 // Library: libhid
6 //
7 // File: HIDXbox360.h
8 //
9 /*! \file
10  *
11  * $LastChangedDate: 2014-07-10 15:03:23 -0600 (Thu, 10 Jul 2014) $
12  * $Rev: 3696 $
13  *
14  * \brief Xbox360 Controller C interface.
15  *
16  * \author Rob Shiely (rob.knight@roadnarrows.com)
17  * \author Daniel Packard (daniel@roadnarrows.com)
18  * \author Robin Knight (robin.knight@roadnarrows.com)
19  *
20  * \copyright
21  * \h_copy 2012-2017. RoadNarrows LLC.\n
22  * http://www.roadnarrows.com\n
23  * All Rights Reserved
24  */
25 // Permission is hereby granted, without written agreement and without
26 // license or royalty fees, to use, copy, modify, and distribute this
27 // software and its documentation for any purpose, provided that
28 // (1) The above copyright notice and the following two paragraphs
29 // appear in all copies of the source code and (2) redistributions
30 // including binaries reproduces these notices in the supporting
31 // documentation. Substantial modifications to this software may be
32 // copyrighted by their authors and need not follow the licensing terms
33 // described here, provided that the new terms are clearly indicated in
34 // all files where they apply.
35 //
36 // IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
37 // OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
38 // PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
39 // DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
40 // EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
41 // THE POSSIBILITY OF SUCH DAMAGE.
42 //
43 // THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
44 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
45 // FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
46 // "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
47 // PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
48 //
49 ////////////////////////////////////////////////////////////////////////////////
50 
51 
52 #ifndef _HIDXBOX360_H
53 #define _HIDXBOX360_H
54 
55 #include <sys/types.h>
56 #include <sys/stat.h>
57 #include <sys/time.h>
58 #include <sys/ioctl.h>
59 #include <limits.h>
60 #include <fcntl.h>
61 #include <termios.h>
62 #include <unistd.h>
63 #include <stdlib.h>
64 #include <stdarg.h>
65 #include <time.h>
66 #include <stdio.h>
67 #include <string.h>
68 #include <pthread.h>
69 
70 #include <map>
71 #include <string>
72 
73 #include <libusb-1.0/libusb.h>
74 
75 #include "rnr/rnrconfig.h"
76 
77 #include "rnr/usbext.h"
78 #include "rnr/hid/HID.h"
79 
80 /*!
81  * \ingroup periph_hid
82  * \defgroup periph_hid_xbox Xbox360
83  * \{
84  *
85  * Xbox360 Human Interface Device interface.
86  *
87  * \}
88  */
89 
90 /*!
91  * \ingroup periph_hid_xbox
92  * \defgroup periph_hid_xbox_intro Intro
93  * \{
94 
95 The Xbox 360 game controller is a RoadNarrows supported HID.
96 It provides an easy to use interface to manually
97 control RoadNarrows robotic platforms such as the Kuon\h_reg and the
98 Hekateros\h_reg.
99 \htmlonly
100 <div style="float:left; border:0 solid; padding:0; width: 300px; margin:10px;">
101 \endhtmlonly
102 \image html xbox360.jpg "Mircrosoft&reg; Xbox 360&reg; controller."
103 \htmlonly
104 </div>
105 \endhtmlonly
106 \htmlonly
107 <div style="float:left; border:0 solid; padding:0; width: 300px; margin:10px;">
108 \endhtmlonly
109 \section xbox_intro_in Input Features
110 \termblock
111 \term \b Feature
112  \termdata \b Type
113  \termdata \b Min
114  \termdata \b Max
115 \endterm
116 \term D-Pad Up, Down, Left, Right
117  \termdata bi-state
118  \termdata 0
119  \termdata 1
120 \endterm
121 \term Start Button
122  \termdata bi-state
123  \termdata 0
124  \termdata 1
125 \endterm
126 \term Center X (LED) Button
127  \termdata bi-state
128  \termdata 0
129  \termdata 1
130 \endterm
131 \term Back Button
132  \termdata bi-state
133  \termdata 0
134  \termdata 1
135 \endterm
136 \term Left Joystick X Y
137  \termdata range
138  \termdata -32767
139  \termdata 32767
140 \endterm
141 \term Left Joystick Click
142  \termdata bi-state
143  \termdata 0
144  \termdata 1
145 \term Right Joystick X Y
146  \termdata range
147  \termdata -32767
148  \termdata 32767
149 \endterm
150 \term Right Joystick Click
151  \termdata bi-state
152  \termdata 0
153  \termdata 1
154 \endterm
155 \term Left Trigger
156  \termdata range
157  \termdata 0
158  \termdata 255
159 \endterm
160 \term Right Trigger
161  \termdata range
162  \termdata 0
163  \termdata 255
164 \endterm
165 \term Left Bump
166  \termdata bi-state
167  \termdata 0
168  \termdata 1
169 \endterm
170 \term Right Bump
171  \termdata bi-state
172  \termdata 0
173  \termdata 1
174 \endterm
175 \endtermblock
176 
177 \section xbox_intro_out Output Features
178 \termblock
179 \term \b Feature
180  \termdata \b Type
181  \termdata \b Min
182  \termdata \b Max
183 \endterm
184 \term Left Low-Frequency Rumble Motor
185  \termdata range
186  \termdata 0
187  \termdata 255
188 \endterm
189 \term Right High-Frequency Rumble Motor
190  \termdata range
191  \termdata 0
192  \termdata 255
193 \endterm
194 \term LED Pattern
195  \termdata enum
196  \termdata 0
197  \termdata 14
198 \endterm
199 \endtermblock
200 \htmlonly
201 </div>
202 \endhtmlonly
203  *
204  * \}
205  */
206 
207 /*!
208  * \ingroup periph_hid_xbox
209  * \defgroup periph_hid_xbox_usb USB Protocol
210  * \{
211  *
212  * Xbox USB protocol interface.
213  */
214 
215 /*! USB Microsoft Vendor ID */
216 #define MICROSOFT_VENDOR_ID 0x045e
217 
218 /*! USB product ID for USB only Xbox controller */
219 #define XBOX360_WIRED_PROD_ID 0x028e
220 
221 /*! USB product ID for wireless Xbox controoler tethered via a charging cable */
222 #define XBOX360_TETHERED_PROD_ID 0x028f
223 
224 /*! USB product ID for Microsoft wireless dongle */
225 #define XBOX360_WIRELESS_PROD_ID 0x0719
226 #define XBOX360_WIRELESS_WIN_PROD_ID 0x0291
227 
228 /*! no product id */
229 #define XBOX360_NO_PROD_ID 0x0000
230 
231 /*! USB endpoint where current controller state is read from */
232 #define XBOX360_READ_ENDPOINT 0x81
233 
234 /*! USB endpoint where controller state is modified */
235 #define XBOX360_WRITE_ENDPOINT 0x01
236 
237 /*! USB typical number of interfaces for a microsoft device intended for Xbox */
238 #define XBOX360_NUM_OF_INTERFACES 4
239 
240 /*! USB debug level for libusb (values: 0:off, 1:error, 2:warning, 3:info) */
241 #define XBOX_LIBUSB_DEBUG_OFF 0 ///< libusb logging off
242 #define XBOX_LIBUSB_DEBUG_ERROR 1 ///< log libusb errors
243 #define XBOX_LIBUSB_DEBUG_WARN 2 ///< log libusb warnings and errors
244 #define XBOX_LIBUSB_DEBUG_INFO 3 ///< log libusb info, warnings and errors
245 #define XBOX_LIBUSB_DEBUG_DFT XBOX_LIBUSB_DEBUG_OFF
246  ///< default libusb logging
247 
248 /*!
249  * \}
250  */
251 
252 /*!
253  * \ingroup periph_hid_xbox
254  * \defgroup periph_hid_xbox_msg Messages
255  * \{
256  *
257  * Xbox360 application message interface.
258  */
259 
260 //
261 // Message offsets in packet.
262 //
263 #define XBOX360_MSG_OFFSET_DFT 0 ///< default message start byte offset
264 #define XBOX360_MSG_OFFSET_W 0 ///< wired message start byte offset
265 #define XBOX360_MSG_OFFSET_WL 4 ///< wireless message start byte offset
266 #define XBOX360_WL_PKT_HDR_LEN XBOX360_MSG_OFFSET_WL
267  ///< wireless packet header length
268 
269 
270 // .............................................................................
271 // Wireless controller packet header and specific packets
272 // .............................................................................
273 
274 //
275 // Packet lengths.
276 //
277 #define XBOX360_WL_LINK_PKT_LEN 2 ///< link status change packet length
278 #define XBOX360_WL_NORM_PKT_LEN 29 ///< normal packet length
279 
280 //
281 // Packet type header byte.
282 //
283 #define XBOX360_WL_PKT_TYPE_POS 0 ///< byte position
284 #define XBOX360_WL_PKT_TYPE_NORM 0x00 ///< normal link message
285 #define XBOX360_WL_PKT_TYPE_CHG 0x08 ///< link status changed message
286 #define XBOX360_WL_PKT_TYPE_20 0x20 ///< ? 20 d7 67 fc 01 88 ff ff 00...
287 #define XBOX360_WL_PKT_TYPE_40 0x40 ///< ? 40 7c 78 a7 01 88 ff ff 00...
288 #define XBOX360_WL_PKT_TYPE_60 0x40 ///< ? 60 4e 23 ff 01 88 ff ff 00...
289 #define XBOX360_WL_PKT_TYPE_80 0x80 ///< ? 80 dc 67 fc 01 88 ff ff 68
290  ///< c4 63 ac 01 88 ff ff c4 63
291  ///< ac 01 88 ff ff 00...
292 #define XBOX360_WL_PKT_TYPE_C0 0xc0 ///< ? c0 46 40 ac 01 88 ff ff 68
293  ///< 2c 39 e0 10 88 ff ff d0 2c
294  ///< e9 e0 01 88 ff ff 00...
295 
296 //
297 // Link status header byte.
298 //
299 // T body
300 // 08 status
301 //
302 #define XBOX360_WL_LINK_STATUS_POS 1 ///< byte position
303 #define XBOX360_WL_LINK_STATUS_NO_LINK 0x00 ///< no link
304 #define XBOX360_WL_LINK_STATUS_HEADSET 0x40 ///< linked w/ headset
305 #define XBOX360_WL_LINK_STATUS_CTLR 0x80 ///< linked w/ controller
306 #define XBOX360_WL_LINK_STATUS_CTLR_HEADSET 0xc0 ///< linked w/ ctlr & headset
307 
308 //
309 // Announcement header and message packet.
310 //
311 // T 1 2 3 body
312 // 00 0f 00 f0 s/n battery
313 //
314 #define XBOX360_WL_ANN_PAT_1 0x0f ///< header pattern at byte 1
315 #define XBOX360_WL_ANN_PAT_2 0x00 ///< header pattern at byte 2
316 #define XBOX360_WL_ANN_PAT_3 0xf0 ///< header pattern at byte 3
317 #define XBOX360_WL_ANN_SN_POS 7 ///< serial number field position
318 #define XBOX360_WL_ANN_SN_LEN 7 ///< serial number field length
319 #define XBOX360_WL_ANN_BATT_POS 17 ///< battery status field position
320 #define XBOX360_WL_ANN_BATT_LEN 1 ///< battery status field length
321 
322 //
323 // Battery status header and message packet.
324 //
325 // T 1 2 3 body
326 // 00 00 00 13 battery
327 //
328 #define XBOX360_WL_BATT_PAT_1 0x00 ///< header pattern at byte 1
329 #define XBOX360_WL_BATT_PAT_2 0x00 ///< header pattern at byte 2
330 #define XBOX360_WL_BATT_PAT_3 0x13 ///< header pattern at byte 3
331 #define XBOX360_WL_BATT_BATT_POS 4 ///< battery status field position
332 #define XBOX360_WL_BATT_BATT_LEN 1 ///< battery status field length
333 
334 //
335 // 'Null' status header and message packet. Most are uknown. Byte 3 patterns
336 // 10, 20, 00, 40, in that order, seem to be ascciated with (re)initial state.
337 //
338 // T 1 2 3 body
339 // 00 00 00 00 62 00 00...
340 // 00 00 00 10 e2 00 00...
341 // 00 00 00 20 1d 00 00...
342 // 00 00 00 40 01 00 00...
343 // 00 00 00 f0 00 00 00... set led acknowledge
344 //
345 #define XBOX360_WL_NULL_PAT_1 0x00 ///< header pattern at byte 1
346 #define XBOX360_WL_NULL_PAT_2 0x00 ///< header pattern at byte 2
347 #define XBOX360_WL_NULL_PAT_3_00 0x00 ///< header pattern at byte 3
348 #define XBOX360_WL_NULL_PAT_3_10 0x10 ///< header pattern at byte 3
349 #define XBOX360_WL_NULL_PAT_3_20 0x20 ///< header pattern at byte 3
350 #define XBOX360_WL_NULL_PAT_3_40 0x40 ///< header pattern at byte 3
351 #define XBOX360_WL_NULL_PAT_3_ACK 0xf0 ///< header pattern at byte 3
352 
353 //
354 // Button state update header packet.
355 //
356 // T 1 2 3 body
357 // 00 01 00 f0 buttons...
358 //
359 #define XBOX360_WL_BTTN_PAT_1 0x01 ///< header pattern at byte 1
360 #define XBOX360_WL_BTTN_PAT_2 0x00 ///< header pattern at byte 2
361 #define XBOX360_WL_BTTN_PAT_3 0xf0 ///< header pattern at byte 3
362 
363 //
364 // Rumble state request/response header packet.
365 //
366 // T 1 2 3 body
367 // 00 01 0f c0 00 left right
368 //
369 #define XBOX360_WL_RUMBLE_PAT_1 0x01 ///< header pattern at byte 1
370 #define XBOX360_WL_RUMBLE_PAT_2 0x0f ///< header pattern at byte 2
371 #define XBOX360_WL_RUMBLE_PAT_3 0xc0 ///< header pattern at byte 3
372 
373 //
374 // LED state request/response/update header packet.
375 //
376 // T 1 2 3
377 // 00 00 08 led
378 //
379 #define XBOX360_WL_LED_PAT_1 0x00 ///< header pattern at byte 1
380 #define XBOX360_WL_LED_PAT_2 0x08 ///< header pattern at byte 2
381 #define XBOX360_WL_LED_PAT_3 0x40 ///< header pattern at byte 3
382 
383 //
384 // F8 state request/response/update header packet. Appears to be tied to link
385 // or controller operational state.
386 //
387 // T 1 2 3 body
388 // 00 f8 02 00 00...
389 //
390 #define XBOX360_WL_F8_PAT_1 0xf8 ///< header pattern at byte 1
391 #define XBOX360_WL_F8_PAT_2_1 0x01 ///< header pattern at byte 2
392 #define XBOX360_WL_F8_PAT_2_2 0x02 ///< header pattern at byte 2
393 #define XBOX360_WL_F8_PAT_2_3 0x03 ///< header pattern at byte 2
394 #define XBOX360_WL_F8_PAT_3 0x00 ///< header pattern at byte 3
395 
396 //
397 // 32 state request/response/update header packet. Unknown.
398 //
399 // T 1 2 3 body
400 // 00 32 5f fa 01 88 ff ff 50 c8 19 81 ff ff ff ff 30 c8 19 81 ff ff ff ff 40
401 // f0 0d 81 ff
402 //
403 #define XBOX360_WL_32_PAT_1 0x32 ///< header pattern at byte 1
404 #define XBOX360_WL_32_PAT_2 0x00 ///< header pattern at byte 2?
405 #define XBOX360_WL_32_PAT_3 0x00 ///< header pattern at byte 3?
406 
407 
408 // .............................................................................
409 // Wired controller specific packets
410 // .............................................................................
411 
412 // no specifics yet
413 
414 
415 // .............................................................................
416 // Common controller messages
417 // .............................................................................
418 
419 //
420 // Message header.
421 //
422 #define XBOX360_MSG_TYPE_POS 0 ///< message type byte position
423 #define XBOX360_MSG_LEN_POS 1 ///< message length byte position
424 
425 // --
426 // The messages. All positions are relative to message start within packet.
427 // --
428 
429 // --
430 // Button state update message.
431 //
432 // Button state update message length are different for the wired and
433 // wireles interface. However both have trailing padding bytes, so take the
434 // smallest of the two as the minumum
435 // --
436 #define XBOX360_BTTN_MSG_TYPE 0x00 ///< button update msg type
437 #define XBOX360_BTTN_MSG_LEN 20 ///< wired button msg length
438 #define XBOX360_WL_BTTN_MSG_LEN 19 ///< wireless button msg len
439 #define XBOX360_BTTN_MSG_MIN_LEN XBOX360_WL_BTTN_MSG_LEN // length
440 
441 // D pad
442 #define XBOX360_BTTN_PAD_UP_POS 2 ///< byte position
443 #define XBOX360_BTTN_PAD_UP_MASK 0x01 ///< bit mask
444 
445 #define XBOX360_BTTN_PAD_DOWN_POS 2 ///< byte position
446 #define XBOX360_BTTN_PAD_DOWN_MASK 0x02 ///< bit mask
447 
448 #define XBOX360_BTTN_PAD_LEFT_POS 2 ///< byte position
449 #define XBOX360_BTTN_PAD_LEFT_MASK 0x04 ///< bit mask
450 
451 #define XBOX360_BTTN_PAD_RIGHT_POS 2 ///< byte position
452 #define XBOX360_BTTN_PAD_RIGHT_MASK 0x08 ///< bit mask
453 
454 // start button
455 #define XBOX360_BTTN_START_POS 2 ///< byte position
456 #define XBOX360_BTTN_START_MASK 0x10 ///< bit mask
457 
458 // back button
459 #define XBOX360_BTTN_BACK_POS 2 ///< byte position
460 #define XBOX360_BTTN_BACK_MASK 0x20 ///< bit mask
461 
462 // left stick click
463 #define XBOX360_BTTN_LEFT_STICK_CLICK_POS 2 ///< byte position
464 #define XBOX360_BTTN_LEFT_STICK_CLICK_MASK 0x40 ///< bit mask
465 
466 // right stick click
467 #define XBOX360_BTTN_RIGHT_STICK_CLICK_POS 2 ///< byte position
468 #define XBOX360_BTTN_RIGHT_STICK_CLICK_MASK 0x80 ///< bit mask
469 
470 // left bump
471 #define XBOX360_BTTN_LEFT_BUMP_POS 3 ///< byte position
472 #define XBOX360_BTTN_LEFT_BUMP_MASK 0x01 ///< bit mask
473 
474 // right bump
475 #define XBOX360_BTTN_RIGHT_BUMP_POS 3 ///< byte position
476 #define XBOX360_BTTN_RIGHT_BUMP_MASK 0x02 ///< bit mask
477 
478 // center X button
479 #define XBOX360_BTTN_CENTER_X_POS 3 ///< byte position
480 #define XBOX360_BTTN_CENTER_X_MASK 0x04 ///< bit mask
481 
482 // A button
483 #define XBOX360_BTTN_A_BUTTON_POS 3 ///< byte position
484 #define XBOX360_BTTN_A_BUTTON_MASK 0x10 ///< bit mask
485 
486 // B button
487 #define XBOX360_BTTN_B_BUTTON_POS 3 ///< byte position
488 #define XBOX360_BTTN_B_BUTTON_MASK 0x20 ///< bit mask
489 
490 // X button
491 #define XBOX360_BTTN_X_BUTTON_POS 3 ///< byte position
492 #define XBOX360_BTTN_X_BUTTON_MASK 0x40 ///< bit mask
493 
494 // Y button
495 #define XBOX360_BTTN_Y_BUTTON_POS 3 ///< byte position
496 #define XBOX360_BTTN_Y_BUTTON_MASK 0x80 ///< bit mask
497 
498 // left trigger
499 #define XBOX360_BTTN_LEFT_TRIGGER_POS 4 ///< byte position
500 #define XBOX360_BTTN_LEFT_TRIGGER_MASK 0xff ///< bit mask
501 
502 // right trigger
503 #define XBOX360_BTTN_RIGHT_TRIGGER_POS 5 ///< byte position
504 #define XBOX360_BTTN_RIGHT_TRIGGER_MASK 0xff ///< bit mask
505 
506 #define XBOX360_TRIGGER_MIN 0 ///< minimum trigger value
507 #define XBOX360_TRIGGER_MAX 255 ///< maximum trigger value
508 
509 // left stick
510 #define XBOX360_BTTN_LEFT_X_POS_L 6 ///< byte position
511 #define XBOX360_BTTN_LEFT_X_POS_H 7 ///< byte position
512 #define XBOX360_BTTN_LEFT_X_MASK 0xffff ///< bit mask
513 
514 #define XBOX360_BTTN_LEFT_Y_POS_L 8 ///< byte position
515 #define XBOX360_BTTN_LEFT_Y_POS_H 9 ///< byte position
516 #define XBOX360_BTTN_LEFT_Y_MASK 0xffff ///< bit mask
517 
518 // right stick
519 #define XBOX360_BTTN_RIGHT_X_POS_L 10 ///< byte position
520 #define XBOX360_BTTN_RIGHT_X_POS_H 11 ///< byte position
521 #define XBOX360_BTTN_RIGHT_X_MASK 0xffff ///< bit mask
522 
523 #define XBOX360_BTTN_RIGHT_Y_POS_L 12 ///< byte position
524 #define XBOX360_BTTN_RIGHT_Y_POS_H 13 ///< byte position
525 #define XBOX360_BTTN_RIGHT_Y_MASK 0xffff ///< bit mask
526 
527 #define XBOX360_JOY_MIN -32767 ///< minimum joystick value
528 #define XBOX360_JOY_MAX 32767 ///< maximum joystick value
529 
530 // --
531 // Rumble motors request/response message
532 // --
533 #define XBOX360_RUMBLE_MSG_TYPE 0x00 ///< rumble message type
534 #define XBOX360_RUMBLE_MSG_LEN 8 ///< rumble read/write message length
535 
536 #define XBOX360_RUMBLE_ZERO_2 2 ///< byte position with 0 value
537 
538 // low-frequency left rumble motor
539 #define XBOX360_RUMBLE_LEFT_POS 3 ///< byte position
540 #define XBOX360_RUMBLE_LEFT_MASK 0xff ///< bit mask
541 #define XBOX360_RUMBLE_LEFT_MAX 255 ///< left rumble motor maximum
542 
543 // high-frequency right rumble motor
544 #define XBOX360_RUMBLE_RIGHT_POS 4 ///< byte position
545 #define XBOX360_RUMBLE_RIGHT_MASK 0xff ///< bit mask
546 #define XBOX360_RUMBLE_RIGHT_MAX 255 ///< right rumble motor maximum
547 
548 // pad
549 #define XBOX360_RUMBLE_ZERO_5 5 ///< byte position with 0 value
550 #define XBOX360_RUMBLE_ZERO_6 6 ///< byte position with 0 value
551 #define XBOX360_RUMBLE_ZERO_7 7 ///< byte position with 0 value
552 
553 
554 // --
555 // LED request/response/update pattern message
556 // --
557 
558 // wired
559 #define XBOX360_LED_MSG_TYPE 0x01 ///< LED message type
560 #define XBOX360_LED_MSG_LEN 3 ///< LED message length
561 #define XBOX360_LED_PAT_POS 2 ///< LED messge byte position
562 
563 // wireless
564 #define XBOX360_WL_LED_MSG_TYPE 0x00 ///< LED message type
565 #define XBOX360_WL_LED_MSG_LEN 8 ///< LED message length
566 #define XBOX360_WL_LED_PAT_POS 3 ///< LED position is in PACKET header
567 
568 // patterns
569 #define XBOX360_LED_PAT_MASK 0x0f ///< bit mask
570 #define XBOX360_LED_PAT_ALL_OFF 0 ///< all 4 LEDs off
571 #define XBOX360_LED_PAT_ALL_BLINK_ON_1 1 ///< all 4 LEDs blink, then #1 on
572 #define XBOX360_LED_PAT_1_BLINK_ON 2 ///< #1 LED blinks, then #1 on
573 #define XBOX360_LED_PAT_2_BLINK_ON 3 ///< #2 LED blinks, then #2 on
574 #define XBOX360_LED_PAT_3_BLINK_ON 4 ///< #3 LED blinks, then #3 on
575 #define XBOX360_LED_PAT_4_BLINK_ON 5 ///< #4 LED blinks, then #4 on
576 #define XBOX360_LED_PAT_1_ON 6 ///< #1 LED on
577 #define XBOX360_LED_PAT_2_ON 7 ///< #2 LED on
578 #define XBOX360_LED_PAT_3_ON 8 ///< #3 LED on
579 #define XBOX360_LED_PAT_4_ON 9 ///< #4 LED on
580 #define XBOX360_LED_PAT_ALL_SPIN 10 ///< spinnig LEDs one at a time
581 #define XBOX360_LED_PAT_4_BLINK_ON_L 11 ///< #4 LED blinks long, then #4 on
582 #define XBOX360_LED_PAT_4_BLINK 12 ///< #4 LED blinks slowly
583 #define XBOX360_LED_PAT_ALL_SPIN_2 13 ///< spinnig LEDs two at a time
584  ///< auto-transitions to previous
585 #define XBOX360_LED_PAT_ALL_BLINK 14 ///< all 4 LEDs blink continuously
586 #define XBOX360_LED_PAT_NUMOF 15 ///< number of patterns
587 
588 /*!
589  * \}
590  */
591 
592 /*!
593  * \ingroup periph_hid_xbox
594  * \defgroup periph_hid_xbox_tune Tuning
595  * \{
596  *
597  * Xbox tuning heuristics.
598  */
599 
600 /*! left joystick deadzone */
601 #define XBOX360_LEFT_DEAD_ZONE ((int)(0.25 * XBOX360_JOY_MAX))
602 
603 /*! right joystick deadzone */
604 #define XBOX360_RIGHT_DEAD_ZONE ((int)(0.25 * XBOX360_JOY_MAX))
605 
606 /*!
607  * \}
608  */
609 
610 
611 namespace rnr
612 {
613  /*!
614  * \ingroup periph_hid_xbox
615  * \brief Xbox360 Feature IDs.
616  */
617  typedef enum
618  {
619  Xbox360FeatIdPadUp = 0, ///< dpad up [0,1]
620  Xbox360FeatIdPadDown, ///< dpad down [0,1]
621  Xbox360FeatIdPadLeft, ///< dpad left [0,1]
622  Xbox360FeatIdPadRight, ///< dpad right [0,1]
623  Xbox360FeatIdStart, ///< start button [0,1]
624  Xbox360FeatIdBack, ///< back button [0,1]
625  Xbox360FeatIdLeftStickClick, ///< left joystick click(left thumb) [0,1]
626  Xbox360FeatIdRightStickClick, ///< right joystick click(right thumb) [0,1]
627  Xbox360FeatIdLeftBump, ///< left bump (left shoulder) [0,1]
628  Xbox360FeatIdRightBump, ///< right bump (right shoulder) [0,1]
629  Xbox360FeatIdCenterX, ///< center x (big button) [0,1]
630  Xbox360FeatIdAButton, ///< A button [0,1]
631  Xbox360FeatIdBButton, ///< B button [0,1]
632  Xbox360FeatIdXButton, ///< X button [0,1]
633  Xbox360FeatIdYButton, ///< Y button [0,1]
634  Xbox360FeatIdLeftTrigger, ///< left trigger [0-255]
635  Xbox360FeatIdRightTrigger, ///< right trigger [0-255]
636  Xbox360FeatIdLeftJoyX, ///< left joystick x value [-32768-32767]
637  Xbox360FeatIdLeftJoyY, ///< left joystick y value [-32768-32767]
638  Xbox360FeatIdRightJoyX, ///< right joystick x value [-32768-32767]
639  Xbox360FeatIdRightJoyY, ///< right joystick x value [-32768-32767]
640  Xbox360FeatIdLeftRumble, ///< left low-freq rumble motor [0-255]
641  Xbox360FeatIdRightRumble, ///< right hi-freq rumble motor [0-255]
642  Xbox360FeatIdLEDPat, ///< LED pattern feature
643 
644  Xbox360FeatIdNumOf, ///< number of features (keep last)
645  } Xbox360FeatId;
646 
647  /*!
648  * \ingroup periph_hid_xbox
649  * \brief Xbox360 controller HID input class.
650  */
651  class HIDXbox360 : public HIDInput
652  {
653  public:
654  /*! \brief Transfer buffer maximum byte size. */
655  static const int UsbTransferBufSize = 32;
656 
657  /*! \brief Consecutive receive timeouts threshold default. */
658  static const int NErrorRcvTimeoutThDft = 0;
659 
660  /*! \brief Consecutive receive errors threshold default. */
661  static const int NErrorRcvThDft = 5;
662 
663  /*! \brief Total receive errors threshold default. */
664  static const int NErrorTotalThDft = 0;
665 
666  /*!
667  * \brief Update thread states.
668  */
670  {
671  ThreadStateNone, ///< no thread
672  ThreadStateRunning, ///< thread running
673  ThreadStateExit ///< thread to exit
674  };
675 
676  /*!
677  * \brief USB transfer packet purpose.
678  */
679  typedef enum
680  {
681  UsbPktIdInput = 0, ///< normal input (read) transfer packet
682  UsbPktIdRumble, ///< rumble (write) transfer packet
683  UsbPktIdLED, ///< LED (write) transfer packet
684  UsbPktIdGoad, ///< Goad (write) transfer packet
685  UsbPktIdNumOf ///< number of transfer packet ids
686  } UsbPktId;
687 
688  /*!
689  * \brief USB application packet transfer structure.
690  */
691  typedef struct
692  {
693  struct libusb_transfer *m_usbTransfer; ///< allocated transfer packet
694  byte_t m_bufData[UsbTransferBufSize]; ///< transfer data buffer
695  bool m_bHasSubmitted; ///< has [not] been submitted
696  bool m_bCancel; ///< do [not] cancel submissions
697  } UsbPkt_T;
698 
699  /*!
700  * \brief Default initialization constructor.
701  */
702  HIDXbox360(int usbDebugLevel=XBOX_LIBUSB_DEBUG_DFT);
703 
704  /*!
705  * \brief Destructor.
706  */
707  ~HIDXbox360();
708 
709  // .........................................................................
710  // Common HID interface
711  // .........................................................................
712 
713  /*!
714  * \brief Get the value associated with the mapped user mnemonic.
715  *
716  * \param iMnem User-specific mnemonic.
717  *
718  * \return Feature current value.
719  */
720  virtual int getFeatureVal(int iMnem)
721  {
722  FeatMap_T::iterator pos;
723 
724  if( (pos = m_featMap.find(iMnem)) != m_featMap.end() )
725  {
726  return m_state[pos->second];
727  }
728  else
729  {
730  return -1;
731  }
732  }
733 
734  /*!
735  * \brief Set the value associated with the mapped user mnemonic.
736  *
737  * \param iMnem User-specific mnemonic.
738  * \param [in] nVal New input HID output value (e.g.LED).
739  * Meaning is specific to input.
740  *
741  * \copydoc doc_return_usb
742  */
743  virtual int setFeatureVal(int iMnem, int nVal);
744 
745  /*!
746  * \brief Get the feature properties.
747  *
748  * \param iMnem User-specific mnemonic.
749  * \param [out] eFeatType Feature property type.
750  * \param [out] nDir Feature is an input (HID_FEAT_INPUT) to host
751  * and/or an output (HID_FEAT_OUTPUT) to device.
752  * \param [out] nMin Feature minimum value.
753  * \param [out] nMax Feature maximum value.
754  * \param [out] nStep Feature step size between [min,max]
755  *
756  * \copydoc doc_return_usb
757  */
758  virtual int getFeatureProp(int iMnem,
759  HIDFeatType &eFeatType,
760  int &nDir,
761  int &nMin,
762  int &nMax,
763  int &nStep);
764 
765  /*!
766  * \brief Get the current full state of the device.
767  *
768  * \return Return (int *) pointer to the device-specific input state.
769  */
770  virtual void *getCurrentState() { return m_state; }
771 
772  /*!
773  * \brief Ping device if it is connected and is responding.
774  *
775  * \note Ping does not work for wireless controllers unless link is up which
776  * kind of defeats the purpose of ping.
777  *
778  * \not Ping is not reliable. Always return true.
779  *
780  * \return Returns true if the HID is responding, else false.
781  */
782  virtual bool ping();
783 
784  /*!
785  * \brief Get the string associated with the error number.
786  *
787  * \param nError HID-specific error number.
788  *
789  * \return Null-terminated error string.
790  */
791  virtual const char *getStrError(int nError) const
792  {
793  return nError < 0? libusb_error_name(nError): "Ok";
794  }
795 
796  /*!
797  * \brief Open connection to an USB Xbox360 controller.
798  *
799  * The first Xbox found will be opened. All intefaces will be bound to this
800  * class instance.
801  *
802  * \copydoc doc_return_usb
803  */
804  virtual int open();
805 
806  /*!
807  * \brief Close connection to an opened USB Xbox360 controller.
808  *
809  * All interface will be released.
810  *
811  * \copydoc doc_return_usb
812  */
813  virtual int close();
814 
815  /*!
816  * \brief Create and run USB update in thread.
817  *
818  * \param hz Update Hertz.
819  *
820  * \return Returns 0 on success, \h_lt 0 on failure.
821  */
822  virtual int run(float hz=30.0);
823 
824  /*!
825  * \brief Stop and destroy USB update thread.
826  *
827  * \return Returns 0 on success, \h_lt 0 on failure.
828  */
829  virtual int stop();
830 
831  /*!
832  * \brief Read device and update HID state.
833  *
834  * \note The application must call this function at regular intervals to
835  * insure that the USB stream does not stall.
836  *
837  * \param uMSec Block wait at most the given milliseconds for events.\n
838  * If zero, then update() will handle any already-pending
839  * events and then immediately return (non-blocking).\n
840  * Otherwise, if no events are currently pending, update()
841  * will block waiting for events for up to the specified
842  * timeout. If an event arrives update() will return early.
843  *
844  * \copydoc doc_return_std
845  */
846  virtual int update(uint_t uMSec=T_UPDATE_DFT);
847 
848  // .........................................................................
849  // Device-specific interface
850  // .........................................................................
851 
852  /*!
853  * \brief Set the rumble instensity.
854  *
855  * \note The left motor only really works above 255.
856  * \note The right motor only really works below 256.
857  *
858  * \todo Does not work for wireless xbox controller yet.
859  *
860  * \param nLeftMot Left, low-frequency motor [0-255].
861  * \param nRightMot Right, high-frequency motor [0-255].
862  *
863  * \copydoc doc_return_usb
864  */
865  int setRumble(int nLeftMot, int nRightMot);
866 
867  /*!
868  * \brief Set the LED pattern.
869  *
870  * \todo Does not work for wireless xbox controller yet.
871  *
872  * \param nPattern LED pattern. See \ref XBOX_LED_PAT_ALL_OFF ...
873  *
874  * \copydoc doc_return_usb
875  */
876  int setLED(int nPattern);
877 
878  /*!
879  * \brief Set joysticks calibration.
880  *
881  * For each joystick, there exist a deadzone near the center that should
882  * be treated as zero due to hardware induced noise. Each stick output is
883  * calibrated as illustrated:
884  * \verbatim
885  *
886  * Uncalibrated DeadZone Calibrated
887  * | |
888  * max - . max - .
889  * | . | .
890  * | . | .
891  * | . ==> | .
892  * | . | .
893  * |. | .
894  * 0 +----------|-- 0 +....------|--
895  * max dz max
896  *
897  *
898  * \endverbatim
899  */
900  void calibrateJoySticks(int nLeftDeadZone, int nRightDeadZone);
901 
902  /*!
903  * \brief Set error count thresholds.
904  *
905  * \param nErrorRcvTimeoutTh Consecutive receive timeout error count
906  * threshold. Set to 0 to ignore.
907  * \param nErrorRcvTh Consecutive receive error count threshold.
908  * Set to 0 to ignore.
909  * \param nErrorTotalTh Total receive error count threshold.
910  * Set to 0 to ignore.
911  */
912  void setErrorThresholds(int nErrorRcvTimeoutTh = NErrorRcvTimeoutThDft,
913  int nErrorRcvTh = NErrorRcvThDft,
914  int nErrorTotalTh = NErrorTotalThDft)
915  {
916  m_nErrorRcvTimeoutTh = nErrorRcvTimeoutTh;
917  m_nErrorRcvTh = nErrorRcvTh;
918  m_nErrorTotalTh = nErrorTotalTh;
919  }
920 
921  /*!
922  * \brief Check if Xbox360 hardware is wireless.
923  *
924  * \return Returns true or false.
925  */
926  bool isWireless()
927  {
929  m_nProdId == XBOX360_WIRELESS_WIN_PROD_ID;
930  }
931 
932  /*!
933  * \brief Simple debug print header.
934  */
935  void debugPrintHdr();
936 
937  /*!
938  * \brief Simple debug print state.
939  */
940  void debugPrintState();
941 
942  protected:
943  // XBox360 specifics
944  int m_nProdId; ///< Xbox USB product id
945  std::string m_strProdName; ///< product name
946  std::string m_strSerialNum; ///< serial number
947  int m_nBatteryStatus; ///< batter status
948  int m_nStatus; ///< raw status byte
949  int m_nOffset; ///< start offset in message
950 
951  // calibration
952  int m_nLeftJoyDeadZone; ///< left joystick deadzone
953  float m_fLeftJoyM; ///< left joystick slope
954  float m_fLeftJoyB; ///< left joystick y intercept
955  int m_nRightJoyDeadZone; ///< right joystick deadzone
956  float m_fRightJoyM; ///< right joystick slope
957  float m_fRightJoyB; ///< right joystick y intercept
958 
959  // state
960  int m_state[Xbox360FeatIdNumOf]; ///< current state
961  int m_nLEDPattern; ///< target LED pattern
962  int m_nLeftRumble; ///< target left rumble intensity
963  int m_nRightRumble; ///< target right rumble intensity
964 
965  // Errors
966  int m_nErrorRcv; ///< consecutive rcv error count
967  int m_nErrorRcvTh; ///< consec rcv error threshold
968  int m_nErrorRcvTimeout; ///< consec rcv timeout error cnt
969  int m_nErrorRcvTimeoutTh; ///< consec rcv timeout error th
970  int m_nErrorSend; ///< consec send error count
971  int m_nErrorTotal; ///< total error count
972  int m_nErrorTotalTh; ///< total error threshold
973 
974  // USB
975  struct libusb_context *m_usbContext; ///< libusb session context
976  struct libusb_device_handle *m_usbHandle; ///< USB open handle
978  ///< USB transfer packets
979  // thread
980  ThreadState m_eThreadState; ///< update thread state
981  struct timespec m_tsThread; ///< update thread period
982  pthread_mutex_t m_mutexUpdate; ///< mutex
983  pthread_cond_t m_condUpdate; ///< condition
984  pthread_t m_threadId; ///< update pthread identifier
985  /*!
986  * \brief Set new link state.
987  *
988  * \param bNewState New link state.
989  */
990  virtual void setLinkState(bool bNewState);
991 
992  /*!
993  * \brief Open Xbox360 connection.
994  *
995  * Checks for all supported Xbox Controllers and opens the first for
996  * communication All interfaces are claimed.
997  *
998  * \copydoc doc_return_usb
999  */
1000  int openXbox();
1001 
1002  /*!
1003  * \brief Claim all Xbox USB interfaces.
1004  *
1005  * Any any kernel claimed drivers will be detached.
1006  *
1007  * \copydoc doc_return_usb
1008  */
1009  int claimXboxInterfaces();
1010 
1011  /*!
1012  * \brief Allocate and initialize all USB transfer packets.
1013  *
1014  * \copydoc doc_return_usb
1015  */
1016  int initTransferPkts();
1017 
1018  /*!
1019  * \brief Free all USB transfer packets.
1020  *
1021  * \copydoc doc_return_usb
1022  */
1023  void freeTransferPkts();
1024 
1025  /*!
1026  * \brief Submit read Xbox input asynchronous transfer.
1027  *
1028  * \copydoc doc_return_usb
1029  */
1030  int submitReadTransfer();
1031 
1032  /*!
1033  * \brief Flush any pending input transfer buffers.
1034  */
1035  void flushInput();
1036 
1037  /*!
1038  * \brief Input transfer function callback.
1039  *
1040  * \param transfer USB transfer block structure.
1041  */
1042  static void transferCallbackInput(struct libusb_transfer *transfer);
1043 
1044  /*!
1045  * \brief Write rumble motor values transfer function callback.
1046  *
1047  * \param transfer USB transfer block structure.
1048  */
1049  static void transferCallbackRumble(struct libusb_transfer *transfer);
1050 
1051  /*!
1052  * \brief Write LED pattern transfer function callback.
1053  *
1054  * \param transfer USB transfer block structure.
1055  */
1056  static void transferCallbackLED(struct libusb_transfer *transfer);
1057 
1058  /*!
1059  * \brief Write packet to force a response packet from the Xbox360.
1060  *
1061  * Note that the response is captured in the input transfer.
1062  *
1063  * \param transfer USB transfer block structure.
1064  */
1065  static void transferCallbackGoad(struct libusb_transfer *transfer);
1066 
1067  /*!
1068  * \brief Cancel all pending USB transfers.
1069  */
1070  void cancelTransfers();
1071 
1072  /*!
1073  * \brief Goad the Xbox360 to send a message.
1074  *
1075  * Are you still linked?
1076  */
1077  void goad();
1078 
1079  /*!
1080  * \brief Parse wireless Xbox360 input events.
1081  *
1082  * While executing, the Xbox controller can be unplugged. For a wireless
1083  * connection, the dongle - Xbox controller can become unpaired,
1084  * out-of-range, or wonking connection.
1085  *
1086  * \param buf Raw USB input buffer.
1087  * \param n Length of data in buffer.
1088  *
1089  * \return Returns true if good parse, else false.
1090  */
1091  bool parseWireless(byte_t buf[], ssize_t n);
1092 
1093  /*!
1094  * \brief Parse wired Xbox360 input events.
1095  *
1096  * \param buf Raw USB input buffer.
1097  * \param n Length of data in buffer.
1098  *
1099  * \return Returns true if good parse, else false.
1100  */
1101  bool parseWired(byte_t buf[], ssize_t n);
1102 
1103  /*!
1104  * \brief Update the controller input button state.
1105  *
1106  * \param msg Message containing raw controller state data.
1107  * \param n Length of data in message.
1108  */
1109  void updateButtonState(byte_t msg[], ssize_t n);
1110 
1111  /*!
1112  * \brief Update the rumble motors state.
1113  *
1114  * \param msg Message containing raw controller status data.
1115  * \param n Length of data in message.
1116  */
1117  void updateRumbleState(byte_t msg[], ssize_t n);
1118 
1119  /*!
1120  * \brief Update the LED pattern state.
1121  *
1122  * \note Wireless controller has LED state in the packet header for
1123  * some idiotic reason.
1124  *
1125  * \param buf Raw USB input buffer.
1126  * \param n Length of data in buffer.
1127  */
1128  void updateLEDState(byte_t buf[], ssize_t n);
1129 
1130  /*!
1131  * \brief Clear error counts.
1132  */
1134  {
1135  m_nErrorRcvTimeout = 0;
1136  m_nErrorRcv = 0;
1137  m_nErrorSend = 0;
1138  m_nErrorTotal = 0;
1139  }
1140 
1141  /*!
1142  * \brief Check errors against thresholds.
1143  *
1144  * If any of the error counts exceed their respective thresholds, then
1145  * the connection state is automatically transitioned to the 'no link'
1146  * state.
1147  *
1148  * \return Returns true if link is good, else false.
1149  */
1150  bool checkErrorThresholds();
1151 
1152  /*!
1153  * \brief Zero controller shadowed state.
1154  */
1155  void zeroState()
1156  {
1157  memset(m_state, 0, sizeof(m_state));
1158  }
1159 
1160  /*!
1161  * \brief Convert bit value to button up/down state.
1162  *
1163  * \param nBit Bit value.
1164  *
1165  * \return HID_BTTN_UP or HID_BTTN_DOWN
1166  */
1167  int updown(int nBit)
1168  {
1169  return nBit != 0? HID_BTTN_DOWN: HID_BTTN_UP;
1170  }
1171 
1172  /*!
1173  * \brief Convert two bytes to signed integer.
1174  *
1175  * \param lsb Least significant byte.
1176  * \param msb Most significant byte.
1177  *
1178  * \return Converted integer value.
1179  */
1180  int convertToInt(byte_t lsb, byte_t msb);
1181 
1182  /*!
1183  * \brief Create USB update thread.
1184  *
1185  * \return Returns 0 on success, \h_lt 0 on failure.
1186  */
1187  int createUpdateThread();
1188 
1189  /*!
1190  * \brief Cancel (destroy) USB update thread.
1191  *
1192  * \return Returns 0 on success, \h_lt 0 on failure.
1193  */
1194  int cancelUpdateThread();
1195 
1196  /*!
1197  * \brief Lock update thread.
1198  *
1199  * The calling thread will block while waiting for the mutex to become
1200  * available. Once locked, the update thread will block.
1201  *
1202  * The lock()/unlock() primitives provide a safe mechanism to modify state.
1203  *
1204  * \par Context:
1205  * Any.
1206  */
1207  void lock()
1208  {
1209  pthread_mutex_lock(&m_mutexUpdate);
1210  }
1211 
1212  /*!
1213  * \brief Unlock the update thread.
1214  *
1215  * The update() will be available to execute.
1216  *
1217  * \par Context:
1218  * Any.
1219  */
1220  void unlock()
1221  {
1222  pthread_mutex_unlock(&m_mutexUpdate);
1223  }
1224 
1225  /*!
1226  * \brief Signal update thread of change of state.
1227  *
1228  * \par Context:
1229  * Calling thread or Update thread.
1230  */
1231  void signalUpdateThread();
1232 
1233  /*!
1234  * \brief Blocked timed wait.
1235  *
1236  * \par Context:
1237  * Update thread.
1238  */
1239  void blockWait();
1240 
1241  /*!
1242  * \brief The update thread.
1243  *
1244  * \param pArg Point to this class instance.
1245  *
1246  * \return NULL
1247  */
1248  static void *updateThread(void *pArg);
1249 
1250  /*!
1251  * \brief Debug print USB transfer buffer contents.
1252  *
1253  * \param strPreface User identifying preface string.
1254  * \param tranfer Pointer to USB transfer buffer.
1255  */
1256  void debugPrintTransferBuf(const std::string &strPreface,
1257  struct libusb_transfer *transfer)
1258  {
1259  debugPrintTransferBuf(strPreface, transfer->buffer,
1260  transfer->actual_length);
1261  }
1262 
1263  /*!
1264  * \brief Debug print USB transfer buffer contents.
1265  *
1266  * \param strPreface User identifying preface string.
1267  * \param buf USB data buffer.
1268  * \param n USB data buffer length
1269  */
1270  void debugPrintTransferBuf(const std::string &strPreface,
1271  byte_t buf[],
1272  ssize_t n,
1273  const std::string &strEoR="\n");
1274 
1275  };
1276 } // namespace
1277 
1278 
1279 #endif // _HIDXBOX360_H
static void transferCallbackGoad(struct libusb_transfer *transfer)
Write packet to force a response packet from the Xbox360.
virtual int close()
Close connection to an opened USB Xbox360 controller.
Definition: HIDXbox360.cxx:230
B button [0,1].
Definition: HIDXbox360.h:631
struct libusb_transfer * m_usbTransfer
allocated transfer packet
Definition: HIDXbox360.h:693
int createUpdateThread()
Create USB update thread.
void flushInput()
Flush any pending input transfer buffers.
static const int T_UPDATE_DFT
default update timeout (msec)
Definition: HID.h:104
UsbPkt_T m_usbPkt[UsbPktIdNumOf]
USB transfer packets.
Definition: HIDXbox360.h:977
~HIDXbox360()
Destructor.
Definition: HIDXbox360.cxx:141
virtual int open()
Open connection to an USB Xbox360 controller.
Definition: HIDXbox360.cxx:159
void freeTransferPkts()
Free all USB transfer packets.
Definition: HIDXbox360.cxx:999
rumble (write) transfer packet
Definition: HIDXbox360.h:682
void debugPrintState()
Simple debug print state.
back button [0,1]
Definition: HIDXbox360.h:624
void clearErrorCnts()
Clear error counts.
Definition: HIDXbox360.h:1133
void unlock()
Unlock the update thread.
Definition: HIDXbox360.h:1220
static const int UsbTransferBufSize
Transfer buffer maximum byte size.
Definition: HIDXbox360.h:655
void signalUpdateThread()
Signal update thread of change of state.
void debugPrintTransferBuf(const std::string &strPreface, struct libusb_transfer *transfer)
Debug print USB transfer buffer contents.
Definition: HIDXbox360.h:1256
std::string m_strProdName
product name
Definition: HIDXbox360.h:945
void zeroState()
Zero controller shadowed state.
Definition: HIDXbox360.h:1155
float m_fRightJoyB
right joystick y intercept
Definition: HIDXbox360.h:957
#define HID_BTTN_UP
button/key state is up or unpressed
Definition: HID.h:52
virtual void * getCurrentState()
Get the current full state of the device.
Definition: HIDXbox360.h:770
bool m_bCancel
do [not] cancel submissions
Definition: HIDXbox360.h:696
int initTransferPkts()
Allocate and initialize all USB transfer packets.
Definition: HIDXbox360.cxx:933
int m_nProdId
Xbox USB product id.
Definition: HIDXbox360.h:944
void lock()
Lock update thread.
Definition: HIDXbox360.h:1207
int m_nErrorSend
consec send error count
Definition: HIDXbox360.h:970
int m_nRightRumble
target right rumble intensity
Definition: HIDXbox360.h:963
virtual bool ping()
Ping device if it is connected and is responding.
Definition: HIDXbox360.cxx:414
right trigger [0-255]
Definition: HIDXbox360.h:635
pthread_mutex_t m_mutexUpdate
mutex
Definition: HIDXbox360.h:982
static void transferCallbackRumble(struct libusb_transfer *transfer)
Write rumble motor values transfer function callback.
float m_fLeftJoyB
left joystick y intercept
Definition: HIDXbox360.h:954
float m_fRightJoyM
right joystick slope
Definition: HIDXbox360.h:956
Common Human Interface Device Interface.
float m_fLeftJoyM
left joystick slope
Definition: HIDXbox360.h:953
normal input (read) transfer packet
Definition: HIDXbox360.h:681
center x (big button) [0,1]
Definition: HIDXbox360.h:629
static const int NErrorTotalThDft
Total receive errors threshold default.
Definition: HIDXbox360.h:664
FeatMap_T m_featMap
feature-userid association map
Definition: HID.h:301
int openXbox()
Open Xbox360 connection.
Definition: HIDXbox360.cxx:783
int convertToInt(byte_t lsb, byte_t msb)
Convert two bytes to signed integer.
void setErrorThresholds(int nErrorRcvTimeoutTh=NErrorRcvTimeoutThDft, int nErrorRcvTh=NErrorRcvThDft, int nErrorTotalTh=NErrorTotalThDft)
Set error count thresholds.
Definition: HIDXbox360.h:912
void goad()
Goad the Xbox360 to send a message.
Definition: HIDXbox360.cxx:420
USB application packet transfer structure.
Definition: HIDXbox360.h:691
void debugPrintHdr()
Simple debug print header.
virtual const char * getStrError(int nError) const
Get the string associated with the error number.
Definition: HIDXbox360.h:791
LED (write) transfer packet.
Definition: HIDXbox360.h:683
bool checkErrorThresholds()
Check errors against thresholds.
int m_nRightJoyDeadZone
right joystick deadzone
Definition: HIDXbox360.h:955
int m_nOffset
start offset in message
Definition: HIDXbox360.h:949
virtual int stop()
Stop and destroy USB update thread.
Definition: HIDXbox360.cxx:322
Y button [0,1].
Definition: HIDXbox360.h:633
int setRumble(int nLeftMot, int nRightMot)
Set the rumble instensity.
Definition: HIDXbox360.cxx:492
#define HID_BTTN_DOWN
button/key state is down or pressed
Definition: HID.h:53
bool m_bHasSubmitted
has [not] been submitted
Definition: HIDXbox360.h:695
A button [0,1].
Definition: HIDXbox360.h:630
bool isWireless()
Check if Xbox360 hardware is wireless.
Definition: HIDXbox360.h:926
void updateLEDState(byte_t buf[], ssize_t n)
Update the LED pattern state.
right joystick click(right thumb) [0,1]
Definition: HIDXbox360.h:626
left joystick click(left thumb) [0,1]
Definition: HIDXbox360.h:625
static const int NErrorRcvTimeoutThDft
Consecutive receive timeouts threshold default.
Definition: HIDXbox360.h:658
virtual int getFeatureProp(int iMnem, HIDFeatType &eFeatType, int &nDir, int &nMin, int &nMax, int &nStep)
Get the feature properties.
Definition: HIDXbox360.cxx:353
right hi-freq rumble motor [0-255]
Definition: HIDXbox360.h:641
int m_state[Xbox360FeatIdNumOf]
current state
Definition: HIDXbox360.h:960
int cancelUpdateThread()
Cancel (destroy) USB update thread.
right joystick x value [-32768-32767]
Definition: HIDXbox360.h:638
HIDXbox360(int usbDebugLevel=0)
Default initialization constructor.
Definition: HIDXbox360.cxx:80
std::string m_strSerialNum
serial number
Definition: HIDXbox360.h:946
int m_nErrorRcv
consecutive rcv error count
Definition: HIDXbox360.h:966
right joystick x value [-32768-32767]
Definition: HIDXbox360.h:639
void updateButtonState(byte_t msg[], ssize_t n)
Update the controller input button state.
static void transferCallbackInput(struct libusb_transfer *transfer)
Input transfer function callback.
void blockWait()
Blocked timed wait.
virtual int getFeatureVal(int iMnem)
Get the value associated with the mapped user mnemonic.
Definition: HIDXbox360.h:720
left joystick x value [-32768-32767]
Definition: HIDXbox360.h:636
Xbox360 controller HID input class.
Definition: HIDXbox360.h:651
int submitReadTransfer()
Submit read Xbox input asynchronous transfer.
int m_nStatus
raw status byte
Definition: HIDXbox360.h:948
static void * updateThread(void *pArg)
The update thread.
virtual int update(uint_t uMSec=T_UPDATE_DFT)
Read device and update HID state.
LED pattern feature.
Definition: HIDXbox360.h:642
ThreadState m_eThreadState
update thread state
Definition: HIDXbox360.h:980
Input Human Interface Device Abstract Base Class.
Definition: HID.h:100
int m_nBatteryStatus
batter status
Definition: HIDXbox360.h:947
static void transferCallbackLED(struct libusb_transfer *transfer)
Write LED pattern transfer function callback.
left joystick y value [-32768-32767]
Definition: HIDXbox360.h:637
int setLED(int nPattern)
Set the LED pattern.
Definition: HIDXbox360.cxx:623
ThreadState
Update thread states.
Definition: HIDXbox360.h:669
Xbox360FeatId
Xbox360 Feature IDs.
Definition: HIDXbox360.h:617
void cancelTransfers()
Cancel all pending USB transfers.
virtual int setFeatureVal(int iMnem, int nVal)
Set the value associated with the mapped user mnemonic.
Definition: HIDXbox360.cxx:331
left bump (left shoulder) [0,1]
Definition: HIDXbox360.h:627
bool parseWired(byte_t buf[], ssize_t n)
Parse wired Xbox360 input events.
dpad right [0,1]
Definition: HIDXbox360.h:622
void calibrateJoySticks(int nLeftDeadZone, int nRightDeadZone)
Set joysticks calibration.
Definition: HIDXbox360.cxx:741
start button [0,1]
Definition: HIDXbox360.h:623
USB extensions and capatabilities.
int updown(int nBit)
Convert bit value to button up/down state.
Definition: HIDXbox360.h:1167
int m_nErrorRcvTimeoutTh
consec rcv timeout error th
Definition: HIDXbox360.h:969
#define XBOX_LIBUSB_DEBUG_DFT
default libusb logging
Definition: HIDXbox360.h:245
int m_nErrorRcvTh
consec rcv error threshold
Definition: HIDXbox360.h:967
UsbPktId
USB transfer packet purpose.
Definition: HIDXbox360.h:679
#define XBOX360_WIRELESS_PROD_ID
Definition: HIDXbox360.h:225
void updateRumbleState(byte_t msg[], ssize_t n)
Update the rumble motors state.
struct timespec m_tsThread
update thread period
Definition: HIDXbox360.h:981
static const int NErrorRcvThDft
Consecutive receive errors threshold default.
Definition: HIDXbox360.h:661
number of transfer packet ids
Definition: HIDXbox360.h:685
virtual int run(float hz=30.0)
Create and run USB update in thread.
Definition: HIDXbox360.cxx:292
bool parseWireless(byte_t buf[], ssize_t n)
Parse wireless Xbox360 input events.
left low-freq rumble motor [0-255]
Definition: HIDXbox360.h:640
pthread_t m_threadId
update pthread identifier
Definition: HIDXbox360.h:984
HIDFeatType
Feature property types.
Definition: HID.h:85
right bump (right shoulder) [0,1]
Definition: HIDXbox360.h:628
dpad left [0,1]
Definition: HIDXbox360.h:621
int m_nErrorTotalTh
total error threshold
Definition: HIDXbox360.h:972
int m_nLEDPattern
target LED pattern
Definition: HIDXbox360.h:961
pthread_cond_t m_condUpdate
condition
Definition: HIDXbox360.h:983
dpad up [0,1]
Definition: HIDXbox360.h:619
int m_nLeftRumble
target left rumble intensity
Definition: HIDXbox360.h:962
number of features (keep last)
Definition: HIDXbox360.h:644
struct libusb_device_handle * m_usbHandle
USB open handle.
Definition: HIDXbox360.h:976
int m_nLeftJoyDeadZone
left joystick deadzone
Definition: HIDXbox360.h:952
RoadNarrows Robotics standard namespace.
Definition: HID.h:65
X button [0,1].
Definition: HIDXbox360.h:632
struct libusb_context * m_usbContext
libusb session context
Definition: HIDXbox360.h:975
dpad down [0,1]
Definition: HIDXbox360.h:620
Goad (write) transfer packet.
Definition: HIDXbox360.h:684
left trigger [0-255]
Definition: HIDXbox360.h:634
int claimXboxInterfaces()
Claim all Xbox USB interfaces.
Definition: HIDXbox360.cxx:861
virtual void setLinkState(bool bNewState)
Set new link state.
Definition: HIDXbox360.cxx:759
int m_nErrorTotal
total error count
Definition: HIDXbox360.h:971
int m_nErrorRcvTimeout
consec rcv timeout error cnt
Definition: HIDXbox360.h:968