X-TOUCH to djay bridge

Dependencies:   mbed mbed-rtos FATFileSystem

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