Shivanand Gowda / USBHOST

Fork of USBHOST by ST

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBHostKeyboard.cpp Source File

USBHostKeyboard.cpp

00001 /* mbed USBHost Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "USBHostKeyboard.h"
00018 
00019 #if USBHOST_KEYBOARD
00020 
00021 static uint8_t keymap[4][0x39] = {
00022     //{ 0, 0, 0, 0, 'a', 'b' /*0x05*/,
00023 //      'c', 'd', 'e', 'f', 'g' /*0x0a*/,
00024 //      'h', 'i', 'j', 'k', 'l'/*0x0f*/,
00025 //      'm', 'n', 'o', 'p', 'q'/*0x14*/,
00026 //      'r', 's', 't', 'u', 'v'/*0x19*/,
00027 //      'w', 'x', 'y', 'z', '1'/*0x1E*/,
00028 //      '2', '3', '4', '5', '6'/*0x23*/,
00029 //      '7', '8', '9', '0', 0x0A /*enter*/, /*0x28*/
00030 //      0x1B /*escape*/, 0x08 /*backspace*/, 0x09/*tab*/, 0x20/*space*/, '-', /*0x2d*/
00031 //      '=', '[', ']', '\\', '#', /*0x32*/
00032 //      ';', '\'', 0, ',', '.', /*0x37*/
00033 //      '/'},
00034       
00035        { 0, 0, 0, 0, 'A', 'B' /*0x05*/,
00036       'C', 'D', 'E', 'F', 'G' /*0x0a*/,
00037       'H', 'I', 'J', 'K', 'L'/*0x0f*/,
00038       'M', 'N', 'O', 'P', 'Q'/*0x14*/,
00039       'R', 'S', 'T', 'U', 'V'/*0x19*/,
00040       'W', 'X', 'Y', 'Z', '1'/*0x1E*/,
00041       '2', '3', '4', '5', '6'/*0x23*/,
00042       '7', '8', '9', '0', 0x0A /*enter*/, /*0x28*/
00043       0x1B /*escape*/, 0x08 /*backspace*/, 0x09/*tab*/, 0x20/*space*/, '-', /*0x2d*/
00044       '=', '[', ']', '\\', '#', /*0x32*/
00045       ';', '\'', 0, ',', '.', /*0x37*/
00046       '/'},
00047 
00048     /* CTRL MODIFIER */
00049     { 0, 0, 0, 0, 0, 0 /*0x05*/,
00050       0, 0, 0, 0, 0 /*0x0a*/,
00051       0, 0, 0, 0, 0/*0x0f*/,
00052       0, 0, 0, 0, 0/*0x14*/,
00053       0, 0, 0, 0, 0/*0x19*/,
00054       0, 0, 0, 0, 0/*0x1E*/,
00055       0, 0, 0, 0, 0/*0x23*/,
00056       0, 0, 0, 0, 0 /*enter*/, /*0x28*/
00057       0, 0, 0, 0, 0, /*0x2d*/
00058       0, 0, 0, 0, 0, /*0x32*/
00059       0, 0, 0, 0, 0, /*0x37*/
00060       0},
00061 
00062     /* SHIFT MODIFIER */
00063     { 0, 0, 0, 0, 'A', 'B' /*0x05*/,
00064       'C', 'D', 'E', 'F', 'G' /*0x0a*/,
00065       'H', 'I', 'J', 'K', 'L'/*0x0f*/,
00066       'M', 'N', 'O', 'P', 'Q'/*0x14*/,
00067       'R', 'S', 'T', 'U', 'V'/*0x19*/,
00068       'W', 'X', 'Y', 'Z', '!'/*0x1E*/,
00069       '@', '#', '$', '%', '^'/*0x23*/,
00070       '&', '*', '(', ')', 0, /*0x28*/
00071       0, 0, 0, 0, 0, /*0x2d*/
00072       '+', '{', '}', '|', '~', /*0x32*/
00073       ':', '"', 0, '<', '>', /*0x37*/
00074       '?'},
00075 
00076     /* ALT MODIFIER */
00077     { 0, 0, 0, 0, 0, 0 /*0x05*/,
00078       0, 0, 0, 0, 0 /*0x0a*/,
00079       0, 0, 0, 0, 0/*0x0f*/,
00080       0, 0, 0, 0, 0/*0x14*/,
00081       0, 0, 0, 0, 0/*0x19*/,
00082       0, 0, 0, 0, 0/*0x1E*/,
00083       0, 0, 0, 0, 0/*0x23*/,
00084       0, 0, 0, 0, 0 /*enter*/, /*0x28*/
00085       0, 0, 0, 0, 0, /*0x2d*/
00086       0, 0, 0, 0, 0, /*0x32*/
00087       0, 0, 0, 0, 0, /*0x37*/
00088       0}
00089 
00090 };
00091 
00092 
00093 USBHostKeyboard::USBHostKeyboard() {
00094     host = USBHost::getHostInst();
00095     init();
00096 }
00097 
00098 
00099 void USBHostKeyboard::init() {
00100     dev = NULL;
00101     int_in = NULL;
00102     report_id = 0;
00103     onKey = NULL;
00104     onKeyCode = NULL;
00105     dev_connected = false;
00106     keyboard_intf = -1;
00107     keyboard_device_found = false;
00108 }
00109 
00110 bool USBHostKeyboard::connected() {
00111     return dev_connected;
00112 }
00113 
00114 
00115 bool USBHostKeyboard::connect()
00116 {
00117 
00118     if (dev_connected) {
00119         return true;
00120     }
00121 
00122     for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
00123         if ((dev = host->getDevice(i)) != NULL) {
00124 
00125             if (host->enumerate(dev, this))
00126                 break;
00127 
00128             if (keyboard_device_found) {
00129                 {
00130                     /* As this is done in a specific thread
00131                      * this lock is taken to avoid to process the device
00132                      * disconnect in usb process during the device registering */
00133                     USBHost::Lock  Lock(host);
00134 
00135                     int_in = dev->getEndpoint(keyboard_intf, INTERRUPT_ENDPOINT, IN);
00136 
00137                     if (!int_in)
00138                         break;
00139 
00140                     USB_INFO("New Keyboard device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, keyboard_intf);
00141                     dev->setName("Keyboard", keyboard_intf);
00142                     host->registerDriver(dev, keyboard_intf, this, &USBHostKeyboard::init);
00143 
00144                     int_in->attach(this, &USBHostKeyboard::rxHandler);
00145                 }
00146                 host->interruptRead(dev, int_in, report, int_in->getSize(), false);
00147 
00148                 dev_connected = true;
00149                 return true;
00150             }
00151         }
00152     }
00153     init();
00154     return false;
00155 }
00156 
00157 void USBHostKeyboard::rxHandler() {
00158     int len = int_in->getLengthTransferred();
00159     int index = (len == 9) ? 1 : 0;
00160     int len_listen = int_in->getSize();
00161     uint8_t key = 0;
00162     if (len == 8 || len == 9) {
00163         uint8_t modifier = (report[index] == 4) ? 3 : report[index];
00164         len_listen = len;
00165         key = keymap[modifier][report[index + 2]];
00166         if (key && onKey) {
00167             (*onKey)(key);
00168         }
00169         if ((report[index + 2] || modifier) && onKeyCode) {
00170             (*onKeyCode)(report[index + 2], modifier);
00171         }
00172     }
00173     if (dev && int_in)
00174         host->interruptRead(dev, int_in, report, len_listen, false);
00175 }
00176 
00177 /*virtual*/ void USBHostKeyboard::setVidPid(uint16_t vid, uint16_t pid)
00178 {
00179     // we don't check VID/PID for keyboard driver
00180 }
00181 
00182 /*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
00183 {
00184     if ((keyboard_intf == -1) &&
00185         (intf_class == HID_CLASS) &&
00186         (intf_subclass == 0x01) &&
00187         (intf_protocol == 0x01)) {
00188         keyboard_intf = intf_nb;
00189         return true;
00190     }
00191     return false;
00192 }
00193 
00194 /*virtual*/ bool USBHostKeyboard::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
00195 {
00196     if (intf_nb == keyboard_intf) {
00197         if (type == INTERRUPT_ENDPOINT && dir == IN) {
00198             keyboard_device_found = true;
00199             return true;
00200         }
00201     }
00202     return false;
00203 }
00204 
00205 #endif