Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

329 lines
10KB

  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. uint16_t buttons_prev = 0;
  19. uint16_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. uint8_t joystick_left_trigger_value = 0;
  34. uint8_t joystick_right_trigger_value = 0;
  35. uint64_t joystick_full_notify_mask = (uint64_t)-1;
  36. int psAxis[64];
  37. bool first_joystick_message = true;
  38. void setup()
  39. {
  40. /* Serial4.begin( 1843200 );
  41. debBegin_tt( &Serial4, LED_BUILTIN, 12);
  42. debTraceShow_tt( -1, "", "", "" );
  43. Serial4.println("\n" __FILE__ " " __DATE__ " " __TIME__);
  44. Serial4.println("\n********\n T4 connected Serial1 *******\n");
  45. Serial4.println("\n" __FILE__ " " __DATE__ " " __TIME__);
  46. Serial4.println("\n********\n T4 connected Serial4 *******\n");
  47. */
  48. Serial1.begin(2000000);
  49. while (!Serial) ; // wait for Arduino Serial Monitor
  50. //debTraceShow_tt( -2, "", "", "" );
  51. //Serial4.println("\n" __FILE__ " " __DATE__ " " __TIME__);
  52. //Serial1.begin( 1843200 );
  53. Serial.println("\n\nUSB Host Testing");
  54. Serial.println(sizeof(USBHub), DEC);
  55. myusb.begin();
  56. delay(2000);
  57. rawhid1.attachReceive(OnReceiveHidData);
  58. rawhid2.attachReceive(OnReceiveHidData);
  59. }
  60. void loop()
  61. {
  62. myusb.Task();
  63. if (Serial.available()) {
  64. int ch = Serial.read(); // get the first char.
  65. while (Serial.read() != -1) ;
  66. if ((ch == 'b') || (ch == 'B')) {
  67. Serial.println("Only notify on Basic Axis changes");
  68. joystick1.axisChangeNotifyMask(0x3ff);
  69. } else if ((ch == 'f') || (ch == 'F')) {
  70. Serial.println("Only notify on Full Axis changes");
  71. joystick1.axisChangeNotifyMask(joystick_full_notify_mask);
  72. } else {
  73. if (show_changed_only) {
  74. show_changed_only = false;
  75. Serial.println("\n*** Show All fields mode ***");
  76. } else {
  77. show_changed_only = true;
  78. Serial.println("\n*** Show only changed fields mode ***");
  79. }
  80. }
  81. }
  82. for (uint8_t i = 0; i < CNT_DEVICES; i++) {
  83. if (*drivers[i] != driver_active[i]) {
  84. if (driver_active[i]) {
  85. Serial.printf("*** Device %s - disconnected ***\n", driver_names[i]);
  86. driver_active[i] = false;
  87. } else {
  88. Serial.printf("*** Device %s %x:%x - connected ***\n", driver_names[i], drivers[i]->idVendor(), drivers[i]->idProduct());
  89. driver_active[i] = true;
  90. const uint8_t *psz = drivers[i]->manufacturer();
  91. if (psz && *psz) Serial.printf(" manufacturer: %s\n", psz);
  92. psz = drivers[i]->product();
  93. if (psz && *psz) Serial.printf(" product: %s\n", psz);
  94. psz = drivers[i]->serialNumber();
  95. if (psz && *psz) Serial.printf(" Serial: %s\n", psz);
  96. }
  97. }
  98. }
  99. for (uint8_t i = 0; i < CNT_HIDDEVICES; i++) {
  100. if (*hiddrivers[i] != hid_driver_active[i]) {
  101. if (hid_driver_active[i]) {
  102. Serial.printf("*** HID Device %s - disconnected ***\n", hid_driver_names[i]);
  103. hid_driver_active[i] = false;
  104. } else {
  105. Serial.printf("*** HID Device %s %x:%x - connected ***\n", hid_driver_names[i], hiddrivers[i]->idVendor(), hiddrivers[i]->idProduct());
  106. hid_driver_active[i] = true;
  107. const uint8_t *psz = hiddrivers[i]->manufacturer();
  108. if (psz && *psz) Serial.printf(" manufacturer: %s\n", psz);
  109. psz = hiddrivers[i]->product();
  110. if (psz && *psz) Serial.printf(" product: %s\n", psz);
  111. psz = hiddrivers[i]->serialNumber();
  112. if (psz && *psz) Serial.printf(" Serial: %s\n", psz);
  113. }
  114. }
  115. }
  116. if (joystick1.available()) {
  117. if (first_joystick_message) {
  118. Serial.printf("*** First Joystick message %x:%x ***\n",
  119. joystick1.idVendor(), joystick1.idProduct());
  120. first_joystick_message = false;
  121. const uint8_t *psz = joystick1.manufacturer();
  122. if (psz && *psz) Serial.printf(" manufacturer: %s\n", psz);
  123. psz = joystick1.product();
  124. if (psz && *psz) Serial.printf(" product: %s\n", psz);
  125. psz =joystick1.serialNumber();
  126. if (psz && *psz) Serial.printf(" Serial: %s\n", psz);
  127. }
  128. for (uint8_t i = 0; i<64; i++) {
  129. psAxis[i] = joystick1.getAxis(i);
  130. }
  131. switch (joystick1.joystickType) {
  132. case JoystickController::UNKNOWN:
  133. case JoystickController::PS4:
  134. displayPS4Data();
  135. break;
  136. case JoystickController::PS3:
  137. displayPS3Data();
  138. break;
  139. case JoystickController::XBOXONE:
  140. case JoystickController::XBOX360:;
  141. displayRawData();
  142. break;
  143. }
  144. //for (uint8_t i = 0; i < 24; i++) {
  145. // Serial.printf(" %d:%d", i, psAxis[i]);
  146. //}
  147. //Serial.println();
  148. joystick1.joystickDataClear();
  149. }
  150. // See if we have some RAW data
  151. if (rawhid1) {
  152. int ch;
  153. uint8_t buffer[64];
  154. uint8_t count_chars = 0;
  155. memset(buffer, 0, sizeof(buffer));
  156. if (Serial.available()) {
  157. while (((ch = Serial.read()) != -1) && (count_chars < sizeof(buffer))) {
  158. buffer[count_chars++] = ch;
  159. }
  160. rawhid1.sendPacket(buffer);
  161. }
  162. }
  163. }
  164. void displayPS4Data()
  165. {
  166. Serial.printf("LX: %d, LY: %d, RX: %d, RY: %d \r\n", psAxis[1], psAxis[2], psAxis[3], psAxis[4]);
  167. Serial.printf("L-Trig: %d, R-Trig: %d, Trig-Button: %d \r\n", psAxis[8], psAxis[9], psAxis[6]);
  168. Serial.printf("Buttons: %d, PS: %d\r\n", psAxis[5], psAxis[7]);
  169. Serial.printf("Arrows: %d\r\n", psAxis[0]);
  170. Serial.printf("Battery level percentage: %2f.0 \r\n", (((float) psAxis[12])/255.0f)*100.0f);
  171. Serial.println();
  172. uint8_t ltv;
  173. uint8_t rtv;
  174. ltv = psAxis[8];
  175. rtv = psAxis[9];
  176. if ((ltv != joystick_left_trigger_value) || (rtv != joystick_right_trigger_value)) {
  177. joystick_left_trigger_value = ltv;
  178. joystick_right_trigger_value = rtv;
  179. Serial.printf("Rumbling: %d, %d\r\n", ltv, rtv);
  180. joystick1.setRumble(ltv, rtv);
  181. }
  182. /* Arrow Buttons (psAxis[0]):
  183. * 0x08 is released,
  184. * 0=N, 1=NE, 2=E, 3=SE, 4=S,
  185. * 5=SW, 6=W, 7=NW)
  186. */
  187. if (psAxis[5] != buttons_prev) {
  188. uint8_t lr = (psAxis[5] & 1) ? 0xff : 0; //Srq
  189. uint8_t lg = (psAxis[5] & 4) ? 0xff : 0; //Cir
  190. uint8_t lb = (psAxis[5] & 8) ? 0xff : 0; //Tri
  191. //Cross = 2
  192. Serial.print(psAxis[5]); Serial.print(", ");
  193. Serial.print(lr); Serial.print(", ");
  194. Serial.print(lg); Serial.print(", ");
  195. Serial.println(lb);
  196. joystick1.setLEDs(lr, lg, lb);
  197. buttons_prev =psAxis[5];
  198. }
  199. }
  200. void displayPS3Data()
  201. {
  202. buttons = psAxis[2] | ((uint16_t)psAxis[3] << 8);
  203. // Use L3 (Left joystick button) to toggle Show Raw or not...
  204. if ((buttons & 0x02) && !(buttons_prev & 0x02)) show_raw_data = !show_raw_data;
  205. if (show_raw_data) {
  206. displayRawData();
  207. } else {
  208. Serial.printf("LX: %d, LY: %d, RX: %d, RY: %d \r\n", psAxis[6], psAxis[7], psAxis[8], psAxis[9]);
  209. Serial.printf("L-Trig: %d, R-Trig: %d\r\n", psAxis[18], psAxis[19]);
  210. Serial.printf("Buttons: %x\r\n", buttons);
  211. }
  212. uint8_t ltv;
  213. uint8_t rtv;
  214. ltv = psAxis[18];
  215. rtv = psAxis[19];
  216. if ((ltv != joystick_left_trigger_value) || (rtv != joystick_right_trigger_value)) {
  217. joystick_left_trigger_value = ltv;
  218. joystick_right_trigger_value = rtv;
  219. Serial.printf("Rumbling: %d, %d\r\n", ltv, rtv);
  220. joystick1.setRumble(ltv, rtv);
  221. }
  222. if (buttons != buttons_prev) {
  223. uint8_t leds = 0;
  224. if (buttons & 0x8000) leds = 1; //Srq
  225. if (buttons & 0x2000) leds = 2; //Cir
  226. if (buttons & 0x1000) leds = 3; //Tri
  227. //Cross = 2
  228. joystick1.setLEDs(leds);
  229. buttons_prev = buttons;
  230. }
  231. }
  232. void displayRawData() {
  233. uint64_t axis_mask = joystick1.axisMask();
  234. uint64_t changed_mask = joystick1.axisChangedMask();
  235. if (!changed_mask) return;
  236. Serial.printf("%lx %lx:", axis_mask, changed_mask);
  237. for (uint16_t index=0; axis_mask; index++) {
  238. Serial.printf("%02x ", psAxis[index]);
  239. axis_mask >>= 1;
  240. }
  241. Serial.println();
  242. //for (uint8_t i = 0; i < 24; i++) {
  243. // Serial.printf(" %d:%d", i, psAxis[i]);
  244. //}
  245. //Serial.println();
  246. }
  247. bool OnReceiveHidData(uint32_t usage, const uint8_t *data, uint32_t len) {
  248. // Called for maybe both HIDS for rawhid basic test. One is for the Teensy
  249. // to output to Serial. while still having Raw Hid...
  250. if (usage == 0xffc90004) {
  251. // Lets trim off trailing null characters.
  252. while ((len > 0) && (data[len-1] == 0)) {
  253. len--;
  254. }
  255. if (len) {
  256. Serial.print("RawHid Serial: ");
  257. Serial.write(data, len);
  258. }
  259. } else {
  260. Serial.print("RawHID data: ");
  261. Serial.println(usage, HEX);
  262. while (len) {
  263. uint8_t cb = (len > 16)? 16 : len;
  264. const uint8_t *p = data;
  265. uint8_t i;
  266. for (i = 0; i < cb; i++) {
  267. Serial.printf("%02x ", *p++);
  268. }
  269. Serial.print(": ");
  270. for (i = 0; i < cb; i++) {
  271. Serial.write(((*data >= ' ')&&(*data <= '~'))? *data : '.');
  272. data++;
  273. }
  274. len -= cb;
  275. Serial.println();
  276. }
  277. }
  278. return true;
  279. }