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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

XBOXOLD.h 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* Copyright (C) 2013 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 _xboxold_h_
  15. #define _xboxold_h_
  16. #include "Usb.h"
  17. #include "hid.h"
  18. #include "controllerEnums.h"
  19. /* Data Xbox taken from descriptors */
  20. #define EP_MAXPKTSIZE 32 // Max size for data via USB
  21. /* Names we give to the 3 Xbox 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 XBOX_OLD_PID1 0x0202 // Original Microsoft Xbox controller (US)
  30. #define XBOX_OLD_PID2 0x0285 // Original Microsoft Xbox controller (Japan)
  31. #define XBOX_OLD_PID3 0x0287 // Microsoft Microsoft Xbox Controller S
  32. #define XBOX_OLD_PID4 0x0289 // Smaller Microsoft Xbox controller (US)
  33. #define XBOX_MAX_ENDPOINTS 3
  34. /** This class implements support for a the original Xbox controller via USB. */
  35. class XBOXOLD : public USBDeviceConfig {
  36. public:
  37. /**
  38. * Constructor for the XBOXOLD class.
  39. * @param pUsb Pointer to USB class instance.
  40. */
  41. XBOXOLD(USB *pUsb);
  42. /** @name USBDeviceConfig implementation */
  43. /**
  44. * Initialize the Xbox Controller.
  45. * @param parent Hub number.
  46. * @param port Port number on the hub.
  47. * @param lowspeed Speed of the device.
  48. * @return 0 on success.
  49. */
  50. uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
  51. /**
  52. * Release the USB device.
  53. * @return 0 on success.
  54. */
  55. uint8_t Release();
  56. /**
  57. * Poll the USB Input endpoins and run the state machines.
  58. * @return 0 on success.
  59. */
  60. uint8_t Poll();
  61. /**
  62. * Get the device address.
  63. * @return The device address.
  64. */
  65. virtual uint8_t GetAddress() {
  66. return bAddress;
  67. };
  68. /**
  69. * Used to check if the controller has been initialized.
  70. * @return True if it's ready.
  71. */
  72. virtual bool isReady() {
  73. return bPollEnable;
  74. };
  75. /**
  76. * Used by the USB core to check what this driver support.
  77. * @param vid The device's VID.
  78. * @param pid The device's PID.
  79. * @return Returns true if the device's VID and PID matches this driver.
  80. */
  81. virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) {
  82. return ((vid == XBOX_VID || vid == MADCATZ_VID || vid == JOYTECH_VID) && (pid == XBOX_OLD_PID1 || pid == XBOX_OLD_PID2 || pid == XBOX_OLD_PID3 || pid == XBOX_OLD_PID4));
  83. };
  84. /**@}*/
  85. /** @name Xbox Controller functions */
  86. /**
  87. * getButtonPress(ButtonEnum b) will return true as long as the button is held down.
  88. *
  89. * While getButtonClick(ButtonEnum b) will only return it once.
  90. *
  91. * So you instance if you need to increase a variable once you would use getButtonClick(ButtonEnum b),
  92. * but if you need to drive a robot forward you would use getButtonPress(ButtonEnum b).
  93. * @param b ::ButtonEnum to read.
  94. * @return getButtonClick(ButtonEnum b) will return a bool, while getButtonPress(ButtonEnum b) will return a byte if reading ::L2 or ::R2.
  95. */
  96. uint8_t getButtonPress(ButtonEnum b);
  97. bool getButtonClick(ButtonEnum b);
  98. /**@}*/
  99. /** @name Xbox Controller functions */
  100. /**
  101. * Return the analog value from the joysticks on the controller.
  102. * @param a Either ::LeftHatX, ::LeftHatY, ::RightHatX or ::RightHatY.
  103. * @return Returns a signed 16-bit integer.
  104. */
  105. int16_t getAnalogHat(AnalogHatEnum a);
  106. /** Turn rumble off the controller. */
  107. void setRumbleOff() {
  108. setRumbleOn(0, 0);
  109. };
  110. /**
  111. * Turn rumble on.
  112. * @param lValue Left motor (big weight) inside the controller.
  113. * @param rValue Right motor (small weight) inside the controller.
  114. */
  115. void setRumbleOn(uint8_t lValue, uint8_t rValue);
  116. /**
  117. * Used to call your own function when the controller is successfully initialized.
  118. * @param funcOnInit Function to call.
  119. */
  120. void attachOnInit(void (*funcOnInit)(void)) {
  121. pFuncOnInit = funcOnInit;
  122. };
  123. /**@}*/
  124. /** True if a Xbox controller is connected. */
  125. bool XboxConnected;
  126. protected:
  127. /** Pointer to USB class instance. */
  128. USB *pUsb;
  129. /** Device address. */
  130. uint8_t bAddress;
  131. /** Endpoint info structure. */
  132. EpInfo epInfo[XBOX_MAX_ENDPOINTS];
  133. private:
  134. /**
  135. * Called when the controller is successfully initialized.
  136. * Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
  137. * This is useful for instance if you want to set the LEDs in a specific way.
  138. */
  139. void (*pFuncOnInit)(void); // Pointer to function called in onInit()
  140. bool bPollEnable;
  141. /* Variables to store the digital buttons */
  142. uint8_t ButtonState;
  143. uint8_t OldButtonState;
  144. uint8_t ButtonClickState;
  145. /* Variables to store the analog buttons */
  146. uint8_t buttonValues[8]; // A, B, X, Y, BLACK, WHITE, L1, and R1
  147. uint8_t oldButtonValues[8];
  148. bool buttonClicked[8];
  149. int16_t hatValue[4]; // Joystick values
  150. uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data
  151. void readReport(); // Read incoming data
  152. void printReport(uint16_t length); // Print incoming date
  153. /* Private commands */
  154. void XboxCommand(uint8_t* data, uint16_t nbytes);
  155. };
  156. #endif