Zoltan Hudak / USBHost-STM32F4

Dependents:   STM32F407VET6_USBHostMSD STM32F407VET6_USBHostMouse STM32F407VET6_USBHostKeyboard STM32F407VET6_USBHostMSD_1

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 #include "USBHostMouse.h"
00017 
00018 #if USBHOST_MOUSE
00019 
00020 /**
00021  * @brief
00022  * @note
00023  * @param
00024  * @retval
00025  */
00026 USBHostMouse::USBHostMouse()
00027 {
00028     host = USBHost::getHostInst();
00029     init();
00030 }
00031 
00032 /**
00033  * @brief
00034  * @note
00035  * @param
00036  * @retval
00037  */
00038 void USBHostMouse::init()
00039 {
00040     dev = NULL;
00041     int_in = NULL;
00042     onUpdate = NULL;
00043     onButtonUpdate = NULL;
00044     onXUpdate = NULL;
00045     onYUpdate = NULL;
00046     onZUpdate = NULL;
00047     report_id = 0;
00048     dev_connected = false;
00049     mouse_device_found = false;
00050     mouse_intf = -1;
00051 
00052     buttons = 0;
00053     x = 0;
00054     y = 0;
00055     z = 0;
00056 }
00057 
00058 /**
00059  * @brief
00060  * @note
00061  * @param
00062  * @retval
00063  */
00064 bool USBHostMouse::connected()
00065 {
00066     return dev_connected;
00067 }
00068 
00069 /**
00070  * @brief
00071  * @note
00072  * @param
00073  * @retval
00074  */
00075 bool USBHostMouse::connect()
00076 {
00077     int len_listen;
00078 
00079     if (dev_connected) {
00080         return true;
00081     }
00082 
00083     for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
00084         if ((dev = host->getDevice(i)) != NULL) {
00085             if (host->enumerate(dev, this))
00086                 break;
00087 
00088             if (mouse_device_found) {
00089                 int_in = dev->getEndpoint(mouse_intf, INTERRUPT_ENDPOINT, IN);
00090                 if (!int_in)
00091                     break;
00092 
00093                 USB_INFO
00094                 (
00095                     "New Mouse device: VID:%04x PID:%04x [dev: %p - intf: %d]",
00096                     dev->getVid(),
00097                     dev->getPid(),
00098                     dev,
00099                     mouse_intf
00100                 );
00101                 dev->setName("Mouse", mouse_intf);
00102                 host->registerDriver(dev, mouse_intf, this, &USBHostMouse::init);
00103 
00104                 int_in->attach(this, &USBHostMouse::rxHandler);
00105                 len_listen = int_in->getSize();
00106                 if (len_listen > sizeof(report)) {
00107                     len_listen = sizeof(report);
00108                 }
00109 
00110                 host->interruptRead(dev, int_in, report, len_listen, false);
00111 
00112                 dev_connected = true;
00113                 return true;
00114             }
00115         }
00116     }
00117 
00118     init();
00119     return false;
00120 }
00121 
00122 /**
00123  * @brief
00124  * @note
00125  * @param
00126  * @retval
00127  */
00128 void USBHostMouse::rxHandler()
00129 {
00130     int len_listen = int_in->getSize();
00131 
00132     if (onUpdate) {
00133         (*onUpdate) (report[0] & 0x07, report[1], report[2], report[3]);
00134     }
00135 
00136     if (onButtonUpdate && (buttons != (report[0] & 0x07))) {
00137         (*onButtonUpdate) (report[0] & 0x07);
00138     }
00139 
00140     if (onXUpdate && (x != report[1])) {
00141         (*onXUpdate) (report[1]);
00142     }
00143 
00144     if (onYUpdate && (y != report[2])) {
00145         (*onYUpdate) (report[2]);
00146     }
00147 
00148     if (onZUpdate && (z != report[3])) {
00149         (*onZUpdate) (report[3]);
00150     }
00151 
00152     // update mouse state
00153     buttons = report[0] & 0x07;
00154     x = report[1];
00155     y = report[2];
00156     z = report[3];
00157 
00158     if (dev)
00159         host->interruptRead(dev, int_in, report, len_listen, false);
00160 }
00161 
00162 /*virtual*/
00163 void USBHostMouse::setVidPid(uint16_t vid, uint16_t pid)
00164 {
00165     // we don't check VID/PID for mouse driver
00166 }
00167 
00168 /*virtual*/
00169 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
00170 {
00171     if ((mouse_intf == -1) && (intf_class == HID_CLASS) && (intf_subclass == 0x01) && (intf_protocol == 0x02)) {
00172         mouse_intf = intf_nb;
00173         return true;
00174     }
00175 
00176     return false;
00177 }
00178 
00179 /*virtual*/
00180 bool USBHostMouse::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
00181 {
00182     if (intf_nb == mouse_intf) {
00183         if (type == INTERRUPT_ENDPOINT && dir == IN) {
00184             mouse_device_found = true;
00185             return true;
00186         }
00187     }
00188 
00189     return false;
00190 }
00191 #endif