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

1217 lines
41KB

  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. //#define DEBUG_JOYSTICK
  28. #ifdef DEBUG_JOYSTICK
  29. #define DBGPrintf USBHDBGSerial.printf
  30. #else
  31. #define DBGPrintf(...)
  32. #endif
  33. // PID/VID to joystick mapping - Only the XBOXOne is used to claim the USB interface directly,
  34. // The others are used after claim-hid code to know which one we have and to use it for
  35. // doing other features.
  36. JoystickController::product_vendor_mapping_t JoystickController::pid_vid_mapping[] = {
  37. { 0x045e, 0x02ea, XBOXONE, false },{ 0x045e, 0x02dd, XBOXONE, false },
  38. { 0x045e, 0x0719, XBOX360, false},
  39. { 0x045e, 0x028E, SWITCH, false}, // Switch?
  40. { 0x054C, 0x0268, PS3, true},
  41. { 0x054C, 0x042F, PS3, true}, // PS3 Navigation controller
  42. { 0x054C, 0x03D5, PS3_MOTION, true}, // PS3 Motion controller
  43. { 0x054C, 0x05C4, PS4, true}, {0x054C, 0x09CC, PS4, true },
  44. { 0x046D, 0xC626, SpaceNav, true}, // 3d Connextion Space Navigator, 0x10008
  45. { 0x046D, 0xC628, SpaceNav, true} // 3d Connextion Space Navigator, 0x10008
  46. };
  47. //-----------------------------------------------------------------------------
  48. void JoystickController::init()
  49. {
  50. contribute_Pipes(mypipes, sizeof(mypipes)/sizeof(Pipe_t));
  51. contribute_Transfers(mytransfers, sizeof(mytransfers)/sizeof(Transfer_t));
  52. contribute_String_Buffers(mystring_bufs, sizeof(mystring_bufs)/sizeof(strbuf_t));
  53. driver_ready_for_device(this);
  54. USBHIDParser::driver_ready_for_hid_collection(this);
  55. BluetoothController::driver_ready_for_bluetooth(this);
  56. }
  57. //-----------------------------------------------------------------------------
  58. JoystickController::joytype_t JoystickController::mapVIDPIDtoJoystickType(uint16_t idVendor, uint16_t idProduct, bool exclude_hid_devices)
  59. {
  60. for (uint8_t i = 0; i < (sizeof(pid_vid_mapping)/sizeof(pid_vid_mapping[0])); i++) {
  61. if ((idVendor == pid_vid_mapping[i].idVendor) && (idProduct == pid_vid_mapping[i].idProduct)) {
  62. println("Match PID/VID: ", i, DEC);
  63. if (exclude_hid_devices && pid_vid_mapping[i].hidDevice) return UNKNOWN;
  64. return pid_vid_mapping[i].joyType;
  65. }
  66. }
  67. return UNKNOWN; // Not in our list
  68. }
  69. //*****************************************************************************
  70. // Some simple query functions depend on which interface we are using...
  71. //*****************************************************************************
  72. uint16_t JoystickController::idVendor()
  73. {
  74. if (device != nullptr) return device->idVendor;
  75. if (mydevice != nullptr) return mydevice->idVendor;
  76. return 0;
  77. }
  78. uint16_t JoystickController::idProduct()
  79. {
  80. if (device != nullptr) return device->idProduct;
  81. if (mydevice != nullptr) return mydevice->idProduct;
  82. return 0;
  83. }
  84. const uint8_t *JoystickController::manufacturer()
  85. {
  86. if ((device != nullptr) && (device->strbuf != nullptr)) return &device->strbuf->buffer[device->strbuf->iStrings[strbuf_t::STR_ID_MAN]];
  87. //if ((btdevice != nullptr) && (btdevice->strbuf != nullptr)) return &btdevice->strbuf->buffer[btdevice->strbuf->iStrings[strbuf_t::STR_ID_MAN]];
  88. if ((mydevice != nullptr) && (mydevice->strbuf != nullptr)) return &mydevice->strbuf->buffer[mydevice->strbuf->iStrings[strbuf_t::STR_ID_MAN]];
  89. return nullptr;
  90. }
  91. const uint8_t *JoystickController::product()
  92. {
  93. if ((device != nullptr) && (device->strbuf != nullptr)) return &device->strbuf->buffer[device->strbuf->iStrings[strbuf_t::STR_ID_PROD]];
  94. if ((mydevice != nullptr) && (mydevice->strbuf != nullptr)) return &mydevice->strbuf->buffer[mydevice->strbuf->iStrings[strbuf_t::STR_ID_PROD]];
  95. if (btdevice != nullptr) return remote_name_;
  96. return nullptr;
  97. }
  98. const uint8_t *JoystickController::serialNumber()
  99. {
  100. if ((device != nullptr) && (device->strbuf != nullptr)) return &device->strbuf->buffer[device->strbuf->iStrings[strbuf_t::STR_ID_SERIAL]];
  101. if ((mydevice != nullptr) && (mydevice->strbuf != nullptr)) return &mydevice->strbuf->buffer[mydevice->strbuf->iStrings[strbuf_t::STR_ID_SERIAL]];
  102. return nullptr;
  103. }
  104. static uint8_t rumble_counter = 0;
  105. bool JoystickController::setRumble(uint8_t lValue, uint8_t rValue, uint8_t timeout)
  106. {
  107. // Need to know which joystick we are on. Start off with XBox support - maybe need to add some enum value for the known
  108. // joystick types.
  109. rumble_lValue_ = lValue;
  110. rumble_rValue_ = rValue;
  111. rumble_timeout_ = timeout;
  112. switch (joystickType_) {
  113. default:
  114. break;
  115. case PS3:
  116. return transmitPS3UserFeedbackMsg();
  117. case PS3_MOTION:
  118. return transmitPS3MotionUserFeedbackMsg();
  119. case PS4:
  120. return transmitPS4UserFeedbackMsg();
  121. case XBOXONE:
  122. // Lets try sending a request to the XBox 1.
  123. txbuf_[0] = 0x9;
  124. txbuf_[1] = 0x0;
  125. txbuf_[2] = 0x0;
  126. txbuf_[3] = 0x09; // Substructure (what substructure rest of this packet has)
  127. txbuf_[4] = 0x00; // Mode
  128. txbuf_[5] = 0x0f; // Rumble mask (what motors are activated) (0000 lT rT L R)
  129. txbuf_[6] = 0x0; // lT force
  130. txbuf_[7] = 0x0; // rT force
  131. txbuf_[8] = lValue; // L force
  132. txbuf_[9] = rValue; // R force
  133. txbuf_[10] = 0xff; // Length of pulse
  134. txbuf_[11] = 0x00; // Period between pulses
  135. txbuf_[12] = 0x00; // Repeat
  136. if (!queue_Data_Transfer(txpipe_, txbuf_, 13, this)) {
  137. println("XBoxOne rumble transfer fail");
  138. }
  139. return true; //
  140. case XBOX360:
  141. txbuf_[0] = 0x00;
  142. txbuf_[1] = 0x01;
  143. txbuf_[2] = 0x0F;
  144. txbuf_[3] = 0xC0;
  145. txbuf_[4] = 0x00;
  146. txbuf_[5] = lValue;
  147. txbuf_[6] = rValue;
  148. txbuf_[7] = 0x00;
  149. txbuf_[8] = 0x00;
  150. txbuf_[9] = 0x00;
  151. txbuf_[10] = 0x00;
  152. txbuf_[11] = 0x00;
  153. if (!queue_Data_Transfer(txpipe_, txbuf_, 12, this)) {
  154. println("XBox360 rumble transfer fail");
  155. }
  156. return true;
  157. case SWITCH:
  158. memset(txbuf_, 0, 10); // make sure it is cleared out
  159. txbuf_[0] = 0x80;
  160. txbuf_[1] = 0x92;
  161. txbuf_[3] = 0x31;
  162. txbuf_[8] = 0x10; // Command
  163. // Now add in subcommand data:
  164. // Probably do this better soon
  165. txbuf_[9+0] = rumble_counter++; //
  166. txbuf_[9+1] = 0x80;
  167. txbuf_[9+2] = 0x00;
  168. txbuf_[9+3] = 0x40;
  169. txbuf_[9+4] = 0x40;
  170. txbuf_[9+5] = 0x80;
  171. txbuf_[9+6] = 0x00;
  172. txbuf_[9+7] = 0x40;
  173. txbuf_[9+8] = 0x40;
  174. if (lValue != 0) {
  175. txbuf_[9+5] = 0x08;
  176. txbuf_[9+6] = lValue;
  177. } else if (rValue != 0) {
  178. txbuf_[9+5] = 0x10;
  179. txbuf_[9+6] = rValue;
  180. }
  181. if (!queue_Data_Transfer(txpipe_, txbuf_, 18, this)) {
  182. println("switch rumble transfer fail");
  183. Serial.printf("Switch Rumble transfer fail\n");
  184. }
  185. return true;
  186. }
  187. return false;
  188. }
  189. bool JoystickController::setLEDs(uint8_t lr, uint8_t lg, uint8_t lb)
  190. {
  191. // Need to know which joystick we are on. Start off with XBox support - maybe need to add some enum value for the known
  192. // joystick types.
  193. Serial.printf("::setLEDS(%x %x %x)\n", lr, lg, lb);
  194. if ((leds_[0] != lr) || (leds_[1] != lg) || (leds_[2] != lb)) {
  195. leds_[0] = lr;
  196. leds_[1] = lg;
  197. leds_[2] = lb;
  198. switch (joystickType_) {
  199. case PS3:
  200. return transmitPS3UserFeedbackMsg();
  201. case PS3_MOTION:
  202. return transmitPS3MotionUserFeedbackMsg();
  203. case PS4:
  204. return transmitPS4UserFeedbackMsg();
  205. case XBOX360:
  206. // 0: off, 1: all blink then return to before
  207. // 2-5(TL, TR, BL, BR) - blink on then stay on
  208. // 6-9() - On
  209. // ...
  210. txbuf_[1] = 0x00;
  211. txbuf_[2] = 0x08;
  212. txbuf_[3] = 0x40 + lr;
  213. txbuf_[4] = 0x00;
  214. txbuf_[5] = 0x00;
  215. txbuf_[6] = 0x00;
  216. txbuf_[7] = 0x00;
  217. txbuf_[8] = 0x00;
  218. txbuf_[9] = 0x00;
  219. txbuf_[10] = 0x00;
  220. txbuf_[11] = 0x00;
  221. if (!queue_Data_Transfer(txpipe_, txbuf_, 12, this)) {
  222. println("XBox360 set leds fail");
  223. }
  224. return true;
  225. case SWITCH:
  226. memset(txbuf_, 0, 10); // make sure it is cleared out
  227. txbuf_[0] = 0x80;
  228. txbuf_[1] = 0x92;
  229. txbuf_[3] = 0x31;
  230. txbuf_[8] = 0x01; // Command
  231. // Now add in subcommand data:
  232. // Probably do this better soon
  233. txbuf_[9+0] = rumble_counter++; //
  234. txbuf_[9+1] = 0x00;
  235. txbuf_[9+2] = 0x01;
  236. txbuf_[9+3] = 0x40;
  237. txbuf_[9+4] = 0x40;
  238. txbuf_[9+5] = 0x00;
  239. txbuf_[9+6] = 0x01;
  240. txbuf_[9+7] = 0x40;
  241. txbuf_[9+8] = 0x40;
  242. txbuf_[9+9] = 0x30; // LED Command
  243. txbuf_[9+10] = lr;
  244. println("Switch set leds: driver? ", (uint32_t)driver_, HEX);
  245. print_hexbytes((uint8_t*)txbuf_, 20);
  246. if (!queue_Data_Transfer(txpipe_, txbuf_, 20, this)) {
  247. println("switch set leds fail");
  248. }
  249. case XBOXONE:
  250. default:
  251. return false;
  252. }
  253. }
  254. return false;
  255. }
  256. bool JoystickController::transmitPS4UserFeedbackMsg() {
  257. if (driver_) {
  258. uint8_t packet[32];
  259. memset(packet, 0, sizeof(packet));
  260. packet[0] = 0x05; // Report ID
  261. packet[1]= 0xFF;
  262. packet[4] = rumble_lValue_; // Small Rumble
  263. packet[5] = rumble_rValue_; // Big rumble
  264. packet[6] = leds_[0]; // RGB value
  265. packet[7] = leds_[1];
  266. packet[8] = leds_[2];
  267. // 9, 10 flash ON, OFF times in 100ths of second? 2.5 seconds = 255
  268. DBGPrintf("Joystick update Rumble/LEDs\n");
  269. return driver_->sendPacket(packet, 32);
  270. } else if (btdriver_) {
  271. uint8_t packet[79];
  272. memset(packet, 0, sizeof(packet));
  273. //0xa2, 0x11, 0xc0, 0x20, 0xf0, 0x04, 0x00
  274. packet[0] = 0x52;
  275. packet[1] = 0x11; // Report ID
  276. packet[2] = 0x80;
  277. //packet[3] = 0x20;
  278. packet[4] = 0xFF;
  279. packet[7] = rumble_lValue_; // Small Rumble
  280. packet[8] = rumble_rValue_; // Big rumble
  281. packet[9] = leds_[0]; // RGB value
  282. packet[10] = leds_[1];
  283. packet[11] = leds_[2];
  284. // 12, 13 flash ON, OFF times in 100ths of sedond? 2.5 seconds = 255
  285. DBGPrintf("Joystick update Rumble/LEDs\n");
  286. btdriver_->sendL2CapCommand(packet, sizeof(packet), 0x40);
  287. return true;
  288. }
  289. return false;
  290. }
  291. static const uint8_t PS3_USER_FEEDBACK_INIT[] = {
  292. 0x00, 0x00, 0x00, 0x00, 0x00,
  293. 0x00, 0x00, 0x00, 0x00, 0x00,
  294. 0xff, 0x27, 0x10, 0x00, 0x32,
  295. 0xff, 0x27, 0x10, 0x00, 0x32,
  296. 0xff, 0x27, 0x10, 0x00, 0x32,
  297. 0xff, 0x27, 0x10, 0x00, 0x32,
  298. 0x00, 0x00, 0x00, 0x00, 0x00,
  299. 0x00, 0x00, 0x00, 0x00, 0x00,
  300. 0x00, 0x00, 0x00, 0x00, 0x00,
  301. 0x00, 0x00, 0x00 };
  302. bool JoystickController::transmitPS3UserFeedbackMsg() {
  303. if (driver_) {
  304. memcpy(txbuf_, PS3_USER_FEEDBACK_INIT, 48);
  305. txbuf_[1] = rumble_lValue_? rumble_timeout_ : 0;
  306. txbuf_[2] = rumble_lValue_; // Small Rumble
  307. txbuf_[3] = rumble_rValue_? rumble_timeout_ : 0;
  308. txbuf_[4] = rumble_rValue_; // Big rumble
  309. txbuf_[9] = leds_[2] << 1; // RGB value // using third led now...
  310. //DBGPrintf("\nJoystick update Rumble/LEDs %d %d %d %d %d\n", txbuf_[1], txbuf_[2], txbuf_[3], txbuf_[4], txbuf_[9]);
  311. return driver_->sendControlPacket(0x21, 9, 0x201, 0, 48, txbuf_);
  312. } else if (btdriver_) {
  313. txbuf_[0] = 0x52;
  314. txbuf_[1] = 0x1;
  315. memcpy(&txbuf_[2], PS3_USER_FEEDBACK_INIT, 48);
  316. txbuf_[3] = rumble_lValue_? rumble_timeout_ : 0;
  317. txbuf_[4] = rumble_lValue_; // Small Rumble
  318. txbuf_[5] = rumble_rValue_? rumble_timeout_ : 0;
  319. txbuf_[6] = rumble_rValue_; // Big rumble
  320. txbuf_[11] = leds_[2] << 1; // RGB value
  321. DBGPrintf("\nJoystick update Rumble/LEDs %d %d %d %d %d\n", txbuf_[3], txbuf_[4], txbuf_[5], txbuf_[6], txbuf_[11]);
  322. btdriver_->sendL2CapCommand(txbuf_, 50, BluetoothController::CONTROL_SCID);
  323. return true;
  324. }
  325. return false;
  326. }
  327. #define MOVE_REPORT_BUFFER_SIZE 7
  328. #define MOVE_HID_BUFFERSIZE 50 // Size of the buffer for the Playstation Motion Controller
  329. bool JoystickController::transmitPS3MotionUserFeedbackMsg() {
  330. if (driver_) {
  331. txbuf_[0] = 0x02; // Set report ID, this is needed for Move commands to work
  332. txbuf_[2] = leds_[0];
  333. txbuf_[3] = leds_[1];
  334. txbuf_[4] = leds_[2];
  335. txbuf_[6] = rumble_lValue_; // Set the rumble value into the write buffer
  336. //return driver_->sendControlPacket(0x21, 9, 0x201, 0, MOVE_REPORT_BUFFER_SIZE, txbuf_);
  337. return driver_->sendPacket(txbuf_, MOVE_REPORT_BUFFER_SIZE);
  338. } else if (btdriver_) {
  339. txbuf_[0] = 0xA2; // HID BT DATA_request (0xA0) | Report Type (Output 0x02)
  340. txbuf_[1] = 0x02; // Report ID
  341. txbuf_[3] = leds_[0];
  342. txbuf_[4] = leds_[1];
  343. txbuf_[5] = leds_[2];
  344. txbuf_[7] = rumble_lValue_;
  345. btdriver_->sendL2CapCommand(txbuf_, MOVE_HID_BUFFERSIZE, BluetoothController::INTERRUPT_SCID);
  346. return true;
  347. }
  348. return false;
  349. }
  350. //*****************************************************************************
  351. // Support for Joysticks that Use HID data.
  352. //*****************************************************************************
  353. hidclaim_t JoystickController::claim_collection(USBHIDParser *driver, Device_t *dev, uint32_t topusage)
  354. {
  355. // only claim Desktop/Joystick and Desktop/Gamepad
  356. if (topusage != 0x10004 && topusage != 0x10005 && topusage != 0x10008) return CLAIM_NO;
  357. // only claim from one physical device
  358. if (mydevice != NULL && dev != mydevice) return CLAIM_NO;
  359. // Also don't allow us to claim if it is used as a standard usb object (XBox...)
  360. if (device != nullptr) return CLAIM_NO;
  361. mydevice = dev;
  362. collections_claimed++;
  363. anychange = true; // always report values on first read
  364. driver_ = driver; // remember the driver.
  365. driver_->setTXBuffers(txbuf_, nullptr, sizeof(txbuf_));
  366. connected_ = true; // remember that hardware is actually connected...
  367. // Lets see if we know what type of joystick this is. That is, is it a PS3 or PS4 or ...
  368. joystickType_ = mapVIDPIDtoJoystickType(mydevice->idVendor, mydevice->idProduct, false);
  369. DBGPrintf("JoystickController::claim_collection joystickType_=%d\n", joystickType_);
  370. switch (joystickType_) {
  371. case PS3:
  372. case PS3_MOTION: // not sure yet
  373. additional_axis_usage_page_ = 0x1;
  374. additional_axis_usage_start_ = 0x100;
  375. additional_axis_usage_count_ = 39;
  376. axis_change_notify_mask_ = (uint64_t)-1; // Start off assume all bits
  377. break;
  378. case PS4:
  379. additional_axis_usage_page_ = 0xFF00;
  380. additional_axis_usage_start_ = 0x21;
  381. additional_axis_usage_count_ = 54;
  382. axis_change_notify_mask_ = (uint64_t)0xfffffffffffff3ffl; // Start off assume all bits - 10 and 11
  383. break;
  384. default:
  385. additional_axis_usage_page_ = 0x09;
  386. additional_axis_usage_start_ = 0x21;
  387. additional_axis_usage_count_ = 5;
  388. axis_change_notify_mask_ = 0x3ff; // Start off assume only the 10 bits...
  389. }
  390. DBGPrintf("Claim Additional axis: %x %x %d\n", additional_axis_usage_page_, additional_axis_usage_start_, additional_axis_usage_count_);
  391. return CLAIM_REPORT;
  392. }
  393. void JoystickController::disconnect_collection(Device_t *dev)
  394. {
  395. if (--collections_claimed == 0) {
  396. mydevice = NULL;
  397. driver_ = nullptr;
  398. axis_mask_ = 0;
  399. axis_changed_mask_ = 0;
  400. }
  401. }
  402. void JoystickController::hid_input_begin(uint32_t topusage, uint32_t type, int lgmin, int lgmax)
  403. {
  404. // TODO: set up translation from logical min/max to consistent 16 bit scale
  405. }
  406. void JoystickController::hid_input_data(uint32_t usage, int32_t value)
  407. {
  408. DBGPrintf("joystickType_=%d\n", joystickType_);
  409. DBGPrintf("Joystick: usage=%X, value=%d\n", usage, value);
  410. uint32_t usage_page = usage >> 16;
  411. usage &= 0xFFFF;
  412. if (usage_page == 9 && usage >= 1 && usage <= 32) {
  413. uint32_t bit = 1 << (usage -1);
  414. if (value == 0) {
  415. if (buttons & bit) {
  416. buttons &= ~bit;
  417. anychange = true;
  418. }
  419. } else {
  420. if (!(buttons & bit)) {
  421. buttons |= bit;
  422. anychange = true;
  423. }
  424. }
  425. } else if (usage_page == 1 && usage >= 0x30 && usage <= 0x39) {
  426. // TODO: need scaling of value to consistent API, 16 bit signed?
  427. // TODO: many joysticks repeat slider usage. Detect & map to axis?
  428. uint32_t i = usage - 0x30;
  429. axis_mask_ |= (1 << i); // Keep record of which axis we have data on.
  430. if (axis[i] != value) {
  431. axis[i] = value;
  432. axis_changed_mask_ |= (1 << i);
  433. if (axis_changed_mask_ & axis_change_notify_mask_)
  434. anychange = true;
  435. }
  436. } else if (usage_page == additional_axis_usage_page_) {
  437. // see if the usage is witin range.
  438. //DBGPrintf("UP: usage_page=%x usage=%x User: %x %d\n", usage_page, usage, user_buttons_usage_start, user_buttons_count_);
  439. if ((usage >= additional_axis_usage_start_) && (usage < (additional_axis_usage_start_ + additional_axis_usage_count_))) {
  440. // We are in the user range.
  441. uint16_t usage_index = usage - additional_axis_usage_start_ + STANDARD_AXIS_COUNT;
  442. if (usage_index < (sizeof(axis)/sizeof(axis[0]))) {
  443. if (axis[usage_index] != value) {
  444. axis[usage_index] = value;
  445. if (usage_index > 63) usage_index = 63; // don't overflow our mask
  446. axis_changed_mask_ |= ((uint64_t)1 << usage_index); // Keep track of which ones changed.
  447. if (axis_changed_mask_ & axis_change_notify_mask_)
  448. anychange = true; // We have changes...
  449. }
  450. axis_mask_ |= ((uint64_t)1 << usage_index); // Keep record of which axis we have data on.
  451. }
  452. //DBGPrintf("UB: index=%x value=%x\n", usage_index, value);
  453. }
  454. } else {
  455. DBGPrintf("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_);
  456. }
  457. // TODO: hat switch?
  458. }
  459. void JoystickController::hid_input_end()
  460. {
  461. if (anychange) {
  462. joystickEvent = true;
  463. }
  464. }
  465. bool JoystickController::hid_process_out_data(const Transfer_t *transfer)
  466. {
  467. //DBGPrintf("JoystickController::hid_process_out_data\n");
  468. return true;
  469. }
  470. void JoystickController::joystickDataClear() {
  471. joystickEvent = false;
  472. anychange = false;
  473. axis_changed_mask_ = 0;
  474. axis_mask_ = 0;
  475. }
  476. //*****************************************************************************
  477. // Support for Joysticks that are class specific and do not use HID
  478. // Example: XBox One controller.
  479. //*****************************************************************************
  480. static uint8_t xboxone_start_input[] = {0x05, 0x20, 0x00, 0x01, 0x00};
  481. static uint8_t xbox360w_inquire_present[] = {0x08, 0x00, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  482. //static uint8_t switch_start_input[] = {0x19, 0x01, 0x03, 0x07, 0x00, 0x00, 0x92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10};
  483. static uint8_t switch_start_input[] = {0x80, 0x02};
  484. bool JoystickController::claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_t len)
  485. {
  486. println("JoystickController claim this=", (uint32_t)this, HEX);
  487. // Don't try to claim if it is used as USB device or HID device
  488. if (mydevice != NULL) return false;
  489. if (device != nullptr) return false;
  490. // Try claiming at the interface level.
  491. if (type != 1) return false;
  492. print_hexbytes(descriptors, len);
  493. JoystickController::joytype_t jtype = mapVIDPIDtoJoystickType(dev->idVendor, dev->idProduct, true);
  494. println("Jtype=", (uint8_t)jtype, DEC);
  495. if (jtype == UNKNOWN)
  496. return false;
  497. // XBOX One
  498. // 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...
  499. // 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
  500. // Lets do some verifications to make sure.
  501. // XBOX 360 wireless... Has 8 interfaces. 4 joysticks (1, 3, 5, 7) and 4 headphones assume 2,4,6, 8...
  502. // Shows data for #1 only...
  503. // Also they have some unknown data type we need to ignore between interface and end points.
  504. // 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
  505. // 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
  506. // 29 30 1 2 3 4 5 6 7 8 9 40 41 42
  507. // 07 05 81 03 20 00 01 07 05 01 03 20 00 08
  508. // Switch
  509. // 09 04 00 00 02 FF 5D 01 00
  510. // 10 21 10 01 01 24 81 14 03 00 03 13 02 00 03 00
  511. // 07 05 81 03 20 00 08
  512. // 07 05 02 03 20 00 08
  513. if (len < 9+7+7) return false;
  514. // Some common stuff for both XBoxs
  515. uint32_t count_end_points = descriptors[4];
  516. if (count_end_points < 2) return false;
  517. if (descriptors[5] != 0xff) return false; // bInterfaceClass, 3 = HID
  518. rx_ep_ = 0;
  519. uint32_t txep = 0;
  520. uint8_t rx_interval = 0;
  521. uint8_t tx_interval = 0;
  522. rx_size_ = 0;
  523. tx_size_ = 0;
  524. uint32_t descriptor_index = 9;
  525. if (descriptors[descriptor_index+1] == 0x22) {
  526. if (descriptors[descriptor_index] != 0x14) return false; // only support specific versions...
  527. descriptor_index += descriptors[descriptor_index]; // XBox360w ignore this unknown setup...
  528. }
  529. while ((rx_ep_ == 0) || txep == 0) {
  530. print(" Index:", descriptor_index, DEC);
  531. if (descriptor_index >= len) return false; // we ran off the end and did not get end points
  532. // see if the next data is an end point descript
  533. if ((descriptors[descriptor_index] == 7) && (descriptors[descriptor_index+1] == 5)) {
  534. if ((descriptors[descriptor_index+3] == 3) // Type 3...
  535. && (descriptors[descriptor_index+4] <= 64)
  536. && (descriptors[descriptor_index+5] == 0)) {
  537. // have a bulk EP size
  538. if (descriptors[descriptor_index+2] & 0x80 ) {
  539. rx_ep_ = descriptors[descriptor_index+2];
  540. rx_size_ = descriptors[descriptor_index+4];
  541. rx_interval = descriptors[descriptor_index+6];
  542. } else {
  543. txep = descriptors[descriptor_index+2];
  544. tx_size_ = descriptors[descriptor_index+4];
  545. tx_interval = descriptors[descriptor_index+6];
  546. }
  547. }
  548. }
  549. descriptor_index += descriptors[descriptor_index]; // setup to look at next one...
  550. }
  551. if ((rx_ep_ == 0) || (txep == 0)) return false; // did not find two end points.
  552. print("JoystickController, rx_ep_=", rx_ep_ & 15);
  553. print("(", rx_size_);
  554. print("), txep=", txep);
  555. print("(", tx_size_);
  556. println(")");
  557. rxpipe_ = new_Pipe(dev, 3, rx_ep_ & 15, 1, rx_size_, rx_interval);
  558. if (!rxpipe_) return false;
  559. txpipe_ = new_Pipe(dev, 3, txep, 0, tx_size_, tx_interval);
  560. if (!txpipe_) {
  561. //free_Pipe(rxpipe_);
  562. return false;
  563. }
  564. rxpipe_->callback_function = rx_callback;
  565. queue_Data_Transfer(rxpipe_, rxbuf_, rx_size_, this);
  566. txpipe_->callback_function = tx_callback;
  567. if (jtype == XBOXONE) {
  568. queue_Data_Transfer(txpipe_, xboxone_start_input, sizeof(xboxone_start_input), this);
  569. connected_ = true; // remember that hardware is actually connected...
  570. } else if (jtype == XBOX360) {
  571. queue_Data_Transfer(txpipe_, xbox360w_inquire_present, sizeof(xbox360w_inquire_present), this);
  572. connected_ = 0; // remember that hardware is actually connected...
  573. } else if (jtype == SWITCH) {
  574. queue_Data_Transfer(txpipe_, switch_start_input, sizeof(switch_start_input), this);
  575. connected_ = true; // remember that hardware is actually connected...
  576. }
  577. memset(axis, 0, sizeof(axis)); // clear out any data.
  578. joystickType_ = jtype; // remember we are an XBox One.
  579. DBGPrintf(" JoystickController::claim joystickType_ %d\n", joystickType_);
  580. return true;
  581. }
  582. void JoystickController::control(const Transfer_t *transfer)
  583. {
  584. }
  585. /************************************************************/
  586. // Interrupt-based Data Movement
  587. /************************************************************/
  588. void JoystickController::rx_callback(const Transfer_t *transfer)
  589. {
  590. if (!transfer->driver) return;
  591. ((JoystickController *)(transfer->driver))->rx_data(transfer);
  592. }
  593. void JoystickController::tx_callback(const Transfer_t *transfer)
  594. {
  595. if (!transfer->driver) return;
  596. ((JoystickController *)(transfer->driver))->tx_data(transfer);
  597. }
  598. /************************************************************/
  599. // Interrupt-based Data Movement
  600. // XBox one input data when type == 0x20
  601. // Information came from several places on the web including:
  602. // https://github.com/quantus/xbox-one-controller-protocol
  603. /************************************************************/
  604. // 20 00 C5 0E 00 00 00 00 00 00 F0 06 AD FB 7A 0A DD F7 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  605. // 20 00 E0 0E 40 00 00 00 00 00 F0 06 AD FB 7A 0A DD F7 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  606. typedef struct {
  607. uint8_t type;
  608. uint8_t const_0;
  609. uint16_t id;
  610. // From online references button order:
  611. // sync, dummy, start, back, a, b, x, y
  612. // dpad up, down left, right
  613. // lb, rb, left stick, right stick
  614. // Axis:
  615. // lt, rt, lx, ly, rx, ry
  616. //
  617. uint16_t buttons;
  618. int16_t axis[6];
  619. } xbox1data20_t;
  620. typedef struct {
  621. uint8_t state;
  622. uint8_t id_or_type;
  623. uint16_t controller_status;
  624. uint16_t unknown;
  625. // From online references button order:
  626. // sync, dummy, start, back, a, b, x, y
  627. // dpad up, down left, right
  628. // lb, rb, left stick, right stick
  629. // Axis:
  630. // lt, rt, lx, ly, rx, ry
  631. //
  632. uint16_t buttons;
  633. uint8_t lt;
  634. uint8_t rt;
  635. int16_t axis[4];
  636. } xbox360data_t;
  637. typedef struct {
  638. uint8_t state;
  639. uint8_t id_or_type;
  640. // From online references button order:
  641. // sync, dummy, start, back, a, b, x, y
  642. // dpad up, down left, right
  643. // lb, rb, left stick, right stick
  644. // Axis:
  645. // lt, rt, lx, ly, rx, ry
  646. //
  647. uint8_t buttons_h;
  648. uint8_t buttons_l;
  649. uint8_t lt;
  650. uint8_t rt;
  651. int16_t axis[4];
  652. } switchdataUSB_t;
  653. static const uint8_t xbox_axis_order_mapping[] = {3, 4, 0, 1, 2, 5};
  654. void JoystickController::rx_data(const Transfer_t *transfer)
  655. {
  656. #ifdef DEBUG_JOYSTICK
  657. print("JoystickController::rx_data (", joystickType_, DEC);
  658. print("): ");
  659. print_hexbytes((uint8_t*)transfer->buffer, transfer->length);
  660. #endif
  661. if (joystickType_ == XBOXONE) {
  662. // Process XBOX One data
  663. axis_mask_ = 0x3f;
  664. axis_changed_mask_ = 0; // assume none for now
  665. xbox1data20_t *xb1d = (xbox1data20_t *)transfer->buffer;
  666. if ((xb1d->type == 0x20) && (transfer->length >= sizeof (xbox1data20_t))) {
  667. // We have a data transfer. Lets see what is new...
  668. if (xb1d->buttons != buttons) {
  669. buttons = xb1d->buttons;
  670. anychange = true;
  671. joystickEvent = true;
  672. println(" Button Change: ", buttons, HEX);
  673. }
  674. for (uint8_t i = 0; i < sizeof (xbox_axis_order_mapping); i++) {
  675. // The first two values were unsigned.
  676. int axis_value = (i < 2)? (int)(uint16_t)xb1d->axis[i] : xb1d->axis[i];
  677. if (axis_value != axis[xbox_axis_order_mapping[i]]) {
  678. axis[xbox_axis_order_mapping[i]] = axis_value;
  679. axis_changed_mask_ |= (1 << xbox_axis_order_mapping[i]);
  680. anychange = true;
  681. }
  682. }
  683. joystickEvent = true;
  684. }
  685. } else if (joystickType_ == XBOX360) {
  686. // First byte appears to status - if the byte is 0x8 it is a connect or disconnect of the controller.
  687. xbox360data_t *xb360d = (xbox360data_t *)transfer->buffer;
  688. if (xb360d->state == 0x08) {
  689. if (xb360d->id_or_type != connected_) {
  690. connected_ = xb360d->id_or_type; // remember it...
  691. if (connected_) {
  692. println("XBox360w - Connected type:", connected_, HEX);
  693. // rx_ep_ should be 1, 3, 5, 7 for the wireless convert to 2-5 on led
  694. setLEDs(2+rx_ep_/2); // Right now hard coded to first joystick...
  695. } else {
  696. println("XBox360w - disconnected");
  697. }
  698. }
  699. } else if((xb360d->id_or_type == 0x00) && (xb360d->controller_status & 0x1300)) {
  700. // Controller status report - Maybe we should save away and allow the user access?
  701. println("XBox360w - controllerStatus: ", xb360d->controller_status, HEX);
  702. } else if(xb360d->id_or_type == 0x01) { // Lets only process report 1.
  703. //const uint8_t *pbuffer = (uint8_t*)transfer->buffer;
  704. //for (uint8_t i = 0; i < transfer->length; i++) DBGPrintf("%02x ", pbuffer[i]);
  705. //DBGPrintf("\n");
  706. if (buttons != xb360d->buttons) {
  707. buttons = xb360d->buttons;
  708. anychange = true;
  709. }
  710. axis_mask_ = 0x3f;
  711. axis_changed_mask_ = 0; // assume none for now
  712. for (uint8_t i = 0; i < 4; i++) {
  713. if (axis[i] != xb360d->axis[i]) {
  714. axis[i] = xb360d->axis[i];
  715. axis_changed_mask_ |= (1 << i);
  716. anychange = true;
  717. }
  718. }
  719. // the two triggers show up as 4 and 5
  720. if (axis[4] != xb360d->lt) {
  721. axis[4] = xb360d->lt;
  722. axis_changed_mask_ |= (1 << 4);
  723. anychange = true;
  724. }
  725. if (axis[5] != xb360d->rt) {
  726. axis[5] = xb360d->rt;
  727. axis_changed_mask_ |= (1 << 5);
  728. anychange = true;
  729. }
  730. if (anychange) joystickEvent = true;
  731. }
  732. } else if (joystickType_ == SWITCH) {
  733. switchdataUSB_t *switchd = (switchdataUSB_t *)transfer->buffer;
  734. uint16_t cur_buttons = (switchd->buttons_h << 8) | switchd->buttons_l;
  735. if (buttons != cur_buttons) {
  736. buttons = cur_buttons;
  737. anychange = true;
  738. }
  739. axis_mask_ = 0x3f;
  740. axis_changed_mask_ = 0; // assume none for now
  741. for (uint8_t i = 0; i < 4; i++) {
  742. if (axis[i] != switchd->axis[i]) {
  743. axis[i] = switchd->axis[i];
  744. axis_changed_mask_ |= (1 << i);
  745. anychange = true;
  746. }
  747. }
  748. // the two triggers show up as 4 and 5
  749. if (axis[4] != switchd->lt) {
  750. axis[4] = switchd->lt;
  751. axis_changed_mask_ |= (1 << 4);
  752. anychange = true;
  753. }
  754. if (axis[5] != switchd->rt) {
  755. axis[5] = switchd->rt;
  756. axis_changed_mask_ |= (1 << 5);
  757. anychange = true;
  758. }
  759. if (anychange) joystickEvent = true;
  760. }
  761. queue_Data_Transfer(rxpipe_, rxbuf_, rx_size_, this);
  762. }
  763. void JoystickController::tx_data(const Transfer_t *transfer)
  764. {
  765. }
  766. void JoystickController::disconnect()
  767. {
  768. axis_mask_ = 0;
  769. axis_changed_mask_ = 0;
  770. // TODO: free resources
  771. }
  772. bool JoystickController::claim_bluetooth(BluetoothController *driver, uint32_t bluetooth_class, uint8_t *remoteName)
  773. {
  774. // If we are already in use than don't grab another one. Likewise don't grab if it is used as USB or HID object
  775. if (btdevice && (btdevice != (Device_t*)driver)) return false;
  776. if (mydevice != NULL) return false;
  777. if (device != nullptr) return false;
  778. if ((((bluetooth_class & 0xff00) == 0x2500) || (((bluetooth_class & 0xff00) == 0x500))) && ((bluetooth_class & 0x3C) == 0x08)) {
  779. DBGPrintf("JoystickController::claim_bluetooth TRUE\n");
  780. btdriver_ = driver;
  781. btdevice = (Device_t*)driver; // remember this way
  782. if (remoteName) mapNameToJoystickType(remoteName);
  783. return true;
  784. }
  785. if (remoteName && mapNameToJoystickType(remoteName)) {
  786. if ((joystickType_ == PS3) || (joystickType_ == PS3_MOTION)) {
  787. DBGPrintf("JoystickController::claim_bluetooth TRUE PS3 hack...\n");
  788. btdriver_ = driver;
  789. btdevice = (Device_t*)driver; // remember this way
  790. special_process_required = SP_PS3_IDS; // PS3 maybe needs different IDS.
  791. return true;
  792. }
  793. }
  794. return false;
  795. }
  796. bool JoystickController::process_bluetooth_HID_data(const uint8_t *data, uint16_t length)
  797. {
  798. // Example data from PS4 controller
  799. //01 7e 7f 82 84 08 00 00 00 00
  800. // LX LY RX RY BT BT PS LT RT
  801. DBGPrintf("JoystickController::process_bluetooth_HID_data: data[0]=%x\n", data[0]);
  802. // May have to look at this one with other controllers...
  803. if (data[0] == 1) {
  804. //print(" Joystick Data: ");
  805. // print_hexbytes(data, length);
  806. if (length > TOTAL_AXIS_COUNT) length = TOTAL_AXIS_COUNT; // don't overflow arrays...
  807. DBGPrintf(" Joystick Data: ");
  808. for(uint16_t i =0; i < length; i++) DBGPrintf("%02x ", data[i]);
  809. DBGPrintf("\r\n");
  810. if (joystickType_ == PS3) {
  811. // Quick and dirty hack to match PS3 HID data
  812. uint32_t cur_buttons = data[2] | ((uint16_t)data[3] << 8) | ((uint32_t)data[4] << 16);
  813. if (cur_buttons != buttons) {
  814. buttons = cur_buttons;
  815. joystickEvent = true; // something changed.
  816. }
  817. uint64_t mask = 0x1;
  818. axis_mask_ = 0x27; // assume bits 0, 1, 2, 5
  819. for (uint16_t i = 0; i < 3; i++) {
  820. if (axis[i] != data[i+6]) {
  821. axis_changed_mask_ |= mask;
  822. axis[i] = data[i+6];
  823. }
  824. mask <<= 1; // shift down the mask.
  825. }
  826. if (axis[5] != data[9]) {
  827. axis_changed_mask_ |= (1<<5);
  828. axis[5] = data[9];
  829. }
  830. if (axis[3] != data[18]) {
  831. axis_changed_mask_ |= (1<<3);
  832. axis[3] = data[18];
  833. }
  834. if (axis[4] != data[19]) {
  835. axis_changed_mask_ |= (1<<4);
  836. axis[4] = data[19];
  837. }
  838. // Then rest of data
  839. mask = 0x1 << 10; // setup for other bits
  840. for (uint16_t i = 10; i < length; i++ ) {
  841. axis_mask_ |= mask;
  842. if(data[i] != axis[i]) {
  843. axis_changed_mask_ |= mask;
  844. axis[i] = data[i];
  845. }
  846. mask <<= 1; // shift down the mask.
  847. }
  848. } else if (joystickType_ == PS3_MOTION) {
  849. // Quick and dirty PS3_Motion data.
  850. uint32_t cur_buttons = data[1] | ((uint16_t)data[2] << 8) | ((uint32_t)data[3] << 16);
  851. if (cur_buttons != buttons) {
  852. buttons = cur_buttons;
  853. joystickEvent = true; // something changed.
  854. }
  855. // Hard to know what is best here. for now just copy raw data over...
  856. // will do this for now... Format of thought to be data.
  857. // data[1-3] Buttons (mentioned 4 as well but appears to be counter
  858. // axis[0-1] data[5] Trigger, Previous trigger value
  859. // 2-5 Unknown probably place holders for Axis like data for other PS3
  860. // 6 - Time stamp
  861. // 7 - Battery
  862. // 8-19 - Accel: XL, XH, YL, YH, ZL, ZH, XL2, XH2, YL2, YH2, ZL2, ZH2
  863. // 20-31 - Gyro: Xl,Xh,Yl,Yh,Zl,Zh,Xl2,Xh2,Yl2,Yh2,Zl2,Zh2
  864. // 32 - Temp High
  865. // 33 - Temp Low (4 bits) Maybe Magneto x High on other??
  866. uint64_t mask = 0x1;
  867. axis_mask_ = 0; // assume bits 0, 1, 2, 5
  868. // Then rest of data
  869. mask = 0x1 << 10; // setup for other bits
  870. for (uint16_t i = 5; i < length; i++ ) {
  871. axis_mask_ |= mask;
  872. if(data[i] != axis[i-5]) {
  873. axis_changed_mask_ |= mask;
  874. axis[i-5] = data[i];
  875. }
  876. mask <<= 1; // shift down the mask.
  877. }
  878. } else {
  879. uint64_t mask = 0x1;
  880. axis_mask_ = 0;
  881. for (uint16_t i = 0; i < length; i++ ) {
  882. axis_mask_ |= mask;
  883. if(data[i] != axis[i]) {
  884. axis_changed_mask_ |= mask;
  885. axis[i] = data[i];
  886. }
  887. mask <<= 1; // shift down the mask.
  888. // DBGPrintf("%02x ", axis[i]);
  889. }
  890. }
  891. if (axis_changed_mask_ & axis_change_notify_mask_)
  892. joystickEvent = true;
  893. connected_ = true;
  894. return true;
  895. } else if(data[0] == 0x11){
  896. DBGPrintf("\n Joystick Data: ");
  897. uint64_t mask = 0x1;
  898. axis_mask_ = 0;
  899. axis_changed_mask_ = 0;
  900. //This moves data to be equivalent to what we see for
  901. //data[0] = 0x01
  902. uint8_t tmp_data[length-2];
  903. for (uint16_t i = 0; i < (length-2); i++ ) {
  904. tmp_data[i] = 0;
  905. tmp_data[i] = data[i+2];
  906. }
  907. /*
  908. * [1] LX, [2] = LY, [3] = RX, [4] = RY
  909. * [5] combo, tri, cir, x, sqr, D-PAD (4bits, 0-3
  910. * [6] R3,L3, opt, share, R2, L2, R1, L1
  911. * [7] Counter (bit7-2), T-PAD, PS
  912. * [8] Left Trigger, [9] Right Trigger
  913. * [10-11] Timestamp
  914. * [12] Battery (0 to 0xff)
  915. * [13-14] acceleration x
  916. * [15-16] acceleration y
  917. * [17-18] acceleration z
  918. * [19-20] gyro x
  919. * [21-22] gyro y
  920. * [23-24] gyro z
  921. * [25-29] unknown
  922. * [30] 0x00,phone,mic, usb, battery level (4bits)
  923. * rest is trackpad? to do implement?
  924. */
  925. //PS Bit
  926. tmp_data[7] = (tmp_data[7] >> 0) & 1;
  927. //set arrow buttons to axis[0]
  928. tmp_data[10] = tmp_data[5] & ((1 << 4) - 1);
  929. //set buttons for last 4bits in the axis[5]
  930. tmp_data[5] = tmp_data[5] >> 4;
  931. // Lets try mapping the DPAD buttons to high bits
  932. // up up/right right R DN DOWN L DN Left LUP
  933. static const uint32_t dpad_to_buttons[] = {0x10000, 0x30000, 0x20000, 0x60000, 0x40000, 0xC0000, 0x80000, 0x90000};
  934. // Quick and dirty hack to match PS4 HID data
  935. uint32_t cur_buttons = ((uint32_t)tmp_data[7] << 12) | (((uint32_t)tmp_data[6]*0x10)) | ((uint16_t)tmp_data[5] ) ;
  936. if (tmp_data[10] < 8) cur_buttons |= dpad_to_buttons[tmp_data[10]];
  937. if (cur_buttons != buttons) {
  938. buttons = cur_buttons;
  939. joystickEvent = true; // something changed.
  940. }
  941. mask = 0x1;
  942. axis_mask_ = 0x27; // assume bits 0, 1, 2, 5
  943. for (uint16_t i = 0; i < 3; i++) {
  944. if (axis[i] != tmp_data[i+1]) {
  945. axis_changed_mask_ |= mask;
  946. axis[i] = tmp_data[i+1];
  947. }
  948. mask <<= 1; // shift down the mask.
  949. }
  950. if (axis[5] != tmp_data[4]) {
  951. axis_changed_mask_ |= (1<<5);
  952. axis[5] = tmp_data[4];
  953. }
  954. if (axis[3] != tmp_data[8]) {
  955. axis_changed_mask_ |= (1<<3);
  956. axis[3] = tmp_data[8];
  957. }
  958. if (axis[4] != tmp_data[9]) {
  959. axis_changed_mask_ |= (1<<4);
  960. axis[4] = tmp_data[9];
  961. }
  962. //limit for masking
  963. mask = 0x1;
  964. for (uint16_t i = 6; i < (64); i++ ) {
  965. axis_mask_ |= mask;
  966. if(tmp_data[i] != axis[i]) {
  967. axis_changed_mask_ |= mask;
  968. axis[i] = tmp_data[i];
  969. }
  970. mask <<= 1; // shift down the mask.
  971. DBGPrintf("%02x ", axis[i]);
  972. }
  973. DBGPrintf("\n");
  974. //DBGPrintf("Axis Mask (axis_mask_, axis_changed_mask_; %d, %d\n", axis_mask_,axis_changed_mask_);
  975. joystickEvent = true;
  976. connected_ = true;
  977. }
  978. return false;
  979. }
  980. bool JoystickController::mapNameToJoystickType(const uint8_t *remoteName)
  981. {
  982. // Sort of a hack, but try to map the name given from remote to a type...
  983. if (strncmp((const char *)remoteName, "Wireless Controller", 19) == 0) {
  984. DBGPrintf(" JoystickController::mapNameToJoystickType %s - set to PS4\n", remoteName);
  985. joystickType_ = PS4;
  986. } else if (strncmp((const char *)remoteName, "PLAYSTATION(R)3", 15) == 0) {
  987. DBGPrintf(" JoystickController::mapNameToJoystickType %x %s - set to PS3\n", (uint32_t)this, remoteName);
  988. joystickType_ = PS3;
  989. } else if (strncmp((const char *)remoteName, "Navigation Controller", 21) == 0) {
  990. DBGPrintf(" JoystickController::mapNameToJoystickType %x %s - set to PS3\n", (uint32_t)this, remoteName);
  991. joystickType_ = PS3;
  992. } else if (strncmp((const char *)remoteName, "Motion Controller", 17) == 0) {
  993. DBGPrintf(" JoystickController::mapNameToJoystickType %x %s - set to PS3 Motion\n", (uint32_t)this, remoteName);
  994. joystickType_ = PS3_MOTION;
  995. } else if (strncmp((const char *)remoteName, "Xbox Wireless", 13) == 0) {
  996. DBGPrintf(" JoystickController::mapNameToJoystickType %x %s - set to XBOXONE\n", (uint32_t)this, remoteName);
  997. joystickType_ = XBOXONE;
  998. } else {
  999. DBGPrintf(" JoystickController::mapNameToJoystickType %s - Unknown\n", remoteName);
  1000. }
  1001. DBGPrintf(" Joystick Type: %d\n", joystickType_);
  1002. return true;
  1003. }
  1004. bool JoystickController::remoteNameComplete(const uint8_t *remoteName)
  1005. {
  1006. // Sort of a hack, but try to map the name given from remote to a type...
  1007. if (mapNameToJoystickType(remoteName)) {
  1008. switch (joystickType_) {
  1009. case PS4: special_process_required = SP_NEED_CONNECT; break;
  1010. case PS3: special_process_required = SP_PS3_IDS; break;
  1011. case PS3_MOTION: special_process_required = SP_PS3_IDS; break;
  1012. default:
  1013. break;
  1014. }
  1015. }
  1016. return true;
  1017. }
  1018. void JoystickController::connectionComplete()
  1019. {
  1020. DBGPrintf(" JoystickController::connectionComplete %x joystick type %d\n", (uint32_t)this, joystickType_);
  1021. switch (joystickType_) {
  1022. case PS4:
  1023. {
  1024. uint8_t packet[2];
  1025. packet[0] = 0x43; // HID BT Get_report (0x40) | Report Type (Feature 0x03)
  1026. packet[1] = 0x02; // Report ID
  1027. DBGPrintf("Set PS4 report\n");
  1028. delay(1);
  1029. btdriver_->sendL2CapCommand(packet, sizeof(packet), 0x40);
  1030. }
  1031. break;
  1032. case PS3:
  1033. {
  1034. uint8_t packet[6];
  1035. packet[0] = 0x53; // HID BT Set_report (0x50) | Report Type (Feature 0x03)
  1036. packet[1] = 0xF4; // Report ID
  1037. packet[2] = 0x42; // Special PS3 Controller enable commands
  1038. packet[3] = 0x03;
  1039. packet[4] = 0x00;
  1040. packet[5] = 0x00;
  1041. DBGPrintf("enable six axis\n");
  1042. delay(1);
  1043. btdriver_->sendL2CapCommand(packet, sizeof(packet), BluetoothController::CONTROL_SCID);
  1044. }
  1045. break;
  1046. case PS3_MOTION:
  1047. setLEDs(0, 0xff, 0); // Maybe try setting to green?
  1048. default:
  1049. break;
  1050. }
  1051. }
  1052. void JoystickController::release_bluetooth()
  1053. {
  1054. btdevice = nullptr; // remember this way
  1055. btdriver_ = nullptr;
  1056. connected_ = false;
  1057. special_process_required = false;
  1058. }
  1059. bool JoystickController::PS3Pair(uint8_t* bdaddr) {
  1060. if (!driver_) return false;
  1061. if (joystickType_ == PS3) {
  1062. /* Set the internal Bluetooth address */
  1063. txbuf_[0] = 0x01;
  1064. txbuf_[1] = 0x00;
  1065. for(uint8_t i = 0; i < 6; i++)
  1066. txbuf_[i + 2] = bdaddr[5 - i]; // Copy into buffer, has to be written reversed, so it is MSB first
  1067. // bmRequest = Host to device (0x00) | Class (0x20) | Interface (0x01) = 0x21, bRequest = Set Report (0x09), Report ID (0xF5), Report Type (Feature 0x03), interface (0x00), datalength, datalength, data
  1068. return driver_->sendControlPacket(0x21, 9, 0x3f5, 0, 8, txbuf_);
  1069. } else if (joystickType_ == PS3_MOTION) {
  1070. // Slightly different than other PS3 units...
  1071. txbuf_[0] = 0x05;
  1072. for(uint8_t i = 0; i < 6; i++)
  1073. txbuf_[i + 1] = bdaddr[i]; // Order different looks like LSB First?
  1074. txbuf_[7] = 0x10;
  1075. txbuf_[8] = 0x01;
  1076. txbuf_[9] = 0x02;
  1077. txbuf_[10] = 0x12;
  1078. // bmRequest = Host to device (0x00) | Class (0x20) | Interface (0x01) = 0x21, bRequest = Set Report (0x09), Report ID (0xF5), Report Type (Feature 0x03), interface (0x00), datalength, datalength, data
  1079. return driver_->sendControlPacket(0x21, 9, 0x305, 0, 11, txbuf_);
  1080. }
  1081. return false;
  1082. }