2018.07.26

Dependencies:   FATFileSystem2 mbed-rtos

Fork of USBHost by mbed official

Committer:
sayzyas
Date:
Thu Jul 26 00:29:30 2018 +0000
Revision:
44:e437b1c7c61e
Parent:
43:78f328f311dc
2018.07.26

Who changed what in which revision?

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