You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

254 lines
8.1KB

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