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