dhgdh

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by joey shelton

Committer:
cyberjoey
Date:
Sat Oct 22 01:31:58 2016 +0000
Revision:
9:6bb35cef007d
Parent:
1:55a6170b404f
WORKING

Who changed what in which revision?

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