USB BARCODE READER

Fork of USBHOST by ST

Committer:
frq08711@LMECWL0871.LME.ST.COM
Date:
Wed Feb 15 10:49:44 2017 +0100
Revision:
1:ab240722d7ef
Child:
5:fc157e6bd5a5
update to mbed 5.3.5

Who changed what in which revision?

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