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

355 行
10KB

  1. /* USB EHCI Host for Teensy 3.6
  2. * Copyright 2017 Paul Stoffregen (paul@pjrc.com)
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <Arduino.h>
  24. #include "USBHost_t36.h" // Read this header first for key info
  25. #include "keylayouts.h" // from Teensyduino core library
  26. typedef struct {
  27. KEYCODE_TYPE code;
  28. uint8_t ascii;
  29. } keycode_extra_t;
  30. typedef struct {
  31. KEYCODE_TYPE code;
  32. KEYCODE_TYPE codeNumlockOff;
  33. uint8_t charNumlockOn; // We will assume when num lock is on we have all characters...
  34. } keycode_numlock_t;
  35. #ifdef M
  36. #undef M
  37. #endif
  38. #define M(n) ((n) & KEYCODE_MASK)
  39. keycode_extra_t keycode_extras[] = {
  40. {M(KEY_ENTER), '\n'},
  41. {M(KEY_ESC), 0x1b},
  42. {M(KEY_TAB), 0x9 },
  43. {M(KEY_UP), KEYD_UP },
  44. {M(KEY_DOWN), KEYD_DOWN },
  45. {M(KEY_LEFT), KEYD_LEFT },
  46. {M(KEY_RIGHT), KEYD_RIGHT },
  47. {M(KEY_INSERT), KEYD_INSERT },
  48. {M(KEY_DELETE), KEYD_DELETE },
  49. {M(KEY_PAGE_UP), KEYD_PAGE_UP },
  50. {M(KEY_PAGE_DOWN), KEYD_PAGE_DOWN },
  51. {M(KEY_HOME), KEYD_HOME },
  52. {M(KEY_END), KEYD_END },
  53. {M(KEY_F1), KEYD_F1 },
  54. {M(KEY_F2), KEYD_F2 },
  55. {M(KEY_F3), KEYD_F3 },
  56. {M(KEY_F4), KEYD_F4 },
  57. {M(KEY_F5), KEYD_F5 },
  58. {M(KEY_F6), KEYD_F6 },
  59. {M(KEY_F7), KEYD_F7 },
  60. {M(KEY_F8), KEYD_F8 },
  61. {M(KEY_F9), KEYD_F9 },
  62. {M(KEY_F10), KEYD_F10 },
  63. {M(KEY_F11), KEYD_F11 },
  64. {M(KEY_F12), KEYD_F12 }
  65. };
  66. // Some of these mapped to key + shift.
  67. keycode_numlock_t keycode_numlock[] = {
  68. {M(KEYPAD_SLASH), '/', '/'},
  69. {M(KEYPAD_ASTERIX), '*', '*'},
  70. {M(KEYPAD_MINUS), '-', '-'},
  71. {M(KEYPAD_PLUS), '+', '+'},
  72. {M(KEYPAD_ENTER), '\n', '\n'},
  73. {M(KEYPAD_1), 0x80 | M(KEY_END), '1'},
  74. {M(KEYPAD_2), 0x80 | M(KEY_DOWN), '2'},
  75. {M(KEYPAD_3), 0x80 | M(KEY_PAGE_DOWN), '3'},
  76. {M(KEYPAD_4), 0x80 | M(KEY_LEFT), '4'},
  77. {M(KEYPAD_5), 0x00, '5'},
  78. {M(KEYPAD_6), 0x80 | M(KEY_RIGHT), '6'},
  79. {M(KEYPAD_7), 0x80 | M(KEY_HOME), '7'},
  80. {M(KEYPAD_8), 0x80 | M(KEY_UP), '8'},
  81. {M(KEYPAD_9), 0x80 | M(KEY_PAGE_UP), '9'},
  82. {M(KEYPAD_0), 0x80 | M(KEY_INSERT), '0'},
  83. {M(KEYPAD_PERIOD), 0x80 | M(KEY_DELETE), '.'}
  84. };
  85. #define print USBHost::print_
  86. #define println USBHost::println_
  87. void KeyboardController::init()
  88. {
  89. contribute_Pipes(mypipes, sizeof(mypipes)/sizeof(Pipe_t));
  90. contribute_Transfers(mytransfers, sizeof(mytransfers)/sizeof(Transfer_t));
  91. contribute_String_Buffers(mystring_bufs, sizeof(mystring_bufs)/sizeof(strbuf_t));
  92. driver_ready_for_device(this);
  93. }
  94. bool KeyboardController::claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_t len)
  95. {
  96. println("KeyboardController claim this=", (uint32_t)this, HEX);
  97. // only claim at interface level
  98. if (type != 1) return false;
  99. if (len < 9+9+7) return false;
  100. uint32_t numendpoint = descriptors[4];
  101. if (numendpoint < 1) return false;
  102. if (descriptors[5] != 3) return false; // bInterfaceClass, 3 = HID
  103. if (descriptors[6] != 1) return false; // bInterfaceSubClass, 1 = Boot Device
  104. if (descriptors[7] != 1) return false; // bInterfaceProtocol, 1 = Keyboard
  105. if (descriptors[9] != 9) return false;
  106. if (descriptors[10] != 33) return false; // HID descriptor (ignored, Boot Protocol)
  107. if (descriptors[18] != 7) return false;
  108. if (descriptors[19] != 5) return false; // endpoint descriptor
  109. uint32_t endpoint = descriptors[20];
  110. println("ep = ", endpoint, HEX);
  111. if ((endpoint & 0xF0) != 0x80) return false; // must be IN direction
  112. endpoint &= 0x0F;
  113. if (endpoint == 0) return false;
  114. if (descriptors[21] != 3) return false; // must be interrupt type
  115. uint32_t size = descriptors[22] | (descriptors[23] << 8);
  116. println("packet size = ", size);
  117. if (size != 8) {
  118. return false; // must be 8 bytes for Keyboard Boot Protocol
  119. }
  120. uint32_t interval = descriptors[24];
  121. println("polling interval = ", interval);
  122. datapipe = new_Pipe(dev, 3, endpoint, 1, 8, interval);
  123. datapipe->callback_function = callback;
  124. queue_Data_Transfer(datapipe, report, 8, this);
  125. mk_setup(setup, 0x21, 10, 0, 0, 0); // 10=SET_IDLE
  126. queue_Control_Transfer(dev, &setup, NULL, this);
  127. return true;
  128. }
  129. void KeyboardController::control(const Transfer_t *transfer)
  130. {
  131. }
  132. void KeyboardController::callback(const Transfer_t *transfer)
  133. {
  134. //println("KeyboardController Callback (static)");
  135. if (transfer->driver) {
  136. ((KeyboardController *)(transfer->driver))->new_data(transfer);
  137. }
  138. }
  139. void KeyboardController::disconnect()
  140. {
  141. // TODO: free resources
  142. }
  143. // Arduino defined this static weak symbol callback, and their
  144. // examples use it as the only way to detect new key presses,
  145. // so unfortunate as static weak callbacks are, it probably
  146. // needs to be supported for compatibility
  147. extern "C" {
  148. void __keyboardControllerEmptyCallback() { }
  149. }
  150. void keyPressed() __attribute__ ((weak, alias("__keyboardControllerEmptyCallback")));
  151. void keyReleased() __attribute__ ((weak, alias("__keyboardControllerEmptyCallback")));
  152. static bool contains(uint8_t b, const uint8_t *data)
  153. {
  154. if (data[2] == b || data[3] == b || data[4] == b) return true;
  155. if (data[5] == b || data[6] == b || data[7] == b) return true;
  156. return false;
  157. }
  158. void KeyboardController::new_data(const Transfer_t *transfer)
  159. {
  160. processing_new_data_ = true;
  161. println("KeyboardController Callback (member)");
  162. print(" KB Data: ");
  163. print_hexbytes(transfer->buffer, 8);
  164. for (int i=2; i < 8; i++) {
  165. uint32_t key = prev_report[i];
  166. if (key >= 4 && !contains(key, report)) {
  167. key_release(prev_report[0], key);
  168. }
  169. }
  170. for (int i=2; i < 8; i++) {
  171. uint32_t key = report[i];
  172. if (key >= 4 && !contains(key, prev_report)) {
  173. key_press(report[0], key);
  174. }
  175. }
  176. memcpy(prev_report, report, 8);
  177. queue_Data_Transfer(datapipe, report, 8, this);
  178. processing_new_data_ = false;
  179. // See if we have any outstanding leds to update
  180. if (update_leds_) {
  181. updateLEDS();
  182. }
  183. }
  184. void KeyboardController::numLock(bool f) {
  185. if (leds_.numLock != f) {
  186. leds_.numLock = f;
  187. updateLEDS();
  188. }
  189. }
  190. void KeyboardController::capsLock(bool f) {
  191. if (leds_.capsLock != f) {
  192. leds_.capsLock = f;
  193. updateLEDS();
  194. }
  195. }
  196. void KeyboardController::scrollLock(bool f) {
  197. if (leds_.scrollLock != f) {
  198. leds_.scrollLock = f;
  199. updateLEDS();
  200. }
  201. }
  202. void KeyboardController::key_press(uint32_t mod, uint32_t key)
  203. {
  204. // TODO: queue events, perform callback from Task
  205. println(" press, key=", key);
  206. modifiers = mod;
  207. keyOEM = key;
  208. keyCode = convert_to_unicode(mod, key);
  209. println(" unicode = ", keyCode);
  210. if (keyPressedFunction) {
  211. keyPressedFunction(keyCode);
  212. } else {
  213. keyPressed();
  214. }
  215. }
  216. void KeyboardController::key_release(uint32_t mod, uint32_t key)
  217. {
  218. // TODO: queue events, perform callback from Task
  219. println(" release, key=", key);
  220. modifiers = mod;
  221. keyOEM = key;
  222. // Look for modifier keys
  223. if (key == M(KEY_NUM_LOCK)) {
  224. numLock(!leds_.numLock);
  225. // Lets toggle Numlock
  226. } else if (key == M(KEY_CAPS_LOCK)) {
  227. capsLock(!leds_.capsLock);
  228. } else if (key == M(KEY_SCROLL_LOCK)) {
  229. scrollLock(!leds_.scrollLock);
  230. } else {
  231. keyCode = convert_to_unicode(mod, key);
  232. if (keyReleasedFunction) {
  233. keyReleasedFunction(keyCode);
  234. } else {
  235. keyReleased();
  236. }
  237. }
  238. }
  239. uint16_t KeyboardController::convert_to_unicode(uint32_t mod, uint32_t key)
  240. {
  241. // WIP: special keys
  242. // TODO: dead key sequences
  243. if (key & SHIFT_MASK) {
  244. // Many of these keys will look like they are other keys with shift mask...
  245. // Check for any of our mapped extra keys
  246. for (uint8_t i = 0; i < (sizeof(keycode_numlock)/sizeof(keycode_numlock[0])); i++) {
  247. if (keycode_numlock[i].code == key) {
  248. // See if the user is using numlock or not...
  249. if (leds_.numLock) {
  250. return keycode_numlock[i].charNumlockOn;
  251. } else {
  252. key = keycode_numlock[i].codeNumlockOff;
  253. if (!(key & 0x80)) return key; // we have hard coded value
  254. key &= 0x7f; // mask off the extra and break out to process as other characters...
  255. break;
  256. }
  257. }
  258. }
  259. }
  260. // Check for any of our mapped extra keys - Done early as some of these keys are
  261. // above and some below the SHIFT_MASK value
  262. for (uint8_t i = 0; i < (sizeof(keycode_extras)/sizeof(keycode_extras[0])); i++) {
  263. if (keycode_extras[i].code == key) {
  264. return keycode_extras[i].ascii;
  265. }
  266. }
  267. // If we made it here without doing something then return 0;
  268. if (key & SHIFT_MASK) return 0;
  269. if ((mod & 0x02) || (mod & 0x20)) key |= SHIFT_MASK;
  270. if (leds_.capsLock) key ^= SHIFT_MASK; // Caps lock will switch the Shift;
  271. for (int i=0; i < 96; i++) {
  272. if (keycodes_ascii[i] == key) {
  273. if ((mod & 1) || (mod & 0x10)) return (i+32) & 0x1f; // Control key is down
  274. return i + 32;
  275. }
  276. }
  277. #ifdef ISO_8859_1_A0
  278. for (int i=0; i < 96; i++) {
  279. if (keycodes_iso_8859_1[i] == key) return i + 160;
  280. }
  281. #endif
  282. return 0;
  283. }
  284. void KeyboardController::LEDS(uint8_t leds) {
  285. println("Keyboard setLEDS ", leds, HEX);
  286. leds_.byte = leds;
  287. updateLEDS();
  288. }
  289. void KeyboardController::updateLEDS() {
  290. println("KBD: Update LEDS", leds_.byte, HEX);
  291. if (processing_new_data_) {
  292. println(" Update defered");
  293. update_leds_ = true;
  294. return; // defer until later
  295. }
  296. // Now lets tell keyboard new state.
  297. static uint8_t keyboard_keys_report[1] = {0};
  298. setup_t keys_setup;
  299. keyboard_keys_report[0] = leds_.byte;
  300. queue_Data_Transfer(datapipe, report, 8, this);
  301. mk_setup(keys_setup, 0x21, 9, 0x200, 0, sizeof(keyboard_keys_report)); // hopefully this sets leds
  302. queue_Control_Transfer(device, &keys_setup, keyboard_keys_report, this);
  303. update_leds_ = false;
  304. }