選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

701 行
24KB

  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_) {
  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. } else if (btdriver_) {
  199. uint8_t packet[32];
  200. memset(packet, 0, sizeof(packet));
  201. packet[0] = 0x05; // Report ID
  202. packet[1]= 0xFF;
  203. packet[4] = rumble_lValue_; // Small Rumble
  204. packet[5] = rumble_rValue_; // Big rumble
  205. packet[6] = leds_[0]; // RGB value
  206. packet[7] = leds_[1];
  207. packet[8] = leds_[2];
  208. // 9, 10 flash ON, OFF times in 100ths of sedond? 2.5 seconds = 255
  209. Serial.printf("Joystick update Rumble/LEDs");
  210. btdriver_->sendL2CapCommand(packet, sizeof(packet));
  211. return true;
  212. }
  213. return false;
  214. }
  215. static const uint8_t PS3_USER_FEEDBACK_INIT[] = {
  216. 0x00, 0x00, 0x00, 0x00, 0x00,
  217. 0x00, 0x00, 0x00, 0x00, 0x00,
  218. 0xff, 0x27, 0x10, 0x00, 0x32,
  219. 0xff, 0x27, 0x10, 0x00, 0x32,
  220. 0xff, 0x27, 0x10, 0x00, 0x32,
  221. 0xff, 0x27, 0x10, 0x00, 0x32,
  222. 0x00, 0x00, 0x00, 0x00, 0x00,
  223. 0x00, 0x00, 0x00, 0x00, 0x00,
  224. 0x00, 0x00, 0x00, 0x00, 0x00,
  225. 0x00, 0x00, 0x00 };
  226. bool JoystickController::transmitPS3UserFeedbackMsg() {
  227. if (!driver_) return false;
  228. memcpy(txbuf_, PS3_USER_FEEDBACK_INIT, 48);
  229. txbuf_[1] = rumble_lValue_? rumble_timeout_ : 0;
  230. txbuf_[2] = rumble_lValue_; // Small Rumble
  231. txbuf_[3] = rumble_rValue_? rumble_timeout_ : 0;
  232. txbuf_[4] = rumble_rValue_; // Big rumble
  233. txbuf_[9] = leds_[0] << 1; // RGB value
  234. //Serial.printf("\nJoystick update Rumble/LEDs %d %d %d %d %d\n", txbuf_[1], txbuf_[2], txbuf_[3], txbuf_[4], txbuf_[9]);
  235. return driver_->sendControlPacket(0x21, 9, 0x201, 0, 48, txbuf_);
  236. }
  237. //*****************************************************************************
  238. // Support for Joysticks that Use HID data.
  239. //*****************************************************************************
  240. hidclaim_t JoystickController::claim_collection(USBHIDParser *driver, Device_t *dev, uint32_t topusage)
  241. {
  242. // only claim Desktop/Joystick and Desktop/Gamepad
  243. if (topusage != 0x10004 && topusage != 0x10005) return CLAIM_NO;
  244. // only claim from one physical device
  245. if (mydevice != NULL && dev != mydevice) return CLAIM_NO;
  246. // Also don't allow us to claim if it is used as a standard usb object (XBox...)
  247. if (device != nullptr) return CLAIM_NO;
  248. mydevice = dev;
  249. collections_claimed++;
  250. anychange = true; // always report values on first read
  251. driver_ = driver; // remember the driver.
  252. driver_->setTXBuffers(txbuf_, nullptr, sizeof(txbuf_));
  253. connected_ = true; // remember that hardware is actually connected...
  254. // Lets see if we know what type of joystick this is. That is, is it a PS3 or PS4 or ...
  255. joystickType = mapVIDPIDtoJoystickType(mydevice->idVendor, mydevice->idProduct, false);
  256. switch (joystickType) {
  257. case PS3:
  258. additional_axis_usage_page_ = 0x1;
  259. additional_axis_usage_start_ = 0x100;
  260. additional_axis_usage_count_ = 39;
  261. axis_change_notify_mask_ = (uint64_t)-1; // Start off assume all bits
  262. break;
  263. case PS4:
  264. additional_axis_usage_page_ = 0xFF00;
  265. additional_axis_usage_start_ = 0x21;
  266. additional_axis_usage_count_ = 54;
  267. axis_change_notify_mask_ = (uint64_t)0xfffffffffffff3ffl; // Start off assume all bits - 10 and 11
  268. break;
  269. default:
  270. additional_axis_usage_page_ = 0;
  271. additional_axis_usage_start_ = 0;
  272. additional_axis_usage_count_ = 0;
  273. axis_change_notify_mask_ = 0x3ff; // Start off assume only the 10 bits...
  274. }
  275. Serial.printf("Claim Additional axis: %x %x %d\n", additional_axis_usage_page_, additional_axis_usage_start_, additional_axis_usage_count_);
  276. return CLAIM_REPORT;
  277. }
  278. void JoystickController::disconnect_collection(Device_t *dev)
  279. {
  280. if (--collections_claimed == 0) {
  281. mydevice = NULL;
  282. driver_ = nullptr;
  283. axis_mask_ = 0;
  284. axis_changed_mask_ = 0;
  285. }
  286. }
  287. void JoystickController::hid_input_begin(uint32_t topusage, uint32_t type, int lgmin, int lgmax)
  288. {
  289. // TODO: set up translation from logical min/max to consistent 16 bit scale
  290. }
  291. void JoystickController::hid_input_data(uint32_t usage, int32_t value)
  292. {
  293. //Serial.printf("Joystick: usage=%X, value=%d\n", usage, value);
  294. uint32_t usage_page = usage >> 16;
  295. usage &= 0xFFFF;
  296. if (usage_page == 9 && usage >= 1 && usage <= 32) {
  297. uint32_t bit = 1 << (usage -1);
  298. if (value == 0) {
  299. if (buttons & bit) {
  300. buttons &= ~bit;
  301. anychange = true;
  302. }
  303. } else {
  304. if (!(buttons & bit)) {
  305. buttons |= bit;
  306. anychange = true;
  307. }
  308. }
  309. } else if (usage_page == 1 && usage >= 0x30 && usage <= 0x39) {
  310. // TODO: need scaling of value to consistent API, 16 bit signed?
  311. // TODO: many joysticks repeat slider usage. Detect & map to axis?
  312. uint32_t i = usage - 0x30;
  313. axis_mask_ |= (1 << i); // Keep record of which axis we have data on.
  314. if (axis[i] != value) {
  315. axis[i] = value;
  316. axis_changed_mask_ |= (1 << i);
  317. if (axis_changed_mask_ & axis_change_notify_mask_)
  318. anychange = true;
  319. }
  320. } else if (usage_page == additional_axis_usage_page_) {
  321. // see if the usage is witin range.
  322. //Serial.printf("UP: usage_page=%x usage=%x User: %x %d\n", usage_page, usage, user_buttons_usage_start, user_buttons_count_);
  323. if ((usage >= additional_axis_usage_start_) && (usage < (additional_axis_usage_start_ + additional_axis_usage_count_))) {
  324. // We are in the user range.
  325. uint16_t usage_index = usage - additional_axis_usage_start_ + STANDARD_AXIS_COUNT;
  326. if (usage_index < (sizeof(axis)/sizeof(axis[0]))) {
  327. if (axis[usage_index] != value) {
  328. axis[usage_index] = value;
  329. if (usage_index > 63) usage_index = 63; // don't overflow our mask
  330. axis_changed_mask_ |= ((uint64_t)1 << usage_index); // Keep track of which ones changed.
  331. if (axis_changed_mask_ & axis_change_notify_mask_)
  332. anychange = true; // We have changes...
  333. }
  334. axis_mask_ |= ((uint64_t)1 << usage_index); // Keep record of which axis we have data on.
  335. }
  336. //Serial.printf("UB: index=%x value=%x\n", usage_index, value);
  337. }
  338. } else {
  339. 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_);
  340. }
  341. // TODO: hat switch?
  342. }
  343. void JoystickController::hid_input_end()
  344. {
  345. if (anychange) {
  346. joystickEvent = true;
  347. }
  348. }
  349. bool JoystickController::hid_process_out_data(const Transfer_t *transfer)
  350. {
  351. //Serial.printf("JoystickController::hid_process_out_data\n");
  352. return true;
  353. }
  354. void JoystickController::joystickDataClear() {
  355. joystickEvent = false;
  356. anychange = false;
  357. axis_changed_mask_ = 0;
  358. axis_mask_ = 0;
  359. }
  360. //*****************************************************************************
  361. // Support for Joysticks that are class specific and do not use HID
  362. // Example: XBox One controller.
  363. //*****************************************************************************
  364. static uint8_t xboxone_start_input[] = {0x05, 0x20, 0x00, 0x01, 0x00};
  365. static uint8_t xbox360w_inquire_present[] = {0x08, 0x00, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  366. bool JoystickController::claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_t len)
  367. {
  368. println("JoystickController claim this=", (uint32_t)this, HEX);
  369. // Don't try to claim if it is used as USB device or HID device
  370. if (mydevice != NULL) return false;
  371. if (device != nullptr) return false;
  372. // Try claiming at the interface level.
  373. if (type != 1) return false;
  374. print_hexbytes(descriptors, len);
  375. JoystickController::joytype_t jtype = mapVIDPIDtoJoystickType(dev->idVendor, dev->idProduct, true);
  376. println("Jtype=", (uint8_t)jtype, DEC);
  377. if (jtype == UNKNOWN)
  378. return false;
  379. // XBOX One
  380. // 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...
  381. // 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
  382. // Lets do some verifications to make sure.
  383. // XBOX 360 wireless... Has 8 interfaces. 4 joysticks (1, 3, 5, 7) and 4 headphones assume 2,4,6, 8...
  384. // Shows data for #1 only...
  385. // Also they have some unknown data type we need to ignore between interface and end points.
  386. // 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
  387. // 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
  388. // 29 30 1 2 3 4 5 6 7 8 9 40 41 42
  389. // 07 05 81 03 20 00 01 07 05 01 03 20 00 08
  390. if (len < 9+7+7) return false;
  391. // Some common stuff for both XBoxs
  392. uint32_t count_end_points = descriptors[4];
  393. if (count_end_points < 2) return false;
  394. if (descriptors[5] != 0xff) return false; // bInterfaceClass, 3 = HID
  395. rx_ep_ = 0;
  396. uint32_t txep = 0;
  397. uint8_t rx_interval = 0;
  398. uint8_t tx_interval = 0;
  399. rx_size_ = 0;
  400. tx_size_ = 0;
  401. uint32_t descriptor_index = 9;
  402. if (descriptors[descriptor_index+1] == 0x22) {
  403. if (descriptors[descriptor_index] != 0x14) return false; // only support specific versions...
  404. descriptor_index += descriptors[descriptor_index]; // XBox360w ignore this unknown setup...
  405. }
  406. while (count_end_points-- && ((rx_ep_ == 0) || txep == 0)) {
  407. if (descriptors[descriptor_index] != 7) return false; // length 7
  408. if (descriptors[descriptor_index+1] != 5) return false; // ep desc
  409. if ((descriptors[descriptor_index+3] == 3) // Type 3...
  410. && (descriptors[descriptor_index+4] <= 64)
  411. && (descriptors[descriptor_index+5] == 0)) {
  412. // have a bulk EP size
  413. if (descriptors[descriptor_index+2] & 0x80 ) {
  414. rx_ep_ = descriptors[descriptor_index+2];
  415. rx_size_ = descriptors[descriptor_index+4];
  416. rx_interval = descriptors[descriptor_index+6];
  417. } else {
  418. txep = descriptors[descriptor_index+2];
  419. tx_size_ = descriptors[descriptor_index+4];
  420. tx_interval = descriptors[descriptor_index+6];
  421. }
  422. }
  423. descriptor_index += 7; // setup to look at next one...
  424. }
  425. if ((rx_ep_ == 0) || (txep == 0)) return false; // did not find two end points.
  426. print("JoystickController, rx_ep_=", rx_ep_ & 15);
  427. print("(", rx_size_);
  428. print("), txep=", txep);
  429. print("(", tx_size_);
  430. println(")");
  431. rxpipe_ = new_Pipe(dev, 3, rx_ep_ & 15, 1, rx_size_, rx_interval);
  432. if (!rxpipe_) return false;
  433. txpipe_ = new_Pipe(dev, 3, txep, 0, tx_size_, tx_interval);
  434. if (!txpipe_) {
  435. //free_Pipe(rxpipe_);
  436. return false;
  437. }
  438. rxpipe_->callback_function = rx_callback;
  439. queue_Data_Transfer(rxpipe_, rxbuf_, rx_size_, this);
  440. txpipe_->callback_function = tx_callback;
  441. if (jtype == XBOXONE) {
  442. queue_Data_Transfer(txpipe_, xboxone_start_input, sizeof(xboxone_start_input), this);
  443. connected_ = true; // remember that hardware is actually connected...
  444. } else if (jtype == XBOX360) {
  445. queue_Data_Transfer(txpipe_, xbox360w_inquire_present, sizeof(xbox360w_inquire_present), this);
  446. connected_ = 0; // remember that hardware is actually connected...
  447. }
  448. memset(axis, 0, sizeof(axis)); // clear out any data.
  449. joystickType = jtype; // remember we are an XBox One.
  450. return true;
  451. }
  452. void JoystickController::control(const Transfer_t *transfer)
  453. {
  454. }
  455. /************************************************************/
  456. // Interrupt-based Data Movement
  457. /************************************************************/
  458. void JoystickController::rx_callback(const Transfer_t *transfer)
  459. {
  460. if (!transfer->driver) return;
  461. ((JoystickController *)(transfer->driver))->rx_data(transfer);
  462. }
  463. void JoystickController::tx_callback(const Transfer_t *transfer)
  464. {
  465. if (!transfer->driver) return;
  466. ((JoystickController *)(transfer->driver))->tx_data(transfer);
  467. }
  468. /************************************************************/
  469. // Interrupt-based Data Movement
  470. // XBox one input data when type == 0x20
  471. // Information came from several places on the web including:
  472. // https://github.com/quantus/xbox-one-controller-protocol
  473. /************************************************************/
  474. typedef struct {
  475. uint8_t type;
  476. uint8_t const_0;
  477. uint16_t id;
  478. // From online references button order:
  479. // sync, dummy, start, back, a, b, x, y
  480. // dpad up, down left, right
  481. // lb, rb, left stick, right stick
  482. // Axis:
  483. // lt, rt, lx, ly, rx, ry
  484. //
  485. uint16_t buttons;
  486. int16_t axis[6];
  487. } xbox1data20_t;
  488. typedef struct {
  489. uint8_t state;
  490. uint8_t id_or_type;
  491. uint16_t controller_status;
  492. uint16_t unknown;
  493. // From online references button order:
  494. // sync, dummy, start, back, a, b, x, y
  495. // dpad up, down left, right
  496. // lb, rb, left stick, right stick
  497. // Axis:
  498. // lt, rt, lx, ly, rx, ry
  499. //
  500. uint16_t buttons;
  501. uint8_t lt;
  502. uint8_t rt;
  503. int16_t axis[4];
  504. } xbox360data_t;
  505. static const uint8_t xbox_axis_order_mapping[] = {4, 5, 0, 1, 2, 3};
  506. void JoystickController::rx_data(const Transfer_t *transfer)
  507. {
  508. print("JoystickController::rx_data: ");
  509. print_hexbytes((uint8_t*)transfer->buffer, transfer->length);
  510. if (joystickType == XBOXONE) {
  511. // Process XBOX One data
  512. axis_mask_ = 0x3f;
  513. axis_changed_mask_ = 0; // assume none for now
  514. xbox1data20_t *xb1d = (xbox1data20_t *)transfer->buffer;
  515. if ((xb1d->type == 0x20) && (transfer->length >= sizeof (xbox1data20_t))) {
  516. // We have a data transfer. Lets see what is new...
  517. if (xb1d->buttons != buttons) {
  518. buttons = xb1d->buttons;
  519. anychange = true;
  520. }
  521. for (uint8_t i = 0; i < sizeof (xbox_axis_order_mapping); i++) {
  522. // The first two values were unsigned.
  523. int axis_value = (i < 2)? (int)(uint16_t)xb1d->axis[i] : xb1d->axis[i];
  524. if (axis_value != axis[xbox_axis_order_mapping[i]]) {
  525. axis[xbox_axis_order_mapping[i]] = axis_value;
  526. axis_changed_mask_ |= (1 << xbox_axis_order_mapping[i]);
  527. anychange = true;
  528. }
  529. }
  530. joystickEvent = true;
  531. }
  532. } else if (joystickType == XBOX360) {
  533. // First byte appears to status - if the byte is 0x8 it is a connect or disconnect of the controller.
  534. xbox360data_t *xb360d = (xbox360data_t *)transfer->buffer;
  535. if (xb360d->state == 0x08) {
  536. if (xb360d->id_or_type != connected_) {
  537. connected_ = xb360d->id_or_type; // remember it...
  538. if (connected_) {
  539. println("XBox360w - Connected type:", connected_, HEX);
  540. // rx_ep_ should be 1, 3, 5, 7 for the wireless convert to 2-5 on led
  541. setLEDs(2+rx_ep_/2); // Right now hard coded to first joystick...
  542. } else {
  543. println("XBox360w - disconnected");
  544. }
  545. }
  546. } else if((xb360d->id_or_type == 0x00) && (xb360d->controller_status & 0x1300)) {
  547. // Controller status report - Maybe we should save away and allow the user access?
  548. println("XBox360w - controllerStatus: ", xb360d->controller_status, HEX);
  549. } else if(xb360d->id_or_type == 0x01) { // Lets only process report 1.
  550. //const uint8_t *pbuffer = (uint8_t*)transfer->buffer;
  551. //for (uint8_t i = 0; i < transfer->length; i++) Serial.printf("%02x ", pbuffer[i]);
  552. //Serial.printf("\n");
  553. if (buttons != xb360d->buttons) {
  554. buttons = xb360d->buttons;
  555. anychange = true;
  556. }
  557. axis_mask_ = 0x3f;
  558. axis_changed_mask_ = 0; // assume none for now
  559. for (uint8_t i = 0; i < 4; i++) {
  560. if (axis[i] != xb360d->axis[i]) {
  561. axis[i] = xb360d->axis[i];
  562. axis_changed_mask_ |= (1 << i);
  563. anychange = true;
  564. }
  565. }
  566. // the two triggers show up as 4 and 5
  567. if (axis[4] != xb360d->lt) {
  568. axis[4] = xb360d->lt;
  569. axis_changed_mask_ |= (1 << 4);
  570. anychange = true;
  571. }
  572. if (axis[5] != xb360d->rt) {
  573. axis[5] = xb360d->rt;
  574. axis_changed_mask_ |= (1 << 5);
  575. anychange = true;
  576. }
  577. if (anychange) joystickEvent = true;
  578. }
  579. }
  580. queue_Data_Transfer(rxpipe_, rxbuf_, rx_size_, this);
  581. }
  582. void JoystickController::tx_data(const Transfer_t *transfer)
  583. {
  584. }
  585. void JoystickController::disconnect()
  586. {
  587. axis_mask_ = 0;
  588. axis_changed_mask_ = 0;
  589. // TODO: free resources
  590. }
  591. bool JoystickController::claim_bluetooth(BluetoothController *driver, uint32_t bluetooth_class)
  592. {
  593. if ((((bluetooth_class & 0xff00) == 0x2500) || (((bluetooth_class & 0xff00) == 0x500))) && ((bluetooth_class & 0x3C) == 0x08)) {
  594. Serial.printf("JoystickController::claim_bluetooth TRUE\n");
  595. btdriver_ = driver;
  596. return true;
  597. }
  598. return false;
  599. }
  600. bool JoystickController::process_bluetooth_HID_data(const uint8_t *data, uint16_t length)
  601. {
  602. // Example data from PS4 controller
  603. //01 7e 7f 82 84 08 00 00 00 00
  604. // LX LY RX RY BT BT PS LT RT
  605. Serial.printf("JoystickController::process_bluetooth_HID_data\n");
  606. if (data[0] != 1) return false;
  607. print(" Joystick Data: ");
  608. print_hexbytes(data, length);
  609. Serial.printf(" Joystick Data: ");
  610. for (uint16_t i = 0; i < length; i++ ) {
  611. Serial.printf("%02x ", data[i]);
  612. }
  613. Serial.printf("\n");
  614. connected_ = true;
  615. return true;
  616. }
  617. void JoystickController::release_bluetooth()
  618. {
  619. btdriver_ = nullptr;
  620. connected_ = false;
  621. }