z ysaito / USBHost2

Dependencies:   FATFileSystem2 mbed-rtos

Fork of USBHost by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBHostMouse.cpp Source File

USBHostMouse.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 "USBHostMouse.h"
00018 
00019 #if USBHOST_MOUSE
00020 
00021 USBHostMouse::USBHostMouse() {
00022     host = USBHost::getHostInst();
00023     init();
00024 }
00025 
00026 void USBHostMouse::init() {
00027     dev = NULL;
00028     int_in = NULL;
00029     onUpdate = NULL;
00030     onButtonUpdate = NULL;
00031     onXUpdate = NULL;
00032     onYUpdate = NULL;
00033     onZUpdate = NULL;
00034     report_id = 0;
00035     dev_connected = false;
00036     mouse_device_found = false;
00037     mouse_intf = -1;
00038 
00039     buttons = 0;
00040     x = 0;
00041     y = 0;
00042     z = 0;
00043 }
00044 
00045 bool USBHostMouse::connected() {
00046     return dev_connected;
00047 }
00048 
00049 bool USBHostMouse::connect() {
00050     int len_listen;
00051     
00052     if (dev_connected) {
00053         return true;
00054     }
00055 
00056     for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
00057         if ((dev = host->getDevice(i)) != NULL) {
00058             if(host->enumerate(dev, this)){
00059                 break;
00060             }
00061             if (mouse_device_found) {
00062                 int_in = dev->getEndpoint(mouse_intf, INTERRUPT_ENDPOINT, IN);
00063                 if (!int_in){
00064                     USB_INFO("b");
00065                     break;
00066                 }
00067 
00068                 USB_INFO("New Mouse device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, mouse_intf);
00069                 dev->setName("Mouse", mouse_intf);
00070                 host->registerDriver(dev, mouse_intf, this, &USBHostMouse::init);
00071 
00072                 int_in->attach(this, &USBHostMouse::rxHandler);
00073                 len_listen = int_in->getSize();
00074                 if (len_listen > sizeof(report)) {
00075                     len_listen = sizeof(report);
00076                 }
00077                 host->interruptRead(dev, int_in, report, len_listen, false);
00078 
00079                 dev_connected = true;
00080                 return true;
00081             }
00082         }
00083     }
00084     init();
00085     return false;
00086 }
00087 
00088 void USBHostMouse::rxHandler() {
00089     // このハンドラーでマウスイベントを取得している。
00090     int len_listen = int_in->getSize();
00091 
00092     if (onUpdate) {
00093         (*onUpdate)(report[0] & 0x07, report[1], report[2], report[3]);
00094     }
00095 
00096     if (onButtonUpdate && (buttons != (report[0] & 0x07))) {
00097         (*onButtonUpdate)(report[0] & 0x07);
00098     }
00099 
00100     if (onXUpdate && (x != report[1])) {
00101         (*onXUpdate)(report[1]);
00102     }
00103 
00104     if (onYUpdate && (y != report[2])) {
00105         (*onYUpdate)(report[2]);
00106     }
00107 
00108     if (onZUpdate && (z != report[3])) {
00109         (*onZUpdate)(report[3]);
00110     }
00111 
00112     // update mouse state
00113     buttons = report[0] & 0x07;
00114     x = report[1];
00115     y = report[2];
00116     z = report[3];
00117 
00118     if (len_listen > sizeof(report)) {
00119         len_listen = sizeof(report);
00120     }
00121 
00122     if (dev)
00123         host->interruptRead(dev, int_in, report, len_listen, false);
00124 }
00125 
00126 /*virtual*/ void USBHostMouse::setVidPid(uint16_t vid, uint16_t pid)
00127 {
00128     // we don't check VID/PID for mouse driver
00129 }
00130 
00131 /*virtual*/ bool USBHostMouse::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
00132 {
00133     if ((mouse_intf == -1) &&
00134         (intf_class == HID_CLASS) &&
00135         (intf_subclass == 0x01) &&
00136         (intf_protocol == 0x02)) {
00137         mouse_intf = intf_nb;
00138         return true;
00139     }
00140     return false;
00141 }
00142 
00143 /*virtual*/ bool USBHostMouse::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
00144 {
00145     if (intf_nb == mouse_intf) {
00146         if (type == INTERRUPT_ENDPOINT && dir == IN) {
00147             mouse_device_found = true;
00148             return true;
00149         }
00150     }
00151     return false;
00152 }
00153 
00154 #endif