Adaptation of the official mbed USBHost repository to work with the LPC4088 Display Module

Dependents:   DMSupport DMSupport DMSupport DMSupport

Fork of DM_USBHost by EmbeddedArtists AB

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     report = host->getSafeMem(4);
00026 }
00027 
00028 USBHostMouse::~USBHostMouse()
00029 {
00030     if (report != NULL) {
00031         host->returnSafeMem(report);
00032         report = NULL;
00033     }
00034 }
00035 
00036 void USBHostMouse::init() {
00037     dev = NULL;
00038     int_in = NULL;
00039     onUpdate = NULL;
00040     onButtonUpdate = NULL;
00041     onXUpdate = NULL;
00042     onYUpdate = NULL;
00043     onZUpdate = NULL;
00044     report_id = 0;
00045     dev_connected = false;
00046     mouse_device_found = false;
00047     mouse_intf = -1;
00048 
00049     buttons = 0;
00050     x = 0;
00051     y = 0;
00052     z = 0;
00053 }
00054 
00055 bool USBHostMouse::connected() {
00056     return dev_connected;
00057 }
00058 
00059 bool USBHostMouse::connect() {
00060 
00061     if (dev_connected) {
00062         return true;
00063     }
00064 
00065     for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
00066         if ((dev = host->getDevice(i)) != NULL) {
00067 
00068             if(host->enumerate(dev, this))
00069                 break;
00070 
00071             if (mouse_device_found) {
00072 
00073                 int_in = dev->getEndpoint(mouse_intf, INTERRUPT_ENDPOINT, IN);
00074                 if (!int_in)
00075                     break;
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                 host->interruptRead(dev, int_in, report, int_in->getSize(), false);
00083 
00084                 dev_connected = true;
00085                 return true;
00086             }
00087         }
00088     }
00089     init();
00090     return false;
00091 }
00092 
00093 void USBHostMouse::rxHandler() {
00094     int len_listen = int_in->getSize();
00095 
00096     if (onUpdate) {
00097         (*onUpdate)(report[0] & 0x07, report[1], report[2], report[3]);
00098     }
00099 
00100     if (onButtonUpdate && (buttons != (report[0] & 0x07))) {
00101         (*onButtonUpdate)(report[0] & 0x07);
00102     }
00103 
00104     if (onXUpdate && (x != report[1])) {
00105         (*onXUpdate)(report[1]);
00106     }
00107 
00108     if (onYUpdate && (y != report[2])) {
00109         (*onYUpdate)(report[2]);
00110     }
00111 
00112     if (onZUpdate && (z != report[3])) {
00113         (*onZUpdate)(report[3]);
00114     }
00115 
00116     // update mouse state
00117     buttons = report[0] & 0x07;
00118     x = report[1];
00119     y = report[2];
00120     z = report[3];
00121 
00122     if (dev) {
00123         host->interruptRead(dev, int_in, report, len_listen, false);
00124     }
00125 }
00126 
00127 /*virtual*/ void USBHostMouse::setVidPid(uint16_t vid, uint16_t pid)
00128 {
00129     // we don't check VID/PID for mouse driver
00130 }
00131 
00132 /*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
00133 {
00134     if ((mouse_intf == -1) &&
00135         (intf_class == HID_CLASS) &&
00136         (intf_subclass == 0x01) &&
00137         (intf_protocol == 0x02)) {
00138         mouse_intf = intf_nb;
00139         return true;
00140     }
00141     return false;
00142 }
00143 
00144 /*virtual*/ bool USBHostMouse::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
00145 {
00146     if (intf_nb == mouse_intf) {
00147         if (type == INTERRUPT_ENDPOINT && dir == IN) {
00148             mouse_device_found = true;
00149             return true;
00150         }
00151     }
00152     return false;
00153 }
00154 
00155 #endif