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.

XBOXOLD.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. #include "XBOXOLD.h"
  15. // To enable serial debugging see "settings.h"
  16. //#define EXTRADEBUG // Uncomment to get even more debugging data
  17. //#define PRINTREPORT // Uncomment to print the report send by the Xbox controller
  18. /** Buttons on the controllers */
  19. const uint8_t XBOXOLD_BUTTONS[] PROGMEM = {
  20. 0x01, // UP
  21. 0x08, // RIGHT
  22. 0x02, // DOWN
  23. 0x04, // LEFT
  24. 0x20, // BACK
  25. 0x10, // START
  26. 0x40, // L3
  27. 0x80, // R3
  28. // A, B, X, Y, BLACK, WHITE, L1, and R1 are analog buttons
  29. 4, // BLACK
  30. 5, // WHTIE
  31. 6, // L1
  32. 7, // R1
  33. 1, // B
  34. 0, // A
  35. 2, // X
  36. 3, // Y
  37. };
  38. XBOXOLD::XBOXOLD(USB *p) :
  39. pUsb(p), // pointer to USB class instance - mandatory
  40. bAddress(0), // device address - mandatory
  41. bPollEnable(false) { // don't start polling before dongle is connected
  42. for(uint8_t i = 0; i < XBOX_MAX_ENDPOINTS; i++) {
  43. epInfo[i].epAddr = 0;
  44. epInfo[i].maxPktSize = (i) ? 0 : 8;
  45. epInfo[i].epAttribs = 0;
  46. epInfo[i].bmNakPower = (i) ? USB_NAK_NOWAIT : USB_NAK_MAX_POWER;
  47. }
  48. if(pUsb) // register in USB subsystem
  49. pUsb->RegisterDeviceClass(this); //set devConfig[] entry
  50. }
  51. uint8_t XBOXOLD::Init(uint8_t parent, uint8_t port, bool lowspeed) {
  52. uint8_t buf[sizeof (USB_DEVICE_DESCRIPTOR)];
  53. USB_DEVICE_DESCRIPTOR * udd = reinterpret_cast<USB_DEVICE_DESCRIPTOR*>(buf);
  54. uint8_t rcode;
  55. UsbDevice *p = NULL;
  56. EpInfo *oldep_ptr = NULL;
  57. uint16_t PID;
  58. uint16_t VID;
  59. // get memory address of USB device address pool
  60. AddressPool &addrPool = pUsb->GetAddressPool();
  61. #ifdef EXTRADEBUG
  62. Notify(PSTR("\r\nXBOXUSB Init"), 0x80);
  63. #endif
  64. // check if address has already been assigned to an instance
  65. if(bAddress) {
  66. #ifdef DEBUG_USB_HOST
  67. Notify(PSTR("\r\nAddress in use"), 0x80);
  68. #endif
  69. return USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE;
  70. }
  71. // Get pointer to pseudo device with address 0 assigned
  72. p = addrPool.GetUsbDevicePtr(0);
  73. if(!p) {
  74. #ifdef DEBUG_USB_HOST
  75. Notify(PSTR("\r\nAddress not found"), 0x80);
  76. #endif
  77. return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
  78. }
  79. if(!p->epinfo) {
  80. #ifdef DEBUG_USB_HOST
  81. Notify(PSTR("\r\nepinfo is null"), 0x80);
  82. #endif
  83. return USB_ERROR_EPINFO_IS_NULL;
  84. }
  85. // Save old pointer to EP_RECORD of address 0
  86. oldep_ptr = p->epinfo;
  87. // Temporary assign new pointer to epInfo to p->epinfo in order to avoid toggle inconsistence
  88. p->epinfo = epInfo;
  89. p->lowspeed = lowspeed;
  90. // Get device descriptor
  91. rcode = pUsb->getDevDescr(0, 0, sizeof (USB_DEVICE_DESCRIPTOR), (uint8_t*)buf); // Get device descriptor - addr, ep, nbytes, data
  92. // Restore p->epinfo
  93. p->epinfo = oldep_ptr;
  94. if(rcode)
  95. goto FailGetDevDescr;
  96. VID = udd->idVendor;
  97. PID = udd->idProduct;
  98. if((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)) // Check if VID and PID match
  99. goto FailUnknownDevice;
  100. // Allocate new address according to device class
  101. bAddress = addrPool.AllocAddress(parent, false, port);
  102. if(!bAddress)
  103. return USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL;
  104. // Extract Max Packet Size from device descriptor
  105. epInfo[0].maxPktSize = udd->bMaxPacketSize0;
  106. // Assign new address to the device
  107. rcode = pUsb->setAddr(0, 0, bAddress);
  108. if(rcode) {
  109. p->lowspeed = false;
  110. addrPool.FreeAddress(bAddress);
  111. bAddress = 0;
  112. #ifdef DEBUG_USB_HOST
  113. Notify(PSTR("\r\nsetAddr: "), 0x80);
  114. D_PrintHex<uint8_t > (rcode, 0x80);
  115. #endif
  116. return rcode;
  117. }
  118. #ifdef EXTRADEBUG
  119. Notify(PSTR("\r\nAddr: "), 0x80);
  120. D_PrintHex<uint8_t > (bAddress, 0x80);
  121. #endif
  122. //delay(300); // Spec says you should wait at least 200ms
  123. p->lowspeed = false;
  124. //get pointer to assigned address record
  125. p = addrPool.GetUsbDevicePtr(bAddress);
  126. if(!p)
  127. return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
  128. p->lowspeed = lowspeed;
  129. // Assign epInfo to epinfo pointer - only EP0 is known
  130. rcode = pUsb->setEpInfoEntry(bAddress, 1, epInfo);
  131. if(rcode)
  132. goto FailSetDevTblEntry;
  133. /* The application will work in reduced host mode, so we can save program and data
  134. memory space. After verifying the VID we will use known values for the
  135. configuration values for device, interface, endpoints and HID for the XBOX controllers */
  136. /* Initialize data structures for endpoints of device */
  137. epInfo[ XBOX_INPUT_PIPE ].epAddr = 0x01; // XBOX report endpoint
  138. epInfo[ XBOX_INPUT_PIPE ].epAttribs = USB_TRANSFER_TYPE_INTERRUPT;
  139. epInfo[ XBOX_INPUT_PIPE ].bmNakPower = USB_NAK_NOWAIT; // Only poll once for interrupt endpoints
  140. epInfo[ XBOX_INPUT_PIPE ].maxPktSize = EP_MAXPKTSIZE;
  141. epInfo[ XBOX_INPUT_PIPE ].bmSndToggle = 0;
  142. epInfo[ XBOX_INPUT_PIPE ].bmRcvToggle = 0;
  143. epInfo[ XBOX_OUTPUT_PIPE ].epAddr = 0x02; // XBOX output endpoint
  144. epInfo[ XBOX_OUTPUT_PIPE ].epAttribs = USB_TRANSFER_TYPE_INTERRUPT;
  145. epInfo[ XBOX_OUTPUT_PIPE ].bmNakPower = USB_NAK_NOWAIT; // Only poll once for interrupt endpoints
  146. epInfo[ XBOX_OUTPUT_PIPE ].maxPktSize = EP_MAXPKTSIZE;
  147. epInfo[ XBOX_OUTPUT_PIPE ].bmSndToggle = 0;
  148. epInfo[ XBOX_OUTPUT_PIPE ].bmRcvToggle = 0;
  149. rcode = pUsb->setEpInfoEntry(bAddress, 3, epInfo);
  150. if(rcode)
  151. goto FailSetDevTblEntry;
  152. delay(200); // Give time for address change
  153. rcode = pUsb->setConf(bAddress, epInfo[ XBOX_CONTROL_PIPE ].epAddr, 1);
  154. if(rcode)
  155. goto FailSetConfDescr;
  156. #ifdef DEBUG_USB_HOST
  157. Notify(PSTR("\r\nXbox Controller Connected\r\n"), 0x80);
  158. #endif
  159. if(pFuncOnInit)
  160. pFuncOnInit(); // Call the user function
  161. XboxConnected = true;
  162. bPollEnable = true;
  163. return 0; // Successful configuration
  164. /* Diagnostic messages */
  165. FailGetDevDescr:
  166. #ifdef DEBUG_USB_HOST
  167. NotifyFailGetDevDescr();
  168. goto Fail;
  169. #endif
  170. FailSetDevTblEntry:
  171. #ifdef DEBUG_USB_HOST
  172. NotifyFailSetDevTblEntry();
  173. goto Fail;
  174. #endif
  175. FailSetConfDescr:
  176. #ifdef DEBUG_USB_HOST
  177. NotifyFailSetConfDescr();
  178. #endif
  179. goto Fail;
  180. FailUnknownDevice:
  181. #ifdef DEBUG_USB_HOST
  182. NotifyFailUnknownDevice(VID, PID);
  183. #endif
  184. rcode = USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED;
  185. Fail:
  186. #ifdef DEBUG_USB_HOST
  187. Notify(PSTR("\r\nXbox Init Failed, error code: "), 0x80);
  188. NotifyFail(rcode);
  189. #endif
  190. Release();
  191. return rcode;
  192. }
  193. /* Performs a cleanup after failed Init() attempt */
  194. uint8_t XBOXOLD::Release() {
  195. XboxConnected = false;
  196. pUsb->GetAddressPool().FreeAddress(bAddress);
  197. bAddress = 0;
  198. bPollEnable = false;
  199. return 0;
  200. }
  201. uint8_t XBOXOLD::Poll() {
  202. if(!bPollEnable)
  203. return 0;
  204. uint16_t BUFFER_SIZE = EP_MAXPKTSIZE;
  205. pUsb->inTransfer(bAddress, epInfo[ XBOX_INPUT_PIPE ].epAddr, &BUFFER_SIZE, readBuf); // input on endpoint 1
  206. readReport();
  207. #ifdef PRINTREPORT
  208. printReport(BUFFER_SIZE); // Uncomment "#define PRINTREPORT" to print the report send by the Xbox controller
  209. #endif
  210. return 0;
  211. }
  212. void XBOXOLD::readReport() {
  213. ButtonState = readBuf[2];
  214. for(uint8_t i = 0; i < sizeof (buttonValues); i++)
  215. buttonValues[i] = readBuf[i + 4]; // A, B, X, Y, BLACK, WHITE, L1, and R1
  216. hatValue[LeftHatX] = (int16_t)(((uint16_t)readBuf[12] << 8) | readBuf[13]);
  217. hatValue[LeftHatY] = (int16_t)(((uint16_t)readBuf[14] << 8) | readBuf[15]);
  218. hatValue[RightHatX] = (int16_t)(((uint16_t)readBuf[16] << 8) | readBuf[17]);
  219. hatValue[RightHatY] = (int16_t)(((uint16_t)readBuf[18] << 8) | readBuf[19]);
  220. //Notify(PSTR("\r\nButtonState"), 0x80);
  221. //PrintHex<uint8_t>(ButtonState, 0x80);
  222. if(ButtonState != OldButtonState || memcmp(buttonValues, oldButtonValues, sizeof (buttonValues)) != 0) {
  223. ButtonClickState = ButtonState & ~OldButtonState; // Update click state variable
  224. OldButtonState = ButtonState;
  225. for(uint8_t i = 0; i < sizeof (buttonValues); i++) {
  226. if(oldButtonValues[i] == 0 && buttonValues[i] != 0)
  227. buttonClicked[i] = true; // Update A, B, X, Y, BLACK, WHITE, L1, and R1 click state
  228. oldButtonValues[i] = buttonValues[i];
  229. }
  230. }
  231. }
  232. void XBOXOLD::printReport(uint16_t length) { //Uncomment "#define PRINTREPORT" to print the report send by the Xbox controller
  233. #ifdef PRINTREPORT
  234. if(readBuf == NULL)
  235. return;
  236. for(uint8_t i = 0; i < length; i++) {
  237. D_PrintHex<uint8_t > (readBuf[i], 0x80);
  238. Notify(PSTR(" "), 0x80);
  239. }
  240. Notify(PSTR("\r\n"), 0x80);
  241. #endif
  242. }
  243. uint8_t XBOXOLD::getButtonPress(ButtonEnum b) {
  244. uint8_t button = pgm_read_byte(&XBOXOLD_BUTTONS[(uint8_t)b]);
  245. if(b == A || b == B || b == X || b == Y || b == BLACK || b == WHITE || b == L1 || b == R1) // A, B, X, Y, BLACK, WHITE, L1, and R1 are analog buttons
  246. return buttonValues[button]; // Analog buttons
  247. return (ButtonState & button); // Digital buttons
  248. }
  249. bool XBOXOLD::getButtonClick(ButtonEnum b) {
  250. uint8_t button = pgm_read_byte(&XBOXOLD_BUTTONS[(uint8_t)b]);
  251. if(b == A || b == B || b == X || b == Y || b == BLACK || b == WHITE || b == L1 || b == R1) { // A, B, X, Y, BLACK, WHITE, L1, and R1 are analog buttons
  252. if(buttonClicked[button]) {
  253. buttonClicked[button] = false;
  254. return true;
  255. }
  256. return false;
  257. }
  258. bool click = (ButtonClickState & button);
  259. ButtonClickState &= ~button; // clear "click" event
  260. return click;
  261. }
  262. int16_t XBOXOLD::getAnalogHat(AnalogHatEnum a) {
  263. return hatValue[a];
  264. }
  265. /* Xbox Controller commands */
  266. void XBOXOLD::XboxCommand(uint8_t* data, uint16_t nbytes) {
  267. //bmRequest = Host to device (0x00) | Class (0x20) | Interface (0x01) = 0x21, bRequest = Set Report (0x09), Report ID (0x00), Report Type (Output 0x02), interface (0x00), datalength, datalength, data)
  268. pUsb->ctrlReq(bAddress, epInfo[XBOX_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0x00, 0x02, 0x00, nbytes, nbytes, data, NULL);
  269. }
  270. void XBOXOLD::setRumbleOn(uint8_t lValue, uint8_t rValue) {
  271. uint8_t writeBuf[6];
  272. writeBuf[0] = 0x00;
  273. writeBuf[1] = 0x06;
  274. writeBuf[2] = 0x00;
  275. writeBuf[3] = rValue; // small weight
  276. writeBuf[4] = 0x00;
  277. writeBuf[5] = lValue; // big weight
  278. XboxCommand(writeBuf, 6);
  279. }