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