mbed-os

Fork of mbed-os by erkin yucel

Committer:
xuaner
Date:
Thu Jul 20 14:26:57 2017 +0000
Revision:
1:3deb71413561
Parent:
0:f269e3021894
mbed_os

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:f269e3021894 1 /* mbed USBHost Library
elessair 0:f269e3021894 2 * Copyright (c) 2006-2013 ARM Limited
elessair 0:f269e3021894 3 *
elessair 0:f269e3021894 4 * Licensed under the Apache License, Version 2.0 (the "License");
elessair 0:f269e3021894 5 * you may not use this file except in compliance with the License.
elessair 0:f269e3021894 6 * You may obtain a copy of the License at
elessair 0:f269e3021894 7 *
elessair 0:f269e3021894 8 * http://www.apache.org/licenses/LICENSE-2.0
elessair 0:f269e3021894 9 *
elessair 0:f269e3021894 10 * Unless required by applicable law or agreed to in writing, software
elessair 0:f269e3021894 11 * distributed under the License is distributed on an "AS IS" BASIS,
elessair 0:f269e3021894 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
elessair 0:f269e3021894 13 * See the License for the specific language governing permissions and
elessair 0:f269e3021894 14 * limitations under the License.
elessair 0:f269e3021894 15 */
elessair 0:f269e3021894 16
elessair 0:f269e3021894 17 #include "USBHostMouse.h"
elessair 0:f269e3021894 18
elessair 0:f269e3021894 19 #if USBHOST_MOUSE
elessair 0:f269e3021894 20
elessair 0:f269e3021894 21 USBHostMouse::USBHostMouse() {
elessair 0:f269e3021894 22 host = USBHost::getHostInst();
elessair 0:f269e3021894 23 init();
elessair 0:f269e3021894 24 }
elessair 0:f269e3021894 25
elessair 0:f269e3021894 26 void USBHostMouse::init() {
elessair 0:f269e3021894 27 dev = NULL;
elessair 0:f269e3021894 28 int_in = NULL;
elessair 0:f269e3021894 29 onUpdate = NULL;
elessair 0:f269e3021894 30 onButtonUpdate = NULL;
elessair 0:f269e3021894 31 onXUpdate = NULL;
elessair 0:f269e3021894 32 onYUpdate = NULL;
elessair 0:f269e3021894 33 onZUpdate = NULL;
elessair 0:f269e3021894 34 report_id = 0;
elessair 0:f269e3021894 35 dev_connected = false;
elessair 0:f269e3021894 36 mouse_device_found = false;
elessair 0:f269e3021894 37 mouse_intf = -1;
elessair 0:f269e3021894 38
elessair 0:f269e3021894 39 buttons = 0;
elessair 0:f269e3021894 40 x = 0;
elessair 0:f269e3021894 41 y = 0;
elessair 0:f269e3021894 42 z = 0;
elessair 0:f269e3021894 43 }
elessair 0:f269e3021894 44
elessair 0:f269e3021894 45 bool USBHostMouse::connected() {
elessair 0:f269e3021894 46 return dev_connected;
elessair 0:f269e3021894 47 }
elessair 0:f269e3021894 48
elessair 0:f269e3021894 49 bool USBHostMouse::connect() {
elessair 0:f269e3021894 50 int len_listen;
elessair 0:f269e3021894 51
elessair 0:f269e3021894 52 if (dev_connected) {
elessair 0:f269e3021894 53 return true;
elessair 0:f269e3021894 54 }
elessair 0:f269e3021894 55
elessair 0:f269e3021894 56 for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
elessair 0:f269e3021894 57 if ((dev = host->getDevice(i)) != NULL) {
elessair 0:f269e3021894 58
elessair 0:f269e3021894 59 if(host->enumerate(dev, this))
elessair 0:f269e3021894 60 break;
elessair 0:f269e3021894 61
elessair 0:f269e3021894 62 if (mouse_device_found) {
elessair 0:f269e3021894 63
elessair 0:f269e3021894 64 int_in = dev->getEndpoint(mouse_intf, INTERRUPT_ENDPOINT, IN);
elessair 0:f269e3021894 65 if (!int_in)
elessair 0:f269e3021894 66 break;
elessair 0:f269e3021894 67
elessair 0:f269e3021894 68 USB_INFO("New Mouse device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, mouse_intf);
elessair 0:f269e3021894 69 dev->setName("Mouse", mouse_intf);
elessair 0:f269e3021894 70 host->registerDriver(dev, mouse_intf, this, &USBHostMouse::init);
elessair 0:f269e3021894 71
elessair 0:f269e3021894 72 int_in->attach(this, &USBHostMouse::rxHandler);
elessair 0:f269e3021894 73 len_listen = int_in->getSize();
elessair 0:f269e3021894 74 if (len_listen > sizeof(report)) {
elessair 0:f269e3021894 75 len_listen = sizeof(report);
elessair 0:f269e3021894 76 }
elessair 0:f269e3021894 77 host->interruptRead(dev, int_in, report, len_listen, false);
elessair 0:f269e3021894 78
elessair 0:f269e3021894 79 dev_connected = true;
elessair 0:f269e3021894 80 return true;
elessair 0:f269e3021894 81 }
elessair 0:f269e3021894 82 }
elessair 0:f269e3021894 83 }
elessair 0:f269e3021894 84 init();
elessair 0:f269e3021894 85 return false;
elessair 0:f269e3021894 86 }
elessair 0:f269e3021894 87
elessair 0:f269e3021894 88 void USBHostMouse::rxHandler() {
elessair 0:f269e3021894 89 int len_listen = int_in->getSize();
elessair 0:f269e3021894 90
elessair 0:f269e3021894 91 if (onUpdate) {
elessair 0:f269e3021894 92 (*onUpdate)(report[0] & 0x07, report[1], report[2], report[3]);
elessair 0:f269e3021894 93 }
elessair 0:f269e3021894 94
elessair 0:f269e3021894 95 if (onButtonUpdate && (buttons != (report[0] & 0x07))) {
elessair 0:f269e3021894 96 (*onButtonUpdate)(report[0] & 0x07);
elessair 0:f269e3021894 97 }
elessair 0:f269e3021894 98
elessair 0:f269e3021894 99 if (onXUpdate && (x != report[1])) {
elessair 0:f269e3021894 100 (*onXUpdate)(report[1]);
elessair 0:f269e3021894 101 }
elessair 0:f269e3021894 102
elessair 0:f269e3021894 103 if (onYUpdate && (y != report[2])) {
elessair 0:f269e3021894 104 (*onYUpdate)(report[2]);
elessair 0:f269e3021894 105 }
elessair 0:f269e3021894 106
elessair 0:f269e3021894 107 if (onZUpdate && (z != report[3])) {
elessair 0:f269e3021894 108 (*onZUpdate)(report[3]);
elessair 0:f269e3021894 109 }
elessair 0:f269e3021894 110
elessair 0:f269e3021894 111 // update mouse state
elessair 0:f269e3021894 112 buttons = report[0] & 0x07;
elessair 0:f269e3021894 113 x = report[1];
elessair 0:f269e3021894 114 y = report[2];
elessair 0:f269e3021894 115 z = report[3];
elessair 0:f269e3021894 116
elessair 0:f269e3021894 117 if (len_listen > sizeof(report)) {
elessair 0:f269e3021894 118 len_listen = sizeof(report);
elessair 0:f269e3021894 119 }
elessair 0:f269e3021894 120
elessair 0:f269e3021894 121 if (dev)
elessair 0:f269e3021894 122 host->interruptRead(dev, int_in, report, len_listen, false);
elessair 0:f269e3021894 123 }
elessair 0:f269e3021894 124
elessair 0:f269e3021894 125 /*virtual*/ void USBHostMouse::setVidPid(uint16_t vid, uint16_t pid)
elessair 0:f269e3021894 126 {
elessair 0:f269e3021894 127 // we don't check VID/PID for mouse driver
elessair 0:f269e3021894 128 }
elessair 0:f269e3021894 129
elessair 0:f269e3021894 130 /*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
elessair 0:f269e3021894 131 {
elessair 0:f269e3021894 132 if ((mouse_intf == -1) &&
elessair 0:f269e3021894 133 (intf_class == HID_CLASS) &&
elessair 0:f269e3021894 134 (intf_subclass == 0x01) &&
elessair 0:f269e3021894 135 (intf_protocol == 0x02)) {
elessair 0:f269e3021894 136 mouse_intf = intf_nb;
elessair 0:f269e3021894 137 return true;
elessair 0:f269e3021894 138 }
elessair 0:f269e3021894 139 return false;
elessair 0:f269e3021894 140 }
elessair 0:f269e3021894 141
elessair 0:f269e3021894 142 /*virtual*/ bool USBHostMouse::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
elessair 0:f269e3021894 143 {
elessair 0:f269e3021894 144 if (intf_nb == mouse_intf) {
elessair 0:f269e3021894 145 if (type == INTERRUPT_ENDPOINT && dir == IN) {
elessair 0:f269e3021894 146 mouse_device_found = true;
elessair 0:f269e3021894 147 return true;
elessair 0:f269e3021894 148 }
elessair 0:f269e3021894 149 }
elessair 0:f269e3021894 150 return false;
elessair 0:f269e3021894 151 }
elessair 0:f269e3021894 152
elessair 0:f269e3021894 153 #endif