Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

joystick.cpp 37KB

5 lat temu
5 lat temu
5 lat temu
5 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  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 we are already in use than don't grab another one. Likewise don't grab if it is used as USB or HID object
  660. if (btdevice && (btdevice != (Device_t*)driver)) return false;
  661. if (mydevice != NULL) return false;
  662. if (device != nullptr) return false;
  663. if ((((bluetooth_class & 0xff00) == 0x2500) || (((bluetooth_class & 0xff00) == 0x500))) && ((bluetooth_class & 0x3C) == 0x08)) {
  664. DBGPrintf("JoystickController::claim_bluetooth TRUE\n");
  665. btdriver_ = driver;
  666. btdevice = (Device_t*)driver; // remember this way
  667. if (remoteName) mapNameToJoystickType(remoteName);
  668. return true;
  669. }
  670. if (remoteName && mapNameToJoystickType(remoteName)) {
  671. if ((joystickType_ == PS3) || (joystickType_ == PS3_MOTION)) {
  672. DBGPrintf("JoystickController::claim_bluetooth TRUE PS3 hack...\n");
  673. btdriver_ = driver;
  674. btdevice = (Device_t*)driver; // remember this way
  675. special_process_required = SP_PS3_IDS; // PS3 maybe needs different IDS.
  676. return true;
  677. }
  678. }
  679. return false;
  680. }
  681. bool JoystickController::process_bluetooth_HID_data(const uint8_t *data, uint16_t length)
  682. {
  683. // Example data from PS4 controller
  684. //01 7e 7f 82 84 08 00 00 00 00
  685. // LX LY RX RY BT BT PS LT RT
  686. DBGPrintf("JoystickController::process_bluetooth_HID_data: data[0]=%x\n", data[0]);
  687. // May have to look at this one with other controllers...
  688. if (data[0] == 1) {
  689. //print(" Joystick Data: ");
  690. // print_hexbytes(data, length);
  691. if (length > TOTAL_AXIS_COUNT) length = TOTAL_AXIS_COUNT; // don't overflow arrays...
  692. DBGPrintf(" Joystick Data: ");
  693. for(uint16_t i =0; i < length; i++) DBGPrintf("%02x ", data[i]);
  694. DBGPrintf("\r\n");
  695. if (joystickType_ == PS3) {
  696. // Quick and dirty hack to match PS3 HID data
  697. uint32_t cur_buttons = data[2] | ((uint16_t)data[3] << 8) | ((uint32_t)data[4] << 16);
  698. if (cur_buttons != buttons) {
  699. buttons = cur_buttons;
  700. joystickEvent = true; // something changed.
  701. }
  702. uint64_t mask = 0x1;
  703. axis_mask_ = 0x27; // assume bits 0, 1, 2, 5
  704. for (uint16_t i = 0; i < 3; i++) {
  705. if (axis[i] != data[i+6]) {
  706. axis_changed_mask_ |= mask;
  707. axis[i] = data[i+6];
  708. }
  709. mask <<= 1; // shift down the mask.
  710. }
  711. if (axis[5] != data[9]) {
  712. axis_changed_mask_ |= (1<<5);
  713. axis[5] = data[9];
  714. }
  715. if (axis[3] != data[18]) {
  716. axis_changed_mask_ |= (1<<3);
  717. axis[3] = data[18];
  718. }
  719. if (axis[4] != data[19]) {
  720. axis_changed_mask_ |= (1<<4);
  721. axis[4] = data[19];
  722. }
  723. // Then rest of data
  724. mask = 0x1 << 10; // setup for other bits
  725. for (uint16_t i = 10; i < length; i++ ) {
  726. axis_mask_ |= mask;
  727. if(data[i] != axis[i]) {
  728. axis_changed_mask_ |= mask;
  729. axis[i] = data[i];
  730. }
  731. mask <<= 1; // shift down the mask.
  732. }
  733. } else if (joystickType_ == PS3_MOTION) {
  734. // Quick and dirty PS3_Motion data.
  735. uint32_t cur_buttons = data[1] | ((uint16_t)data[2] << 8) | ((uint32_t)data[3] << 16);
  736. if (cur_buttons != buttons) {
  737. buttons = cur_buttons;
  738. joystickEvent = true; // something changed.
  739. }
  740. // Hard to know what is best here. for now just copy raw data over...
  741. // will do this for now... Format of thought to be data.
  742. // data[1-3] Buttons (mentioned 4 as well but appears to be counter
  743. // axis[0-1] data[5] Trigger, Previous trigger value
  744. // 2-5 Unknown probably place holders for Axis like data for other PS3
  745. // 6 - Time stamp
  746. // 7 - Battery
  747. // 8-19 - Accel: XL, XH, YL, YH, ZL, ZH, XL2, XH2, YL2, YH2, ZL2, ZH2
  748. // 20-31 - Gyro: Xl,Xh,Yl,Yh,Zl,Zh,Xl2,Xh2,Yl2,Yh2,Zl2,Zh2
  749. // 32 - Temp High
  750. // 33 - Temp Low (4 bits) Maybe Magneto x High on other??
  751. uint64_t mask = 0x1;
  752. axis_mask_ = 0; // assume bits 0, 1, 2, 5
  753. // Then rest of data
  754. mask = 0x1 << 10; // setup for other bits
  755. for (uint16_t i = 5; i < length; i++ ) {
  756. axis_mask_ |= mask;
  757. if(data[i] != axis[i-5]) {
  758. axis_changed_mask_ |= mask;
  759. axis[i-5] = data[i];
  760. }
  761. mask <<= 1; // shift down the mask.
  762. }
  763. } else {
  764. uint64_t mask = 0x1;
  765. axis_mask_ = 0;
  766. for (uint16_t i = 0; i < length; i++ ) {
  767. axis_mask_ |= mask;
  768. if(data[i] != axis[i]) {
  769. axis_changed_mask_ |= mask;
  770. axis[i] = data[i];
  771. }
  772. mask <<= 1; // shift down the mask.
  773. // DBGPrintf("%02x ", axis[i]);
  774. }
  775. }
  776. if (axis_changed_mask_ & axis_change_notify_mask_)
  777. joystickEvent = true;
  778. connected_ = true;
  779. return true;
  780. } else if(data[0] == 0x11){
  781. DBGPrintf("\n Joystick Data: ");
  782. uint64_t mask = 0x1;
  783. axis_mask_ = 0;
  784. axis_changed_mask_ = 0;
  785. //This moves data to be equivalent to what we see for
  786. //data[0] = 0x01
  787. uint8_t tmp_data[length-2];
  788. for (uint16_t i = 0; i < (length-2); i++ ) {
  789. tmp_data[i] = 0;
  790. tmp_data[i] = data[i+2];
  791. }
  792. /*
  793. * [1] LX, [2] = LY, [3] = RX, [4] = RY
  794. * [5] combo, tri, cir, x, sqr, D-PAD (4bits, 0-3
  795. * [6] R3,L3, opt, share, R2, L2, R1, L1
  796. * [7] Counter (bit7-2), T-PAD, PS
  797. * [8] Left Trigger, [9] Right Trigger
  798. * [10-11] Timestamp
  799. * [12] Battery (0 to 0xff)
  800. * [13-14] acceleration x
  801. * [15-16] acceleration y
  802. * [17-18] acceleration z
  803. * [19-20] gyro x
  804. * [21-22] gyro y
  805. * [23-24] gyro z
  806. * [25-29] unknown
  807. * [30] 0x00,phone,mic, usb, battery level (4bits)
  808. * rest is trackpad? to do implement?
  809. */
  810. //PS Bit
  811. tmp_data[7] = (tmp_data[7] >> 0) & 1;
  812. //set arrow buttons to axis[0]
  813. tmp_data[10] = tmp_data[5] & ((1 << 4) - 1);
  814. //set buttons for last 4bits in the axis[5]
  815. tmp_data[5] = tmp_data[5] >> 4;
  816. // Quick and dirty hack to match PS4 HID data
  817. uint32_t cur_buttons = tmp_data[7] | (tmp_data[10]) | ((tmp_data[6]*10)) | ((uint16_t)tmp_data[5] << 16) ;
  818. if (cur_buttons != buttons) {
  819. buttons = cur_buttons;
  820. joystickEvent = true; // something changed.
  821. }
  822. mask = 0x1;
  823. axis_mask_ = 0x27; // assume bits 0, 1, 2, 5
  824. for (uint16_t i = 0; i < 3; i++) {
  825. if (axis[i] != tmp_data[i+1]) {
  826. axis_changed_mask_ |= mask;
  827. axis[i] = tmp_data[i+1];
  828. }
  829. mask <<= 1; // shift down the mask.
  830. }
  831. if (axis[5] != tmp_data[4]) {
  832. axis_changed_mask_ |= (1<<5);
  833. axis[5] = tmp_data[4];
  834. }
  835. if (axis[3] != tmp_data[8]) {
  836. axis_changed_mask_ |= (1<<3);
  837. axis[3] = tmp_data[8];
  838. }
  839. if (axis[4] != tmp_data[9]) {
  840. axis_changed_mask_ |= (1<<4);
  841. axis[4] = tmp_data[9];
  842. }
  843. //limit for masking
  844. mask = 0x1;
  845. for (uint16_t i = 6; i < (64); i++ ) {
  846. axis_mask_ |= mask;
  847. if(tmp_data[i] != axis[i]) {
  848. axis_changed_mask_ |= mask;
  849. axis[i] = tmp_data[i];
  850. }
  851. mask <<= 1; // shift down the mask.
  852. DBGPrintf("%02x ", axis[i]);
  853. }
  854. DBGPrintf("\n");
  855. //DBGPrintf("Axis Mask (axis_mask_, axis_changed_mask_; %d, %d\n", axis_mask_,axis_changed_mask_);
  856. joystickEvent = true;
  857. connected_ = true;
  858. }
  859. return false;
  860. }
  861. bool JoystickController::mapNameToJoystickType(const uint8_t *remoteName)
  862. {
  863. // Sort of a hack, but try to map the name given from remote to a type...
  864. if (strncmp((const char *)remoteName, "Wireless Controller", 19) == 0) {
  865. DBGPrintf(" JoystickController::mapNameToJoystickType %s - set to PS4\n", remoteName);
  866. joystickType_ = PS4;
  867. } else if (strncmp((const char *)remoteName, "PLAYSTATION(R)3", 15) == 0) {
  868. DBGPrintf(" JoystickController::mapNameToJoystickType %x %s - set to PS3\n", (uint32_t)this, remoteName);
  869. joystickType_ = PS3;
  870. } else if (strncmp((const char *)remoteName, "Navigation Controller", 21) == 0) {
  871. DBGPrintf(" JoystickController::mapNameToJoystickType %x %s - set to PS3\n", (uint32_t)this, remoteName);
  872. joystickType_ = PS3;
  873. } else if (strncmp((const char *)remoteName, "Motion Controller", 17) == 0) {
  874. DBGPrintf(" JoystickController::mapNameToJoystickType %x %s - set to PS3 Motion\n", (uint32_t)this, remoteName);
  875. joystickType_ = PS3_MOTION;
  876. } else if (strncmp((const char *)remoteName, "Xbox Wireless", 13) == 0) {
  877. DBGPrintf(" JoystickController::mapNameToJoystickType %x %s - set to XBOXONE\n", (uint32_t)this, remoteName);
  878. joystickType_ = XBOXONE;
  879. } else {
  880. DBGPrintf(" JoystickController::mapNameToJoystickType %s - Unknown\n", remoteName);
  881. }
  882. DBGPrintf(" Joystick Type: %d\n", joystickType_);
  883. return true;
  884. }
  885. bool JoystickController::remoteNameComplete(const uint8_t *remoteName)
  886. {
  887. // Sort of a hack, but try to map the name given from remote to a type...
  888. if (mapNameToJoystickType(remoteName)) {
  889. switch (joystickType_) {
  890. case PS4: special_process_required = SP_NEED_CONNECT; break;
  891. case PS3: special_process_required = SP_PS3_IDS; break;
  892. case PS3_MOTION: special_process_required = SP_PS3_IDS; break;
  893. default:
  894. break;
  895. }
  896. }
  897. return true;
  898. }
  899. void JoystickController::connectionComplete()
  900. {
  901. DBGPrintf(" JoystickController::connectionComplete %x joystick type %d\n", (uint32_t)this, joystickType_);
  902. switch (joystickType_) {
  903. case PS4:
  904. {
  905. uint8_t packet[2];
  906. packet[0] = 0x43; // HID BT Get_report (0x40) | Report Type (Feature 0x03)
  907. packet[1] = 0x02; // Report ID
  908. DBGPrintf("Set PS4 report\n");
  909. delay(1);
  910. btdriver_->sendL2CapCommand(packet, sizeof(packet), 0x40);
  911. }
  912. break;
  913. case PS3:
  914. {
  915. uint8_t packet[6];
  916. packet[0] = 0x53; // HID BT Set_report (0x50) | Report Type (Feature 0x03)
  917. packet[1] = 0xF4; // Report ID
  918. packet[2] = 0x42; // Special PS3 Controller enable commands
  919. packet[3] = 0x03;
  920. packet[4] = 0x00;
  921. packet[5] = 0x00;
  922. DBGPrintf("enable six axis\n");
  923. delay(1);
  924. btdriver_->sendL2CapCommand(packet, sizeof(packet), BluetoothController::CONTROL_SCID);
  925. }
  926. break;
  927. case PS3_MOTION:
  928. setLEDs(0, 0xff, 0); // Maybe try setting to green?
  929. default:
  930. break;
  931. }
  932. }
  933. void JoystickController::release_bluetooth()
  934. {
  935. btdevice = nullptr; // remember this way
  936. btdriver_ = nullptr;
  937. connected_ = false;
  938. special_process_required = false;
  939. }
  940. bool JoystickController::PS3Pair(uint8_t* bdaddr) {
  941. if (!driver_) return false;
  942. if (joystickType_ == PS3) {
  943. /* Set the internal Bluetooth address */
  944. txbuf_[0] = 0x01;
  945. txbuf_[1] = 0x00;
  946. for(uint8_t i = 0; i < 6; i++)
  947. txbuf_[i + 2] = bdaddr[5 - i]; // Copy into buffer, has to be written reversed, so it is MSB first
  948. // 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
  949. return driver_->sendControlPacket(0x21, 9, 0x3f5, 0, 8, txbuf_);
  950. } else if (joystickType_ == PS3_MOTION) {
  951. // Slightly different than other PS3 units...
  952. txbuf_[0] = 0x05;
  953. for(uint8_t i = 0; i < 6; i++)
  954. txbuf_[i + 1] = bdaddr[i]; // Order different looks like LSB First?
  955. txbuf_[7] = 0x10;
  956. txbuf_[8] = 0x01;
  957. txbuf_[9] = 0x02;
  958. txbuf_[10] = 0x12;
  959. // 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
  960. return driver_->sendControlPacket(0x21, 9, 0x305, 0, 11, txbuf_);
  961. }
  962. return false;
  963. }