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.

Joystick.ino 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Simple test of USB Host Joystick
  2. //
  3. // This example is in the public domain
  4. #include "USBHost_t36.h"
  5. USBHost myusb;
  6. USBHub hub1(myusb);
  7. USBHIDParser hid1(myusb);
  8. USBHIDParser hid2(myusb);
  9. USBHIDParser hid3(myusb);
  10. USBHIDParser hid4(myusb);
  11. #define COUNT_JOYSTICKS 4
  12. JoystickController joysticks[COUNT_JOYSTICKS](myusb);
  13. int user_axis[64];
  14. uint32_t buttons_prev = 0;
  15. USBDriver *drivers[] = {&hub1, &joysticks[0], &joysticks[1], &joysticks[2], &joysticks[3], &hid1, &hid2, &hid3, &hid4};
  16. #define CNT_DEVICES (sizeof(drivers)/sizeof(drivers[0]))
  17. const char * driver_names[CNT_DEVICES] = {"Hub1", "joystick[0D]", "joystick[1D]", "joystick[2D]", "joystick[3D]", "HID1", "HID2", "HID3", "HID4"};
  18. bool driver_active[CNT_DEVICES] = {false, false, false, false};
  19. // Lets also look at HID Input devices
  20. USBHIDInput *hiddrivers[] = {&joysticks[0], &joysticks[1], &joysticks[2], &joysticks[3]};
  21. #define CNT_HIDDEVICES (sizeof(hiddrivers)/sizeof(hiddrivers[0]))
  22. const char * hid_driver_names[CNT_DEVICES] = {"joystick[0H]", "joystick[1H]", "joystick[2H]", "joystick[3H]"};
  23. bool hid_driver_active[CNT_DEVICES] = {false};
  24. bool show_changed_only = false;
  25. uint8_t joystick_left_trigger_value[COUNT_JOYSTICKS] = {0};
  26. uint8_t joystick_right_trigger_value[COUNT_JOYSTICKS] = {0};
  27. uint64_t joystick_full_notify_mask = (uint64_t) - 1;
  28. //=============================================================================
  29. // Setup
  30. //=============================================================================
  31. void setup()
  32. {
  33. while (!Serial) ; // wait for Arduino Serial Monitor
  34. Serial.println("\n\nUSB Host Joystick Testing");
  35. myusb.begin();
  36. }
  37. //=============================================================================
  38. // loop
  39. //=============================================================================
  40. void loop()
  41. {
  42. myusb.Task();
  43. PrintDeviceListChanges();
  44. if (Serial.available()) {
  45. int ch = Serial.read(); // get the first char.
  46. while (Serial.read() != -1) ;
  47. if ((ch == 'b') || (ch == 'B')) {
  48. Serial.println("Only notify on Basic Axis changes");
  49. for (int joystick_index = 0; joystick_index < COUNT_JOYSTICKS; joystick_index++)
  50. joysticks[joystick_index].axisChangeNotifyMask(0x3ff);
  51. } else if ((ch == 'f') || (ch == 'F')) {
  52. Serial.println("Only notify on Full Axis changes");
  53. for (int joystick_index = 0; joystick_index < COUNT_JOYSTICKS; joystick_index++)
  54. joysticks[joystick_index].axisChangeNotifyMask(joystick_full_notify_mask);
  55. } else {
  56. if (show_changed_only) {
  57. show_changed_only = false;
  58. Serial.println("\n*** Show All fields mode ***");
  59. } else {
  60. show_changed_only = true;
  61. Serial.println("\n*** Show only changed fields mode ***");
  62. }
  63. }
  64. }
  65. for (int joystick_index = 0; joystick_index < COUNT_JOYSTICKS; joystick_index++) {
  66. if (joysticks[joystick_index].available()) {
  67. uint64_t axis_mask = joysticks[joystick_index].axisMask();
  68. uint64_t axis_changed_mask = joysticks[joystick_index].axisChangedMask();
  69. uint32_t buttons = joysticks[joystick_index].getButtons();
  70. Serial.printf("Joystick(%d): buttons = %x", joystick_index, buttons);
  71. //Serial.printf(" AMasks: %x %x:%x", axis_mask, (uint32_t)(user_axis_mask >> 32), (uint32_t)(user_axis_mask & 0xffffffff));
  72. //Serial.printf(" M: %lx %lx", axis_mask, joysticks[joystick_index].axisChangedMask());
  73. if (show_changed_only) {
  74. for (uint8_t i = 0; axis_changed_mask != 0; i++, axis_changed_mask >>= 1) {
  75. if (axis_changed_mask & 1) {
  76. Serial.printf(" %d:%d", i, joysticks[joystick_index].getAxis(i));
  77. }
  78. }
  79. } else {
  80. for (uint8_t i = 0; axis_mask != 0; i++, axis_mask >>= 1) {
  81. if (axis_mask & 1) {
  82. Serial.printf(" %d:%d", i, joysticks[joystick_index].getAxis(i));
  83. }
  84. }
  85. }
  86. uint8_t ltv;
  87. uint8_t rtv;
  88. switch (joysticks[joystick_index].joystickType()) {
  89. default:
  90. break;
  91. case JoystickController::PS4:
  92. ltv = joysticks[joystick_index].getAxis(3);
  93. rtv = joysticks[joystick_index].getAxis(4);
  94. if ((ltv != joystick_left_trigger_value[joystick_index]) || (rtv != joystick_right_trigger_value[joystick_index])) {
  95. joystick_left_trigger_value[joystick_index] = ltv;
  96. joystick_right_trigger_value[joystick_index] = rtv;
  97. joysticks[joystick_index].setRumble(ltv, rtv);
  98. }
  99. break;
  100. case JoystickController::PS3:
  101. ltv = joysticks[joystick_index].getAxis(18);
  102. rtv = joysticks[joystick_index].getAxis(19);
  103. if ((ltv != joystick_left_trigger_value[joystick_index]) || (rtv != joystick_right_trigger_value[joystick_index])) {
  104. joystick_left_trigger_value[joystick_index] = ltv;
  105. joystick_right_trigger_value[joystick_index] = rtv;
  106. joysticks[joystick_index].setRumble(ltv, rtv, 50);
  107. }
  108. break;
  109. case JoystickController::XBOXONE:
  110. case JoystickController::XBOX360:
  111. ltv = joysticks[joystick_index].getAxis(4);
  112. rtv = joysticks[joystick_index].getAxis(5);
  113. if ((ltv != joystick_left_trigger_value[joystick_index]) || (rtv != joystick_right_trigger_value[joystick_index])) {
  114. joystick_left_trigger_value[joystick_index] = ltv;
  115. joystick_right_trigger_value[joystick_index] = rtv;
  116. joysticks[joystick_index].setRumble(ltv, rtv);
  117. Serial.printf(" Set Rumble %d %d", ltv, rtv);
  118. }
  119. break;
  120. }
  121. if (buttons != buttons_prev) {
  122. if (joysticks[joystick_index].joystickType() == JoystickController::PS3) {
  123. joysticks[joystick_index].setLEDs((buttons >> 12) & 0xf); // try to get to TRI/CIR/X/SQuare
  124. } else {
  125. uint8_t lr = (buttons & 1) ? 0xff : 0;
  126. uint8_t lg = (buttons & 2) ? 0xff : 0;
  127. uint8_t lb = (buttons & 4) ? 0xff : 0;
  128. joysticks[joystick_index].setLEDs(lr, lg, lb);
  129. }
  130. buttons_prev = buttons;
  131. }
  132. Serial.println();
  133. joysticks[joystick_index].joystickDataClear();
  134. }
  135. }
  136. }
  137. //=============================================================================
  138. // Show when devices are added or removed
  139. //=============================================================================
  140. void PrintDeviceListChanges() {
  141. for (uint8_t i = 0; i < CNT_DEVICES; i++) {
  142. if (*drivers[i] != driver_active[i]) {
  143. if (driver_active[i]) {
  144. Serial.printf("*** Device %s - disconnected ***\n", driver_names[i]);
  145. driver_active[i] = false;
  146. } else {
  147. Serial.printf("*** Device %s %x:%x - connected ***\n", driver_names[i], drivers[i]->idVendor(), drivers[i]->idProduct());
  148. driver_active[i] = true;
  149. const uint8_t *psz = drivers[i]->manufacturer();
  150. if (psz && *psz) Serial.printf(" manufacturer: %s\n", psz);
  151. psz = drivers[i]->product();
  152. if (psz && *psz) Serial.printf(" product: %s\n", psz);
  153. psz = drivers[i]->serialNumber();
  154. if (psz && *psz) Serial.printf(" Serial: %s\n", psz);
  155. }
  156. }
  157. }
  158. for (uint8_t i = 0; i < CNT_HIDDEVICES; i++) {
  159. if (*hiddrivers[i] != hid_driver_active[i]) {
  160. if (hid_driver_active[i]) {
  161. Serial.printf("*** HID Device %s - disconnected ***\n", hid_driver_names[i]);
  162. hid_driver_active[i] = false;
  163. } else {
  164. Serial.printf("*** HID Device %s %x:%x - connected ***\n", hid_driver_names[i], hiddrivers[i]->idVendor(), hiddrivers[i]->idProduct());
  165. hid_driver_active[i] = true;
  166. const uint8_t *psz = hiddrivers[i]->manufacturer();
  167. if (psz && *psz) Serial.printf(" manufacturer: %s\n", psz);
  168. psz = hiddrivers[i]->product();
  169. if (psz && *psz) Serial.printf(" product: %s\n", psz);
  170. psz = hiddrivers[i]->serialNumber();
  171. if (psz && *psz) Serial.printf(" Serial: %s\n", psz);
  172. }
  173. }
  174. }
  175. }