2018.07.26
Dependencies: FATFileSystem2 mbed-rtos
Fork of USBHost by
USBHostHID/USBHostKeyboard.cpp@4:b320d68e98e7, 2013-03-12 (annotated)
- Committer:
- samux
- Date:
- Tue Mar 12 17:23:37 2013 +0000
- Revision:
- 4:b320d68e98e7
- Parent:
- 0:a554658735bf
- Child:
- 5:e48791a1ef18
bug fixes - support composite device
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:a554658735bf | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
mbed_official | 0:a554658735bf | 2 | * |
mbed_official | 0:a554658735bf | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
mbed_official | 0:a554658735bf | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
mbed_official | 0:a554658735bf | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
mbed_official | 0:a554658735bf | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
mbed_official | 0:a554658735bf | 7 | * Software is furnished to do so, subject to the following conditions: |
mbed_official | 0:a554658735bf | 8 | * |
mbed_official | 0:a554658735bf | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
mbed_official | 0:a554658735bf | 10 | * substantial portions of the Software. |
mbed_official | 0:a554658735bf | 11 | * |
mbed_official | 0:a554658735bf | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
mbed_official | 0:a554658735bf | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
mbed_official | 0:a554658735bf | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
mbed_official | 0:a554658735bf | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
mbed_official | 0:a554658735bf | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
mbed_official | 0:a554658735bf | 17 | */ |
mbed_official | 0:a554658735bf | 18 | |
mbed_official | 0:a554658735bf | 19 | #include "USBHostKeyboard.h" |
mbed_official | 0:a554658735bf | 20 | |
mbed_official | 0:a554658735bf | 21 | #if USBHOST_KEYBOARD |
mbed_official | 0:a554658735bf | 22 | |
mbed_official | 0:a554658735bf | 23 | static uint8_t keymap[4][0x39] = { |
mbed_official | 0:a554658735bf | 24 | { 0, 0, 0, 0, 'a', 'b' /*0x05*/, |
mbed_official | 0:a554658735bf | 25 | 'c', 'd', 'e', 'f', 'g' /*0x0a*/, |
mbed_official | 0:a554658735bf | 26 | 'h', 'i', 'j', 'k', 'l'/*0x0f*/, |
mbed_official | 0:a554658735bf | 27 | 'm', 'n', 'o', 'p', 'q'/*0x14*/, |
mbed_official | 0:a554658735bf | 28 | 'r', 's', 't', 'u', 'v'/*0x19*/, |
mbed_official | 0:a554658735bf | 29 | 'w', 'x', 'y', 'z', '1'/*0x1E*/, |
mbed_official | 0:a554658735bf | 30 | '2', '3', '4', '5', '6'/*0x23*/, |
mbed_official | 0:a554658735bf | 31 | '7', '8', '9', '0', 0x0A /*enter*/, /*0x28*/ |
mbed_official | 0:a554658735bf | 32 | 0x1B /*escape*/, 0x08 /*backspace*/, 0x09/*tab*/, 0x20/*space*/, '-', /*0x2d*/ |
mbed_official | 0:a554658735bf | 33 | '=', '[', ']', '\\', '#', /*0x32*/ |
mbed_official | 0:a554658735bf | 34 | ';', '\'', 0, ',', '.', /*0x37*/ |
mbed_official | 0:a554658735bf | 35 | '/'}, |
mbed_official | 0:a554658735bf | 36 | |
mbed_official | 0:a554658735bf | 37 | /* CTRL MODIFIER */ |
mbed_official | 0:a554658735bf | 38 | { 0, 0, 0, 0, 0, 0 /*0x05*/, |
mbed_official | 0:a554658735bf | 39 | 0, 0, 0, 0, 0 /*0x0a*/, |
mbed_official | 0:a554658735bf | 40 | 0, 0, 0, 0, 0/*0x0f*/, |
mbed_official | 0:a554658735bf | 41 | 0, 0, 0, 0, 0/*0x14*/, |
mbed_official | 0:a554658735bf | 42 | 0, 0, 0, 0, 0/*0x19*/, |
mbed_official | 0:a554658735bf | 43 | 0, 0, 0, 0, 0/*0x1E*/, |
mbed_official | 0:a554658735bf | 44 | 0, 0, 0, 0, 0/*0x23*/, |
mbed_official | 0:a554658735bf | 45 | 0, 0, 0, 0, 0 /*enter*/, /*0x28*/ |
mbed_official | 0:a554658735bf | 46 | 0, 0, 0, 0, 0, /*0x2d*/ |
mbed_official | 0:a554658735bf | 47 | 0, 0, 0, 0, 0, /*0x32*/ |
mbed_official | 0:a554658735bf | 48 | 0, 0, 0, 0, 0, /*0x37*/ |
mbed_official | 0:a554658735bf | 49 | 0}, |
mbed_official | 0:a554658735bf | 50 | |
mbed_official | 0:a554658735bf | 51 | /* SHIFT MODIFIER */ |
mbed_official | 0:a554658735bf | 52 | { 0, 0, 0, 0, 'A', 'B' /*0x05*/, |
mbed_official | 0:a554658735bf | 53 | 'C', 'D', 'E', 'F', 'G' /*0x0a*/, |
mbed_official | 0:a554658735bf | 54 | 'H', 'I', 'J', 'K', 'L'/*0x0f*/, |
mbed_official | 0:a554658735bf | 55 | 'M', 'N', 'O', 'P', 'Q'/*0x14*/, |
mbed_official | 0:a554658735bf | 56 | 'R', 'S', 'T', 'U', 'V'/*0x19*/, |
mbed_official | 0:a554658735bf | 57 | 'W', 'X', 'Y', 'Z', '!'/*0x1E*/, |
mbed_official | 0:a554658735bf | 58 | '@', '#', '$', '%', '^'/*0x23*/, |
mbed_official | 0:a554658735bf | 59 | '&', '*', '(', ')', 0, /*0x28*/ |
mbed_official | 0:a554658735bf | 60 | 0, 0, 0, 0, 0, /*0x2d*/ |
mbed_official | 0:a554658735bf | 61 | '+', '{', '}', '|', '~', /*0x32*/ |
mbed_official | 0:a554658735bf | 62 | ':', '"', 0, '<', '>', /*0x37*/ |
mbed_official | 0:a554658735bf | 63 | '?'}, |
mbed_official | 0:a554658735bf | 64 | |
mbed_official | 0:a554658735bf | 65 | /* ALT MODIFIER */ |
mbed_official | 0:a554658735bf | 66 | { 0, 0, 0, 0, 0, 0 /*0x05*/, |
mbed_official | 0:a554658735bf | 67 | 0, 0, 0, 0, 0 /*0x0a*/, |
mbed_official | 0:a554658735bf | 68 | 0, 0, 0, 0, 0/*0x0f*/, |
mbed_official | 0:a554658735bf | 69 | 0, 0, 0, 0, 0/*0x14*/, |
mbed_official | 0:a554658735bf | 70 | 0, 0, 0, 0, 0/*0x19*/, |
mbed_official | 0:a554658735bf | 71 | 0, 0, 0, 0, 0/*0x1E*/, |
mbed_official | 0:a554658735bf | 72 | 0, 0, 0, 0, 0/*0x23*/, |
mbed_official | 0:a554658735bf | 73 | 0, 0, 0, 0, 0 /*enter*/, /*0x28*/ |
mbed_official | 0:a554658735bf | 74 | 0, 0, 0, 0, 0, /*0x2d*/ |
mbed_official | 0:a554658735bf | 75 | 0, 0, 0, 0, 0, /*0x32*/ |
mbed_official | 0:a554658735bf | 76 | 0, 0, 0, 0, 0, /*0x37*/ |
mbed_official | 0:a554658735bf | 77 | 0} |
mbed_official | 0:a554658735bf | 78 | |
mbed_official | 0:a554658735bf | 79 | }; |
mbed_official | 0:a554658735bf | 80 | |
mbed_official | 0:a554658735bf | 81 | |
mbed_official | 0:a554658735bf | 82 | USBHostKeyboard::USBHostKeyboard() { |
mbed_official | 0:a554658735bf | 83 | host = USBHost::getHostInst(); |
mbed_official | 0:a554658735bf | 84 | init(); |
mbed_official | 0:a554658735bf | 85 | } |
mbed_official | 0:a554658735bf | 86 | |
mbed_official | 0:a554658735bf | 87 | |
mbed_official | 0:a554658735bf | 88 | void USBHostKeyboard::init() { |
mbed_official | 0:a554658735bf | 89 | dev = NULL; |
mbed_official | 0:a554658735bf | 90 | int_in = NULL; |
mbed_official | 0:a554658735bf | 91 | report_id = 0; |
mbed_official | 0:a554658735bf | 92 | onKey = NULL; |
mbed_official | 0:a554658735bf | 93 | onKeyCode = NULL; |
mbed_official | 0:a554658735bf | 94 | dev_connected = false; |
mbed_official | 0:a554658735bf | 95 | keyboard_intf = -1; |
mbed_official | 0:a554658735bf | 96 | keyboard_device_found = false; |
mbed_official | 0:a554658735bf | 97 | } |
mbed_official | 0:a554658735bf | 98 | |
mbed_official | 0:a554658735bf | 99 | bool USBHostKeyboard::connected() { |
mbed_official | 0:a554658735bf | 100 | return dev_connected; |
mbed_official | 0:a554658735bf | 101 | } |
mbed_official | 0:a554658735bf | 102 | |
mbed_official | 0:a554658735bf | 103 | |
mbed_official | 0:a554658735bf | 104 | bool USBHostKeyboard::connect() { |
mbed_official | 0:a554658735bf | 105 | |
mbed_official | 0:a554658735bf | 106 | U8 i; |
mbed_official | 0:a554658735bf | 107 | |
mbed_official | 0:a554658735bf | 108 | if (dev_connected) { |
mbed_official | 0:a554658735bf | 109 | return true; |
mbed_official | 0:a554658735bf | 110 | } |
mbed_official | 0:a554658735bf | 111 | |
mbed_official | 0:a554658735bf | 112 | for (i = 0; i < MAX_DEVICE_CONNECTED; i++) { |
mbed_official | 0:a554658735bf | 113 | if ((dev = host->getDevice(i)) != NULL) { |
mbed_official | 0:a554658735bf | 114 | |
mbed_official | 0:a554658735bf | 115 | if (host->enumerate(dev, this)) |
mbed_official | 0:a554658735bf | 116 | break; |
mbed_official | 0:a554658735bf | 117 | |
mbed_official | 0:a554658735bf | 118 | if (keyboard_device_found) { |
mbed_official | 0:a554658735bf | 119 | int_in = dev->getEndpoint(keyboard_intf, INTERRUPT_ENDPOINT, IN); |
mbed_official | 0:a554658735bf | 120 | |
mbed_official | 0:a554658735bf | 121 | if (!int_in) |
mbed_official | 0:a554658735bf | 122 | break; |
mbed_official | 0:a554658735bf | 123 | |
samux | 4:b320d68e98e7 | 124 | USB_INFO("New Keyboard device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, keyboard_intf); |
samux | 4:b320d68e98e7 | 125 | dev->setName("Keyboard", keyboard_intf); |
mbed_official | 0:a554658735bf | 126 | host->registerDriver(dev, keyboard_intf, this, &USBHostKeyboard::init); |
mbed_official | 0:a554658735bf | 127 | |
mbed_official | 0:a554658735bf | 128 | int_in->attach(this, &USBHostKeyboard::rxHandler); |
mbed_official | 0:a554658735bf | 129 | host->interruptRead(dev, int_in, report, int_in->getSize(), false); |
mbed_official | 0:a554658735bf | 130 | |
mbed_official | 0:a554658735bf | 131 | dev_connected = true; |
mbed_official | 0:a554658735bf | 132 | return true; |
mbed_official | 0:a554658735bf | 133 | } |
mbed_official | 0:a554658735bf | 134 | } |
mbed_official | 0:a554658735bf | 135 | } |
mbed_official | 0:a554658735bf | 136 | init(); |
mbed_official | 0:a554658735bf | 137 | return false; |
mbed_official | 0:a554658735bf | 138 | } |
mbed_official | 0:a554658735bf | 139 | |
mbed_official | 0:a554658735bf | 140 | void USBHostKeyboard::rxHandler() { |
mbed_official | 0:a554658735bf | 141 | int len = int_in->getLengthTransferred(); |
mbed_official | 0:a554658735bf | 142 | int index = (len == 9) ? 1 : 0; |
mbed_official | 0:a554658735bf | 143 | int len_listen = int_in->getSize(); |
mbed_official | 0:a554658735bf | 144 | uint8_t key = 0; |
mbed_official | 0:a554658735bf | 145 | if (len == 8 || len == 9) { |
mbed_official | 0:a554658735bf | 146 | uint8_t modifier = (report[index] == 4) ? 3 : report[index]; |
mbed_official | 0:a554658735bf | 147 | len_listen = len; |
mbed_official | 0:a554658735bf | 148 | key = keymap[modifier][report[index + 2]]; |
mbed_official | 0:a554658735bf | 149 | if (key && onKey) { |
mbed_official | 0:a554658735bf | 150 | (*onKey)(key); |
mbed_official | 0:a554658735bf | 151 | } |
mbed_official | 0:a554658735bf | 152 | if (onKeyCode) { |
mbed_official | 0:a554658735bf | 153 | (*onKeyCode)(report[index + 2], modifier); |
mbed_official | 0:a554658735bf | 154 | } |
mbed_official | 0:a554658735bf | 155 | } |
mbed_official | 0:a554658735bf | 156 | if (dev != NULL) |
mbed_official | 0:a554658735bf | 157 | host->interruptRead(dev, int_in, report, len_listen, false); |
mbed_official | 0:a554658735bf | 158 | } |
mbed_official | 0:a554658735bf | 159 | |
mbed_official | 0:a554658735bf | 160 | /*virtual*/ void USBHostKeyboard::setVidPid(uint16_t vid, uint16_t pid) |
mbed_official | 0:a554658735bf | 161 | { |
mbed_official | 0:a554658735bf | 162 | // we don't check VID/PID for keyboard driver |
mbed_official | 0:a554658735bf | 163 | } |
mbed_official | 0:a554658735bf | 164 | |
mbed_official | 0:a554658735bf | 165 | /*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 | 166 | { |
mbed_official | 0:a554658735bf | 167 | if ((keyboard_intf == -1) && |
mbed_official | 0:a554658735bf | 168 | (intf_class == HID_CLASS) && |
mbed_official | 0:a554658735bf | 169 | (intf_subclass == 0x01) && |
mbed_official | 0:a554658735bf | 170 | (intf_protocol == 0x01)) { |
mbed_official | 0:a554658735bf | 171 | keyboard_intf = intf_nb; |
mbed_official | 0:a554658735bf | 172 | return true; |
mbed_official | 0:a554658735bf | 173 | } |
mbed_official | 0:a554658735bf | 174 | return false; |
mbed_official | 0:a554658735bf | 175 | } |
mbed_official | 0:a554658735bf | 176 | |
mbed_official | 0:a554658735bf | 177 | /*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 | 178 | { |
mbed_official | 0:a554658735bf | 179 | if (intf_nb == keyboard_intf) { |
mbed_official | 0:a554658735bf | 180 | if (type == INTERRUPT_ENDPOINT && dir == IN) { |
mbed_official | 0:a554658735bf | 181 | keyboard_device_found = true; |
mbed_official | 0:a554658735bf | 182 | return true; |
mbed_official | 0:a554658735bf | 183 | } |
mbed_official | 0:a554658735bf | 184 | } |
mbed_official | 0:a554658735bf | 185 | return false; |
mbed_official | 0:a554658735bf | 186 | } |
mbed_official | 0:a554658735bf | 187 | |
mbed_official | 0:a554658735bf | 188 | #endif |