Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

781 linhas
26KB

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