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