Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

JoystickBT.ino 20KB

5 år sedan
5 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. // Simple test of USB Host Mouse/Keyboard
  2. //
  3. // This example is in the public domain
  4. #include "USBHost_t36.h"
  5. //#include "debug_tt.h"
  6. USBHost myusb;
  7. USBHub hub1(myusb);
  8. USBHub hub2(myusb);
  9. USBHIDParser hid1(myusb);
  10. USBHIDParser hid2(myusb);
  11. USBHIDParser hid3(myusb);
  12. USBHIDParser hid4(myusb);
  13. USBHIDParser hid5(myusb);
  14. JoystickController joystick1(myusb);
  15. //BluetoothController bluet(myusb, true, "0000"); // Version does pairing to device
  16. BluetoothController bluet(myusb); // version assumes it already was paired
  17. int user_axis[64];
  18. uint32_t buttons_prev = 0;
  19. uint32_t buttons;
  20. RawHIDController rawhid1(myusb);
  21. RawHIDController rawhid2(myusb, 0xffc90004);
  22. USBDriver *drivers[] = {&hub1, &hub2, &joystick1, &bluet, &hid1, &hid2, &hid3, &hid4, &hid5};
  23. #define CNT_DEVICES (sizeof(drivers)/sizeof(drivers[0]))
  24. const char * driver_names[CNT_DEVICES] = {"Hub1", "Hub2", "JOY1D", "Bluet", "HID1" , "HID2", "HID3", "HID4", "HID5"};
  25. bool driver_active[CNT_DEVICES] = {false, false, false, false};
  26. // Lets also look at HID Input devices
  27. USBHIDInput *hiddrivers[] = {&joystick1, &rawhid1, &rawhid2};
  28. #define CNT_HIDDEVICES (sizeof(hiddrivers)/sizeof(hiddrivers[0]))
  29. const char * hid_driver_names[CNT_DEVICES] = {"Joystick1", "RawHid1", "RawHid2"};
  30. bool hid_driver_active[CNT_DEVICES] = {false, false, false};
  31. BTHIDInput *bthiddrivers[] = {&joystick1};
  32. #define CNT_BTHIDDEVICES (sizeof(bthiddrivers)/sizeof(bthiddrivers[0]))
  33. const char * bthid_driver_names[CNT_HIDDEVICES] = {"joystick"};
  34. bool bthid_driver_active[CNT_HIDDEVICES] = {false};
  35. bool show_changed_only = false;
  36. bool show_raw_data = false;
  37. bool show_changed_data = false;
  38. uint8_t joystick_left_trigger_value = 0;
  39. uint8_t joystick_right_trigger_value = 0;
  40. uint64_t joystick_full_notify_mask = (uint64_t) - 1;
  41. int psAxis[64];
  42. bool first_joystick_message = true;
  43. uint8_t last_bdaddr[6] = {0, 0, 0, 0, 0, 0};
  44. // ps3 motion on USB does not do much, but see if we can pair it and maybe change
  45. // color of bulb...
  46. uint32_t PS3_MOTION_timer = 0;
  47. uint8_t PS3_MOTION_tried_to_pair_state = 0;
  48. #define PS3_MOTION_PERIOD 2500 // not sure yet what would be good period for this..
  49. //=============================================================================
  50. // Setup
  51. //=============================================================================
  52. void setup()
  53. {
  54. Serial1.begin(2000000);
  55. while (!Serial) ; // wait for Arduino Serial Monitor
  56. Serial.println("\n\nUSB Host Testing");
  57. Serial.println(sizeof(USBHub), DEC);
  58. myusb.begin();
  59. delay(2000);
  60. rawhid1.attachReceive(OnReceiveHidData);
  61. rawhid2.attachReceive(OnReceiveHidData);
  62. }
  63. //=============================================================================
  64. // Loop
  65. //=============================================================================
  66. void loop()
  67. {
  68. myusb.Task();
  69. if (Serial.available()) {
  70. int ch = Serial.read(); // get the first char.
  71. while (Serial.read() != -1) ;
  72. if ((ch == 'b') || (ch == 'B')) {
  73. Serial.println("Only notify on Basic Axis changes");
  74. joystick1.axisChangeNotifyMask(0x3ff);
  75. } else if ((ch == 'f') || (ch == 'F')) {
  76. Serial.println("Only notify on Full Axis changes");
  77. joystick1.axisChangeNotifyMask(joystick_full_notify_mask);
  78. } else {
  79. if (show_changed_only) {
  80. show_changed_only = false;
  81. Serial.println("\n*** Show All fields mode ***");
  82. } else {
  83. show_changed_only = true;
  84. Serial.println("\n*** Show only changed fields mode ***");
  85. }
  86. }
  87. }
  88. // check to see if the device list has changed:
  89. UpdateActiveDeviceInfo();
  90. processPS3MotionTimer();
  91. if (joystick1.available()) {
  92. if (first_joystick_message) {
  93. Serial.printf("*** First Joystick message %x:%x ***\n",
  94. joystick1.idVendor(), joystick1.idProduct());
  95. first_joystick_message = false;
  96. const uint8_t *psz = joystick1.manufacturer();
  97. if (psz && *psz) Serial.printf(" manufacturer: %s\n", psz);
  98. psz = joystick1.product();
  99. if (psz && *psz) Serial.printf(" product: %s\n", psz);
  100. psz = joystick1.serialNumber();
  101. if (psz && *psz) Serial.printf(" Serial: %s\n", psz);
  102. // lets try to reduce number of fields that update
  103. joystick1.axisChangeNotifyMask(0xFFFFFl);
  104. }
  105. for (uint8_t i = 0; i < 64; i++) {
  106. psAxis[i] = joystick1.getAxis(i);
  107. }
  108. switch (joystick1.joystickType()) {
  109. case JoystickController::UNKNOWN:
  110. case JoystickController::PS4:
  111. displayPS4Data();
  112. break;
  113. case JoystickController::PS3:
  114. displayPS3Data();
  115. break;
  116. case JoystickController::PS3_MOTION:
  117. displayPS3MotionData();
  118. break;
  119. case JoystickController::XBOXONE:
  120. case JoystickController::XBOX360:
  121. displayXBoxData();
  122. break;
  123. default:
  124. displayRawData();
  125. break;
  126. }
  127. //for (uint8_t i = 0; i < 24; i++) {
  128. // Serial.printf(" %d:%d", i, psAxis[i]);
  129. //}
  130. //Serial.println();
  131. delay(100);
  132. joystick1.joystickDataClear();
  133. }
  134. // See if we have some RAW data
  135. if (rawhid1) {
  136. int ch;
  137. uint8_t buffer[64];
  138. uint8_t count_chars = 0;
  139. memset(buffer, 0, sizeof(buffer));
  140. if (Serial.available()) {
  141. while (((ch = Serial.read()) != -1) && (count_chars < sizeof(buffer))) {
  142. buffer[count_chars++] = ch;
  143. }
  144. rawhid1.sendPacket(buffer);
  145. }
  146. }
  147. }
  148. //=============================================================================
  149. // UpdateActiveDeviceInfo
  150. //=============================================================================
  151. void UpdateActiveDeviceInfo() {
  152. for (uint8_t i = 0; i < CNT_DEVICES; i++) {
  153. if (*drivers[i] != driver_active[i]) {
  154. if (driver_active[i]) {
  155. Serial.printf("*** Device %s - disconnected ***\n", driver_names[i]);
  156. driver_active[i] = false;
  157. } else {
  158. Serial.printf("*** Device %s %x:%x - connected ***\n", driver_names[i], drivers[i]->idVendor(), drivers[i]->idProduct());
  159. driver_active[i] = true;
  160. const uint8_t *psz = drivers[i]->manufacturer();
  161. if (psz && *psz) Serial.printf(" manufacturer: %s\n", psz);
  162. psz = drivers[i]->product();
  163. if (psz && *psz) Serial.printf(" product: %s\n", psz);
  164. psz = drivers[i]->serialNumber();
  165. if (psz && *psz) Serial.printf(" Serial: %s\n", psz);
  166. if (drivers[i] == &bluet) {
  167. const uint8_t *bdaddr = bluet.myBDAddr();
  168. // remember it...
  169. Serial.printf(" BDADDR: %x:%x:%x:%x:%x:%x\n", bdaddr[0], bdaddr[1], bdaddr[2], bdaddr[3], bdaddr[4], bdaddr[5]);
  170. for (uint8_t i = 0; i < 6; i++) last_bdaddr[i] = bdaddr[i];
  171. }
  172. }
  173. }
  174. }
  175. for (uint8_t i = 0; i < CNT_HIDDEVICES; i++) {
  176. if (*hiddrivers[i] != hid_driver_active[i]) {
  177. if (hid_driver_active[i]) {
  178. Serial.printf("*** HID Device %s - disconnected ***\n", hid_driver_names[i]);
  179. hid_driver_active[i] = false;
  180. } else {
  181. Serial.printf("*** HID Device %s %x:%x - connected ***\n", hid_driver_names[i], hiddrivers[i]->idVendor(), hiddrivers[i]->idProduct());
  182. hid_driver_active[i] = true;
  183. const uint8_t *psz = hiddrivers[i]->manufacturer();
  184. if (psz && *psz) Serial.printf(" manufacturer: %s\n", psz);
  185. psz = hiddrivers[i]->product();
  186. if (psz && *psz) Serial.printf(" product: %s\n", psz);
  187. psz = hiddrivers[i]->serialNumber();
  188. if (psz && *psz) Serial.printf(" Serial: %s\n", psz);
  189. // See if this is our joystick object...
  190. if (hiddrivers[i] == &joystick1) {
  191. Serial.printf(" Joystick type: %d\n", joystick1.joystickType());
  192. if (joystick1.joystickType() == JoystickController::PS3_MOTION) {
  193. Serial.println(" PS3 Motion detected");
  194. PS3_MOTION_timer = millis(); // set time for last event
  195. PS3_MOTION_tried_to_pair_state = 0;
  196. }
  197. }
  198. }
  199. }
  200. }
  201. // Then Bluetooth devices
  202. for (uint8_t i = 0; i < CNT_BTHIDDEVICES; i++) {
  203. if (*bthiddrivers[i] != bthid_driver_active[i]) {
  204. if (bthid_driver_active[i]) {
  205. Serial.printf("*** BTHID Device %s - disconnected ***\n", hid_driver_names[i]);
  206. bthid_driver_active[i] = false;
  207. } else {
  208. Serial.printf("*** BTHID Device %s %x:%x - connected ***\n", hid_driver_names[i], hiddrivers[i]->idVendor(), hiddrivers[i]->idProduct());
  209. bthid_driver_active[i] = true;
  210. const uint8_t *psz = bthiddrivers[i]->manufacturer();
  211. if (psz && *psz) Serial.printf(" manufacturer: %s\n", psz);
  212. psz = bthiddrivers[i]->product();
  213. if (psz && *psz) Serial.printf(" product: %s\n", psz);
  214. psz = bthiddrivers[i]->serialNumber();
  215. if (psz && *psz) Serial.printf(" Serial: %s\n", psz);
  216. }
  217. }
  218. }
  219. }
  220. //=============================================================================
  221. // displayPS4Data
  222. //=============================================================================
  223. void displayPS4Data()
  224. {
  225. buttons = joystick1.getButtons();
  226. Serial.printf("LX: %d, LY: %d, RX: %d, RY: %d \r\n", psAxis[0], psAxis[1], psAxis[2], psAxis[5]);
  227. Serial.printf("L-Trig: %d, R-Trig: %d\r\n", psAxis[3], psAxis[4]);
  228. Serial.printf("Buttons: %x\r\n", buttons);
  229. Serial.printf("Battery Status: %d\n", ((psAxis[30] & ((1 << 4) - 1))*10));
  230. printAngles();
  231. Serial.println();
  232. uint8_t ltv;
  233. uint8_t rtv;
  234. ltv = psAxis[3];
  235. rtv = psAxis[4];
  236. if ((ltv != joystick_left_trigger_value) || (rtv != joystick_right_trigger_value)) {
  237. joystick_left_trigger_value = ltv;
  238. joystick_right_trigger_value = rtv;
  239. Serial.printf("Rumbling: %d, %d\r\n", ltv, rtv);
  240. joystick1.setRumble(ltv, rtv);
  241. }
  242. if (buttons != buttons_prev) {
  243. uint8_t lr = 0;
  244. uint8_t lg = 0;
  245. uint8_t lb = 0;
  246. if(buttons == 0x10008){//Srq
  247. lr = 0xff;
  248. }
  249. if(buttons == 0x40008){//Circ
  250. lg = 0xff;
  251. }
  252. if(buttons == 0x80008){//Tri
  253. lb = 0xff;
  254. }
  255. Serial.print(buttons,HEX); Serial.print(", ");
  256. Serial.print(lr); Serial.print(", ");
  257. Serial.print(lg); Serial.print(", ");
  258. Serial.println(lb);
  259. joystick1.setLEDs(lr, lg, lb);
  260. buttons_prev = buttons;
  261. }
  262. }
  263. //=============================================================================
  264. // displayPS3Data
  265. //=============================================================================
  266. void displayPS3Data()
  267. {
  268. buttons = joystick1.getButtons();
  269. //buttons = psAxis[2] | ((uint16_t)psAxis[3] << 8);
  270. // Use L3 (Left joystick button) to toggle Show Raw or not...
  271. if ((buttons & 0x02) && !(buttons_prev & 0x02)) show_raw_data = !show_raw_data;
  272. if ((buttons & 0x04) && !(buttons_prev & 0x04)) show_changed_data = !show_changed_data;
  273. // See about maybe pair...
  274. if ((buttons & 0x10000) && !(buttons_prev & 0x10000) && (buttons & 0x0C01)) {
  275. // PS button just pressed and select button pressed act like PS4 share like...
  276. // Note: you can use either R1 or L1 with the PS button, to work with Sony Move Navigation...
  277. Serial.print("\nPS3 Pairing Request");
  278. if (!last_bdaddr[0] && !last_bdaddr[1] && !last_bdaddr[2] && !last_bdaddr[3] && !last_bdaddr[4] && !last_bdaddr[5]) {
  279. Serial.println(" - failed - no Bluetooth adapter has been plugged in");
  280. } else if (!hiddrivers[0]) { // Kludge see if we are connected as HID?
  281. Serial.println(" - failed - PS3 device not plugged into USB");
  282. } else {
  283. Serial.printf(" - Attempt pair to: %x:%x:%x:%x:%x:%x\n", last_bdaddr[0], last_bdaddr[1], last_bdaddr[2], last_bdaddr[3], last_bdaddr[4], last_bdaddr[5]);
  284. if (! joystick1.PS3Pair(last_bdaddr)) {
  285. Serial.println(" Pairing call Failed");
  286. } else {
  287. Serial.println(" Pairing complete (I hope), make sure Bluetooth adapter is plugged in and try PS3 without USB");
  288. }
  289. }
  290. }
  291. if (show_raw_data) {
  292. displayRawData();
  293. } else {
  294. Serial.printf("LX: %d, LY: %d, RX: %d, RY: %d \r\n", psAxis[0], psAxis[1], psAxis[2], psAxis[5]);
  295. Serial.printf("L-Trig: %d, R-Trig: %d\r\n", psAxis[3], psAxis[4]);
  296. Serial.printf("Buttons: %x\r\n", buttons);
  297. }
  298. uint8_t ltv;
  299. uint8_t rtv;
  300. ltv = psAxis[3];
  301. rtv = psAxis[4];
  302. if ((ltv != joystick_left_trigger_value) || (rtv != joystick_right_trigger_value)) {
  303. joystick_left_trigger_value = ltv;
  304. joystick_right_trigger_value = rtv;
  305. Serial.printf("Rumbling: %d, %d\r\n", ltv, rtv);
  306. joystick1.setRumble(ltv, rtv);
  307. }
  308. if (buttons != buttons_prev) {
  309. uint8_t leds = 0;
  310. if (buttons & 0x8000) leds = 1; //Srq
  311. if (buttons & 0x2000) leds = 2; //Cir
  312. if (buttons & 0x1000) leds = 3; //Tri
  313. //Cross = 2
  314. joystick1.setLEDs(leds);
  315. buttons_prev = buttons;
  316. }
  317. }
  318. //=============================================================================
  319. // displayPS3MotionData
  320. //=============================================================================
  321. void displayPS3MotionData()
  322. {
  323. buttons = joystick1.getButtons();
  324. // Hard to know what is best here. for now just copy raw data over...
  325. // will do this for now... Format of thought to be data.
  326. // data[1-3] Buttons (mentioned 4 as well but appears to be counter
  327. // axis[0-1] data[5] Trigger, Previous trigger value
  328. // 2-5 Unknown probably place holders for Axis like data for other PS3
  329. // 6 - Time stamp
  330. // 7 - Battery
  331. // 8-19 - Accel: XL, XH, YL, YH, ZL, ZH, XL2, XH2, YL2, YH2, ZL2, ZH2
  332. // 20-31 - Gyro: Xl,Xh,Yl,Yh,Zl,Zh,Xl2,Xh2,Yl2,Yh2,Zl2,Zh2
  333. // 32 - Temp High
  334. // 33 - Temp Low (4 bits) Maybe Magneto x High on other??
  335. // Use Select button to choose raw or not
  336. if ((buttons & 0x01) && !(buttons_prev & 0x01)) show_raw_data = !show_raw_data;
  337. if ((buttons & 0x04) && !(buttons_prev & 0x04)) show_changed_data = !show_changed_data;
  338. if (show_raw_data) {
  339. displayRawData();
  340. } else {
  341. uint64_t changed_mask = joystick1.axisChangedMask();
  342. Serial.printf("Changed: %08x Buttons: %x: Trig: %d\r\n", (uint32_t)changed_mask, buttons, psAxis[0]);
  343. Serial.printf("Battery Status: %d\n", psAxis[7]);
  344. printPS3MotionAngles();
  345. Serial.println();
  346. }
  347. uint8_t ltv = psAxis[0];
  348. if ((ltv != joystick_left_trigger_value) ) {
  349. joystick_left_trigger_value = ltv;
  350. Serial.printf("Rumbling: %d\r\n", ltv);
  351. joystick1.setRumble(ltv, 0);
  352. }
  353. if (buttons != buttons_prev) {
  354. uint8_t ledsR = (buttons & 0x8000)? 0xff : 0; //Srq
  355. uint8_t ledsG = (buttons & 0x2000)? 0xff : 0; //Cir
  356. uint8_t ledsB = (buttons & 0x1000)? 0xff : 0; //Tri
  357. Serial.printf("Set Leds %x %x %x\r\n", ledsR, ledsG, ledsB );
  358. joystick1.setLEDs(ledsR, ledsG, ledsB);
  359. buttons_prev = buttons;
  360. }
  361. }
  362. //=============================================================================
  363. // displayXBoxData
  364. //=============================================================================
  365. void displayXBoxData()
  366. {
  367. buttons = joystick1.getButtons();
  368. // Use L3 (Left joystick button) to toggle Show Raw or not...
  369. if ((buttons & 0x4000) && !(buttons_prev & 0x4000)) show_raw_data = !show_raw_data;
  370. if ((buttons & 0x8000) && !(buttons_prev & 0x8000)) show_changed_data = !show_changed_data;
  371. if (show_raw_data) {
  372. displayRawData();
  373. } else {
  374. Serial.printf("LX: %d, LY: %d, RX: %d, RY: %d \r\n", psAxis[0], psAxis[1], psAxis[2], psAxis[5]);
  375. Serial.printf("L-Trig: %d, R-Trig: %d\r\n", psAxis[3], psAxis[4]);
  376. Serial.printf("Buttons: %x\r\n", buttons);
  377. }
  378. uint8_t ltv;
  379. uint8_t rtv;
  380. ltv = psAxis[3];
  381. rtv = psAxis[4];
  382. if ((ltv != joystick_left_trigger_value) || (rtv != joystick_right_trigger_value)) {
  383. joystick_left_trigger_value = ltv;
  384. joystick_right_trigger_value = rtv;
  385. Serial.printf("Rumbling: %d, %d\r\n", ltv, rtv);
  386. joystick1.setRumble(ltv, rtv);
  387. }
  388. if (buttons != buttons_prev) {
  389. uint8_t leds = 0;
  390. if (buttons & 0x8000) leds = 1; //Srq
  391. if (buttons & 0x2000) leds = 2; //Cir
  392. if (buttons & 0x1000) leds = 3; //Tri
  393. //Cross = 2
  394. joystick1.setLEDs(leds);
  395. buttons_prev = buttons;
  396. }
  397. }
  398. //=============================================================================
  399. // displayRawData
  400. //=============================================================================
  401. void displayRawData() {
  402. uint64_t axis_mask = joystick1.axisMask();
  403. uint64_t changed_mask = joystick1.axisChangedMask();
  404. buttons = joystick1.getButtons();
  405. if (!changed_mask && (buttons == buttons_prev)) return;
  406. if (show_changed_data) {
  407. if (!changed_mask) return;
  408. changed_mask &= 0xfffffffffL; // try reducing which ones show...
  409. Serial.printf("%0x - ", joystick1.getButtons());
  410. for (uint16_t index = 0; changed_mask; index++) {
  411. if (changed_mask & 1) {
  412. Serial.printf("%d:%02x ", index, psAxis[index]);
  413. }
  414. changed_mask >>= 1;
  415. }
  416. } else {
  417. axis_mask &= 0xffffff;
  418. Serial.printf("%06x%06x: %06x - ", (uint32_t)(changed_mask >> 32), (uint32_t)(changed_mask & 0xffffffff), joystick1.getButtons());
  419. for (uint16_t index = 0; axis_mask; index++) {
  420. Serial.printf("%02x ", psAxis[index]);
  421. axis_mask >>= 1;
  422. }
  423. }
  424. Serial.println();
  425. buttons_prev = buttons;
  426. }
  427. //=============================================================================
  428. // OnReceiveHidData
  429. //=============================================================================
  430. bool OnReceiveHidData(uint32_t usage, const uint8_t *data, uint32_t len) {
  431. // Called for maybe both HIDS for rawhid basic test. One is for the Teensy
  432. // to output to Serial. while still having Raw Hid...
  433. if (usage == 0xffc90004) {
  434. // Lets trim off trailing null characters.
  435. while ((len > 0) && (data[len - 1] == 0)) {
  436. len--;
  437. }
  438. if (len) {
  439. Serial.print("RawHid Serial: ");
  440. Serial.write(data, len);
  441. }
  442. } else {
  443. Serial.print("RawHID data: ");
  444. Serial.println(usage, HEX);
  445. while (len) {
  446. uint8_t cb = (len > 16) ? 16 : len;
  447. const uint8_t *p = data;
  448. uint8_t i;
  449. for (i = 0; i < cb; i++) {
  450. Serial.printf("%02x ", *p++);
  451. }
  452. Serial.print(": ");
  453. for (i = 0; i < cb; i++) {
  454. Serial.write(((*data >= ' ') && (*data <= '~')) ? *data : '.');
  455. data++;
  456. }
  457. len -= cb;
  458. Serial.println();
  459. }
  460. }
  461. return true;
  462. }
  463. //=============================================================================
  464. // processPS3MotionTimer
  465. //=============================================================================
  466. static const uint32_t PS3_MOTION_colors[] = {0, 0xff, 0xff00, 0xff0000, 0xffff, 0xff00ff, 0xffff00, 0xffffff};
  467. uint8_t PS3_MOTION_colors_index = 0;
  468. void processPS3MotionTimer() {
  469. // See if we have a PS3_MOTION connected and we have run for a certain amount of time
  470. if (PS3_MOTION_timer && ((millis()-PS3_MOTION_timer) >= PS3_MOTION_PERIOD)) {
  471. Serial.println("PS3 Motion Timer"); Serial.flush();
  472. if (joystick1) {
  473. PS3_MOTION_timer = millis(); // joystick not there any more...
  474. // We will first try to set feedback color for the PS3, maybe alternate colors
  475. if (++PS3_MOTION_colors_index >= sizeof(PS3_MOTION_colors)/sizeof(PS3_MOTION_colors[0])) PS3_MOTION_colors_index = 0;
  476. joystick1.setLEDs(PS3_MOTION_colors[PS3_MOTION_colors_index]);
  477. // Next see if we can try to pair.
  478. if (PS3_MOTION_tried_to_pair_state == 0) {
  479. Serial.println("PS3_MOTION Connected");
  480. if (!last_bdaddr[0] && !last_bdaddr[1] && !last_bdaddr[2] && !last_bdaddr[3] && !last_bdaddr[4] && !last_bdaddr[5]) {
  481. Serial.println(" - No Bluetooth adapter has been plugged in - so will not try to pair");
  482. PS3_MOTION_tried_to_pair_state = 1;
  483. }
  484. }
  485. if ((PS3_MOTION_tried_to_pair_state < 2) &&
  486. (last_bdaddr[0] || last_bdaddr[1] || last_bdaddr[2] || last_bdaddr[3] || last_bdaddr[4] || last_bdaddr[5])) {
  487. Serial.println(" - Bluetooth device detected, will try to pair");
  488. // Lets try to pair
  489. if (! joystick1.PS3Pair(last_bdaddr)) {
  490. Serial.println(" - Pairing call Failed");
  491. } else {
  492. Serial.println(" - Pairing complete (I hope), make sure Bluetooth adapter is plugged in and try PS3 without USB");
  493. }
  494. PS3_MOTION_tried_to_pair_state = 2; // don't try again...
  495. }
  496. } else {
  497. Serial.println("PS3 Motion Joystick no longer detected");
  498. PS3_MOTION_timer = 0; // joystick not there any more...
  499. }
  500. }
  501. }