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.
Fork of USBHost by
USBHostKeyboard.cpp
00001 /* mbed USBHost Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "USBHostKeyboard.h" 00018 00019 #if USBHOST_KEYBOARD 00020 00021 static uint8_t keymap[4][0x39] = { 00022 { 0, 0, 0, 0, 'a', 'b' /*0x05*/, 00023 'c', 'd', 'e', 'f', 'g' /*0x0a*/, 00024 'h', 'i', 'j', 'k', 'l'/*0x0f*/, 00025 'm', 'n', 'o', 'p', 'q'/*0x14*/, 00026 'r', 's', 't', 'u', 'v'/*0x19*/, 00027 'w', 'x', 'y', 'z', '1'/*0x1E*/, 00028 '2', '3', '4', '5', '6'/*0x23*/, 00029 '7', '8', '9', '0', 0x0A /*enter*/, /*0x28*/ 00030 0x1B /*escape*/, 0x08 /*backspace*/, 0x09/*tab*/, 0x20/*space*/, '-', /*0x2d*/ 00031 '=', '[', ']', '\\', '#', /*0x32*/ 00032 ';', '\'', 0, ',', '.', /*0x37*/ 00033 '/'}, 00034 00035 /* CTRL MODIFIER */ 00036 { 0, 0, 0, 0, 0, 0 /*0x05*/, 00037 0, 0, 0, 0, 0 /*0x0a*/, 00038 0, 0, 0, 0, 0/*0x0f*/, 00039 0, 0, 0, 0, 0/*0x14*/, 00040 0, 0, 0, 0, 0/*0x19*/, 00041 0, 0, 0, 0, 0/*0x1E*/, 00042 0, 0, 0, 0, 0/*0x23*/, 00043 0, 0, 0, 0, 0 /*enter*/, /*0x28*/ 00044 0, 0, 0, 0, 0, /*0x2d*/ 00045 0, 0, 0, 0, 0, /*0x32*/ 00046 0, 0, 0, 0, 0, /*0x37*/ 00047 0}, 00048 00049 /* SHIFT MODIFIER */ 00050 { 0, 0, 0, 0, 'A', 'B' /*0x05*/, 00051 'C', 'D', 'E', 'F', 'G' /*0x0a*/, 00052 'H', 'I', 'J', 'K', 'L'/*0x0f*/, 00053 'M', 'N', 'O', 'P', 'Q'/*0x14*/, 00054 'R', 'S', 'T', 'U', 'V'/*0x19*/, 00055 'W', 'X', 'Y', 'Z', '!'/*0x1E*/, 00056 '@', '#', '$', '%', '^'/*0x23*/, 00057 '&', '*', '(', ')', 0, /*0x28*/ 00058 0, 0, 0, 0, 0, /*0x2d*/ 00059 '+', '{', '}', '|', '~', /*0x32*/ 00060 ':', '"', 0, '<', '>', /*0x37*/ 00061 '?'}, 00062 00063 /* ALT MODIFIER */ 00064 { 0, 0, 0, 0, 0, 0 /*0x05*/, 00065 0, 0, 0, 0, 0 /*0x0a*/, 00066 0, 0, 0, 0, 0/*0x0f*/, 00067 0, 0, 0, 0, 0/*0x14*/, 00068 0, 0, 0, 0, 0/*0x19*/, 00069 0, 0, 0, 0, 0/*0x1E*/, 00070 0, 0, 0, 0, 0/*0x23*/, 00071 0, 0, 0, 0, 0 /*enter*/, /*0x28*/ 00072 0, 0, 0, 0, 0, /*0x2d*/ 00073 0, 0, 0, 0, 0, /*0x32*/ 00074 0, 0, 0, 0, 0, /*0x37*/ 00075 0} 00076 00077 }; 00078 00079 00080 USBHostKeyboard::USBHostKeyboard() { 00081 host = USBHost::getHostInst(); 00082 init(); 00083 } 00084 00085 00086 void USBHostKeyboard::init() { 00087 dev = NULL; 00088 int_in = NULL; 00089 report_id = 0; 00090 onKey = NULL; 00091 onKeyCode = NULL; 00092 dev_connected = false; 00093 keyboard_intf = -1; 00094 keyboard_device_found = false; 00095 } 00096 00097 bool USBHostKeyboard::connected() { 00098 return dev_connected; 00099 } 00100 00101 00102 bool USBHostKeyboard::connect() { 00103 00104 if (dev_connected) { 00105 return true; 00106 } 00107 00108 for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { 00109 if ((dev = host->getDevice(i)) != NULL) { 00110 00111 if (host->enumerate(dev, this)) 00112 break; 00113 00114 if (keyboard_device_found) { 00115 int_in = dev->getEndpoint(keyboard_intf, INTERRUPT_ENDPOINT, IN); 00116 00117 if (!int_in) 00118 break; 00119 00120 USB_INFO("New Keyboard device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, keyboard_intf); 00121 dev->setName("Keyboard", keyboard_intf); 00122 host->registerDriver(dev, keyboard_intf, this, &USBHostKeyboard::init); 00123 00124 int_in->attach(this, &USBHostKeyboard::rxHandler); 00125 host->interruptRead(dev, int_in, report, int_in->getSize(), false); 00126 00127 dev_connected = true; 00128 return true; 00129 } 00130 } 00131 } 00132 init(); 00133 return false; 00134 } 00135 00136 void USBHostKeyboard::rxHandler() { 00137 int len = int_in->getLengthTransferred(); 00138 int index = (len == 9) ? 1 : 0; 00139 int len_listen = int_in->getSize(); 00140 uint8_t key = 0; 00141 if (len == 8 || len == 9) { 00142 uint8_t modifier = (report[index] == 4) ? 3 : report[index]; 00143 len_listen = len; 00144 key = keymap[modifier][report[index + 2]]; 00145 if (key && onKey) { 00146 (*onKey)(key); 00147 } 00148 if ((report[index + 2] || modifier) && onKeyCode) { 00149 (*onKeyCode)(report[index + 2], modifier); 00150 } 00151 } 00152 if (dev && int_in) 00153 host->interruptRead(dev, int_in, report, len_listen, false); 00154 } 00155 00156 /*virtual*/ void USBHostKeyboard::setVidPid(uint16_t vid, uint16_t pid) 00157 { 00158 // we don't check VID/PID for keyboard driver 00159 } 00160 00161 /*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 00162 { 00163 if ((keyboard_intf == -1) && 00164 (intf_class == HID_CLASS) && 00165 (intf_subclass == 0x01) && 00166 (intf_protocol == 0x01)) { 00167 keyboard_intf = intf_nb; 00168 return true; 00169 } 00170 return false; 00171 } 00172 00173 /*virtual*/ bool USBHostKeyboard::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used 00174 { 00175 if (intf_nb == keyboard_intf) { 00176 if (type == INTERRUPT_ENDPOINT && dir == IN) { 00177 keyboard_device_found = true; 00178 return true; 00179 } 00180 } 00181 return false; 00182 } 00183 00184 #endif
Generated on Sat Jul 16 2022 04:39:31 by
1.7.2
