Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

XBOXUSB.h 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics. All rights reserved.
  2. This software may be distributed and modified under the terms of the GNU
  3. General Public License version 2 (GPL2) as published by the Free Software
  4. Foundation and appearing in the file GPL2.TXT included in the packaging of
  5. this file. Please note that GPL2 Section 2[b] requires that all works based
  6. on this software must also be made publicly available under the terms of
  7. the GPL2 ("Copyleft").
  8. Contact information
  9. -------------------
  10. Kristian Lauszus, TKJ Electronics
  11. Web : http://www.tkjelectronics.com
  12. e-mail : [email protected]
  13. */
  14. #ifndef _xboxusb_h_
  15. #define _xboxusb_h_
  16. #include "Usb.h"
  17. #include "hid.h"
  18. #include "xboxEnums.h"
  19. /* Data Xbox 360 taken from descriptors */
  20. #define EP_MAXPKTSIZE 32 // max size for data via USB
  21. /* Names we give to the 3 Xbox360 pipes */
  22. #define XBOX_CONTROL_PIPE 0
  23. #define XBOX_INPUT_PIPE 1
  24. #define XBOX_OUTPUT_PIPE 2
  25. // PID and VID of the different devices
  26. #define XBOX_VID 0x045E // Microsoft Corporation
  27. #define MADCATZ_VID 0x1BAD // For unofficial Mad Catz controllers
  28. #define JOYTECH_VID 0x162E // For unofficial Joytech controllers
  29. #define GAMESTOP_VID 0x0E6F // Gamestop controller
  30. #define XBOX_WIRED_PID 0x028E // Microsoft 360 Wired controller
  31. #define XBOX_WIRELESS_PID 0x028F // Wireless controller only support charging
  32. #define XBOX_WIRELESS_RECEIVER_PID 0x0719 // Microsoft Wireless Gaming Receiver
  33. #define XBOX_WIRELESS_RECEIVER_THIRD_PARTY_PID 0x0291 // Third party Wireless Gaming Receiver
  34. #define MADCATZ_WIRED_PID 0xF016 // Mad Catz wired controller
  35. #define JOYTECH_WIRED_PID 0xBEEF // For Joytech wired controller
  36. #define GAMESTOP_WIRED_PID 0x0401 // Gamestop wired controller
  37. #define AFTERGLOW_WIRED_PID 0x0213 // Afterglow wired controller - it uses the same VID as a Gamestop controller
  38. #define XBOX_REPORT_BUFFER_SIZE 14 // Size of the input report buffer
  39. #define XBOX_MAX_ENDPOINTS 3
  40. /** This class implements support for a Xbox wired controller via USB. */
  41. class XBOXUSB : public USBDeviceConfig {
  42. public:
  43. /**
  44. * Constructor for the XBOXUSB class.
  45. * @param pUsb Pointer to USB class instance.
  46. */
  47. XBOXUSB(USB *pUsb);
  48. /** @name USBDeviceConfig implementation */
  49. /**
  50. * Initialize the Xbox Controller.
  51. * @param parent Hub number.
  52. * @param port Port number on the hub.
  53. * @param lowspeed Speed of the device.
  54. * @return 0 on success.
  55. */
  56. uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
  57. /**
  58. * Release the USB device.
  59. * @return 0 on success.
  60. */
  61. uint8_t Release();
  62. /**
  63. * Poll the USB Input endpoins and run the state machines.
  64. * @return 0 on success.
  65. */
  66. uint8_t Poll();
  67. /**
  68. * Get the device address.
  69. * @return The device address.
  70. */
  71. virtual uint8_t GetAddress() {
  72. return bAddress;
  73. };
  74. /**
  75. * Used to check if the controller has been initialized.
  76. * @return True if it's ready.
  77. */
  78. virtual bool isReady() {
  79. return bPollEnable;
  80. };
  81. /**
  82. * Used by the USB core to check what this driver support.
  83. * @param vid The device's VID.
  84. * @param pid The device's PID.
  85. * @return Returns true if the device's VID and PID matches this driver.
  86. */
  87. virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) {
  88. return ((vid == XBOX_VID || vid == MADCATZ_VID || vid == JOYTECH_VID || vid == GAMESTOP_VID) && (pid == XBOX_WIRED_PID || pid == MADCATZ_WIRED_PID || pid == GAMESTOP_WIRED_PID || pid == AFTERGLOW_WIRED_PID || pid == JOYTECH_WIRED_PID));
  89. };
  90. /**@}*/
  91. /** @name Xbox Controller functions */
  92. /**
  93. * getButtonPress(ButtonEnum b) will return true as long as the button is held down.
  94. *
  95. * While getButtonClick(ButtonEnum b) will only return it once.
  96. *
  97. * So you instance if you need to increase a variable once you would use getButtonClick(ButtonEnum b),
  98. * but if you need to drive a robot forward you would use getButtonPress(ButtonEnum b).
  99. * @param b ::ButtonEnum to read.
  100. * @return getButtonClick(ButtonEnum b) will return a bool, while getButtonPress(ButtonEnum b) will return a byte if reading ::L2 or ::R2.
  101. */
  102. uint8_t getButtonPress(ButtonEnum b);
  103. bool getButtonClick(ButtonEnum b);
  104. /**@}*/
  105. /** @name Xbox Controller functions */
  106. /**
  107. * Return the analog value from the joysticks on the controller.
  108. * @param a Either ::LeftHatX, ::LeftHatY, ::RightHatX or ::RightHatY.
  109. * @return Returns a signed 16-bit integer.
  110. */
  111. int16_t getAnalogHat(AnalogHatEnum a);
  112. /** Turn rumble off and all the LEDs on the controller. */
  113. void setAllOff() {
  114. setRumbleOn(0, 0);
  115. setLedRaw(0);
  116. };
  117. /** Turn rumble off the controller. */
  118. void setRumbleOff() {
  119. setRumbleOn(0, 0);
  120. };
  121. /**
  122. * Turn rumble on.
  123. * @param lValue Left motor (big weight) inside the controller.
  124. * @param rValue Right motor (small weight) inside the controller.
  125. */
  126. void setRumbleOn(uint8_t lValue, uint8_t rValue);
  127. /**
  128. * Set LED value. Without using the ::LEDEnum or ::LEDModeEnum.
  129. * @param value See:
  130. * setLedOff(), setLedOn(LEDEnum l),
  131. * setLedBlink(LEDEnum l), and setLedMode(LEDModeEnum lm).
  132. */
  133. void setLedRaw(uint8_t value);
  134. /** Turn all LEDs off the controller. */
  135. void setLedOff() {
  136. setLedRaw(0);
  137. };
  138. /**
  139. * Turn on a LED by using ::LEDEnum.
  140. * @param l ::OFF, ::LED1, ::LED2, ::LED3 and ::LED4 is supported by the Xbox controller.
  141. */
  142. void setLedOn(LEDEnum l);
  143. /**
  144. * Turn on a LED by using ::LEDEnum.
  145. * @param l ::ALL, ::LED1, ::LED2, ::LED3 and ::LED4 is supported by the Xbox controller.
  146. */
  147. void setLedBlink(LEDEnum l);
  148. /**
  149. * Used to set special LED modes supported by the Xbox controller.
  150. * @param lm See ::LEDModeEnum.
  151. */
  152. void setLedMode(LEDModeEnum lm);
  153. /**
  154. * Used to call your own function when the controller is successfully initialized.
  155. * @param funcOnInit Function to call.
  156. */
  157. void attachOnInit(void (*funcOnInit)(void)) {
  158. pFuncOnInit = funcOnInit;
  159. };
  160. /**@}*/
  161. /** True if a Xbox 360 controller is connected. */
  162. bool Xbox360Connected;
  163. protected:
  164. /** Pointer to USB class instance. */
  165. USB *pUsb;
  166. /** Device address. */
  167. uint8_t bAddress;
  168. /** Endpoint info structure. */
  169. EpInfo epInfo[XBOX_MAX_ENDPOINTS];
  170. private:
  171. /**
  172. * Called when the controller is successfully initialized.
  173. * Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
  174. * This is useful for instance if you want to set the LEDs in a specific way.
  175. */
  176. void onInit();
  177. void (*pFuncOnInit)(void); // Pointer to function called in onInit()
  178. bool bPollEnable;
  179. /* Variables to store the buttons */
  180. uint32_t ButtonState;
  181. uint32_t OldButtonState;
  182. uint16_t ButtonClickState;
  183. int16_t hatValue[4];
  184. uint16_t controllerStatus;
  185. bool L2Clicked; // These buttons are analog, so we use we use these bools to check if they where clicked or not
  186. bool R2Clicked;
  187. uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data
  188. uint8_t writeBuf[8]; // General purpose buffer for output data
  189. void readReport(); // read incoming data
  190. void printReport(); // print incoming date - Uncomment for debugging
  191. /* Private commands */
  192. void XboxCommand(uint8_t* data, uint16_t nbytes);
  193. };
  194. #endif