Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

380 lines
12KB

  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(1843200);
  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. default:
  150. case JoystickController::XBOXONE:
  151. case JoystickController::XBOX360:;
  152. displayRawData();
  153. break;
  154. }
  155. //for (uint8_t i = 0; i < 24; i++) {
  156. // Serial.printf(" %d:%d", i, psAxis[i]);
  157. //}
  158. //Serial.println();
  159. delay(100);
  160. joystick1.joystickDataClear();
  161. }
  162. // See if we have some RAW data
  163. if (rawhid1) {
  164. int ch;
  165. uint8_t buffer[64];
  166. uint8_t count_chars = 0;
  167. memset(buffer, 0, sizeof(buffer));
  168. if (Serial.available()) {
  169. while (((ch = Serial.read()) != -1) && (count_chars < sizeof(buffer))) {
  170. buffer[count_chars++] = ch;
  171. }
  172. rawhid1.sendPacket(buffer);
  173. }
  174. }
  175. }
  176. void displayPS4Data()
  177. {
  178. buttons = joystick1.getButtons();
  179. Serial.printf("LX: %d, LY: %d, RX: %d, RY: %d \r\n", psAxis[0], psAxis[1], psAxis[2], psAxis[5]);
  180. Serial.printf("L-Trig: %d, R-Trig: %d\r\n", psAxis[3], psAxis[4]);
  181. Serial.printf("Buttons: %x\r\n", buttons);
  182. Serial.printf("Battery Status: %d\n", ((psAxis[30] & (1 << 4) - 1)*10));
  183. printAngles();
  184. Serial.println();
  185. uint8_t ltv;
  186. uint8_t rtv;
  187. ltv = psAxis[3];
  188. rtv = psAxis[4];
  189. if ((ltv != joystick_left_trigger_value) || (rtv != joystick_right_trigger_value)) {
  190. joystick_left_trigger_value = ltv;
  191. joystick_right_trigger_value = rtv;
  192. Serial.printf("Rumbling: %d, %d\r\n", ltv, rtv);
  193. joystick1.setRumble(ltv, rtv);
  194. }
  195. if (buttons != buttons_prev) {
  196. uint8_t lr = 0;
  197. uint8_t lg = 0;
  198. uint8_t lb = 0;
  199. if(buttons == 0x10008){//Srq
  200. lr = 0xff;
  201. }
  202. if(buttons == 0x40008){//Circ
  203. lg = 0xff;
  204. }
  205. if(buttons == 0x80008){//Tri
  206. lb = 0xff;
  207. }
  208. Serial.print(buttons,HEX); Serial.print(", ");
  209. Serial.print(lr); Serial.print(", ");
  210. Serial.print(lg); Serial.print(", ");
  211. Serial.println(lb);
  212. joystick1.setLEDs(lr, lg, lb);
  213. buttons_prev = buttons;
  214. }
  215. }
  216. void displayPS3Data()
  217. {
  218. buttons = joystick1.getButtons();
  219. //buttons = psAxis[2] | ((uint16_t)psAxis[3] << 8);
  220. // Use L3 (Left joystick button) to toggle Show Raw or not...
  221. if ((buttons & 0x02) && !(buttons_prev & 0x02)) show_raw_data = !show_raw_data;
  222. if ((buttons & 0x04) && !(buttons_prev & 0x04)) show_changed_data = !show_changed_data;
  223. // See about maybe pair...
  224. if ((buttons & 0x10000) && !(buttons_prev & 0x10000) && (buttons & 0x0001)) {
  225. // PS button just pressed and select button pressed act like PS4 share like...
  226. Serial.print("\nPS3 Pairing Request");
  227. if (!last_bdaddr[0] && !last_bdaddr[1] && !last_bdaddr[2] && !last_bdaddr[3] && !last_bdaddr[4] && !last_bdaddr[5]) {
  228. Serial.println(" - failed - no Bluetooth adapter has been plugged in");
  229. } else if (!hiddrivers[0]) { // Kludge see if we are connected as HID?
  230. Serial.println(" - failed - PS3 device not plugged into USB");
  231. } else {
  232. 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]);
  233. if (! joystick1.PS3Pair(last_bdaddr)) {
  234. Serial.println(" Pairing call Failed");
  235. } else {
  236. Serial.println(" Pairing complete (I hope), make sure Bluetooth adapter is plugged in and try PS3 without USB");
  237. }
  238. }
  239. }
  240. if (show_raw_data) {
  241. displayRawData();
  242. } else {
  243. Serial.printf("LX: %d, LY: %d, RX: %d, RY: %d \r\n", psAxis[0], psAxis[1], psAxis[2], psAxis[5]);
  244. Serial.printf("L-Trig: %d, R-Trig: %d\r\n", psAxis[3], psAxis[4]);
  245. Serial.printf("Buttons: %x\r\n", buttons);
  246. }
  247. uint8_t ltv;
  248. uint8_t rtv;
  249. ltv = psAxis[3];
  250. rtv = psAxis[4];
  251. if ((ltv != joystick_left_trigger_value) || (rtv != joystick_right_trigger_value)) {
  252. joystick_left_trigger_value = ltv;
  253. joystick_right_trigger_value = rtv;
  254. Serial.printf("Rumbling: %d, %d\r\n", ltv, rtv);
  255. joystick1.setRumble(ltv, rtv);
  256. }
  257. if (buttons != buttons_prev) {
  258. uint8_t leds = 0;
  259. if (buttons & 0x8000) leds = 1; //Srq
  260. if (buttons & 0x2000) leds = 2; //Cir
  261. if (buttons & 0x1000) leds = 3; //Tri
  262. //Cross = 2
  263. joystick1.setLEDs(leds);
  264. buttons_prev = buttons;
  265. }
  266. }
  267. void displayRawData() {
  268. uint64_t axis_mask = joystick1.axisMask();
  269. uint64_t changed_mask = joystick1.axisChangedMask();
  270. if (!changed_mask) return;
  271. if (show_changed_data) {
  272. if (!changed_mask) return;
  273. changed_mask &= 0xfffffffffL; // try reducing which ones show...
  274. Serial.printf("%0x - ", joystick1.getButtons());
  275. for (uint16_t index=0; changed_mask; index++) {
  276. if (changed_mask & 1) {
  277. Serial.printf("%d:%02x ", index, psAxis[index]);
  278. }
  279. changed_mask >>= 1;
  280. }
  281. } else {
  282. axis_mask &= 0xffffff;
  283. Serial.printf("%06x%06x: %06x - ", (uint32_t)(changed_mask >> 32), (uint32_t)(changed_mask & 0xffffffff), joystick1.getButtons());
  284. for (uint16_t index=0; axis_mask; index++) {
  285. Serial.printf("%02x ", psAxis[index]);
  286. axis_mask >>= 1;
  287. }
  288. }
  289. Serial.println();
  290. }
  291. bool OnReceiveHidData(uint32_t usage, const uint8_t *data, uint32_t len) {
  292. // Called for maybe both HIDS for rawhid basic test. One is for the Teensy
  293. // to output to Serial. while still having Raw Hid...
  294. if (usage == 0xffc90004) {
  295. // Lets trim off trailing null characters.
  296. while ((len > 0) && (data[len-1] == 0)) {
  297. len--;
  298. }
  299. if (len) {
  300. Serial.print("RawHid Serial: ");
  301. Serial.write(data, len);
  302. }
  303. } else {
  304. Serial.print("RawHID data: ");
  305. Serial.println(usage, HEX);
  306. while (len) {
  307. uint8_t cb = (len > 16)? 16 : len;
  308. const uint8_t *p = data;
  309. uint8_t i;
  310. for (i = 0; i < cb; i++) {
  311. Serial.printf("%02x ", *p++);
  312. }
  313. Serial.print(": ");
  314. for (i = 0; i < cb; i++) {
  315. Serial.write(((*data >= ' ')&&(*data <= '~'))? *data : '.');
  316. data++;
  317. }
  318. len -= cb;
  319. Serial.println();
  320. }
  321. }
  322. return true;
  323. }