Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

148 lines
5.6KB

  1. // HIDDeviceInfo - Simple HID device example
  2. //
  3. // This Simple test sketch is setup to print out HID information about a device
  4. // The other two tabs are a simple C++ subclass of the USBHIDInput class that is part
  5. // of the USBHost_t36 library.
  6. //
  7. // This subclass simply tries to connect to each different HID object and
  8. // the only thing it does is to try to print out all of the data it receives
  9. // in a reasonable way.
  10. //
  11. // The idea is that with the output from this sketch we can hopefully add support
  12. // for some additional devices that are not currently supported or allows you to
  13. // develop your own.
  14. //
  15. // You can use Serial Input to control how much data is displayed per each HID packet
  16. // received by the sketch.
  17. //
  18. // By Default it displays both the RAW (Hex dump) of the data received, as well
  19. // as the data as the HID interpreter walks through the data into the individual
  20. // fields, which we then print out.
  21. //
  22. // There are options to turn off some of this output, also an option that you can
  23. // toggle on or off (C) to only try to show the changed fields.
  24. //
  25. // This example is in the public domain
  26. #include <USBHost_t36.h>
  27. #include "HIDDumper.h"
  28. #include "USBDeviceInfo.h"
  29. USBHost myusb;
  30. USBHub hub1(myusb);
  31. USBHub hub2(myusb);
  32. USBDeviceInfo dinfo(myusb); // will never claim anything...
  33. USBHIDParser hid1(myusb);
  34. USBHIDParser hid2(myusb);
  35. USBHIDParser hid3(myusb);
  36. USBHIDParser hid4(myusb);
  37. USBHIDParser hid5(myusb);
  38. HIDDumpController hdc1(myusb);
  39. HIDDumpController hdc2(myusb);
  40. HIDDumpController hdc3(myusb);
  41. HIDDumpController hdc4(myusb);
  42. HIDDumpController hdc5(myusb);
  43. USBDriver *drivers[] = {&hub1, &hub2, &hid1, &hid2, &hid3, &hid4, &hid5};
  44. #define CNT_DEVICES (sizeof(drivers)/sizeof(drivers[0]))
  45. const char * driver_names[CNT_DEVICES] = {"Hub1", "Hub2", "HID1" , "HID2", "HID3", "HID4", "HID5"};
  46. bool driver_active[CNT_DEVICES] = {false, false, false, false};
  47. // Lets also look at HID Input devices
  48. USBHIDInput *hiddrivers[] = {&hdc1, &hdc2, &hdc3, &hdc4, &hdc5};
  49. #define CNT_HIDDEVICES (sizeof(hiddrivers)/sizeof(hiddrivers[0]))
  50. const char * hid_driver_names[CNT_DEVICES] = {"hdc1", "hdc2", "hdc3", "hdc4", "hdc5"};
  51. bool hid_driver_active[CNT_DEVICES] = {false, false};
  52. bool show_changed_only = false;
  53. void setup()
  54. {
  55. Serial1.begin(2000000);
  56. while (!Serial) ; // wait for Arduino Serial Monitor
  57. Serial.println("\n\nUSB HID Device Info Program");
  58. Serial.println("\nThis Sketch shows information about plugged in HID devices");
  59. Serial.println("\n*** You can control the output by simple character input to Serial ***");
  60. Serial.println("R - Turns on or off showing the raw data");
  61. Serial.println("C - Toggles showing changed data only on or off");
  62. Serial.println("<anything else> - toggles showing the Hid formatted breakdown of the data\n");
  63. myusb.begin();
  64. }
  65. void loop()
  66. {
  67. myusb.Task();
  68. if (Serial.available()) {
  69. int ch = Serial.read(); // get the first char.
  70. while (Serial.read() != -1) ;
  71. if (ch == 'r' || (ch == 'R')) {
  72. if (HIDDumpController::show_raw_data) {
  73. HIDDumpController::show_raw_data = false;
  74. HIDDumpController::show_formated_data = true;
  75. Serial.println("\n*** Turn off RAW output formatted data is on ***\n");
  76. } else {
  77. HIDDumpController::show_raw_data = true;
  78. Serial.println("\n*** Turn on RAW output ***\n");
  79. }
  80. } else if (ch == 'C' || (ch == 'c')) {
  81. if (HIDDumpController::changed_data_only) {
  82. HIDDumpController::changed_data_only = false;
  83. Serial.println("***\n Now Showing all data ***\n");
  84. } else {
  85. HIDDumpController::changed_data_only = true;
  86. Serial.println("***\n Now Showing changed data only ***\n");
  87. }
  88. } else {
  89. if (HIDDumpController::show_formated_data) {
  90. HIDDumpController::show_formated_data = false;
  91. HIDDumpController::show_raw_data = true; // At least make sure raw raw data is output
  92. Serial.println("\n*** Turn off formatted output formated HID data is on ***\n");
  93. } else {
  94. HIDDumpController::show_formated_data = true;
  95. Serial.println("\n*** Turn on formated output ***\n");
  96. }
  97. }
  98. }
  99. for (uint8_t i = 0; i < CNT_DEVICES; i++) {
  100. if (*drivers[i] != driver_active[i]) {
  101. if (driver_active[i]) {
  102. Serial.printf("*** Device % s - disconnected ***\n", driver_names[i]);
  103. driver_active[i] = false;
  104. } else {
  105. Serial.printf("*** Device % s % x: % x - connected ***\n", driver_names[i], drivers[i]->idVendor(), drivers[i]->idProduct());
  106. driver_active[i] = true;
  107. const uint8_t *psz = drivers[i]->manufacturer();
  108. if (psz && *psz) Serial.printf(" manufacturer: % s\n", psz);
  109. psz = drivers[i]->product();
  110. if (psz && *psz) Serial.printf(" product: % s\n", psz);
  111. psz = drivers[i]->serialNumber();
  112. if (psz && *psz) Serial.printf(" Serial: % s\n", psz);
  113. }
  114. }
  115. }
  116. for (uint8_t i = 0; i < CNT_HIDDEVICES; i++) {
  117. if (*hiddrivers[i] != hid_driver_active[i]) {
  118. if (hid_driver_active[i]) {
  119. Serial.printf("*** HID Device % s - disconnected ***\n", hid_driver_names[i]);
  120. hid_driver_active[i] = false;
  121. } else {
  122. Serial.printf("*** HID Device % s % x: % x - connected ***\n", hid_driver_names[i], hiddrivers[i]->idVendor(), hiddrivers[i]->idProduct());
  123. hid_driver_active[i] = true;
  124. const uint8_t *psz = hiddrivers[i]->manufacturer();
  125. if (psz && *psz) Serial.printf(" manufacturer: % s\n", psz);
  126. psz = hiddrivers[i]->product();
  127. if (psz && *psz) Serial.printf(" product: % s\n", psz);
  128. psz = hiddrivers[i]->serialNumber();
  129. if (psz && *psz) Serial.printf(" Serial: % s\n", psz);
  130. }
  131. }
  132. }
  133. }