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 36KB

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