Dieter Graef / USBHost_DISCO-F746NG

Dependents:   DISCO-F746NG_USB_Host

Fork of KL46Z-USBHost by Norimasa Okamoto

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBHostMouseKeyboard.cpp Source File

USBHostMouseKeyboard.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 "USBHostMouseKeyboard.h"
00018 
00019 #if  USBHOST_MOUSE_KEYBOARD
00020 
00021 //#define Keyb_German 1
00022 
00023 #ifdef Keyb_German
00024 static uint8_t keymap[4][0x39] = {
00025     { 0, 0, 0, 0, 'a', 'b' ,
00026       'c', 'd', 'e', 'f', 'g' ,
00027       'h', 'i', 'j', 'k', 'l',
00028       'm', 'n', 'o', 'p', 'q',
00029       'r', 's', 't', 'u', 'v',
00030       'w', 'x', 'y', 'z', '1',
00031       '2', '3', '4', '5', '6',
00032       '7', '8', '9', '0', 0x0A /*enter*/,
00033       0x1B /*escape*/, 0x08 /*backspace*/, 0x09/*tab*/, 0x20/*space*/, 'ß',
00034       '´', 'ü', '+', '<', '#',
00035       'ö', 'ä', 0, ',', '.',
00036       '-'},
00037 
00038     /* CTRL MODIFIER */
00039     { 0, 0, 0, 0, 0, 0 /*0x05*/,
00040       0, 0, 0, 0, 0 /*0x0a*/,
00041       0, 0, 0, 0, 0/*0x0f*/,
00042       0, 0, 0, 0, 0/*0x14*/,
00043       0, 0, 0, 0, 0/*0x19*/,
00044       0, 0, 0, 0, 0/*0x1E*/,
00045       0, 0, 0, 0, 0/*0x23*/,
00046       0, 0, 0, 0, 0 /*enter*/, /*0x28*/
00047       0, 0, 0, 0, 0, /*0x2d*/
00048       0, 0, 0, 0, 0, /*0x32*/
00049       0, 0, 0, 0, 0, /*0x37*/
00050       0},
00051 
00052     /* SHIFT MODIFIER */
00053     { 0, 0, 0, 0, 'A', 'B' /*0x05*/,
00054       'C', 'D', 'E', 'F', 'G' /*0x0a*/,
00055       'H', 'I', 'J', 'K', 'L'/*0x0f*/,
00056       'M', 'N', 'O', 'P', 'Q'/*0x14*/,
00057       'R', 'S', 'T', 'U', 'V'/*0x19*/,
00058       'W', 'X', 'Y', 'Z', '!'/*0x1E*/,
00059       '"', '§', '$', '%', '&'/*0x23*/,
00060       '/', '(', ')', '=', 0, /*0x28*/
00061       0, 0, 0, 0, '?', /*0x2d*/
00062       '`', 'Ü', '*', '>', '\'', /*0x32*/
00063       'Ö', 'Ä', 0, ';', ':', /*0x37*/
00064       '_'},
00065 
00066     /* ALT MODIFIER */
00067     { 0, 0, 0, 0, 0, 0 /*0x05*/,
00068       0, 0, 0, 0, 0 /*0x0a*/,
00069       0, 0, 0, 0, 0/*0x0f*/,
00070       'µ', 0, 0, 0, '@'/*0x14*/,
00071       0, 0, 0, 0, 0/*0x19*/,
00072       0, 0, 0, 0, 0/*0x1E*/,
00073       '²','³', 0, 0, 0/*0x23*/,
00074       '{', '[', ']', '}', 0 /*enter*/, /*0x28*/
00075       '\'', 0, 0, 0, '\\', /*0x2d*/
00076       0, 0, '~', '|', 0, /*0x32*/
00077       0, 0, 0, 0, 0, /*0x37*/
00078       0}
00079 
00080 };
00081 #else
00082 static uint8_t keymap[4][0x39] = {
00083     { 0, 0, 0, 0, 'a', 'b' /*0x05*/,
00084       'c', 'd', 'e', 'f', 'g' /*0x0a*/,
00085       'h', 'i', 'j', 'k', 'l'/*0x0f*/,
00086       'm', 'n', 'o', 'p', 'q'/*0x14*/,
00087       'r', 's', 't', 'u', 'v'/*0x19*/,
00088       'w', 'x', 'y', 'z', '1'/*0x1E*/,
00089       '2', '3', '4', '5', '6'/*0x23*/,
00090       '7', '8', '9', '0', 0x0A /*enter*/, /*0x28*/
00091       0x1B /*escape*/, 0x08 /*backspace*/, 0x09/*tab*/, 0x20/*space*/, '-', /*0x2d*/
00092       '=', '[', ']', '\\', '#', /*0x32*/
00093       ';', '\'', 0, ',', '.', /*0x37*/
00094       '/'},
00095 
00096     /* CTRL MODIFIER */
00097     { 0, 0, 0, 0, 0, 0 /*0x05*/,
00098       0, 0, 0, 0, 0 /*0x0a*/,
00099       0, 0, 0, 0, 0/*0x0f*/,
00100       0, 0, 0, 0, 0/*0x14*/,
00101       0, 0, 0, 0, 0/*0x19*/,
00102       0, 0, 0, 0, 0/*0x1E*/,
00103       0, 0, 0, 0, 0/*0x23*/,
00104       0, 0, 0, 0, 0 /*enter*/, /*0x28*/
00105       0, 0, 0, 0, 0, /*0x2d*/
00106       0, 0, 0, 0, 0, /*0x32*/
00107       0, 0, 0, 0, 0, /*0x37*/
00108       0},
00109 
00110     /* SHIFT MODIFIER */
00111     { 0, 0, 0, 0, 'A', 'B' /*0x05*/,
00112       'C', 'D', 'E', 'F', 'G' /*0x0a*/,
00113       'H', 'I', 'J', 'K', 'L'/*0x0f*/,
00114       'M', 'N', 'O', 'P', 'Q'/*0x14*/,
00115       'R', 'S', 'T', 'U', 'V'/*0x19*/,
00116       'W', 'X', 'Y', 'Z', '!'/*0x1E*/,
00117       '@', '#', '$', '%', '^'/*0x23*/,
00118       '&', '*', '(', ')', 0, /*0x28*/
00119       0, 0, 0, 0, 0, /*0x2d*/
00120       '+', '{', '}', '|', '~', /*0x32*/
00121       ':', '"', 0, '<', '>', /*0x37*/
00122       '?'},
00123 
00124     /* ALT MODIFIER */
00125     { 0, 0, 0, 0, 0, 0 /*0x05*/,
00126       0, 0, 0, 0, 0 /*0x0a*/,
00127       0, 0, 0, 0, 0/*0x0f*/,
00128       0, 0, 0, 0, 0/*0x14*/,
00129       0, 0, 0, 0, 0/*0x19*/,
00130       0, 0, 0, 0, 0/*0x1E*/,
00131       0, 0, 0, 0, 0/*0x23*/,
00132       0, 0, 0, 0, 0 /*enter*/, /*0x28*/
00133       0, 0, 0, 0, 0, /*0x2d*/
00134       0, 0, 0, 0, 0, /*0x32*/
00135       0, 0, 0, 0, 0, /*0x37*/
00136       0}
00137 
00138 };
00139 #endif
00140 
00141 USBHostMouseKb::USBHostMouseKb(int Interface) {
00142     host = USBHost::getHostInst(Interface);
00143     init();
00144 }
00145 
00146 
00147 void USBHostMouseKb::init() {
00148     dev = NULL;
00149     int_in_mouse = NULL;
00150     int_in_kb=NULL;
00151     report_id = 0;
00152     onUpdate = NULL;
00153     onButtonUpdate = NULL;
00154     onXUpdate = NULL;
00155     onYUpdate = NULL;
00156     onZUpdate = NULL;
00157     onKey = NULL;
00158     onKeyCode = NULL;
00159     dev_connected = false;
00160     mouse_intf = -1;
00161     kb_intf=-1;
00162     mouse_device_found = false;
00163     kb_device_found = false;
00164 }
00165 
00166 bool USBHostMouseKb::connected() {
00167     return dev_connected;
00168 }
00169 
00170 void USBHostMouseKb::poll() {
00171 host->poll();
00172 }
00173 
00174 bool USBHostMouseKb::connect() {
00175 
00176     if (dev_connected) {
00177         return true;
00178     }
00179 
00180     for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
00181         if ((dev = host->getDevice(i)) != NULL) {
00182 
00183             if (host->enumerate(dev, this))
00184                 break;
00185 
00186             if ((mouse_device_found)&&(mouse_intf>-1)) {
00187 
00188             int_in_mouse = dev->getEndpoint(mouse_intf, INTERRUPT_ENDPOINT, IN);
00189             if (!int_in_mouse)
00190                     return false;
00191             USB_INFO("New Mouse - device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, mouse_intf);
00192             dev->setName("Mouse", mouse_intf);
00193             host->registerDriver(dev, mouse_intf, this, &USBHostMouseKb::init);
00194             int_in_mouse->attach(this, &USBHostMouseKb::rxHandlerMouse);
00195             host->interruptRead(dev, int_in_mouse, report, int_in_mouse->getSize(), false);
00196             dev_connected = true;
00197             }
00198 
00199             if ((kb_device_found)&&(kb_intf>-1)) {
00200             int_in_kb = dev->getEndpoint(kb_intf, INTERRUPT_ENDPOINT, IN);
00201             if (!int_in_kb)
00202                     return false;
00203             USB_INFO("New Keyboard - device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, kb_intf);
00204             dev->setName("Keyboard", kb_intf);
00205             host->registerDriver(dev, kb_intf, this, &USBHostMouseKb::init);
00206             int_in_kb->attach(this, &USBHostMouseKb::rxHandlerKb);
00207             host->interruptRead(dev, int_in_kb, report, int_in_kb->getSize(), false);
00208             dev_connected = true;
00209             }
00210             if (dev_connected == true)return true;
00211         }
00212     }
00213     return false;
00214 }
00215 
00216 void USBHostMouseKb::rxHandlerMouse() {
00217     int len = int_in_mouse->getLengthTransferred();
00218     int len_listen = int_in_mouse->getSize();
00219     if (!((report[0]==0)&(report[1]==0)&(report[2]==0)&(report[3]==0)&(report[4]==0)&(report[5]==0)&(report[6]==0)&(report[7]==0)&(report[8]==0)))
00220         USB_INFO("RepMouse %d :%d :%d :%d :%d :%d :%d :%d :%d\r\n",report[0],report[1],report[2],report[3],report[4],report[5],report[6],report[7],report[8]);
00221 
00222     if (len<8)
00223     {
00224     if (onUpdate) {
00225         (*onUpdate)(report[0] & 0x07, report[1], report[2], report[3]);
00226     }
00227 
00228     if (onButtonUpdate && (buttons != (report[0] & 0x07))) {
00229         (*onButtonUpdate)(report[0] & 0x07);
00230     }
00231 
00232     if (onXUpdate && (x != report[1])) {
00233         (*onXUpdate)(report[1]);
00234     }
00235 
00236     if (onYUpdate && (y != report[2])) {
00237         (*onYUpdate)(report[2]);
00238     }
00239 
00240     if (onZUpdate && (z != report[3])) {
00241         (*onZUpdate)(report[3]);
00242     }
00243 
00244     // update mouse state
00245     buttons = report[0] & 0x07;
00246     x = report[1];
00247     y = report[2];
00248     z = report[3];
00249 
00250     if (dev)
00251         host->interruptRead(dev, int_in_mouse, report, len_listen, false);
00252         return;
00253     }
00254 
00255     if(report[0]==REPORT_ID_IDLE )
00256     {
00257      if (dev)
00258         host->interruptRead(dev, int_in_mouse, report, len_listen, false);
00259     }
00260 
00261     if(report[0]==REPORT_ID_MOUSE )
00262     {
00263     if (onUpdate) {
00264         (*onUpdate)(report[1] & 0x07, report[3], report[4], report[5]);
00265     }
00266 
00267     if (onButtonUpdate && (buttons != (report[1] & 0x07))) {
00268         (*onButtonUpdate)(report[1] & 0x07);
00269     }
00270 
00271     if (onXUpdate && (x != report[3])) {
00272         (*onXUpdate)(report[3]);
00273     }
00274 
00275     if (onYUpdate && (y != report[4])) {
00276         (*onYUpdate)(report[4]);
00277     }
00278 
00279     if (onZUpdate && (z != report[5])) {
00280         (*onZUpdate)(report[5]);
00281     }
00282 
00283     // update mouse state
00284     buttons = report[1] & 0x07;
00285     x = report[3];
00286     y = report[4];
00287     z = report[5];
00288 
00289     if (dev)
00290         host->interruptRead(dev, int_in_mouse, report, len_listen, false);
00291     }
00292 }
00293 
00294 void USBHostMouseKb::rxHandlerKb() {
00295     uint8_t key = 0;
00296     int len = int_in_kb->getLengthTransferred();
00297     int index = (len == 9) ? 1 : 0;
00298     int len_listen = int_in_kb->getSize();
00299     if (!((report[0]==0)&(report[1]==0)&(report[2]==0)&(report[3]==0)&(report[4]==0)&(report[5]==0)&(report[6]==0)&(report[7]==0)&(report[8]==0)))
00300         USB_INFO("RepKb %d :%d :%d :%d :%d :%d :%d :%d :%d\r\n",report[0],report[1],report[2],report[3],report[4],report[5],report[6],report[7],report[8]);
00301     if(report[0]==REPORT_ID_IDLE )
00302     {
00303      if (dev)
00304         host->interruptRead(dev, int_in_kb, report, len_listen, false);
00305     }
00306     if ((dev->getVid()==1133)&&(dev->getPid()==50475))
00307     {
00308       uint8_t modifier=report[0];
00309       if (report[7]!=0)
00310       {
00311         USB_INFO("RepKb  :0x%x\r\n",report[7]);
00312         if ((modifier==64)||(modifier==4))modifier=3;
00313         if(report[7]==0x64)report[7]=0x31;
00314         key = keymap[modifier][report[7]];
00315         if (key && onKey) {
00316             (*onKey)(key);
00317         }
00318         if ((report[7] || modifier) && onKeyCode) {
00319             (*onKeyCode)(report[7], modifier);
00320         }
00321       }
00322     }
00323     else
00324     {
00325     if (len == 8 || len == 9) {
00326         uint8_t modifier = (report[index] == 4) ? 3 : report[index];
00327         len_listen = len;
00328         key = keymap[modifier][report[index + 2]];
00329         if (key && onKey) {
00330             (*onKey)(key);
00331         }
00332         if ((report[index + 2] || modifier) && onKeyCode) {
00333             (*onKeyCode)(report[index + 2], modifier);
00334         }
00335     }
00336     }
00337     if (dev && int_in_kb)
00338         host->interruptRead(dev, int_in_kb, report, len_listen, false);
00339 }
00340 
00341 /*virtual*/ void USBHostMouseKb::setVidPid(uint16_t vid, uint16_t pid)
00342 {
00343     // we don't check VID/PID for keyboard driver
00344 }
00345 
00346 /*virtual*/ bool USBHostMouseKb::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
00347 {
00348     if ((mouse_intf == -1) &&
00349         (intf_class == HID_CLASS) &&
00350         (intf_subclass == 0x01) &&
00351         (intf_protocol == 0x02)) {
00352         mouse_intf = intf_nb;
00353         return true;
00354     }
00355     if ((kb_intf == -1) &&
00356         (intf_class == HID_CLASS) &&
00357         (intf_subclass == 0x01) &&
00358         (intf_protocol == 0x01)) {
00359         kb_intf = intf_nb;
00360         return true;
00361     }
00362     return false;
00363 }
00364 
00365 /*virtual*/ bool USBHostMouseKb::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
00366 {
00367     if (intf_nb == mouse_intf) {
00368         if (type == INTERRUPT_ENDPOINT && dir == IN) {
00369             mouse_device_found = true;
00370             return true;
00371         }
00372     }
00373     if (intf_nb == kb_intf) {
00374         if (type == INTERRUPT_ENDPOINT && dir == IN) {
00375             kb_device_found = true;
00376             return true;
00377         }
00378     }
00379     return false;
00380 }
00381 
00382 #endif