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