Greg Steiert / maxim_dev

Dependents:   sensomed

Committer:
switches
Date:
Tue Nov 08 18:27:11 2016 +0000
Revision:
0:0e018d759a2a
Initial commit

Who changed what in which revision?

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