test public

Dependencies:   HttpServer_snapshot_mbed-os

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 #if defined(TARGET_RZ_A2XX)
00024     report  = (uint8_t *)AllocNonCacheMem(8);
00025 #endif
00026     init();
00027 }
00028 
00029 #if defined(TARGET_RZ_A2XX)
00030 USBHostMouse::~USBHostMouse() {
00031   FreeNonCacheMem(report);
00032 }
00033 #endif
00034 
00035 void USBHostMouse::init() {
00036     dev = NULL;
00037     int_in = NULL;
00038     onUpdate = NULL;
00039     onButtonUpdate = NULL;
00040     onXUpdate = NULL;
00041     onYUpdate = NULL;
00042     onZUpdate = NULL;
00043     report_id = 0;
00044     dev_connected = false;
00045     mouse_device_found = false;
00046     mouse_intf = -1;
00047 
00048     buttons = 0;
00049     x = 0;
00050     y = 0;
00051     z = 0;
00052 }
00053 
00054 bool USBHostMouse::connected() {
00055     return dev_connected;
00056 }
00057 
00058 bool USBHostMouse::connect() {
00059     int len_listen;
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                 len_listen = int_in->getSize();
00083 #if defined(TARGET_RZ_A2XX)
00084                 if ((uint32_t)len_listen > 8) {
00085                     len_listen = 8;
00086                 }
00087 #else
00088                 if ((uint32_t)len_listen > sizeof(report)) {
00089                     len_listen = sizeof(report);
00090                 }
00091 #endif
00092                 host->interruptRead(dev, int_in, report, len_listen, false);
00093 
00094                 dev_connected = true;
00095                 return true;
00096             }
00097         }
00098     }
00099     init();
00100     return false;
00101 }
00102 
00103 void USBHostMouse::rxHandler() {
00104     int len_listen = int_in->getSize();
00105 
00106     if ((dev->getVid() == 0x046d) && (dev->getPid() == 0xc52b)) {
00107         int wk_x;
00108         int wk_y;
00109 
00110         wk_x = ((report[4] & 0x0F) << 8) | report[3];
00111         if (wk_x <= 0x7ff) {
00112             if (wk_x > 0x7f) {
00113                 wk_x = 0x7f;
00114             }
00115         } else {
00116             wk_x = -(0xfff - wk_x);
00117             if (wk_x < -128) {
00118                 wk_x = -128;
00119             }
00120         }
00121 
00122         wk_y = (report[5] << 4) | ((report[4] & 0xF0) >> 4);
00123         if (wk_y <= 0x7ff) {
00124             if (wk_y > 0x7f) {
00125                 wk_y = 0x7f;
00126             }
00127         } else {
00128             wk_y = -(0xfff - wk_y);
00129             if (wk_y < -128) {
00130                 wk_y = -128;
00131             }
00132         }
00133 
00134         if (onUpdate) {
00135             (*onUpdate)(report[1] & 0x07, (int8_t)wk_x, (int8_t)wk_y, report[6]);
00136         }
00137 
00138         if (onButtonUpdate && (buttons != (report[1] & 0x07))) {
00139             (*onButtonUpdate)(report[1] & 0x07);
00140         }
00141 
00142         if (onXUpdate && (x != (int8_t)wk_x)) {
00143             (*onXUpdate)((int8_t)wk_x);
00144         }
00145 
00146         if (onYUpdate && (y != (int8_t)wk_y)) {
00147             (*onYUpdate)((int8_t)wk_y);
00148         }
00149 
00150         if (onZUpdate && (z != report[6])) {
00151             (*onZUpdate)(report[6]);
00152         }
00153 
00154         // update mouse state
00155         buttons = report[1] & 0x07;
00156 
00157         x = (int8_t)wk_x;
00158         y = (int8_t)wk_y;
00159         z = report[6];
00160     } else {
00161         if (onUpdate) {
00162             (*onUpdate)(report[0] & 0x07, report[1], report[2], report[3]);
00163         }
00164 
00165         if (onButtonUpdate && (buttons != (report[0] & 0x07))) {
00166             (*onButtonUpdate)(report[0] & 0x07);
00167         }
00168 
00169         if (onXUpdate && (x != report[1])) {
00170             (*onXUpdate)(report[1]);
00171         }
00172 
00173         if (onYUpdate && (y != report[2])) {
00174             (*onYUpdate)(report[2]);
00175         }
00176 
00177         if (onZUpdate && (z != report[3])) {
00178             (*onZUpdate)(report[3]);
00179         }
00180 
00181         // update mouse state
00182         buttons = report[0] & 0x07;
00183         x = report[1];
00184         y = report[2];
00185         z = report[3];
00186     }
00187 
00188 #if defined(TARGET_RZ_A2XX)
00189     if ((uint32_t)len_listen > 8) {
00190         len_listen = 8;
00191     }
00192 #else
00193     if ((uint32_t)len_listen > sizeof(report)) {
00194         len_listen = sizeof(report);
00195     }
00196 #endif
00197 
00198     if (dev)
00199         host->interruptRead(dev, int_in, report, len_listen, false);
00200 }
00201 
00202 /*virtual*/ void USBHostMouse::setVidPid(uint16_t vid, uint16_t pid)
00203 {
00204     // we don't check VID/PID for mouse driver
00205 }
00206 
00207 /*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
00208 {
00209     if ((mouse_intf == -1) &&
00210         (intf_class == HID_CLASS) &&
00211         (intf_subclass == 0x01) &&
00212         (intf_protocol == 0x02)) {
00213         mouse_intf = intf_nb;
00214         return true;
00215     }
00216     return false;
00217 }
00218 
00219 /*virtual*/ bool USBHostMouse::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
00220 {
00221     if (intf_nb == mouse_intf) {
00222         if (type == INTERRUPT_ENDPOINT && dir == IN) {
00223             mouse_device_found = true;
00224             return true;
00225         }
00226     }
00227     return false;
00228 }
00229 
00230 #endif