USBHOST lib for STM

Dependents:   door-access-controller-dev

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