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

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