1 /* mbed USBHost Library
2 * Copyright (c) 2006-2013 ARM Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "USBHostKeyboard.h"
21 static uint8_t keymap[4][0x39] = {
22 { 0, 0, 0, 0, 'a', 'b' /*0x05*/,
23 'c', 'd', 'e', 'f', 'g' /*0x0a*/,
24 'h', 'i', 'j', 'k', 'l'/*0x0f*/,
25 'm', 'n', 'o', 'p', 'q'/*0x14*/,
26 'r', 's', 't', 'u', 'v'/*0x19*/,
27 'w', 'x', 'y', 'z', '1'/*0x1E*/,
28 '2', '3', '4', '5', '6'/*0x23*/,
29 '7', '8', '9', '0', 0x0A /*enter*/, /*0x28*/
30 0x1B /*escape*/, 0x08 /*backspace*/, 0x09/*tab*/, 0x20/*space*/, '-', /*0x2d*/
31 '=', '[', ']', '\\', '#', /*0x32*/
32 ';', '\'', 0, ',', '.', /*0x37*/
36 { 0, 0, 0, 0, 0, 0 /*0x05*/,
37 0, 0, 0, 0, 0 /*0x0a*/,
38 0, 0, 0, 0, 0/*0x0f*/,
39 0, 0, 0, 0, 0/*0x14*/,
40 0, 0, 0, 0, 0/*0x19*/,
41 0, 0, 0, 0, 0/*0x1E*/,
42 0, 0, 0, 0, 0/*0x23*/,
43 0, 0, 0, 0, 0 /*enter*/, /*0x28*/
44 0, 0, 0, 0, 0, /*0x2d*/
45 0, 0, 0, 0, 0, /*0x32*/
46 0, 0, 0, 0, 0, /*0x37*/
50 { 0, 0, 0, 0, 'A', 'B' /*0x05*/,
51 'C', 'D', 'E', 'F', 'G' /*0x0a*/,
52 'H', 'I', 'J', 'K', 'L'/*0x0f*/,
53 'M', 'N', 'O', 'P', 'Q'/*0x14*/,
54 'R', 'S', 'T', 'U', 'V'/*0x19*/,
55 'W', 'X', 'Y', 'Z', '!'/*0x1E*/,
56 '@', '#', '$', '%', '^'/*0x23*/,
57 '&', '*', '(', ')', 0, /*0x28*/
58 0, 0, 0, 0, 0, /*0x2d*/
59 '+', '{', '}', '|', '~', /*0x32*/
60 ':', '"', 0, '<', '>', /*0x37*/
64 { 0, 0, 0, 0, 0, 0 /*0x05*/,
65 0, 0, 0, 0, 0 /*0x0a*/,
66 0, 0, 0, 0, 0/*0x0f*/,
67 0, 0, 0, 0, 0/*0x14*/,
68 0, 0, 0, 0, 0/*0x19*/,
69 0, 0, 0, 0, 0/*0x1E*/,
70 0, 0, 0, 0, 0/*0x23*/,
71 0, 0, 0, 0, 0 /*enter*/, /*0x28*/
72 0, 0, 0, 0, 0, /*0x2d*/
73 0, 0, 0, 0, 0, /*0x32*/
74 0, 0, 0, 0, 0, /*0x37*/
80 USBHostKeyboard::USBHostKeyboard() {
81 host = USBHost::getHostInst();
86 void USBHostKeyboard::init() {
92 dev_connected = false;
94 keyboard_device_found = false;
97 bool USBHostKeyboard::connected() {
102 bool USBHostKeyboard::connect() {
108 for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
109 if ((dev = host->getDevice(i)) != NULL) {
111 if (host->enumerate(dev, this))
114 if (keyboard_device_found) {
115 int_in = dev->getEndpoint(keyboard_intf, INTERRUPT_ENDPOINT, IN);
120 USB_INFO("New Keyboard device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, keyboard_intf);
121 dev->setName("Keyboard", keyboard_intf);
122 host->registerDriver(dev, keyboard_intf, this, &USBHostKeyboard::init);
124 int_in->attach(this, &USBHostKeyboard::rxHandler);
125 host->interruptRead(dev, int_in, report, int_in->getSize(), false);
127 dev_connected = true;
136 void USBHostKeyboard::rxHandler() {
137 int len = int_in->getLengthTransferred();
138 int index = (len == 9) ? 1 : 0;
139 int len_listen = int_in->getSize();
141 if (len == 8 || len == 9) {
142 uint8_t modifier = (report[index] == 4) ? 3 : report[index];
144 key = keymap[modifier][report[index + 2]];
148 if ((report[index + 2] || modifier) && onKeyCode) {
149 (*onKeyCode)(report[index + 2], modifier);
153 host->interruptRead(dev, int_in, report, len_listen, false);
156 /*virtual*/ void USBHostKeyboard::setVidPid(uint16_t vid, uint16_t pid)
158 // we don't check VID/PID for keyboard driver
161 /*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
163 if ((keyboard_intf == -1) &&
164 (intf_class == HID_CLASS) &&
165 (intf_subclass == 0x01) &&
166 (intf_protocol == 0x01)) {
167 keyboard_intf = intf_nb;
173 /*virtual*/ bool USBHostKeyboard::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
175 if (intf_nb == keyboard_intf) {
176 if (type == INTERRUPT_ENDPOINT && dir == IN) {
177 keyboard_device_found = true;