Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

328 lines
9.3KB

  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. };
  44. // Some of these mapped to key + shift.
  45. keycode_numlock_t keycode_numlock[] = {
  46. {M(KEYPAD_SLASH), '/', '/'},
  47. {M(KEYPAD_ASTERIX), '*', '*'},
  48. {M(KEYPAD_MINUS), '-', '-'},
  49. {M(KEYPAD_PLUS), '+', '+'},
  50. {M(KEYPAD_ENTER), '\n', '\n'},
  51. {M(KEYPAD_1), 0x80 | M(KEY_END), '1'},
  52. {M(KEYPAD_2), 0x80 | M(KEY_DOWN), '2'},
  53. {M(KEYPAD_3), 0x80 | M(KEY_PAGE_DOWN), '3'},
  54. {M(KEYPAD_4), 0x80 | M(KEY_LEFT), '4'},
  55. {M(KEYPAD_5), 0x00, '5'},
  56. {M(KEYPAD_6), 0x80 | M(KEY_RIGHT), '6'},
  57. {M(KEYPAD_7), 0x80 | M(KEY_HOME), '7'},
  58. {M(KEYPAD_8), 0x80 | M(KEY_UP), '8'},
  59. {M(KEYPAD_9), 0x80 | M(KEY_PAGE_UP), '9'},
  60. {M(KEYPAD_0), 0x80 | M(KEY_INSERT), '0'},
  61. {M(KEYPAD_PERIOD), 0x80 | M(KEY_DELETE), '.'}
  62. };
  63. void KeyboardController::init()
  64. {
  65. contribute_Pipes(mypipes, sizeof(mypipes)/sizeof(Pipe_t));
  66. contribute_Transfers(mytransfers, sizeof(mytransfers)/sizeof(Transfer_t));
  67. driver_ready_for_device(this);
  68. }
  69. bool KeyboardController::claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_t len)
  70. {
  71. println("KeyboardController claim this=", (uint32_t)this, HEX);
  72. // only claim at interface level
  73. if (type != 1) return false;
  74. if (len < 9+9+7) return false;
  75. uint32_t numendpoint = descriptors[4];
  76. if (numendpoint < 1) return false;
  77. if (descriptors[5] != 3) return false; // bInterfaceClass, 3 = HID
  78. if (descriptors[6] != 1) return false; // bInterfaceSubClass, 1 = Boot Device
  79. if (descriptors[7] != 1) return false; // bInterfaceProtocol, 1 = Keyboard
  80. if (descriptors[9] != 9) return false;
  81. if (descriptors[10] != 33) return false; // HID descriptor (ignored, Boot Protocol)
  82. if (descriptors[18] != 7) return false;
  83. if (descriptors[19] != 5) return false; // endpoint descriptor
  84. uint32_t endpoint = descriptors[20];
  85. println("ep = ", endpoint, HEX);
  86. if ((endpoint & 0xF0) != 0x80) return false; // must be IN direction
  87. endpoint &= 0x0F;
  88. if (endpoint == 0) return false;
  89. if (descriptors[21] != 3) return false; // must be interrupt type
  90. uint32_t size = descriptors[22] | (descriptors[23] << 8);
  91. println("packet size = ", size);
  92. if (size != 8) {
  93. return false; // must be 8 bytes for Keyboard Boot Protocol
  94. }
  95. uint32_t interval = descriptors[24];
  96. println("polling interval = ", interval);
  97. datapipe = new_Pipe(dev, 3, endpoint, 1, 8, interval);
  98. datapipe->callback_function = callback;
  99. queue_Data_Transfer(datapipe, report, 8, this);
  100. mk_setup(setup, 0x21, 10, 0, 0, 0); // 10=SET_IDLE
  101. queue_Control_Transfer(dev, &setup, NULL, this);
  102. return true;
  103. }
  104. void KeyboardController::control(const Transfer_t *transfer)
  105. {
  106. }
  107. void KeyboardController::callback(const Transfer_t *transfer)
  108. {
  109. //println("KeyboardController Callback (static)");
  110. if (transfer->driver) {
  111. ((KeyboardController *)(transfer->driver))->new_data(transfer);
  112. }
  113. }
  114. void KeyboardController::disconnect()
  115. {
  116. // TODO: free resources
  117. }
  118. // Arduino defined this static weak symbol callback, and their
  119. // examples use it as the only way to detect new key presses,
  120. // so unfortunate as static weak callbacks are, it probably
  121. // needs to be supported for compatibility
  122. extern "C" {
  123. void __keyboardControllerEmptyCallback() { }
  124. }
  125. void keyPressed() __attribute__ ((weak, alias("__keyboardControllerEmptyCallback")));
  126. void keyReleased() __attribute__ ((weak, alias("__keyboardControllerEmptyCallback")));
  127. static bool contains(uint8_t b, const uint8_t *data)
  128. {
  129. if (data[2] == b || data[3] == b || data[4] == b) return true;
  130. if (data[5] == b || data[6] == b || data[7] == b) return true;
  131. return false;
  132. }
  133. void KeyboardController::new_data(const Transfer_t *transfer)
  134. {
  135. processing_new_data_ = true;
  136. println("KeyboardController Callback (member)");
  137. print(" KB Data: ");
  138. print_hexbytes(transfer->buffer, 8);
  139. for (int i=2; i < 8; i++) {
  140. uint32_t key = prev_report[i];
  141. if (key >= 4 && !contains(key, report)) {
  142. key_release(prev_report[0], key);
  143. }
  144. }
  145. for (int i=2; i < 8; i++) {
  146. uint32_t key = report[i];
  147. if (key >= 4 && !contains(key, prev_report)) {
  148. key_press(report[0], key);
  149. }
  150. }
  151. memcpy(prev_report, report, 8);
  152. queue_Data_Transfer(datapipe, report, 8, this);
  153. processing_new_data_ = false;
  154. // See if we have any outstanding leds to update
  155. if (update_leds_) {
  156. updateLEDS();
  157. }
  158. }
  159. void KeyboardController::numLock(bool f) {
  160. if (leds_.numLock != f) {
  161. leds_.numLock = f;
  162. updateLEDS();
  163. }
  164. }
  165. void KeyboardController::capsLock(bool f) {
  166. if (leds_.capsLock != f) {
  167. leds_.capsLock = f;
  168. updateLEDS();
  169. }
  170. }
  171. void KeyboardController::scrollLock(bool f) {
  172. if (leds_.scrollLock != f) {
  173. leds_.scrollLock = f;
  174. updateLEDS();
  175. }
  176. }
  177. void KeyboardController::key_press(uint32_t mod, uint32_t key)
  178. {
  179. // TODO: queue events, perform callback from Task
  180. println(" press, key=", key);
  181. modifiers = mod;
  182. keyOEM = key;
  183. keyCode = convert_to_unicode(mod, key);
  184. println(" unicode = ", keyCode);
  185. if (keyPressedFunction) {
  186. keyPressedFunction(keyCode);
  187. } else {
  188. keyPressed();
  189. }
  190. }
  191. void KeyboardController::key_release(uint32_t mod, uint32_t key)
  192. {
  193. // TODO: queue events, perform callback from Task
  194. println(" release, key=", key);
  195. modifiers = mod;
  196. keyOEM = key;
  197. // Look for modifier keys
  198. if (key == M(KEY_NUM_LOCK)) {
  199. numLock(!leds_.numLock);
  200. // Lets toggle Numlock
  201. } else if (key == M(KEY_CAPS_LOCK)) {
  202. capsLock(!leds_.capsLock);
  203. } else if (key == M(KEY_SCROLL_LOCK)) {
  204. scrollLock(!leds_.scrollLock);
  205. } else {
  206. keyCode = convert_to_unicode(mod, key);
  207. if (keyReleasedFunction) {
  208. keyReleasedFunction(keyCode);
  209. } else {
  210. keyReleased();
  211. }
  212. }
  213. }
  214. uint16_t KeyboardController::convert_to_unicode(uint32_t mod, uint32_t key)
  215. {
  216. // TODO: special keys
  217. // TODO: caps lock
  218. // TODO: dead key sequences
  219. if (key & SHIFT_MASK) {
  220. // Many of these keys will look like they are other keys with shift mask...
  221. // Check for any of our mapped extra keys
  222. for (uint8_t i = 0; i < (sizeof(keycode_numlock)/sizeof(keycode_numlock[0])); i++) {
  223. if (keycode_numlock[i].code == key) {
  224. // See if the user is using numlock or not...
  225. if (leds_.numLock) {
  226. return keycode_numlock[i].charNumlockOn;
  227. } else {
  228. key = keycode_numlock[i].codeNumlockOff;
  229. if (!(key & 0x80)) return key; // we have hard coded value
  230. key &= 0x7f; // mask off the extra and break out to process as other characters...
  231. break;
  232. }
  233. }
  234. }
  235. // If we made it here without doing something then return 0;
  236. if (key & SHIFT_MASK) return 0;
  237. }
  238. if ((mod & 0x02) || (mod & 0x20)) key |= SHIFT_MASK;
  239. if (leds_.capsLock) key ^= SHIFT_MASK; // Caps lock will switch the Shift;
  240. for (int i=0; i < 96; i++) {
  241. if (keycodes_ascii[i] == key) {
  242. if ((mod & 1) || (mod & 0x10)) return (i+32) & 0x1f; // Control key is down
  243. return i + 32;
  244. }
  245. }
  246. // Check for any of our mapped extra keys
  247. for (uint8_t i = 0; i < (sizeof(keycode_extras)/sizeof(keycode_extras[0])); i++) {
  248. if (keycode_extras[i].code == key) {
  249. return keycode_extras[i].ascii;
  250. }
  251. }
  252. #ifdef ISO_8859_1_A0
  253. for (int i=0; i < 96; i++) {
  254. if (keycodes_iso_8859_1[i] == key) return i + 160;
  255. }
  256. #endif
  257. return 0;
  258. }
  259. void KeyboardController::LEDS(uint8_t leds) {
  260. println("Keyboard setLEDS ", leds, HEX);
  261. leds_.byte = leds;
  262. updateLEDS();
  263. }
  264. void KeyboardController::updateLEDS() {
  265. println("KBD: Update LEDS", leds_.byte, HEX);
  266. if (processing_new_data_) {
  267. println(" Update defered");
  268. update_leds_ = true;
  269. return; // defer until later
  270. }
  271. // Now lets tell keyboard new state.
  272. static uint8_t keyboard_keys_report[1] = {0};
  273. setup_t keys_setup;
  274. keyboard_keys_report[0] = leds_.byte;
  275. queue_Data_Transfer(datapipe, report, 8, this);
  276. mk_setup(keys_setup, 0x21, 9, 0x200, 0, sizeof(keyboard_keys_report)); // hopefully this sets leds
  277. queue_Control_Transfer(device, &keys_setup, keyboard_keys_report, this);
  278. update_leds_ = false;
  279. }