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

185 lines
5.6KB

  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. void KeyboardController::init()
  27. {
  28. contribute_Pipes(mypipes, sizeof(mypipes)/sizeof(Pipe_t));
  29. contribute_Transfers(mytransfers, sizeof(mytransfers)/sizeof(Transfer_t));
  30. driver_ready_for_device(this);
  31. }
  32. bool KeyboardController::claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_t len)
  33. {
  34. println("KeyboardController claim this=", (uint32_t)this, HEX);
  35. // only claim at interface level
  36. if (type != 1) return false;
  37. if (len < 9+9+7) return false;
  38. uint32_t numendpoint = descriptors[4];
  39. if (numendpoint < 1) return false;
  40. if (descriptors[5] != 3) return false; // bInterfaceClass, 3 = HID
  41. if (descriptors[6] != 1) return false; // bInterfaceSubClass, 1 = Boot Device
  42. if (descriptors[7] != 1) return false; // bInterfaceProtocol, 1 = Keyboard
  43. if (descriptors[9] != 9) return false;
  44. if (descriptors[10] != 33) return false; // HID descriptor (ignored, Boot Protocol)
  45. if (descriptors[18] != 7) return false;
  46. if (descriptors[19] != 5) return false; // endpoint descriptor
  47. uint32_t endpoint = descriptors[20];
  48. println("ep = ", endpoint, HEX);
  49. if ((endpoint & 0xF0) != 0x80) return false; // must be IN direction
  50. endpoint &= 0x0F;
  51. if (endpoint == 0) return false;
  52. if (descriptors[21] != 3) return false; // must be interrupt type
  53. uint32_t size = descriptors[22] | (descriptors[23] << 8);
  54. println("packet size = ", size);
  55. if (size != 8) return false; // must be 8 bytes for Keyboard Boot Protocol
  56. uint32_t interval = descriptors[24];
  57. println("polling interval = ", interval);
  58. datapipe = new_Pipe(dev, 3, endpoint, 1, 8, interval);
  59. datapipe->callback_function = callback;
  60. queue_Data_Transfer(datapipe, report, 8, this);
  61. mk_setup(setup, 0x21, 10, 0, 0, 0); // 10=SET_IDLE
  62. queue_Control_Transfer(dev, &setup, NULL, this);
  63. return true;
  64. }
  65. void KeyboardController::control(const Transfer_t *transfer)
  66. {
  67. }
  68. void KeyboardController::callback(const Transfer_t *transfer)
  69. {
  70. //println("KeyboardController Callback (static)");
  71. if (transfer->driver) {
  72. ((KeyboardController *)(transfer->driver))->new_data(transfer);
  73. }
  74. }
  75. void KeyboardController::disconnect()
  76. {
  77. // TODO: free resources
  78. }
  79. // Arduino defined this static weak symbol callback, and their
  80. // examples use it as the only way to detect new key presses,
  81. // so unfortunate as static weak callbacks are, it probably
  82. // needs to be supported for compatibility
  83. extern "C" {
  84. void __keyboardControllerEmptyCallback() { }
  85. }
  86. void keyPressed() __attribute__ ((weak, alias("__keyboardControllerEmptyCallback")));
  87. void keyReleased() __attribute__ ((weak, alias("__keyboardControllerEmptyCallback")));
  88. static bool contains(uint8_t b, const uint8_t *data)
  89. {
  90. if (data[2] == b || data[3] == b || data[4] == b) return true;
  91. if (data[5] == b || data[6] == b || data[7] == b) return true;
  92. return false;
  93. }
  94. void KeyboardController::new_data(const Transfer_t *transfer)
  95. {
  96. println("KeyboardController Callback (member)");
  97. print(" KB Data: ");
  98. print_hexbytes(transfer->buffer, 8);
  99. for (int i=2; i < 8; i++) {
  100. uint32_t key = prev_report[i];
  101. if (key >= 4 && !contains(key, report)) {
  102. key_release(prev_report[0], key);
  103. }
  104. }
  105. for (int i=2; i < 8; i++) {
  106. uint32_t key = report[i];
  107. if (key >= 4 && !contains(key, prev_report)) {
  108. key_press(report[0], key);
  109. }
  110. }
  111. memcpy(prev_report, report, 8);
  112. queue_Data_Transfer(datapipe, report, 8, this);
  113. }
  114. void KeyboardController::key_press(uint32_t mod, uint32_t key)
  115. {
  116. // TODO: queue events, perform callback from Task
  117. println(" press, key=", key);
  118. modifiers = mod;
  119. keyOEM = key;
  120. keyCode = convert_to_unicode(mod, key);
  121. println(" unicode = ", keyCode);
  122. if (keyPressedFunction) {
  123. keyPressedFunction(keyCode);
  124. } else {
  125. keyPressed();
  126. }
  127. }
  128. void KeyboardController::key_release(uint32_t mod, uint32_t key)
  129. {
  130. // TODO: queue events, perform callback from Task
  131. println(" release, key=", key);
  132. modifiers = mod;
  133. keyOEM = key;
  134. keyCode = convert_to_unicode(mod, key);
  135. if (keyReleasedFunction) {
  136. keyReleasedFunction(keyCode);
  137. } else {
  138. keyReleased();
  139. }
  140. }
  141. uint16_t KeyboardController::convert_to_unicode(uint32_t mod, uint32_t key)
  142. {
  143. // TODO: special keys
  144. // TODO: caps lock
  145. // TODO: dead key sequences
  146. if ((mod & 0x02) || (mod & 0x20)) key |= SHIFT_MASK;
  147. for (int i=0; i < 96; i++) {
  148. if (keycodes_ascii[i] == key) return i + 32;
  149. }
  150. #ifdef ISO_8859_1_A0
  151. for (int i=0; i < 96; i++) {
  152. if (keycodes_iso_8859_1[i] == key) return i + 160;
  153. }
  154. #endif
  155. return 0;
  156. }