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