You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

joystick.cpp 37KB

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