您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

424 行
20KB

  1. // Simple test of USB Host Mouse/Keyboard
  2. //
  3. // This example is in the public domain
  4. #include "USBHost_t36.h"
  5. USBHost myusb;
  6. USBHub hub1(myusb);
  7. USBHub hub2(myusb);
  8. USBHub hub3(myusb);
  9. USBHub hub4(myusb);
  10. KeyboardController keyboard1(myusb);
  11. KeyboardController keyboard2(myusb);
  12. KeyboardHIDExtrasController hidextras(myusb);
  13. USBHIDParser hid1(myusb);
  14. USBHIDParser hid2(myusb);
  15. USBHIDParser hid3(myusb);
  16. USBHIDParser hid4(myusb);
  17. USBHIDParser hid5(myusb);
  18. MouseController mouse1(myusb);
  19. JoystickController joystick1(myusb);
  20. USBDriver *drivers[] = {&hub1, &hub2, &hub3, &hub4, &keyboard1, &keyboard2, &hid1, &hid2, &hid3, &hid4, &hid5};
  21. #define CNT_DEVICES (sizeof(drivers)/sizeof(drivers[0]))
  22. const char * driver_names[CNT_DEVICES] = {"Hub1","Hub2", "Hub3", "Hub4" "KB1", "KB2", "HID1", "HID2", "HID3", "HID4", "HID5" };
  23. bool driver_active[CNT_DEVICES] = {false, false, false, false};
  24. // Lets also look at HID Input devices
  25. USBHIDInput *hiddrivers[] = {&mouse1, &joystick1};
  26. #define CNT_HIDDEVICES (sizeof(hiddrivers)/sizeof(hiddrivers[0]))
  27. const char * hid_driver_names[CNT_DEVICES] = {"Mouse1","Joystick1"};
  28. bool hid_driver_active[CNT_DEVICES] = {false, false};
  29. void setup()
  30. {
  31. while (!Serial) ; // wait for Arduino Serial Monitor
  32. Serial.println("\n\nUSB Host Testing");
  33. Serial.println(sizeof(USBHub), DEC);
  34. myusb.begin();
  35. keyboard1.attachPress(OnPress);
  36. keyboard2.attachPress(OnPress);
  37. hidextras.attachPress(OnHIDExtrasPress);
  38. hidextras.attachRelease(OnHIDExtrasRelease);
  39. }
  40. void loop()
  41. {
  42. myusb.Task();
  43. for (uint8_t i = 0; i < CNT_DEVICES; i++) {
  44. if (*drivers[i] != driver_active[i]) {
  45. if (driver_active[i]) {
  46. Serial.printf("*** Device %s - disconnected ***\n", driver_names[i]);
  47. driver_active[i] = false;
  48. } else {
  49. Serial.printf("*** Device %s %x:%x - connected ***\n", driver_names[i], drivers[i]->idVendor(), drivers[i]->idProduct());
  50. driver_active[i] = true;
  51. const uint8_t *psz = drivers[i]->manufacturer();
  52. if (psz && *psz) Serial.printf(" manufacturer: %s\n", psz);
  53. psz = drivers[i]->product();
  54. if (psz && *psz) Serial.printf(" product: %s\n", psz);
  55. psz = drivers[i]->serialNumber();
  56. if (psz && *psz) Serial.printf(" Serial: %s\n", psz);
  57. }
  58. }
  59. }
  60. for (uint8_t i = 0; i < CNT_HIDDEVICES; i++) {
  61. if (*hiddrivers[i] != hid_driver_active[i]) {
  62. if (hid_driver_active[i]) {
  63. Serial.printf("*** HID Device %s - disconnected ***\n", hid_driver_names[i]);
  64. hid_driver_active[i] = false;
  65. } else {
  66. Serial.printf("*** HID Device %s %x:%x - connected ***\n", hid_driver_names[i], hiddrivers[i]->idVendor(), hiddrivers[i]->idProduct());
  67. hid_driver_active[i] = true;
  68. const uint8_t *psz = hiddrivers[i]->manufacturer();
  69. if (psz && *psz) Serial.printf(" manufacturer: %s\n", psz);
  70. psz = hiddrivers[i]->product();
  71. if (psz && *psz) Serial.printf(" product: %s\n", psz);
  72. psz = hiddrivers[i]->serialNumber();
  73. if (psz && *psz) Serial.printf(" Serial: %s\n", psz);
  74. }
  75. }
  76. }
  77. if(mouse1.available()) {
  78. Serial.print("Mouse: buttons = ");
  79. Serial.print(mouse1.getButtons());
  80. Serial.print(", mouseX = ");
  81. Serial.print(mouse1.getMouseX());
  82. Serial.print(", mouseY = ");
  83. Serial.print(mouse1.getMouseY());
  84. Serial.print(", wheel = ");
  85. Serial.print(mouse1.getWheel());
  86. Serial.print(", wheelH = ");
  87. Serial.print(mouse1.getWheelH());
  88. Serial.println();
  89. mouse1.mouseDataClear();
  90. }
  91. if (joystick1.available()) {
  92. Serial.print("Joystick: buttons = ");
  93. Serial.print(joystick1.getButtons(), HEX);
  94. Serial.print(", X = ");
  95. Serial.print(joystick1.getAxis(0));
  96. Serial.print(", Y = ");
  97. Serial.print(joystick1.getAxis(1));
  98. Serial.print(", Z = ");
  99. Serial.print(joystick1.getAxis(2));
  100. Serial.print(", Rz = ");
  101. Serial.print(joystick1.getAxis(5));
  102. Serial.print(", Rx = ");
  103. Serial.print(joystick1.getAxis(3));
  104. Serial.print(", Ry = ");
  105. Serial.print(joystick1.getAxis(4));
  106. Serial.print(", Hat = ");
  107. Serial.print(joystick1.getAxis(9));
  108. Serial.println();
  109. joystick1.joystickDataClear();
  110. }
  111. }
  112. void OnPress(int key)
  113. {
  114. Serial.print("key '");
  115. switch (key) {
  116. case KEYD_UP : Serial.print("UP"); break;
  117. case KEYD_DOWN : Serial.print("DN"); break;
  118. case KEYD_LEFT : Serial.print("LEFT"); break;
  119. case KEYD_RIGHT : Serial.print("RIGHT"); break;
  120. case KEYD_INSERT : Serial.print("Ins"); break;
  121. case KEYD_DELETE : Serial.print("Del"); break;
  122. case KEYD_PAGE_UP : Serial.print("PUP"); break;
  123. case KEYD_PAGE_DOWN: Serial.print("PDN"); break;
  124. case KEYD_HOME : Serial.print("HOME"); break;
  125. case KEYD_END : Serial.print("END"); break;
  126. case KEYD_F1 : Serial.print("F1"); break;
  127. case KEYD_F2 : Serial.print("F2"); break;
  128. case KEYD_F3 : Serial.print("F3"); break;
  129. case KEYD_F4 : Serial.print("F4"); break;
  130. case KEYD_F5 : Serial.print("F5"); break;
  131. case KEYD_F6 : Serial.print("F6"); break;
  132. case KEYD_F7 : Serial.print("F7"); break;
  133. case KEYD_F8 : Serial.print("F8"); break;
  134. case KEYD_F9 : Serial.print("F9"); break;
  135. case KEYD_F10 : Serial.print("F10"); break;
  136. case KEYD_F11 : Serial.print("F11"); break;
  137. case KEYD_F12 : Serial.print("F12"); break;
  138. default: Serial.print((char)key); break;
  139. }
  140. Serial.print("' ");
  141. Serial.print(key);
  142. Serial.print(" MOD: ");
  143. if (keyboard1) {
  144. Serial.print(keyboard1.getModifiers(), HEX);
  145. Serial.print(" OEM: ");
  146. Serial.print(keyboard1.getOemKey(), HEX);
  147. Serial.print(" LEDS: ");
  148. Serial.println(keyboard1.LEDS(), HEX);
  149. } else {
  150. Serial.print(keyboard2.getModifiers(), HEX);
  151. Serial.print(" OEM: ");
  152. Serial.print(keyboard2.getOemKey(), HEX);
  153. Serial.print(" LEDS: ");
  154. Serial.println(keyboard2.LEDS(), HEX);
  155. }
  156. //Serial.print("key ");
  157. //Serial.print((char)keyboard1.getKey());
  158. //Serial.print(" ");
  159. //Serial.print((char)keyboard2.getKey());
  160. //Serial.println();
  161. }
  162. void OnHIDExtrasPress(uint32_t top, uint16_t key)
  163. {
  164. Serial.print("HID (");
  165. Serial.print(top, HEX);
  166. Serial.print(") key press:");
  167. Serial.print(key, HEX);
  168. if (top == 0xc0000) {
  169. switch (key) {
  170. case 0x20 : Serial.print(" - +10"); break;
  171. case 0x21 : Serial.print(" - +100"); break;
  172. case 0x22 : Serial.print(" - AM/PM"); break;
  173. case 0x30 : Serial.print(" - Power"); break;
  174. case 0x31 : Serial.print(" - Reset"); break;
  175. case 0x32 : Serial.print(" - Sleep"); break;
  176. case 0x33 : Serial.print(" - Sleep After"); break;
  177. case 0x34 : Serial.print(" - Sleep Mode"); break;
  178. case 0x35 : Serial.print(" - Illumination"); break;
  179. case 0x36 : Serial.print(" - Function Buttons"); break;
  180. case 0x40 : Serial.print(" - Menu"); break;
  181. case 0x41 : Serial.print(" - Menu Pick"); break;
  182. case 0x42 : Serial.print(" - Menu Up"); break;
  183. case 0x43 : Serial.print(" - Menu Down"); break;
  184. case 0x44 : Serial.print(" - Menu Left"); break;
  185. case 0x45 : Serial.print(" - Menu Right"); break;
  186. case 0x46 : Serial.print(" - Menu Escape"); break;
  187. case 0x47 : Serial.print(" - Menu Value Increase"); break;
  188. case 0x48 : Serial.print(" - Menu Value Decrease"); break;
  189. case 0x60 : Serial.print(" - Data On Screen"); break;
  190. case 0x61 : Serial.print(" - Closed Caption"); break;
  191. case 0x62 : Serial.print(" - Closed Caption Select"); break;
  192. case 0x63 : Serial.print(" - VCR/TV"); break;
  193. case 0x64 : Serial.print(" - Broadcast Mode"); break;
  194. case 0x65 : Serial.print(" - Snapshot"); break;
  195. case 0x66 : Serial.print(" - Still"); break;
  196. case 0x80 : Serial.print(" - Selection"); break;
  197. case 0x81 : Serial.print(" - Assign Selection"); break;
  198. case 0x82 : Serial.print(" - Mode Step"); break;
  199. case 0x83 : Serial.print(" - Recall Last"); break;
  200. case 0x84 : Serial.print(" - Enter Channel"); break;
  201. case 0x85 : Serial.print(" - Order Movie"); break;
  202. case 0x86 : Serial.print(" - Channel"); break;
  203. case 0x87 : Serial.print(" - Media Selection"); break;
  204. case 0x88 : Serial.print(" - Media Select Computer"); break;
  205. case 0x89 : Serial.print(" - Media Select TV"); break;
  206. case 0x8A : Serial.print(" - Media Select WWW"); break;
  207. case 0x8B : Serial.print(" - Media Select DVD"); break;
  208. case 0x8C : Serial.print(" - Media Select Telephone"); break;
  209. case 0x8D : Serial.print(" - Media Select Program Guide"); break;
  210. case 0x8E : Serial.print(" - Media Select Video Phone"); break;
  211. case 0x8F : Serial.print(" - Media Select Games"); break;
  212. case 0x90 : Serial.print(" - Media Select Messages"); break;
  213. case 0x91 : Serial.print(" - Media Select CD"); break;
  214. case 0x92 : Serial.print(" - Media Select VCR"); break;
  215. case 0x93 : Serial.print(" - Media Select Tuner"); break;
  216. case 0x94 : Serial.print(" - Quit"); break;
  217. case 0x95 : Serial.print(" - Help"); break;
  218. case 0x96 : Serial.print(" - Media Select Tape"); break;
  219. case 0x97 : Serial.print(" - Media Select Cable"); break;
  220. case 0x98 : Serial.print(" - Media Select Satellite"); break;
  221. case 0x99 : Serial.print(" - Media Select Security"); break;
  222. case 0x9A : Serial.print(" - Media Select Home"); break;
  223. case 0x9B : Serial.print(" - Media Select Call"); break;
  224. case 0x9C : Serial.print(" - Channel Increment"); break;
  225. case 0x9D : Serial.print(" - Channel Decrement"); break;
  226. case 0x9E : Serial.print(" - Media Select SAP"); break;
  227. case 0xA0 : Serial.print(" - VCR Plus"); break;
  228. case 0xA1 : Serial.print(" - Once"); break;
  229. case 0xA2 : Serial.print(" - Daily"); break;
  230. case 0xA3 : Serial.print(" - Weekly"); break;
  231. case 0xA4 : Serial.print(" - Monthly"); break;
  232. case 0xB0 : Serial.print(" - Play"); break;
  233. case 0xB1 : Serial.print(" - Pause"); break;
  234. case 0xB2 : Serial.print(" - Record"); break;
  235. case 0xB3 : Serial.print(" - Fast Forward"); break;
  236. case 0xB4 : Serial.print(" - Rewind"); break;
  237. case 0xB5 : Serial.print(" - Scan Next Track"); break;
  238. case 0xB6 : Serial.print(" - Scan Previous Track"); break;
  239. case 0xB7 : Serial.print(" - Stop"); break;
  240. case 0xB8 : Serial.print(" - Eject"); break;
  241. case 0xB9 : Serial.print(" - Random Play"); break;
  242. case 0xBA : Serial.print(" - Select DisC"); break;
  243. case 0xBB : Serial.print(" - Enter Disc"); break;
  244. case 0xBC : Serial.print(" - Repeat"); break;
  245. case 0xBD : Serial.print(" - Tracking"); break;
  246. case 0xBE : Serial.print(" - Track Normal"); break;
  247. case 0xBF : Serial.print(" - Slow Tracking"); break;
  248. case 0xC0 : Serial.print(" - Frame Forward"); break;
  249. case 0xC1 : Serial.print(" - Frame Back"); break;
  250. case 0xC2 : Serial.print(" - Mark"); break;
  251. case 0xC3 : Serial.print(" - Clear Mark"); break;
  252. case 0xC4 : Serial.print(" - Repeat From Mark"); break;
  253. case 0xC5 : Serial.print(" - Return To Mark"); break;
  254. case 0xC6 : Serial.print(" - Search Mark Forward"); break;
  255. case 0xC7 : Serial.print(" - Search Mark Backwards"); break;
  256. case 0xC8 : Serial.print(" - Counter Reset"); break;
  257. case 0xC9 : Serial.print(" - Show Counter"); break;
  258. case 0xCA : Serial.print(" - Tracking Increment"); break;
  259. case 0xCB : Serial.print(" - Tracking Decrement"); break;
  260. case 0xCD : Serial.print(" - Pause/Continue"); break;
  261. case 0xE0 : Serial.print(" - Volume"); break;
  262. case 0xE1 : Serial.print(" - Balance"); break;
  263. case 0xE2 : Serial.print(" - Mute"); break;
  264. case 0xE3 : Serial.print(" - Bass"); break;
  265. case 0xE4 : Serial.print(" - Treble"); break;
  266. case 0xE5 : Serial.print(" - Bass Boost"); break;
  267. case 0xE6 : Serial.print(" - Surround Mode"); break;
  268. case 0xE7 : Serial.print(" - Loudness"); break;
  269. case 0xE8 : Serial.print(" - MPX"); break;
  270. case 0xE9 : Serial.print(" - Volume Up"); break;
  271. case 0xEA : Serial.print(" - Volume Down"); break;
  272. case 0xF0 : Serial.print(" - Speed Select"); break;
  273. case 0xF1 : Serial.print(" - Playback Speed"); break;
  274. case 0xF2 : Serial.print(" - Standard Play"); break;
  275. case 0xF3 : Serial.print(" - Long Play"); break;
  276. case 0xF4 : Serial.print(" - Extended Play"); break;
  277. case 0xF5 : Serial.print(" - Slow"); break;
  278. case 0x100: Serial.print(" - Fan Enable"); break;
  279. case 0x101: Serial.print(" - Fan Speed"); break;
  280. case 0x102: Serial.print(" - Light"); break;
  281. case 0x103: Serial.print(" - Light Illumination Level"); break;
  282. case 0x104: Serial.print(" - Climate Control Enable"); break;
  283. case 0x105: Serial.print(" - Room Temperature"); break;
  284. case 0x106: Serial.print(" - Security Enable"); break;
  285. case 0x107: Serial.print(" - Fire Alarm"); break;
  286. case 0x108: Serial.print(" - Police Alarm"); break;
  287. case 0x150: Serial.print(" - Balance Right"); break;
  288. case 0x151: Serial.print(" - Balance Left"); break;
  289. case 0x152: Serial.print(" - Bass Increment"); break;
  290. case 0x153: Serial.print(" - Bass Decrement"); break;
  291. case 0x154: Serial.print(" - Treble Increment"); break;
  292. case 0x155: Serial.print(" - Treble Decrement"); break;
  293. case 0x160: Serial.print(" - Speaker System"); break;
  294. case 0x161: Serial.print(" - Channel Left"); break;
  295. case 0x162: Serial.print(" - Channel Right"); break;
  296. case 0x163: Serial.print(" - Channel Center"); break;
  297. case 0x164: Serial.print(" - Channel Front"); break;
  298. case 0x165: Serial.print(" - Channel Center Front"); break;
  299. case 0x166: Serial.print(" - Channel Side"); break;
  300. case 0x167: Serial.print(" - Channel Surround"); break;
  301. case 0x168: Serial.print(" - Channel Low Frequency Enhancement"); break;
  302. case 0x169: Serial.print(" - Channel Top"); break;
  303. case 0x16A: Serial.print(" - Channel Unknown"); break;
  304. case 0x170: Serial.print(" - Sub-channel"); break;
  305. case 0x171: Serial.print(" - Sub-channel Increment"); break;
  306. case 0x172: Serial.print(" - Sub-channel Decrement"); break;
  307. case 0x173: Serial.print(" - Alternate Audio Increment"); break;
  308. case 0x174: Serial.print(" - Alternate Audio Decrement"); break;
  309. case 0x180: Serial.print(" - Application Launch Buttons"); break;
  310. case 0x181: Serial.print(" - AL Launch Button Configuration Tool"); break;
  311. case 0x182: Serial.print(" - AL Programmable Button Configuration"); break;
  312. case 0x183: Serial.print(" - AL Consumer Control Configuration"); break;
  313. case 0x184: Serial.print(" - AL Word Processor"); break;
  314. case 0x185: Serial.print(" - AL Text Editor"); break;
  315. case 0x186: Serial.print(" - AL Spreadsheet"); break;
  316. case 0x187: Serial.print(" - AL Graphics Editor"); break;
  317. case 0x188: Serial.print(" - AL Presentation App"); break;
  318. case 0x189: Serial.print(" - AL Database App"); break;
  319. case 0x18A: Serial.print(" - AL Email Reader"); break;
  320. case 0x18B: Serial.print(" - AL Newsreader"); break;
  321. case 0x18C: Serial.print(" - AL Voicemail"); break;
  322. case 0x18D: Serial.print(" - AL Contacts/Address Book"); break;
  323. case 0x18E: Serial.print(" - AL Calendar/Schedule"); break;
  324. case 0x18F: Serial.print(" - AL Task/Project Manager"); break;
  325. case 0x190: Serial.print(" - AL Log/Journal/Timecard"); break;
  326. case 0x191: Serial.print(" - AL Checkbook/Finance"); break;
  327. case 0x192: Serial.print(" - AL Calculator"); break;
  328. case 0x193: Serial.print(" - AL A/V Capture/Playback"); break;
  329. case 0x194: Serial.print(" - AL Local Machine Browser"); break;
  330. case 0x195: Serial.print(" - AL LAN/WAN Browser"); break;
  331. case 0x196: Serial.print(" - AL Internet Browser"); break;
  332. case 0x197: Serial.print(" - AL Remote Networking/ISP Connect"); break;
  333. case 0x198: Serial.print(" - AL Network Conference"); break;
  334. case 0x199: Serial.print(" - AL Network Chat"); break;
  335. case 0x19A: Serial.print(" - AL Telephony/Dialer"); break;
  336. case 0x19B: Serial.print(" - AL Logon"); break;
  337. case 0x19C: Serial.print(" - AL Logoff"); break;
  338. case 0x19D: Serial.print(" - AL Logon/Logoff"); break;
  339. case 0x19E: Serial.print(" - AL Terminal Lock/Screensaver"); break;
  340. case 0x19F: Serial.print(" - AL Control Panel"); break;
  341. case 0x1A0: Serial.print(" - AL Command Line Processor/Run"); break;
  342. case 0x1A1: Serial.print(" - AL Process/Task Manager"); break;
  343. case 0x1A2: Serial.print(" - AL Select Tast/Application"); break;
  344. case 0x1A3: Serial.print(" - AL Next Task/Application"); break;
  345. case 0x1A4: Serial.print(" - AL Previous Task/Application"); break;
  346. case 0x1A5: Serial.print(" - AL Preemptive Halt Task/Application"); break;
  347. case 0x200: Serial.print(" - Generic GUI Application Controls"); break;
  348. case 0x201: Serial.print(" - AC New"); break;
  349. case 0x202: Serial.print(" - AC Open"); break;
  350. case 0x203: Serial.print(" - AC Close"); break;
  351. case 0x204: Serial.print(" - AC Exit"); break;
  352. case 0x205: Serial.print(" - AC Maximize"); break;
  353. case 0x206: Serial.print(" - AC Minimize"); break;
  354. case 0x207: Serial.print(" - AC Save"); break;
  355. case 0x208: Serial.print(" - AC Print"); break;
  356. case 0x209: Serial.print(" - AC Properties"); break;
  357. case 0x21A: Serial.print(" - AC Undo"); break;
  358. case 0x21B: Serial.print(" - AC Copy"); break;
  359. case 0x21C: Serial.print(" - AC Cut"); break;
  360. case 0x21D: Serial.print(" - AC Paste"); break;
  361. case 0x21E: Serial.print(" - AC Select All"); break;
  362. case 0x21F: Serial.print(" - AC Find"); break;
  363. case 0x220: Serial.print(" - AC Find and Replace"); break;
  364. case 0x221: Serial.print(" - AC Search"); break;
  365. case 0x222: Serial.print(" - AC Go To"); break;
  366. case 0x223: Serial.print(" - AC Home"); break;
  367. case 0x224: Serial.print(" - AC Back"); break;
  368. case 0x225: Serial.print(" - AC Forward"); break;
  369. case 0x226: Serial.print(" - AC Stop"); break;
  370. case 0x227: Serial.print(" - AC Refresh"); break;
  371. case 0x228: Serial.print(" - AC Previous Link"); break;
  372. case 0x229: Serial.print(" - AC Next Link"); break;
  373. case 0x22A: Serial.print(" - AC Bookmarks"); break;
  374. case 0x22B: Serial.print(" - AC History"); break;
  375. case 0x22C: Serial.print(" - AC Subscriptions"); break;
  376. case 0x22D: Serial.print(" - AC Zoom In"); break;
  377. case 0x22E: Serial.print(" - AC Zoom Out"); break;
  378. case 0x22F: Serial.print(" - AC Zoom"); break;
  379. case 0x230: Serial.print(" - AC Full Screen View"); break;
  380. case 0x231: Serial.print(" - AC Normal View"); break;
  381. case 0x232: Serial.print(" - AC View Toggle"); break;
  382. case 0x233: Serial.print(" - AC Scroll Up"); break;
  383. case 0x234: Serial.print(" - AC Scroll Down"); break;
  384. case 0x235: Serial.print(" - AC Scroll"); break;
  385. case 0x236: Serial.print(" - AC Pan Left"); break;
  386. case 0x237: Serial.print(" - AC Pan Right"); break;
  387. case 0x238: Serial.print(" - AC Pan"); break;
  388. case 0x239: Serial.print(" - AC New Window"); break;
  389. case 0x23A: Serial.print(" - AC Tile Horizontally"); break;
  390. case 0x23B: Serial.print(" - AC Tile Vertically"); break;
  391. case 0x23C: Serial.print(" - AC Format"); break;
  392. }
  393. }
  394. Serial.println();
  395. }
  396. void OnHIDExtrasRelease(uint32_t top, uint16_t key)
  397. {
  398. Serial.print("HID (");
  399. Serial.print(top, HEX);
  400. Serial.print(") key release:");
  401. Serial.println(key, HEX);
  402. }