Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of USBHost by
USBHostHID/USBHostMouse.cpp@33:0675edbac47d, 2015-11-07 (annotated)
- Committer:
- ksekimoto
- Date:
- Sat Nov 07 12:21:03 2015 +0000
- Revision:
- 33:0675edbac47d
- Parent:
- 27:4206883f4cb7
The first version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 8:93da8ea2708b | 1 | /* mbed USBHost Library |
samux | 8:93da8ea2708b | 2 | * Copyright (c) 2006-2013 ARM Limited |
samux | 8:93da8ea2708b | 3 | * |
samux | 8:93da8ea2708b | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
samux | 8:93da8ea2708b | 5 | * you may not use this file except in compliance with the License. |
samux | 8:93da8ea2708b | 6 | * You may obtain a copy of the License at |
samux | 8:93da8ea2708b | 7 | * |
samux | 8:93da8ea2708b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
samux | 8:93da8ea2708b | 9 | * |
samux | 8:93da8ea2708b | 10 | * Unless required by applicable law or agreed to in writing, software |
samux | 8:93da8ea2708b | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
samux | 8:93da8ea2708b | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
samux | 8:93da8ea2708b | 13 | * See the License for the specific language governing permissions and |
samux | 8:93da8ea2708b | 14 | * limitations under the License. |
samux | 8:93da8ea2708b | 15 | */ |
mbed_official | 0:a554658735bf | 16 | |
mbed_official | 0:a554658735bf | 17 | #include "USBHostMouse.h" |
mbed_official | 0:a554658735bf | 18 | |
mbed_official | 0:a554658735bf | 19 | #if USBHOST_MOUSE |
mbed_official | 0:a554658735bf | 20 | |
mbed_official | 0:a554658735bf | 21 | USBHostMouse::USBHostMouse() { |
mbed_official | 0:a554658735bf | 22 | host = USBHost::getHostInst(); |
mbed_official | 0:a554658735bf | 23 | init(); |
mbed_official | 0:a554658735bf | 24 | } |
mbed_official | 0:a554658735bf | 25 | |
mbed_official | 0:a554658735bf | 26 | void USBHostMouse::init() { |
mbed_official | 0:a554658735bf | 27 | dev = NULL; |
mbed_official | 0:a554658735bf | 28 | int_in = NULL; |
mbed_official | 0:a554658735bf | 29 | onUpdate = NULL; |
samux | 5:e48791a1ef18 | 30 | onButtonUpdate = NULL; |
samux | 5:e48791a1ef18 | 31 | onXUpdate = NULL; |
samux | 5:e48791a1ef18 | 32 | onYUpdate = NULL; |
samux | 5:e48791a1ef18 | 33 | onZUpdate = NULL; |
mbed_official | 0:a554658735bf | 34 | report_id = 0; |
mbed_official | 0:a554658735bf | 35 | dev_connected = false; |
mbed_official | 0:a554658735bf | 36 | mouse_device_found = false; |
mbed_official | 0:a554658735bf | 37 | mouse_intf = -1; |
mbed_official | 24:868cbfe611a7 | 38 | |
samux | 5:e48791a1ef18 | 39 | buttons = 0; |
samux | 5:e48791a1ef18 | 40 | x = 0; |
samux | 5:e48791a1ef18 | 41 | y = 0; |
samux | 5:e48791a1ef18 | 42 | z = 0; |
mbed_official | 0:a554658735bf | 43 | } |
mbed_official | 0:a554658735bf | 44 | |
mbed_official | 0:a554658735bf | 45 | bool USBHostMouse::connected() { |
mbed_official | 0:a554658735bf | 46 | return dev_connected; |
mbed_official | 0:a554658735bf | 47 | } |
mbed_official | 0:a554658735bf | 48 | |
mbed_official | 0:a554658735bf | 49 | bool USBHostMouse::connect() { |
mbed_official | 27:4206883f4cb7 | 50 | int len_listen; |
samux | 5:e48791a1ef18 | 51 | |
mbed_official | 0:a554658735bf | 52 | if (dev_connected) { |
mbed_official | 0:a554658735bf | 53 | return true; |
mbed_official | 0:a554658735bf | 54 | } |
mbed_official | 24:868cbfe611a7 | 55 | |
samux | 5:e48791a1ef18 | 56 | for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { |
mbed_official | 0:a554658735bf | 57 | if ((dev = host->getDevice(i)) != NULL) { |
mbed_official | 0:a554658735bf | 58 | |
mbed_official | 0:a554658735bf | 59 | if(host->enumerate(dev, this)) |
mbed_official | 0:a554658735bf | 60 | break; |
mbed_official | 24:868cbfe611a7 | 61 | |
mbed_official | 0:a554658735bf | 62 | if (mouse_device_found) { |
mbed_official | 24:868cbfe611a7 | 63 | |
mbed_official | 0:a554658735bf | 64 | int_in = dev->getEndpoint(mouse_intf, INTERRUPT_ENDPOINT, IN); |
mbed_official | 0:a554658735bf | 65 | if (!int_in) |
mbed_official | 0:a554658735bf | 66 | break; |
mbed_official | 24:868cbfe611a7 | 67 | |
samux | 4:b320d68e98e7 | 68 | USB_INFO("New Mouse device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, mouse_intf); |
samux | 4:b320d68e98e7 | 69 | dev->setName("Mouse", mouse_intf); |
mbed_official | 0:a554658735bf | 70 | host->registerDriver(dev, mouse_intf, this, &USBHostMouse::init); |
mbed_official | 24:868cbfe611a7 | 71 | |
mbed_official | 0:a554658735bf | 72 | int_in->attach(this, &USBHostMouse::rxHandler); |
mbed_official | 27:4206883f4cb7 | 73 | len_listen = int_in->getSize(); |
mbed_official | 27:4206883f4cb7 | 74 | if (len_listen > sizeof(report)) { |
mbed_official | 27:4206883f4cb7 | 75 | len_listen = sizeof(report); |
mbed_official | 27:4206883f4cb7 | 76 | } |
mbed_official | 27:4206883f4cb7 | 77 | host->interruptRead(dev, int_in, report, len_listen, false); |
mbed_official | 24:868cbfe611a7 | 78 | |
mbed_official | 0:a554658735bf | 79 | dev_connected = true; |
mbed_official | 0:a554658735bf | 80 | return true; |
mbed_official | 0:a554658735bf | 81 | } |
mbed_official | 0:a554658735bf | 82 | } |
mbed_official | 0:a554658735bf | 83 | } |
mbed_official | 0:a554658735bf | 84 | init(); |
mbed_official | 0:a554658735bf | 85 | return false; |
mbed_official | 0:a554658735bf | 86 | } |
mbed_official | 0:a554658735bf | 87 | |
mbed_official | 0:a554658735bf | 88 | void USBHostMouse::rxHandler() { |
mbed_official | 0:a554658735bf | 89 | int len_listen = int_in->getSize(); |
mbed_official | 24:868cbfe611a7 | 90 | |
mbed_official | 0:a554658735bf | 91 | if (onUpdate) { |
mbed_official | 0:a554658735bf | 92 | (*onUpdate)(report[0] & 0x07, report[1], report[2], report[3]); |
mbed_official | 0:a554658735bf | 93 | } |
mbed_official | 24:868cbfe611a7 | 94 | |
samux | 5:e48791a1ef18 | 95 | if (onButtonUpdate && (buttons != (report[0] & 0x07))) { |
samux | 5:e48791a1ef18 | 96 | (*onButtonUpdate)(report[0] & 0x07); |
samux | 5:e48791a1ef18 | 97 | } |
mbed_official | 24:868cbfe611a7 | 98 | |
samux | 5:e48791a1ef18 | 99 | if (onXUpdate && (x != report[1])) { |
samux | 5:e48791a1ef18 | 100 | (*onXUpdate)(report[1]); |
samux | 5:e48791a1ef18 | 101 | } |
mbed_official | 24:868cbfe611a7 | 102 | |
samux | 5:e48791a1ef18 | 103 | if (onYUpdate && (y != report[2])) { |
samux | 5:e48791a1ef18 | 104 | (*onYUpdate)(report[2]); |
samux | 5:e48791a1ef18 | 105 | } |
mbed_official | 24:868cbfe611a7 | 106 | |
samux | 5:e48791a1ef18 | 107 | if (onZUpdate && (z != report[3])) { |
samux | 5:e48791a1ef18 | 108 | (*onZUpdate)(report[3]); |
samux | 5:e48791a1ef18 | 109 | } |
mbed_official | 24:868cbfe611a7 | 110 | |
samux | 5:e48791a1ef18 | 111 | // update mouse state |
samux | 5:e48791a1ef18 | 112 | buttons = report[0] & 0x07; |
samux | 5:e48791a1ef18 | 113 | x = report[1]; |
samux | 5:e48791a1ef18 | 114 | y = report[2]; |
samux | 5:e48791a1ef18 | 115 | z = report[3]; |
mbed_official | 24:868cbfe611a7 | 116 | |
mbed_official | 27:4206883f4cb7 | 117 | if (len_listen > sizeof(report)) { |
mbed_official | 27:4206883f4cb7 | 118 | len_listen = sizeof(report); |
mbed_official | 27:4206883f4cb7 | 119 | } |
mbed_official | 27:4206883f4cb7 | 120 | |
samux | 5:e48791a1ef18 | 121 | if (dev) |
mbed_official | 0:a554658735bf | 122 | host->interruptRead(dev, int_in, report, len_listen, false); |
mbed_official | 0:a554658735bf | 123 | } |
mbed_official | 0:a554658735bf | 124 | |
mbed_official | 0:a554658735bf | 125 | /*virtual*/ void USBHostMouse::setVidPid(uint16_t vid, uint16_t pid) |
mbed_official | 0:a554658735bf | 126 | { |
mbed_official | 0:a554658735bf | 127 | // we don't check VID/PID for mouse driver |
mbed_official | 0:a554658735bf | 128 | } |
mbed_official | 0:a554658735bf | 129 | |
mbed_official | 0:a554658735bf | 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 |
mbed_official | 0:a554658735bf | 131 | { |
mbed_official | 0:a554658735bf | 132 | if ((mouse_intf == -1) && |
mbed_official | 0:a554658735bf | 133 | (intf_class == HID_CLASS) && |
mbed_official | 0:a554658735bf | 134 | (intf_subclass == 0x01) && |
mbed_official | 0:a554658735bf | 135 | (intf_protocol == 0x02)) { |
mbed_official | 0:a554658735bf | 136 | mouse_intf = intf_nb; |
mbed_official | 0:a554658735bf | 137 | return true; |
mbed_official | 0:a554658735bf | 138 | } |
mbed_official | 0:a554658735bf | 139 | return false; |
mbed_official | 0:a554658735bf | 140 | } |
mbed_official | 0:a554658735bf | 141 | |
mbed_official | 0:a554658735bf | 142 | /*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 | 143 | { |
mbed_official | 0:a554658735bf | 144 | if (intf_nb == mouse_intf) { |
mbed_official | 0:a554658735bf | 145 | if (type == INTERRUPT_ENDPOINT && dir == IN) { |
mbed_official | 0:a554658735bf | 146 | mouse_device_found = true; |
mbed_official | 0:a554658735bf | 147 | return true; |
mbed_official | 0:a554658735bf | 148 | } |
mbed_official | 0:a554658735bf | 149 | } |
mbed_official | 0:a554658735bf | 150 | return false; |
mbed_official | 0:a554658735bf | 151 | } |
mbed_official | 0:a554658735bf | 152 | |
mbed_official | 0:a554658735bf | 153 | #endif |