USBHost library

Dependencies:   FATFileSystem

Dependents:   Project RoboticsCatAndMouse_HumanDriver RoboticsCatAndMouse_AutonomousDriver

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 
00051     if (dev_connected) {
00052         return true;
00053     }
00054     
00055     for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
00056         if ((dev = host->getDevice(i)) != NULL) {
00057 
00058             if(host->enumerate(dev, this))
00059                 break;
00060             
00061             if (mouse_device_found) {
00062                 
00063                 int_in = dev->getEndpoint(mouse_intf, INTERRUPT_ENDPOINT, IN);
00064                 if (!int_in)
00065                     break;
00066                 
00067                 USB_INFO("New Mouse device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, mouse_intf);
00068                 dev->setName("Mouse", mouse_intf);
00069                 host->registerDriver(dev, mouse_intf, this, &USBHostMouse::init);
00070                 
00071                 int_in->attach(this, &USBHostMouse::rxHandler);
00072                 host->interruptRead(dev, int_in, report, int_in->getSize(), false);
00073                 
00074                 dev_connected = true;
00075                 return true;
00076             }
00077         }
00078     }
00079     init();
00080     return false;
00081 }
00082 
00083 void USBHostMouse::rxHandler() {
00084     int len_listen = int_in->getSize();
00085     
00086     if (onUpdate) {
00087         (*onUpdate)(report[0] & 0x07, report[1], report[2], report[3]);
00088     }
00089     
00090     if (onButtonUpdate && (buttons != (report[0] & 0x07))) {
00091         (*onButtonUpdate)(report[0] & 0x07);
00092     }
00093     
00094     if (onXUpdate && (x != report[1])) {
00095         (*onXUpdate)(report[1]);
00096     }
00097     
00098     if (onYUpdate && (y != report[2])) {
00099         (*onYUpdate)(report[2]);
00100     }
00101     
00102     if (onZUpdate && (z != report[3])) {
00103         (*onZUpdate)(report[3]);
00104     }
00105         
00106     // update mouse state
00107     buttons = report[0] & 0x07;
00108     x = report[1];
00109     y = report[2];
00110     z = report[3];
00111     
00112     if (dev)
00113         host->interruptRead(dev, int_in, report, len_listen, false);
00114 }
00115 
00116 /*virtual*/ void USBHostMouse::setVidPid(uint16_t vid, uint16_t pid)
00117 {
00118     // we don't check VID/PID for mouse driver
00119 }
00120 
00121 /*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
00122 {
00123     if ((mouse_intf == -1) &&
00124         (intf_class == HID_CLASS) &&
00125         (intf_subclass == 0x01) &&
00126         (intf_protocol == 0x02)) {
00127         mouse_intf = intf_nb;
00128         return true;
00129     }
00130     return false;
00131 }
00132 
00133 /*virtual*/ bool USBHostMouse::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
00134 {
00135     if (intf_nb == mouse_intf) {
00136         if (type == INTERRUPT_ENDPOINT && dir == IN) {
00137             mouse_device_found = true;
00138             return true;
00139         }
00140     }
00141     return false;
00142 }
00143 
00144 #endif