Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

647 lines
22KB

  1. /* USB EHCI Host for Teensy 3.6
  2. * Copyright 2017 Paul Stoffregen (paul@pjrc.com)
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <Arduino.h>
  24. #include "USBHost_t36.h" // Read this header first for key info
  25. #define print USBHost::print_
  26. #define println USBHost::println_
  27. // PID/VID to joystick mapping - Only the XBOXOne is used to claim the USB interface directly,
  28. // The others are used after claim-hid code to know which one we have and to use it for
  29. // doing other features.
  30. JoystickController::product_vendor_mapping_t JoystickController::pid_vid_mapping[] = {
  31. { 0x045e, 0x02ea, XBOXONE, false },{ 0x045e, 0x02dd, XBOXONE, false },
  32. { 0x045e, 0x0719, XBOX360, false},
  33. { 0x054C, 0x0268, PS3, true},
  34. { 0x054C, 0x05C4, PS4, true}, {0x054C, 0x09CC, PS4, true }
  35. };
  36. //-----------------------------------------------------------------------------
  37. void JoystickController::init()
  38. {
  39. contribute_Pipes(mypipes, sizeof(mypipes)/sizeof(Pipe_t));
  40. contribute_Transfers(mytransfers, sizeof(mytransfers)/sizeof(Transfer_t));
  41. contribute_String_Buffers(mystring_bufs, sizeof(mystring_bufs)/sizeof(strbuf_t));
  42. driver_ready_for_device(this);
  43. USBHIDParser::driver_ready_for_hid_collection(this);
  44. }
  45. //-----------------------------------------------------------------------------
  46. JoystickController::joytype_t JoystickController::mapVIDPIDtoJoystickType(uint16_t idVendor, uint16_t idProduct, bool exclude_hid_devices)
  47. {
  48. for (uint8_t i = 0; i < (sizeof(pid_vid_mapping)/sizeof(pid_vid_mapping[0])); i++) {
  49. if ((idVendor == pid_vid_mapping[i].idVendor) && (idProduct == pid_vid_mapping[i].idProduct)) {
  50. println("Match PID/VID: ", i, DEC);
  51. if (exclude_hid_devices && pid_vid_mapping[i].hidDevice) return UNKNOWN;
  52. return pid_vid_mapping[i].joyType;
  53. }
  54. }
  55. return UNKNOWN; // Not in our list
  56. }
  57. //*****************************************************************************
  58. // Some simple query functions depend on which interface we are using...
  59. //*****************************************************************************
  60. uint16_t JoystickController::idVendor()
  61. {
  62. if (device != nullptr) return device->idVendor;
  63. if (mydevice != nullptr) return mydevice->idVendor;
  64. return 0;
  65. }
  66. uint16_t JoystickController::idProduct()
  67. {
  68. if (device != nullptr) return device->idProduct;
  69. if (mydevice != nullptr) return mydevice->idProduct;
  70. return 0;
  71. }
  72. const uint8_t *JoystickController::manufacturer()
  73. {
  74. if ((device != nullptr) && (device->strbuf != nullptr)) return &device->strbuf->buffer[device->strbuf->iStrings[strbuf_t::STR_ID_MAN]];
  75. if ((mydevice != nullptr) && (mydevice->strbuf != nullptr)) return &mydevice->strbuf->buffer[mydevice->strbuf->iStrings[strbuf_t::STR_ID_MAN]];
  76. return nullptr;
  77. }
  78. const uint8_t *JoystickController::product()
  79. {
  80. if ((device != nullptr) && (device->strbuf != nullptr)) return &device->strbuf->buffer[device->strbuf->iStrings[strbuf_t::STR_ID_PROD]];
  81. if ((mydevice != nullptr) && (mydevice->strbuf != nullptr)) return &mydevice->strbuf->buffer[mydevice->strbuf->iStrings[strbuf_t::STR_ID_PROD]];
  82. return nullptr;
  83. }
  84. const uint8_t *JoystickController::serialNumber()
  85. {
  86. if ((device != nullptr) && (device->strbuf != nullptr)) return &device->strbuf->buffer[device->strbuf->iStrings[strbuf_t::STR_ID_SERIAL]];
  87. if ((mydevice != nullptr) && (mydevice->strbuf != nullptr)) return &mydevice->strbuf->buffer[mydevice->strbuf->iStrings[strbuf_t::STR_ID_SERIAL]];
  88. return nullptr;
  89. }
  90. bool JoystickController::setRumble(uint8_t lValue, uint8_t rValue, uint8_t timeout)
  91. {
  92. // Need to know which joystick we are on. Start off with XBox support - maybe need to add some enum value for the known
  93. // joystick types.
  94. rumble_lValue_ = lValue;
  95. rumble_rValue_ = rValue;
  96. rumble_timeout_ = timeout;
  97. switch (joystickType) {
  98. default:
  99. break;
  100. case PS3:
  101. return transmitPS3UserFeedbackMsg();
  102. case PS4:
  103. return transmitPS4UserFeedbackMsg();
  104. case XBOXONE:
  105. // Lets try sending a request to the XBox 1.
  106. txbuf_[0] = 0x9;
  107. txbuf_[1] = 0x0;
  108. txbuf_[2] = 0x0;
  109. txbuf_[3] = 0x09; // Substructure (what substructure rest of this packet has)
  110. txbuf_[4] = 0x00; // Mode
  111. txbuf_[5] = 0x0f; // Rumble mask (what motors are activated) (0000 lT rT L R)
  112. txbuf_[6] = 0x0; // lT force
  113. txbuf_[7] = 0x0; // rT force
  114. txbuf_[8] = lValue; // L force
  115. txbuf_[9] = rValue; // R force
  116. txbuf_[10] = 0xff; // Length of pulse
  117. txbuf_[11] = 0x00; // Period between pulses
  118. txbuf_[12] = 0x00; // Repeat
  119. if (!queue_Data_Transfer(txpipe_, txbuf_, 13, this)) {
  120. println("XBoxOne rumble transfer fail");
  121. }
  122. return true; //
  123. case XBOX360:
  124. txbuf_[0] = 0x00;
  125. txbuf_[1] = 0x01;
  126. txbuf_[2] = 0x0F;
  127. txbuf_[3] = 0xC0;
  128. txbuf_[4] = 0x00;
  129. txbuf_[5] = lValue;
  130. txbuf_[6] = rValue;
  131. txbuf_[7] = 0x00;
  132. txbuf_[8] = 0x00;
  133. txbuf_[9] = 0x00;
  134. txbuf_[10] = 0x00;
  135. txbuf_[11] = 0x00;
  136. if (!queue_Data_Transfer(txpipe_, txbuf_, 12, this)) {
  137. println("XBox360 rumble transfer fail");
  138. }
  139. return true;
  140. }
  141. return false;
  142. }
  143. bool JoystickController::setLEDs(uint8_t lr, uint8_t lg, uint8_t lb)
  144. {
  145. // Need to know which joystick we are on. Start off with XBox support - maybe need to add some enum value for the known
  146. // joystick types.
  147. if ((leds_[0] != lr) || (leds_[1] != lg) || (leds_[2] != lb)) {
  148. leds_[0] = lr;
  149. leds_[1] = lg;
  150. leds_[2] = lb;
  151. switch (joystickType) {
  152. case PS3:
  153. return transmitPS3UserFeedbackMsg();
  154. case PS4:
  155. return transmitPS4UserFeedbackMsg();
  156. case XBOX360:
  157. // 0: off, 1: all blink then return to before
  158. // 2-5(TL, TR, BL, BR) - blink on then stay on
  159. // 6-9() - On
  160. // ...
  161. txbuf_[1] = 0x00;
  162. txbuf_[2] = 0x08;
  163. txbuf_[3] = 0x40 + lr;
  164. txbuf_[4] = 0x00;
  165. txbuf_[5] = 0x00;
  166. txbuf_[6] = 0x00;
  167. txbuf_[7] = 0x00;
  168. txbuf_[8] = 0x00;
  169. txbuf_[9] = 0x00;
  170. txbuf_[10] = 0x00;
  171. txbuf_[11] = 0x00;
  172. if (!queue_Data_Transfer(txpipe_, txbuf_, 12, this)) {
  173. println("XBox360 set leds fail");
  174. }
  175. return true;
  176. case XBOXONE:
  177. default:
  178. return false;
  179. }
  180. }
  181. return false;
  182. }
  183. bool JoystickController::transmitPS4UserFeedbackMsg() {
  184. if (!driver_) return false;
  185. uint8_t packet[32];
  186. memset(packet, 0, sizeof(packet));
  187. packet[0] = 0x05; // Report ID
  188. packet[1]= 0xFF;
  189. packet[4] = rumble_lValue_; // Small Rumble
  190. packet[5] = rumble_rValue_; // Big rumble
  191. packet[6] = leds_[0]; // RGB value
  192. packet[7] = leds_[1];
  193. packet[8] = leds_[2];
  194. // 9, 10 flash ON, OFF times in 100ths of sedond? 2.5 seconds = 255
  195. Serial.printf("Joystick update Rumble/LEDs");
  196. return driver_->sendPacket(packet, 32);
  197. }
  198. static const uint8_t PS3_USER_FEEDBACK_INIT[] = {
  199. 0x00, 0x00, 0x00, 0x00, 0x00,
  200. 0x00, 0x00, 0x00, 0x00, 0x00,
  201. 0xff, 0x27, 0x10, 0x00, 0x32,
  202. 0xff, 0x27, 0x10, 0x00, 0x32,
  203. 0xff, 0x27, 0x10, 0x00, 0x32,
  204. 0xff, 0x27, 0x10, 0x00, 0x32,
  205. 0x00, 0x00, 0x00, 0x00, 0x00,
  206. 0x00, 0x00, 0x00, 0x00, 0x00,
  207. 0x00, 0x00, 0x00, 0x00, 0x00,
  208. 0x00, 0x00, 0x00 };
  209. bool JoystickController::transmitPS3UserFeedbackMsg() {
  210. if (!driver_) return false;
  211. memcpy(txbuf_, PS3_USER_FEEDBACK_INIT, 48);
  212. txbuf_[1] = rumble_lValue_? rumble_timeout_ : 0;
  213. txbuf_[2] = rumble_lValue_; // Small Rumble
  214. txbuf_[3] = rumble_rValue_? rumble_timeout_ : 0;
  215. txbuf_[4] = rumble_rValue_; // Big rumble
  216. txbuf_[9] = leds_[0] << 1; // RGB value
  217. //Serial.printf("\nJoystick update Rumble/LEDs %d %d %d %d %d\n", txbuf_[1], txbuf_[2], txbuf_[3], txbuf_[4], txbuf_[9]);
  218. return driver_->sendControlPacket(0x21, 9, 0x201, 0, 48, txbuf_);
  219. }
  220. //*****************************************************************************
  221. // Support for Joysticks that Use HID data.
  222. //*****************************************************************************
  223. hidclaim_t JoystickController::claim_collection(USBHIDParser *driver, Device_t *dev, uint32_t topusage)
  224. {
  225. // only claim Desktop/Joystick and Desktop/Gamepad
  226. if (topusage != 0x10004 && topusage != 0x10005) return CLAIM_NO;
  227. // only claim from one physical device
  228. if (mydevice != NULL && dev != mydevice) return CLAIM_NO;
  229. // Also don't allow us to claim if it is used as a standard usb object (XBox...)
  230. if (device != nullptr) return CLAIM_NO;
  231. mydevice = dev;
  232. collections_claimed++;
  233. anychange = true; // always report values on first read
  234. driver_ = driver; // remember the driver.
  235. driver_->setTXBuffers(txbuf_, nullptr, sizeof(txbuf_));
  236. connected_ = true; // remember that hardware is actually connected...
  237. // Lets see if we know what type of joystick this is. That is, is it a PS3 or PS4 or ...
  238. joystickType = mapVIDPIDtoJoystickType(mydevice->idVendor, mydevice->idProduct, false);
  239. switch (joystickType) {
  240. case PS3:
  241. additional_axis_usage_page_ = 0x1;
  242. additional_axis_usage_start_ = 0x100;
  243. additional_axis_usage_count_ = 39;
  244. axis_change_notify_mask_ = (uint64_t)-1; // Start off assume all bits
  245. break;
  246. case PS4:
  247. additional_axis_usage_page_ = 0xFF00;
  248. additional_axis_usage_start_ = 0x21;
  249. additional_axis_usage_count_ = 54;
  250. axis_change_notify_mask_ = (uint64_t)0xfffffffffffff3ffl; // Start off assume all bits - 10 and 11
  251. break;
  252. default:
  253. additional_axis_usage_page_ = 0;
  254. additional_axis_usage_start_ = 0;
  255. additional_axis_usage_count_ = 0;
  256. axis_change_notify_mask_ = 0x3ff; // Start off assume only the 10 bits...
  257. }
  258. Serial.printf("Claim Additional axis: %x %x %d\n", additional_axis_usage_page_, additional_axis_usage_start_, additional_axis_usage_count_);
  259. return CLAIM_REPORT;
  260. }
  261. void JoystickController::disconnect_collection(Device_t *dev)
  262. {
  263. if (--collections_claimed == 0) {
  264. mydevice = NULL;
  265. driver_ = nullptr;
  266. axis_mask_ = 0;
  267. axis_changed_mask_ = 0;
  268. }
  269. }
  270. void JoystickController::hid_input_begin(uint32_t topusage, uint32_t type, int lgmin, int lgmax)
  271. {
  272. // TODO: set up translation from logical min/max to consistent 16 bit scale
  273. }
  274. void JoystickController::hid_input_data(uint32_t usage, int32_t value)
  275. {
  276. //Serial.printf("Joystick: usage=%X, value=%d\n", usage, value);
  277. uint32_t usage_page = usage >> 16;
  278. usage &= 0xFFFF;
  279. if (usage_page == 9 && usage >= 1 && usage <= 32) {
  280. uint32_t bit = 1 << (usage -1);
  281. if (value == 0) {
  282. if (buttons & bit) {
  283. buttons &= ~bit;
  284. anychange = true;
  285. }
  286. } else {
  287. if (!(buttons & bit)) {
  288. buttons |= bit;
  289. anychange = true;
  290. }
  291. }
  292. } else if (usage_page == 1 && usage >= 0x30 && usage <= 0x39) {
  293. // TODO: need scaling of value to consistent API, 16 bit signed?
  294. // TODO: many joysticks repeat slider usage. Detect & map to axis?
  295. uint32_t i = usage - 0x30;
  296. axis_mask_ |= (1 << i); // Keep record of which axis we have data on.
  297. if (axis[i] != value) {
  298. axis[i] = value;
  299. axis_changed_mask_ |= (1 << i);
  300. if (axis_changed_mask_ & axis_change_notify_mask_)
  301. anychange = true;
  302. }
  303. } else if (usage_page == additional_axis_usage_page_) {
  304. // see if the usage is witin range.
  305. //Serial.printf("UP: usage_page=%x usage=%x User: %x %d\n", usage_page, usage, user_buttons_usage_start, user_buttons_count_);
  306. if ((usage >= additional_axis_usage_start_) && (usage < (additional_axis_usage_start_ + additional_axis_usage_count_))) {
  307. // We are in the user range.
  308. uint16_t usage_index = usage - additional_axis_usage_start_ + STANDARD_AXIS_COUNT;
  309. if (usage_index < (sizeof(axis)/sizeof(axis[0]))) {
  310. if (axis[usage_index] != value) {
  311. axis[usage_index] = value;
  312. if (usage_index > 63) usage_index = 63; // don't overflow our mask
  313. axis_changed_mask_ |= ((uint64_t)1 << usage_index); // Keep track of which ones changed.
  314. if (axis_changed_mask_ & axis_change_notify_mask_)
  315. anychange = true; // We have changes...
  316. }
  317. axis_mask_ |= ((uint64_t)1 << usage_index); // Keep record of which axis we have data on.
  318. }
  319. //Serial.printf("UB: index=%x value=%x\n", usage_index, value);
  320. }
  321. } else {
  322. Serial.printf("UP: usage_page=%x usage=%x add: %x %x %d\n", usage_page, usage, additional_axis_usage_page_, additional_axis_usage_start_, additional_axis_usage_count_);
  323. }
  324. // TODO: hat switch?
  325. }
  326. void JoystickController::hid_input_end()
  327. {
  328. if (anychange) {
  329. joystickEvent = true;
  330. }
  331. }
  332. bool JoystickController::hid_process_out_data(const Transfer_t *transfer)
  333. {
  334. //Serial.printf("JoystickController::hid_process_out_data\n");
  335. return true;
  336. }
  337. void JoystickController::joystickDataClear() {
  338. joystickEvent = false;
  339. anychange = false;
  340. axis_changed_mask_ = 0;
  341. axis_mask_ = 0;
  342. }
  343. //*****************************************************************************
  344. // Support for Joysticks that are class specific and do not use HID
  345. // Example: XBox One controller.
  346. //*****************************************************************************
  347. static uint8_t xboxone_start_input[] = {0x05, 0x20, 0x00, 0x01, 0x00};
  348. static uint8_t xbox360w_inquire_present[] = {0x08, 0x00, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  349. bool JoystickController::claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_t len)
  350. {
  351. println("JoystickController claim this=", (uint32_t)this, HEX);
  352. // Don't try to claim if it is used as USB device or HID device
  353. if (mydevice != NULL) return false;
  354. if (device != nullptr) return false;
  355. // Try claiming at the interface level.
  356. if (type != 1) return false;
  357. print_hexbytes(descriptors, len);
  358. JoystickController::joytype_t jtype = mapVIDPIDtoJoystickType(dev->idVendor, dev->idProduct, true);
  359. println("Jtype=", (uint8_t)jtype, DEC);
  360. if (jtype == UNKNOWN)
  361. return false;
  362. // XBOX One
  363. // 0 1 2 3 4 5 6 7 8 *9 10 1 2 3 4 5 *6 7 8 9 20 1 2 3 4 5 6 7 8 9 30 1...
  364. // 09 04 00 00 02 FF 47 D0 00 07 05 02 03 40 00 04 07 05 82 03 40 00 04 09 04 01 00 00 FF 47 D0 00
  365. // Lets do some verifications to make sure.
  366. // XBOX 360 wireless... Has 8 interfaces. 4 joysticks (1, 3, 5, 7) and 4 headphones assume 2,4,6, 8...
  367. // Shows data for #1 only...
  368. // Also they have some unknown data type we need to ignore between interface and end points.
  369. // 0 1 2 3 4 5 6 7 8 *9 10 1 2 3 4 5 *6 7 8 9 20 1 2 3 4 5 6 7 8
  370. // 09 04 00 00 02 FF 5D 81 00 14 22 00 01 13 81 1D 00 17 01 02 08 13 01 0C 00 0C 01 02 08
  371. // 29 30 1 2 3 4 5 6 7 8 9 40 41 42
  372. // 07 05 81 03 20 00 01 07 05 01 03 20 00 08
  373. if (len < 9+7+7) return false;
  374. // Some common stuff for both XBoxs
  375. uint32_t count_end_points = descriptors[4];
  376. if (count_end_points < 2) return false;
  377. if (descriptors[5] != 0xff) return false; // bInterfaceClass, 3 = HID
  378. rx_ep_ = 0;
  379. uint32_t txep = 0;
  380. uint8_t rx_interval = 0;
  381. uint8_t tx_interval = 0;
  382. rx_size_ = 0;
  383. tx_size_ = 0;
  384. uint32_t descriptor_index = 9;
  385. if (descriptors[descriptor_index+1] == 0x22) {
  386. if (descriptors[descriptor_index] != 0x14) return false; // only support specific versions...
  387. descriptor_index += descriptors[descriptor_index]; // XBox360w ignore this unknown setup...
  388. }
  389. while (count_end_points-- && ((rx_ep_ == 0) || txep == 0)) {
  390. if (descriptors[descriptor_index] != 7) return false; // length 7
  391. if (descriptors[descriptor_index+1] != 5) return false; // ep desc
  392. if ((descriptors[descriptor_index+3] == 3) // Type 3...
  393. && (descriptors[descriptor_index+4] <= 64)
  394. && (descriptors[descriptor_index+5] == 0)) {
  395. // have a bulk EP size
  396. if (descriptors[descriptor_index+2] & 0x80 ) {
  397. rx_ep_ = descriptors[descriptor_index+2];
  398. rx_size_ = descriptors[descriptor_index+4];
  399. rx_interval = descriptors[descriptor_index+6];
  400. } else {
  401. txep = descriptors[descriptor_index+2];
  402. tx_size_ = descriptors[descriptor_index+4];
  403. tx_interval = descriptors[descriptor_index+6];
  404. }
  405. }
  406. descriptor_index += 7; // setup to look at next one...
  407. }
  408. if ((rx_ep_ == 0) || (txep == 0)) return false; // did not find two end points.
  409. print("JoystickController, rx_ep_=", rx_ep_ & 15);
  410. print("(", rx_size_);
  411. print("), txep=", txep);
  412. print("(", tx_size_);
  413. println(")");
  414. rxpipe_ = new_Pipe(dev, 3, rx_ep_ & 15, 1, rx_size_, rx_interval);
  415. if (!rxpipe_) return false;
  416. txpipe_ = new_Pipe(dev, 3, txep, 0, tx_size_, tx_interval);
  417. if (!txpipe_) {
  418. //free_Pipe(rxpipe_);
  419. return false;
  420. }
  421. rxpipe_->callback_function = rx_callback;
  422. queue_Data_Transfer(rxpipe_, rxbuf_, rx_size_, this);
  423. txpipe_->callback_function = tx_callback;
  424. if (jtype == XBOXONE) {
  425. queue_Data_Transfer(txpipe_, xboxone_start_input, sizeof(xboxone_start_input), this);
  426. connected_ = true; // remember that hardware is actually connected...
  427. } else if (jtype == XBOX360) {
  428. queue_Data_Transfer(txpipe_, xbox360w_inquire_present, sizeof(xbox360w_inquire_present), this);
  429. connected_ = 0; // remember that hardware is actually connected...
  430. }
  431. memset(axis, 0, sizeof(axis)); // clear out any data.
  432. joystickType = jtype; // remember we are an XBox One.
  433. return true;
  434. }
  435. void JoystickController::control(const Transfer_t *transfer)
  436. {
  437. }
  438. /************************************************************/
  439. // Interrupt-based Data Movement
  440. /************************************************************/
  441. void JoystickController::rx_callback(const Transfer_t *transfer)
  442. {
  443. if (!transfer->driver) return;
  444. ((JoystickController *)(transfer->driver))->rx_data(transfer);
  445. }
  446. void JoystickController::tx_callback(const Transfer_t *transfer)
  447. {
  448. if (!transfer->driver) return;
  449. ((JoystickController *)(transfer->driver))->tx_data(transfer);
  450. }
  451. /************************************************************/
  452. // Interrupt-based Data Movement
  453. // XBox one input data when type == 0x20
  454. // Information came from several places on the web including:
  455. // https://github.com/quantus/xbox-one-controller-protocol
  456. /************************************************************/
  457. typedef struct {
  458. uint8_t type;
  459. uint8_t const_0;
  460. uint16_t id;
  461. // From online references button order:
  462. // sync, dummy, start, back, a, b, x, y
  463. // dpad up, down left, right
  464. // lb, rb, left stick, right stick
  465. // Axis:
  466. // lt, rt, lx, ly, rx, ry
  467. //
  468. uint16_t buttons;
  469. int16_t axis[6];
  470. } xbox1data20_t;
  471. typedef struct {
  472. uint8_t state;
  473. uint8_t id_or_type;
  474. uint16_t controller_status;
  475. uint16_t unknown;
  476. // From online references button order:
  477. // sync, dummy, start, back, a, b, x, y
  478. // dpad up, down left, right
  479. // lb, rb, left stick, right stick
  480. // Axis:
  481. // lt, rt, lx, ly, rx, ry
  482. //
  483. uint16_t buttons;
  484. uint8_t lt;
  485. uint8_t rt;
  486. int16_t axis[4];
  487. } xbox360data_t;
  488. static const uint8_t xbox_axis_order_mapping[] = {4, 5, 0, 1, 2, 3};
  489. void JoystickController::rx_data(const Transfer_t *transfer)
  490. {
  491. print("JoystickController::rx_data: ");
  492. print_hexbytes((uint8_t*)transfer->buffer, transfer->length);
  493. if (joystickType == XBOXONE) {
  494. // Process XBOX One data
  495. axis_mask_ = 0x3f;
  496. axis_changed_mask_ = 0; // assume none for now
  497. xbox1data20_t *xb1d = (xbox1data20_t *)transfer->buffer;
  498. if ((xb1d->type == 0x20) && (transfer->length >= sizeof (xbox1data20_t))) {
  499. // We have a data transfer. Lets see what is new...
  500. if (xb1d->buttons != buttons) {
  501. buttons = xb1d->buttons;
  502. anychange = true;
  503. }
  504. for (uint8_t i = 0; i < sizeof (xbox_axis_order_mapping); i++) {
  505. // The first two values were unsigned.
  506. int axis_value = (i < 2)? (int)(uint16_t)xb1d->axis[i] : xb1d->axis[i];
  507. if (axis_value != axis[xbox_axis_order_mapping[i]]) {
  508. axis[xbox_axis_order_mapping[i]] = axis_value;
  509. axis_changed_mask_ |= (1 << xbox_axis_order_mapping[i]);
  510. anychange = true;
  511. }
  512. }
  513. joystickEvent = true;
  514. }
  515. } else if (joystickType == XBOX360) {
  516. // First byte appears to status - if the byte is 0x8 it is a connect or disconnect of the controller.
  517. xbox360data_t *xb360d = (xbox360data_t *)transfer->buffer;
  518. if (xb360d->state == 0x08) {
  519. if (xb360d->id_or_type != connected_) {
  520. connected_ = xb360d->id_or_type; // remember it...
  521. if (connected_) {
  522. println("XBox360w - Connected type:", connected_, HEX);
  523. // rx_ep_ should be 1, 3, 5, 7 for the wireless convert to 2-5 on led
  524. setLEDs(2+rx_ep_/2); // Right now hard coded to first joystick...
  525. } else {
  526. println("XBox360w - disconnected");
  527. }
  528. }
  529. } else if((xb360d->id_or_type == 0x00) && (xb360d->controller_status & 0x1300)) {
  530. // Controller status report - Maybe we should save away and allow the user access?
  531. println("XBox360w - controllerStatus: ", xb360d->controller_status, HEX);
  532. } else if(xb360d->id_or_type == 0x01) { // Lets only process report 1.
  533. //const uint8_t *pbuffer = (uint8_t*)transfer->buffer;
  534. //for (uint8_t i = 0; i < transfer->length; i++) Serial.printf("%02x ", pbuffer[i]);
  535. //Serial.printf("\n");
  536. if (buttons != xb360d->buttons) {
  537. buttons = xb360d->buttons;
  538. anychange = true;
  539. }
  540. axis_mask_ = 0x3f;
  541. axis_changed_mask_ = 0; // assume none for now
  542. for (uint8_t i = 0; i < 4; i++) {
  543. if (axis[i] != xb360d->axis[i]) {
  544. axis[i] = xb360d->axis[i];
  545. axis_changed_mask_ |= (1 << i);
  546. anychange = true;
  547. }
  548. }
  549. // the two triggers show up as 4 and 5
  550. if (axis[4] != xb360d->lt) {
  551. axis[4] = xb360d->lt;
  552. axis_changed_mask_ |= (1 << 4);
  553. anychange = true;
  554. }
  555. if (axis[5] != xb360d->rt) {
  556. axis[5] = xb360d->rt;
  557. axis_changed_mask_ |= (1 << 5);
  558. anychange = true;
  559. }
  560. if (anychange) joystickEvent = true;
  561. }
  562. }
  563. queue_Data_Transfer(rxpipe_, rxbuf_, rx_size_, this);
  564. }
  565. void JoystickController::tx_data(const Transfer_t *transfer)
  566. {
  567. }
  568. void JoystickController::disconnect()
  569. {
  570. axis_mask_ = 0;
  571. axis_changed_mask_ = 0;
  572. // TODO: free resources
  573. }