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