Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

USBHostKeyboard.cpp 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* mbed USBHost Library
  2. * Copyright (c) 2006-2013 ARM Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "USBHostKeyboard.h"
  17. #if USBHOST_KEYBOARD
  18. static uint8_t keymap[4][0x39] = {
  19. { 0, 0, 0, 0, 'a', 'b' /*0x05*/,
  20. 'c', 'd', 'e', 'f', 'g' /*0x0a*/,
  21. 'h', 'i', 'j', 'k', 'l'/*0x0f*/,
  22. 'm', 'n', 'o', 'p', 'q'/*0x14*/,
  23. 'r', 's', 't', 'u', 'v'/*0x19*/,
  24. 'w', 'x', 'y', 'z', '1'/*0x1E*/,
  25. '2', '3', '4', '5', '6'/*0x23*/,
  26. '7', '8', '9', '0', 0x0A /*enter*/, /*0x28*/
  27. 0x1B /*escape*/, 0x08 /*backspace*/, 0x09/*tab*/, 0x20/*space*/, '-', /*0x2d*/
  28. '=', '[', ']', '\\', '#', /*0x32*/
  29. ';', '\'', 0, ',', '.', /*0x37*/
  30. '/'},
  31. /* CTRL MODIFIER */
  32. { 0, 0, 0, 0, 0, 0 /*0x05*/,
  33. 0, 0, 0, 0, 0 /*0x0a*/,
  34. 0, 0, 0, 0, 0/*0x0f*/,
  35. 0, 0, 0, 0, 0/*0x14*/,
  36. 0, 0, 0, 0, 0/*0x19*/,
  37. 0, 0, 0, 0, 0/*0x1E*/,
  38. 0, 0, 0, 0, 0/*0x23*/,
  39. 0, 0, 0, 0, 0 /*enter*/, /*0x28*/
  40. 0, 0, 0, 0, 0, /*0x2d*/
  41. 0, 0, 0, 0, 0, /*0x32*/
  42. 0, 0, 0, 0, 0, /*0x37*/
  43. 0},
  44. /* SHIFT MODIFIER */
  45. { 0, 0, 0, 0, 'A', 'B' /*0x05*/,
  46. 'C', 'D', 'E', 'F', 'G' /*0x0a*/,
  47. 'H', 'I', 'J', 'K', 'L'/*0x0f*/,
  48. 'M', 'N', 'O', 'P', 'Q'/*0x14*/,
  49. 'R', 'S', 'T', 'U', 'V'/*0x19*/,
  50. 'W', 'X', 'Y', 'Z', '!'/*0x1E*/,
  51. '@', '#', '$', '%', '^'/*0x23*/,
  52. '&', '*', '(', ')', 0, /*0x28*/
  53. 0, 0, 0, 0, 0, /*0x2d*/
  54. '+', '{', '}', '|', '~', /*0x32*/
  55. ':', '"', 0, '<', '>', /*0x37*/
  56. '?'},
  57. /* ALT MODIFIER */
  58. { 0, 0, 0, 0, 0, 0 /*0x05*/,
  59. 0, 0, 0, 0, 0 /*0x0a*/,
  60. 0, 0, 0, 0, 0/*0x0f*/,
  61. 0, 0, 0, 0, 0/*0x14*/,
  62. 0, 0, 0, 0, 0/*0x19*/,
  63. 0, 0, 0, 0, 0/*0x1E*/,
  64. 0, 0, 0, 0, 0/*0x23*/,
  65. 0, 0, 0, 0, 0 /*enter*/, /*0x28*/
  66. 0, 0, 0, 0, 0, /*0x2d*/
  67. 0, 0, 0, 0, 0, /*0x32*/
  68. 0, 0, 0, 0, 0, /*0x37*/
  69. 0}
  70. };
  71. USBHostKeyboard::USBHostKeyboard() {
  72. host = USBHost::getHostInst();
  73. init();
  74. }
  75. void USBHostKeyboard::init() {
  76. dev = NULL;
  77. int_in = NULL;
  78. report_id = 0;
  79. onKey = NULL;
  80. onKeyCode = NULL;
  81. dev_connected = false;
  82. keyboard_intf = -1;
  83. keyboard_device_found = false;
  84. }
  85. bool USBHostKeyboard::connected() {
  86. return dev_connected;
  87. }
  88. bool USBHostKeyboard::connect() {
  89. if (dev_connected) {
  90. return true;
  91. }
  92. for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
  93. if ((dev = host->getDevice(i)) != NULL) {
  94. if (host->enumerate(dev, this))
  95. break;
  96. if (keyboard_device_found) {
  97. int_in = dev->getEndpoint(keyboard_intf, INTERRUPT_ENDPOINT, IN);
  98. if (!int_in)
  99. break;
  100. USB_INFO("New Keyboard device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, keyboard_intf);
  101. dev->setName("Keyboard", keyboard_intf);
  102. host->registerDriver(dev, keyboard_intf, this, &USBHostKeyboard::init);
  103. int_in->attach(this, &USBHostKeyboard::rxHandler);
  104. host->interruptRead(dev, int_in, report, int_in->getSize(), false);
  105. dev_connected = true;
  106. return true;
  107. }
  108. }
  109. }
  110. init();
  111. return false;
  112. }
  113. void USBHostKeyboard::rxHandler() {
  114. int len = int_in->getLengthTransferred();
  115. int index = (len == 9) ? 1 : 0;
  116. int len_listen = int_in->getSize();
  117. uint8_t key = 0;
  118. if (len == 8 || len == 9) {
  119. uint8_t modifier = (report[index] == 4) ? 3 : report[index];
  120. len_listen = len;
  121. key = keymap[modifier][report[index + 2]];
  122. if (key && onKey) {
  123. (*onKey)(key);
  124. }
  125. if ((report[index + 2] || modifier) && onKeyCode) {
  126. (*onKeyCode)(report[index + 2], modifier);
  127. }
  128. }
  129. if (dev && int_in)
  130. host->interruptRead(dev, int_in, report, len_listen, false);
  131. }
  132. /*virtual*/ void USBHostKeyboard::setVidPid(uint16_t vid, uint16_t pid)
  133. {
  134. // we don't check VID/PID for keyboard driver
  135. }
  136. /*virtual*/ bool USBHostKeyboard::parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) //Must return true if the interface should be parsed
  137. {
  138. if ((keyboard_intf == -1) &&
  139. (intf_class == HID_CLASS) &&
  140. (intf_subclass == 0x01) &&
  141. (intf_protocol == 0x01)) {
  142. keyboard_intf = intf_nb;
  143. return true;
  144. }
  145. return false;
  146. }
  147. /*virtual*/ bool USBHostKeyboard::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
  148. {
  149. if (intf_nb == keyboard_intf) {
  150. if (type == INTERRUPT_ENDPOINT && dir == IN) {
  151. keyboard_device_found = true;
  152. return true;
  153. }
  154. }
  155. return false;
  156. }
  157. #endif