Henry Leinen / USBHost_ParadigmaSerialDevice

Dependencies:   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 
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     int len = int_in->getLengthTransferred();
00086     
00087     if (onUpdate) {
00088         (*onUpdate)(report[0] & 0x07, report[1], report[2], report[3]);
00089     }
00090     
00091     if (onButtonUpdate && (buttons != (report[0] & 0x07))) {
00092         (*onButtonUpdate)(report[0] & 0x07);
00093     }
00094     
00095     if (onXUpdate && (x != report[1])) {
00096         (*onXUpdate)(report[1]);
00097     }
00098     
00099     if (onYUpdate && (y != report[2])) {
00100         (*onYUpdate)(report[2]);
00101     }
00102     
00103     if (onZUpdate && (z != report[3])) {
00104         (*onZUpdate)(report[3]);
00105     }
00106         
00107     // update mouse state
00108     buttons = report[0] & 0x07;
00109     x = report[1];
00110     y = report[2];
00111     z = report[3];
00112     
00113     if (dev)
00114         host->interruptRead(dev, int_in, report, len_listen, false);
00115 }
00116 
00117 /*virtual*/ void USBHostMouse::setVidPid(uint16_t vid, uint16_t pid)
00118 {
00119     // we don't check VID/PID for mouse driver
00120 }
00121 
00122 /*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
00123 {
00124     if ((mouse_intf == -1) &&
00125         (intf_class == HID_CLASS) &&
00126         (intf_subclass == 0x01) &&
00127         (intf_protocol == 0x02)) {
00128         mouse_intf = intf_nb;
00129         return true;
00130     }
00131     return false;
00132 }
00133 
00134 /*virtual*/ bool USBHostMouse::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
00135 {
00136     if (intf_nb == mouse_intf) {
00137         if (type == INTERRUPT_ENDPOINT && dir == IN) {
00138             mouse_device_found = true;
00139             return true;
00140         }
00141     }
00142     return false;
00143 }
00144 
00145 #endif