keyboard->mbed->PC (mbed->SDcard)
Fork of USBHost by
USBHostHID/USBHostKeyboard.cpp@5:e48791a1ef18, 2013-03-13 (annotated)
- Committer:
- samux
- Date:
- Wed Mar 13 10:23:01 2013 +0000
- Revision:
- 5:e48791a1ef18
- Parent:
- 4:b320d68e98e7
- Child:
- 8:93da8ea2708b
add button, x, y, z event callbacks
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 | if (dev_connected) { |
mbed_official | 0:a554658735bf | 107 | return true; |
mbed_official | 0:a554658735bf | 108 | } |
mbed_official | 0:a554658735bf | 109 | |
samux | 5:e48791a1ef18 | 110 | for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { |
mbed_official | 0:a554658735bf | 111 | if ((dev = host->getDevice(i)) != NULL) { |
mbed_official | 0:a554658735bf | 112 | |
mbed_official | 0:a554658735bf | 113 | if (host->enumerate(dev, this)) |
mbed_official | 0:a554658735bf | 114 | break; |
mbed_official | 0:a554658735bf | 115 | |
mbed_official | 0:a554658735bf | 116 | if (keyboard_device_found) { |
mbed_official | 0:a554658735bf | 117 | int_in = dev->getEndpoint(keyboard_intf, INTERRUPT_ENDPOINT, IN); |
mbed_official | 0:a554658735bf | 118 | |
mbed_official | 0:a554658735bf | 119 | if (!int_in) |
mbed_official | 0:a554658735bf | 120 | break; |
mbed_official | 0:a554658735bf | 121 | |
samux | 4:b320d68e98e7 | 122 | USB_INFO("New Keyboard device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, keyboard_intf); |
samux | 4:b320d68e98e7 | 123 | dev->setName("Keyboard", keyboard_intf); |
mbed_official | 0:a554658735bf | 124 | host->registerDriver(dev, keyboard_intf, this, &USBHostKeyboard::init); |
mbed_official | 0:a554658735bf | 125 | |
mbed_official | 0:a554658735bf | 126 | int_in->attach(this, &USBHostKeyboard::rxHandler); |
mbed_official | 0:a554658735bf | 127 | host->interruptRead(dev, int_in, report, int_in->getSize(), false); |
mbed_official | 0:a554658735bf | 128 | |
mbed_official | 0:a554658735bf | 129 | dev_connected = true; |
mbed_official | 0:a554658735bf | 130 | return true; |
mbed_official | 0:a554658735bf | 131 | } |
mbed_official | 0:a554658735bf | 132 | } |
mbed_official | 0:a554658735bf | 133 | } |
mbed_official | 0:a554658735bf | 134 | init(); |
mbed_official | 0:a554658735bf | 135 | return false; |
mbed_official | 0:a554658735bf | 136 | } |
mbed_official | 0:a554658735bf | 137 | |
mbed_official | 0:a554658735bf | 138 | void USBHostKeyboard::rxHandler() { |
mbed_official | 0:a554658735bf | 139 | int len = int_in->getLengthTransferred(); |
mbed_official | 0:a554658735bf | 140 | int index = (len == 9) ? 1 : 0; |
mbed_official | 0:a554658735bf | 141 | int len_listen = int_in->getSize(); |
mbed_official | 0:a554658735bf | 142 | uint8_t key = 0; |
mbed_official | 0:a554658735bf | 143 | if (len == 8 || len == 9) { |
mbed_official | 0:a554658735bf | 144 | uint8_t modifier = (report[index] == 4) ? 3 : report[index]; |
mbed_official | 0:a554658735bf | 145 | len_listen = len; |
mbed_official | 0:a554658735bf | 146 | key = keymap[modifier][report[index + 2]]; |
mbed_official | 0:a554658735bf | 147 | if (key && onKey) { |
mbed_official | 0:a554658735bf | 148 | (*onKey)(key); |
mbed_official | 0:a554658735bf | 149 | } |
samux | 5:e48791a1ef18 | 150 | if ((report[index + 2] || modifier) && onKeyCode) { |
mbed_official | 0:a554658735bf | 151 | (*onKeyCode)(report[index + 2], modifier); |
mbed_official | 0:a554658735bf | 152 | } |
mbed_official | 0:a554658735bf | 153 | } |
samux | 5:e48791a1ef18 | 154 | if (dev && int_in) |
mbed_official | 0:a554658735bf | 155 | host->interruptRead(dev, int_in, report, len_listen, false); |
mbed_official | 0:a554658735bf | 156 | } |
mbed_official | 0:a554658735bf | 157 | |
mbed_official | 0:a554658735bf | 158 | /*virtual*/ void USBHostKeyboard::setVidPid(uint16_t vid, uint16_t pid) |
mbed_official | 0:a554658735bf | 159 | { |
mbed_official | 0:a554658735bf | 160 | // we don't check VID/PID for keyboard driver |
mbed_official | 0:a554658735bf | 161 | } |
mbed_official | 0:a554658735bf | 162 | |
mbed_official | 0:a554658735bf | 163 | /*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 | 164 | { |
mbed_official | 0:a554658735bf | 165 | if ((keyboard_intf == -1) && |
mbed_official | 0:a554658735bf | 166 | (intf_class == HID_CLASS) && |
mbed_official | 0:a554658735bf | 167 | (intf_subclass == 0x01) && |
mbed_official | 0:a554658735bf | 168 | (intf_protocol == 0x01)) { |
mbed_official | 0:a554658735bf | 169 | keyboard_intf = intf_nb; |
mbed_official | 0:a554658735bf | 170 | return true; |
mbed_official | 0:a554658735bf | 171 | } |
mbed_official | 0:a554658735bf | 172 | return false; |
mbed_official | 0:a554658735bf | 173 | } |
mbed_official | 0:a554658735bf | 174 | |
mbed_official | 0:a554658735bf | 175 | /*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 | 176 | { |
mbed_official | 0:a554658735bf | 177 | if (intf_nb == keyboard_intf) { |
mbed_official | 0:a554658735bf | 178 | if (type == INTERRUPT_ENDPOINT && dir == IN) { |
mbed_official | 0:a554658735bf | 179 | keyboard_device_found = true; |
mbed_official | 0:a554658735bf | 180 | return true; |
mbed_official | 0:a554658735bf | 181 | } |
mbed_official | 0:a554658735bf | 182 | } |
mbed_official | 0:a554658735bf | 183 | return false; |
mbed_official | 0:a554658735bf | 184 | } |
mbed_official | 0:a554658735bf | 185 | |
mbed_official | 0:a554658735bf | 186 | #endif |