Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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