2018.07.26

Dependencies:   FATFileSystem3 mbed-rtos

Fork of USBHost by mbed official

Committer:
sayzyas
Date:
Thu Jul 26 00:20:32 2018 +0000
Revision:
43:1675750cca08
2018.07.26

Who changed what in which revision?

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