Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

681 rinda
23KB

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