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.

424 lines
13KB

  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. bool show_changed_only = false;
  32. bool show_raw_data = false;
  33. bool show_changed_data = false;
  34. uint8_t joystick_left_trigger_value = 0;
  35. uint8_t joystick_right_trigger_value = 0;
  36. uint64_t joystick_full_notify_mask = (uint64_t) - 1;
  37. int psAxis[64];
  38. bool first_joystick_message = true;
  39. uint8_t last_bdaddr[6] = {0, 0, 0, 0, 0, 0};
  40. void setup()
  41. {
  42. /* Serial4.begin( 1843200 );
  43. debBegin_tt( &Serial4, LED_BUILTIN, 12);
  44. debTraceShow_tt( -1, "", "", "" );
  45. Serial4.println("\n" __FILE__ " " __DATE__ " " __TIME__);
  46. Serial4.println("\n********\n T4 connected Serial1 *******\n");
  47. Serial4.println("\n" __FILE__ " " __DATE__ " " __TIME__);
  48. Serial4.println("\n********\n T4 connected Serial4 *******\n");
  49. */
  50. Serial1.begin(2000000);
  51. while (!Serial) ; // wait for Arduino Serial Monitor
  52. //debTraceShow_tt( -2, "", "", "" );
  53. //Serial4.println("\n" __FILE__ " " __DATE__ " " __TIME__);
  54. //Serial1.begin( 1843200 );
  55. Serial.println("\n\nUSB Host Testing");
  56. Serial.println(sizeof(USBHub), DEC);
  57. myusb.begin();
  58. delay(2000);
  59. rawhid1.attachReceive(OnReceiveHidData);
  60. rawhid2.attachReceive(OnReceiveHidData);
  61. }
  62. void loop()
  63. {
  64. myusb.Task();
  65. if (Serial.available()) {
  66. int ch = Serial.read(); // get the first char.
  67. while (Serial.read() != -1) ;
  68. if ((ch == 'b') || (ch == 'B')) {
  69. Serial.println("Only notify on Basic Axis changes");
  70. joystick1.axisChangeNotifyMask(0x3ff);
  71. } else if ((ch == 'f') || (ch == 'F')) {
  72. Serial.println("Only notify on Full Axis changes");
  73. joystick1.axisChangeNotifyMask(joystick_full_notify_mask);
  74. } else {
  75. if (show_changed_only) {
  76. show_changed_only = false;
  77. Serial.println("\n*** Show All fields mode ***");
  78. } else {
  79. show_changed_only = true;
  80. Serial.println("\n*** Show only changed fields mode ***");
  81. }
  82. }
  83. }
  84. for (uint8_t i = 0; i < CNT_DEVICES; i++) {
  85. if (*drivers[i] != driver_active[i]) {
  86. if (driver_active[i]) {
  87. Serial.printf("*** Device %s - disconnected ***\n", driver_names[i]);
  88. driver_active[i] = false;
  89. } else {
  90. Serial.printf("*** Device %s %x:%x - connected ***\n", driver_names[i], drivers[i]->idVendor(), drivers[i]->idProduct());
  91. driver_active[i] = true;
  92. const uint8_t *psz = drivers[i]->manufacturer();
  93. if (psz && *psz) Serial.printf(" manufacturer: %s\n", psz);
  94. psz = drivers[i]->product();
  95. if (psz && *psz) Serial.printf(" product: %s\n", psz);
  96. psz = drivers[i]->serialNumber();
  97. if (psz && *psz) Serial.printf(" Serial: %s\n", psz);
  98. if (drivers[i] == &bluet) {
  99. const uint8_t *bdaddr = bluet.myBDAddr();
  100. // remember it...
  101. Serial.printf(" BDADDR: %x:%x:%x:%x:%x:%x\n", bdaddr[0], bdaddr[1], bdaddr[2], bdaddr[3], bdaddr[4], bdaddr[5]);
  102. for (uint8_t i = 0; i < 6; i++) last_bdaddr[i] = bdaddr[i];
  103. }
  104. }
  105. }
  106. }
  107. for (uint8_t i = 0; i < CNT_HIDDEVICES; i++) {
  108. if (*hiddrivers[i] != hid_driver_active[i]) {
  109. if (hid_driver_active[i]) {
  110. Serial.printf("*** HID Device %s - disconnected ***\n", hid_driver_names[i]);
  111. hid_driver_active[i] = false;
  112. } else {
  113. Serial.printf("*** HID Device %s %x:%x - connected ***\n", hid_driver_names[i], hiddrivers[i]->idVendor(), hiddrivers[i]->idProduct());
  114. hid_driver_active[i] = true;
  115. const uint8_t *psz = hiddrivers[i]->manufacturer();
  116. if (psz && *psz) Serial.printf(" manufacturer: %s\n", psz);
  117. psz = hiddrivers[i]->product();
  118. if (psz && *psz) Serial.printf(" product: %s\n", psz);
  119. psz = hiddrivers[i]->serialNumber();
  120. if (psz && *psz) Serial.printf(" Serial: %s\n", psz);
  121. }
  122. }
  123. }
  124. if (joystick1.available()) {
  125. if (first_joystick_message) {
  126. Serial.printf("*** First Joystick message %x:%x ***\n",
  127. joystick1.idVendor(), joystick1.idProduct());
  128. first_joystick_message = false;
  129. const uint8_t *psz = joystick1.manufacturer();
  130. if (psz && *psz) Serial.printf(" manufacturer: %s\n", psz);
  131. psz = joystick1.product();
  132. if (psz && *psz) Serial.printf(" product: %s\n", psz);
  133. psz = joystick1.serialNumber();
  134. if (psz && *psz) Serial.printf(" Serial: %s\n", psz);
  135. // lets try to reduce number of fields that update
  136. joystick1.axisChangeNotifyMask(0xFFFFFl);
  137. }
  138. for (uint8_t i = 0; i < 64; i++) {
  139. psAxis[i] = joystick1.getAxis(i);
  140. }
  141. switch (joystick1.joystickType()) {
  142. case JoystickController::UNKNOWN:
  143. case JoystickController::PS4:
  144. displayPS4Data();
  145. break;
  146. case JoystickController::PS3:
  147. displayPS3Data();
  148. break;
  149. case JoystickController::XBOXONE:
  150. case JoystickController::XBOX360:
  151. displayXBoxData();
  152. break;
  153. default:
  154. displayRawData();
  155. break;
  156. }
  157. //for (uint8_t i = 0; i < 24; i++) {
  158. // Serial.printf(" %d:%d", i, psAxis[i]);
  159. //}
  160. //Serial.println();
  161. delay(100);
  162. joystick1.joystickDataClear();
  163. }
  164. // See if we have some RAW data
  165. if (rawhid1) {
  166. int ch;
  167. uint8_t buffer[64];
  168. uint8_t count_chars = 0;
  169. memset(buffer, 0, sizeof(buffer));
  170. if (Serial.available()) {
  171. while (((ch = Serial.read()) != -1) && (count_chars < sizeof(buffer))) {
  172. buffer[count_chars++] = ch;
  173. }
  174. rawhid1.sendPacket(buffer);
  175. }
  176. }
  177. }
  178. void displayPS4Data()
  179. {
  180. buttons = joystick1.getButtons();
  181. Serial.printf("LX: %d, LY: %d, RX: %d, RY: %d \r\n", psAxis[0], psAxis[1], psAxis[2], psAxis[5]);
  182. Serial.printf("L-Trig: %d, R-Trig: %d\r\n", psAxis[3], psAxis[4]);
  183. Serial.printf("Buttons: %x\r\n", buttons);
  184. Serial.printf("Battery Status: %d\n", ((psAxis[30] & (1 << 4) - 1)*10));
  185. printAngles();
  186. Serial.println();
  187. uint8_t ltv;
  188. uint8_t rtv;
  189. ltv = psAxis[3];
  190. rtv = psAxis[4];
  191. if ((ltv != joystick_left_trigger_value) || (rtv != joystick_right_trigger_value)) {
  192. joystick_left_trigger_value = ltv;
  193. joystick_right_trigger_value = rtv;
  194. Serial.printf("Rumbling: %d, %d\r\n", ltv, rtv);
  195. joystick1.setRumble(ltv, rtv);
  196. }
  197. if (buttons != buttons_prev) {
  198. uint8_t lr = 0;
  199. uint8_t lg = 0;
  200. uint8_t lb = 0;
  201. if(buttons == 0x10008){//Srq
  202. lr = 0xff;
  203. }
  204. if(buttons == 0x40008){//Circ
  205. lg = 0xff;
  206. }
  207. if(buttons == 0x80008){//Tri
  208. lb = 0xff;
  209. }
  210. Serial.print(buttons,HEX); Serial.print(", ");
  211. Serial.print(lr); Serial.print(", ");
  212. Serial.print(lg); Serial.print(", ");
  213. Serial.println(lb);
  214. joystick1.setLEDs(lr, lg, lb);
  215. buttons_prev = buttons;
  216. }
  217. }
  218. void displayPS3Data()
  219. {
  220. buttons = joystick1.getButtons();
  221. //buttons = psAxis[2] | ((uint16_t)psAxis[3] << 8);
  222. // Use L3 (Left joystick button) to toggle Show Raw or not...
  223. if ((buttons & 0x02) && !(buttons_prev & 0x02)) show_raw_data = !show_raw_data;
  224. if ((buttons & 0x04) && !(buttons_prev & 0x04)) show_changed_data = !show_changed_data;
  225. // See about maybe pair...
  226. if ((buttons & 0x10000) && !(buttons_prev & 0x10000) && (buttons & 0x0001)) {
  227. // PS button just pressed and select button pressed act like PS4 share like...
  228. Serial.print("\nPS3 Pairing Request");
  229. if (!last_bdaddr[0] && !last_bdaddr[1] && !last_bdaddr[2] && !last_bdaddr[3] && !last_bdaddr[4] && !last_bdaddr[5]) {
  230. Serial.println(" - failed - no Bluetooth adapter has been plugged in");
  231. } else if (!hiddrivers[0]) { // Kludge see if we are connected as HID?
  232. Serial.println(" - failed - PS3 device not plugged into USB");
  233. } else {
  234. 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]);
  235. if (! joystick1.PS3Pair(last_bdaddr)) {
  236. Serial.println(" Pairing call Failed");
  237. } else {
  238. Serial.println(" Pairing complete (I hope), make sure Bluetooth adapter is plugged in and try PS3 without USB");
  239. }
  240. }
  241. }
  242. if (show_raw_data) {
  243. displayRawData();
  244. } else {
  245. Serial.printf("LX: %d, LY: %d, RX: %d, RY: %d \r\n", psAxis[0], psAxis[1], psAxis[2], psAxis[5]);
  246. Serial.printf("L-Trig: %d, R-Trig: %d\r\n", psAxis[3], psAxis[4]);
  247. Serial.printf("Buttons: %x\r\n", buttons);
  248. }
  249. uint8_t ltv;
  250. uint8_t rtv;
  251. ltv = psAxis[3];
  252. rtv = psAxis[4];
  253. if ((ltv != joystick_left_trigger_value) || (rtv != joystick_right_trigger_value)) {
  254. joystick_left_trigger_value = ltv;
  255. joystick_right_trigger_value = rtv;
  256. Serial.printf("Rumbling: %d, %d\r\n", ltv, rtv);
  257. joystick1.setRumble(ltv, rtv);
  258. }
  259. if (buttons != buttons_prev) {
  260. uint8_t leds = 0;
  261. if (buttons & 0x8000) leds = 1; //Srq
  262. if (buttons & 0x2000) leds = 2; //Cir
  263. if (buttons & 0x1000) leds = 3; //Tri
  264. //Cross = 2
  265. joystick1.setLEDs(leds);
  266. buttons_prev = buttons;
  267. }
  268. }
  269. void displayXBoxData()
  270. {
  271. buttons = joystick1.getButtons();
  272. // Use L3 (Left joystick button) to toggle Show Raw or not...
  273. if ((buttons & 0x4000) && !(buttons_prev & 0x4000)) show_raw_data = !show_raw_data;
  274. if ((buttons & 0x8000) && !(buttons_prev & 0x8000)) show_changed_data = !show_changed_data;
  275. if (show_raw_data) {
  276. displayRawData();
  277. } else {
  278. Serial.printf("LX: %d, LY: %d, RX: %d, RY: %d \r\n", psAxis[0], psAxis[1], psAxis[2], psAxis[5]);
  279. Serial.printf("L-Trig: %d, R-Trig: %d\r\n", psAxis[3], psAxis[4]);
  280. Serial.printf("Buttons: %x\r\n", buttons);
  281. }
  282. uint8_t ltv;
  283. uint8_t rtv;
  284. ltv = psAxis[3];
  285. rtv = psAxis[4];
  286. if ((ltv != joystick_left_trigger_value) || (rtv != joystick_right_trigger_value)) {
  287. joystick_left_trigger_value = ltv;
  288. joystick_right_trigger_value = rtv;
  289. Serial.printf("Rumbling: %d, %d\r\n", ltv, rtv);
  290. joystick1.setRumble(ltv, rtv);
  291. }
  292. if (buttons != buttons_prev) {
  293. uint8_t leds = 0;
  294. if (buttons & 0x8000) leds = 1; //Srq
  295. if (buttons & 0x2000) leds = 2; //Cir
  296. if (buttons & 0x1000) leds = 3; //Tri
  297. //Cross = 2
  298. joystick1.setLEDs(leds);
  299. buttons_prev = buttons;
  300. }
  301. }
  302. void displayRawData() {
  303. uint64_t axis_mask = joystick1.axisMask();
  304. uint64_t changed_mask = joystick1.axisChangedMask();
  305. buttons = joystick1.getButtons();
  306. if (!changed_mask && (buttons == buttons_prev)) return;
  307. if (show_changed_data) {
  308. if (!changed_mask) return;
  309. changed_mask &= 0xfffffffffL; // try reducing which ones show...
  310. Serial.printf("%0x - ", joystick1.getButtons());
  311. for (uint16_t index = 0; changed_mask; index++) {
  312. if (changed_mask & 1) {
  313. Serial.printf("%d:%02x ", index, psAxis[index]);
  314. }
  315. changed_mask >>= 1;
  316. }
  317. } else {
  318. axis_mask &= 0xffffff;
  319. Serial.printf("%06x%06x: %06x - ", (uint32_t)(changed_mask >> 32), (uint32_t)(changed_mask & 0xffffffff), joystick1.getButtons());
  320. for (uint16_t index = 0; axis_mask; index++) {
  321. Serial.printf("%02x ", psAxis[index]);
  322. axis_mask >>= 1;
  323. }
  324. }
  325. Serial.println();
  326. buttons_prev = buttons;
  327. }
  328. bool OnReceiveHidData(uint32_t usage, const uint8_t *data, uint32_t len) {
  329. // Called for maybe both HIDS for rawhid basic test. One is for the Teensy
  330. // to output to Serial. while still having Raw Hid...
  331. if (usage == 0xffc90004) {
  332. // Lets trim off trailing null characters.
  333. while ((len > 0) && (data[len - 1] == 0)) {
  334. len--;
  335. }
  336. if (len) {
  337. Serial.print("RawHid Serial: ");
  338. Serial.write(data, len);
  339. }
  340. } else {
  341. Serial.print("RawHID data: ");
  342. Serial.println(usage, HEX);
  343. while (len) {
  344. uint8_t cb = (len > 16) ? 16 : len;
  345. const uint8_t *p = data;
  346. uint8_t i;
  347. for (i = 0; i < cb; i++) {
  348. Serial.printf("%02x ", *p++);
  349. }
  350. Serial.print(": ");
  351. for (i = 0; i < cb; i++) {
  352. Serial.write(((*data >= ' ') && (*data <= '~')) ? *data : '.');
  353. data++;
  354. }
  355. len -= cb;
  356. Serial.println();
  357. }
  358. }
  359. return true;
  360. }