2018.07.26

Dependencies:   FATFileSystem2 mbed-rtos

Fork of USBHost by mbed official

Committer:
samux
Date:
Wed Mar 13 10:23:01 2013 +0000
Revision:
5:e48791a1ef18
Parent:
4:b320d68e98e7
Child:
8:93da8ea2708b
add button, x, y, z event callbacks

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:a554658735bf 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
mbed_official 0:a554658735bf 2 *
mbed_official 0:a554658735bf 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
mbed_official 0:a554658735bf 4 * and associated documentation files (the "Software"), to deal in the Software without
mbed_official 0:a554658735bf 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
mbed_official 0:a554658735bf 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
mbed_official 0:a554658735bf 7 * Software is furnished to do so, subject to the following conditions:
mbed_official 0:a554658735bf 8 *
mbed_official 0:a554658735bf 9 * The above copyright notice and this permission notice shall be included in all copies or
mbed_official 0:a554658735bf 10 * substantial portions of the Software.
mbed_official 0:a554658735bf 11 *
mbed_official 0:a554658735bf 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
mbed_official 0:a554658735bf 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
mbed_official 0:a554658735bf 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
mbed_official 0:a554658735bf 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
mbed_official 0:a554658735bf 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
mbed_official 0:a554658735bf 17 */
mbed_official 0:a554658735bf 18
mbed_official 0:a554658735bf 19 #include "USBHostMouse.h"
mbed_official 0:a554658735bf 20
mbed_official 0:a554658735bf 21 #if USBHOST_MOUSE
mbed_official 0:a554658735bf 22
mbed_official 0:a554658735bf 23 USBHostMouse::USBHostMouse() {
mbed_official 0:a554658735bf 24 host = USBHost::getHostInst();
mbed_official 0:a554658735bf 25 init();
mbed_official 0:a554658735bf 26 }
mbed_official 0:a554658735bf 27
mbed_official 0:a554658735bf 28 void USBHostMouse::init() {
mbed_official 0:a554658735bf 29 dev = NULL;
mbed_official 0:a554658735bf 30 int_in = NULL;
mbed_official 0:a554658735bf 31 onUpdate = NULL;
samux 5:e48791a1ef18 32 onButtonUpdate = NULL;
samux 5:e48791a1ef18 33 onXUpdate = NULL;
samux 5:e48791a1ef18 34 onYUpdate = NULL;
samux 5:e48791a1ef18 35 onZUpdate = NULL;
mbed_official 0:a554658735bf 36 report_id = 0;
mbed_official 0:a554658735bf 37 dev_connected = false;
mbed_official 0:a554658735bf 38 mouse_device_found = false;
mbed_official 0:a554658735bf 39 mouse_intf = -1;
samux 5:e48791a1ef18 40
samux 5:e48791a1ef18 41 buttons = 0;
samux 5:e48791a1ef18 42 x = 0;
samux 5:e48791a1ef18 43 y = 0;
samux 5:e48791a1ef18 44 z = 0;
mbed_official 0:a554658735bf 45 }
mbed_official 0:a554658735bf 46
mbed_official 0:a554658735bf 47 bool USBHostMouse::connected() {
mbed_official 0:a554658735bf 48 return dev_connected;
mbed_official 0:a554658735bf 49 }
mbed_official 0:a554658735bf 50
mbed_official 0:a554658735bf 51 bool USBHostMouse::connect() {
samux 5:e48791a1ef18 52
mbed_official 0:a554658735bf 53 if (dev_connected) {
mbed_official 0:a554658735bf 54 return true;
mbed_official 0:a554658735bf 55 }
mbed_official 0:a554658735bf 56
samux 5:e48791a1ef18 57 for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
mbed_official 0:a554658735bf 58 if ((dev = host->getDevice(i)) != NULL) {
mbed_official 0:a554658735bf 59
mbed_official 0:a554658735bf 60 if(host->enumerate(dev, this))
mbed_official 0:a554658735bf 61 break;
mbed_official 0:a554658735bf 62
mbed_official 0:a554658735bf 63 if (mouse_device_found) {
mbed_official 0:a554658735bf 64
mbed_official 0:a554658735bf 65 int_in = dev->getEndpoint(mouse_intf, INTERRUPT_ENDPOINT, IN);
mbed_official 0:a554658735bf 66 if (!int_in)
mbed_official 0:a554658735bf 67 break;
mbed_official 0:a554658735bf 68
samux 4:b320d68e98e7 69 USB_INFO("New Mouse device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, mouse_intf);
samux 4:b320d68e98e7 70 dev->setName("Mouse", mouse_intf);
mbed_official 0:a554658735bf 71 host->registerDriver(dev, mouse_intf, this, &USBHostMouse::init);
mbed_official 0:a554658735bf 72
mbed_official 0:a554658735bf 73 int_in->attach(this, &USBHostMouse::rxHandler);
mbed_official 0:a554658735bf 74 host->interruptRead(dev, int_in, report, int_in->getSize(), false);
mbed_official 0:a554658735bf 75
mbed_official 0:a554658735bf 76 dev_connected = true;
mbed_official 0:a554658735bf 77 return true;
mbed_official 0:a554658735bf 78 }
mbed_official 0:a554658735bf 79 }
mbed_official 0:a554658735bf 80 }
mbed_official 0:a554658735bf 81 init();
mbed_official 0:a554658735bf 82 return false;
mbed_official 0:a554658735bf 83 }
mbed_official 0:a554658735bf 84
mbed_official 0:a554658735bf 85 void USBHostMouse::rxHandler() {
mbed_official 0:a554658735bf 86 int len_listen = int_in->getSize();
mbed_official 0:a554658735bf 87 int len = int_in->getLengthTransferred();
samux 5:e48791a1ef18 88
mbed_official 0:a554658735bf 89 if (onUpdate) {
mbed_official 0:a554658735bf 90 (*onUpdate)(report[0] & 0x07, report[1], report[2], report[3]);
mbed_official 0:a554658735bf 91 }
samux 5:e48791a1ef18 92
samux 5:e48791a1ef18 93 if (onButtonUpdate && (buttons != (report[0] & 0x07))) {
samux 5:e48791a1ef18 94 (*onButtonUpdate)(report[0] & 0x07);
samux 5:e48791a1ef18 95 }
samux 5:e48791a1ef18 96
samux 5:e48791a1ef18 97 if (onXUpdate && (x != report[1])) {
samux 5:e48791a1ef18 98 (*onXUpdate)(report[1]);
samux 5:e48791a1ef18 99 }
samux 5:e48791a1ef18 100
samux 5:e48791a1ef18 101 if (onYUpdate && (y != report[2])) {
samux 5:e48791a1ef18 102 (*onYUpdate)(report[2]);
samux 5:e48791a1ef18 103 }
samux 5:e48791a1ef18 104
samux 5:e48791a1ef18 105 if (onZUpdate && (z != report[3])) {
samux 5:e48791a1ef18 106 (*onZUpdate)(report[3]);
samux 5:e48791a1ef18 107 }
samux 5:e48791a1ef18 108
samux 5:e48791a1ef18 109 // update mouse state
samux 5:e48791a1ef18 110 buttons = report[0] & 0x07;
samux 5:e48791a1ef18 111 x = report[1];
samux 5:e48791a1ef18 112 y = report[2];
samux 5:e48791a1ef18 113 z = report[3];
samux 5:e48791a1ef18 114
samux 5:e48791a1ef18 115 if (dev)
mbed_official 0:a554658735bf 116 host->interruptRead(dev, int_in, report, len_listen, false);
mbed_official 0:a554658735bf 117 }
mbed_official 0:a554658735bf 118
mbed_official 0:a554658735bf 119 /*virtual*/ void USBHostMouse::setVidPid(uint16_t vid, uint16_t pid)
mbed_official 0:a554658735bf 120 {
mbed_official 0:a554658735bf 121 // we don't check VID/PID for mouse driver
mbed_official 0:a554658735bf 122 }
mbed_official 0:a554658735bf 123
mbed_official 0:a554658735bf 124 /*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
mbed_official 0:a554658735bf 125 {
mbed_official 0:a554658735bf 126 if ((mouse_intf == -1) &&
mbed_official 0:a554658735bf 127 (intf_class == HID_CLASS) &&
mbed_official 0:a554658735bf 128 (intf_subclass == 0x01) &&
mbed_official 0:a554658735bf 129 (intf_protocol == 0x02)) {
mbed_official 0:a554658735bf 130 mouse_intf = intf_nb;
mbed_official 0:a554658735bf 131 return true;
mbed_official 0:a554658735bf 132 }
mbed_official 0:a554658735bf 133 return false;
mbed_official 0:a554658735bf 134 }
mbed_official 0:a554658735bf 135
mbed_official 0:a554658735bf 136 /*virtual*/ bool USBHostMouse::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
mbed_official 0:a554658735bf 137 {
mbed_official 0:a554658735bf 138 if (intf_nb == mouse_intf) {
mbed_official 0:a554658735bf 139 if (type == INTERRUPT_ENDPOINT && dir == IN) {
mbed_official 0:a554658735bf 140 mouse_device_found = true;
mbed_official 0:a554658735bf 141 return true;
mbed_official 0:a554658735bf 142 }
mbed_official 0:a554658735bf 143 }
mbed_official 0:a554658735bf 144 return false;
mbed_official 0:a554658735bf 145 }
mbed_official 0:a554658735bf 146
mbed_official 0:a554658735bf 147 #endif