keyboard->mbed->PC (mbed->SDcard)
Fork of USBHost by
USBHostHID/USBHostMouse.cpp@24:868cbfe611a7, 2014-06-03 (annotated)
- Committer:
- mbed_official
- Date:
- Tue Jun 03 11:30:38 2014 +0100
- Revision:
- 24:868cbfe611a7
- Parent:
- 13:b58a2204422f
- Child:
- 27:4206883f4cb7
Synchronized with git revision bcacbb9fbf3432829227430830cca4315b57c1b9
Full URL: https://github.com/mbedmicro/mbed/commit/bcacbb9fbf3432829227430830cca4315b57c1b9/
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() { |
samux | 5:e48791a1ef18 | 50 | |
mbed_official | 0:a554658735bf | 51 | if (dev_connected) { |
mbed_official | 0:a554658735bf | 52 | return true; |
mbed_official | 0:a554658735bf | 53 | } |
mbed_official | 24:868cbfe611a7 | 54 | |
samux | 5:e48791a1ef18 | 55 | for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { |
mbed_official | 0:a554658735bf | 56 | if ((dev = host->getDevice(i)) != NULL) { |
mbed_official | 0:a554658735bf | 57 | |
mbed_official | 0:a554658735bf | 58 | if(host->enumerate(dev, this)) |
mbed_official | 0:a554658735bf | 59 | break; |
mbed_official | 24:868cbfe611a7 | 60 | |
mbed_official | 0:a554658735bf | 61 | if (mouse_device_found) { |
mbed_official | 24:868cbfe611a7 | 62 | |
mbed_official | 0:a554658735bf | 63 | int_in = dev->getEndpoint(mouse_intf, INTERRUPT_ENDPOINT, IN); |
mbed_official | 0:a554658735bf | 64 | if (!int_in) |
mbed_official | 0:a554658735bf | 65 | break; |
mbed_official | 24:868cbfe611a7 | 66 | |
samux | 4:b320d68e98e7 | 67 | USB_INFO("New Mouse device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, mouse_intf); |
samux | 4:b320d68e98e7 | 68 | dev->setName("Mouse", mouse_intf); |
mbed_official | 0:a554658735bf | 69 | host->registerDriver(dev, mouse_intf, this, &USBHostMouse::init); |
mbed_official | 24:868cbfe611a7 | 70 | |
mbed_official | 0:a554658735bf | 71 | int_in->attach(this, &USBHostMouse::rxHandler); |
mbed_official | 0:a554658735bf | 72 | host->interruptRead(dev, int_in, report, int_in->getSize(), false); |
mbed_official | 24:868cbfe611a7 | 73 | |
mbed_official | 0:a554658735bf | 74 | dev_connected = true; |
mbed_official | 0:a554658735bf | 75 | return true; |
mbed_official | 0:a554658735bf | 76 | } |
mbed_official | 0:a554658735bf | 77 | } |
mbed_official | 0:a554658735bf | 78 | } |
mbed_official | 0:a554658735bf | 79 | init(); |
mbed_official | 0:a554658735bf | 80 | return false; |
mbed_official | 0:a554658735bf | 81 | } |
mbed_official | 0:a554658735bf | 82 | |
mbed_official | 0:a554658735bf | 83 | void USBHostMouse::rxHandler() { |
mbed_official | 0:a554658735bf | 84 | int len_listen = int_in->getSize(); |
mbed_official | 24:868cbfe611a7 | 85 | |
mbed_official | 0:a554658735bf | 86 | if (onUpdate) { |
mbed_official | 0:a554658735bf | 87 | (*onUpdate)(report[0] & 0x07, report[1], report[2], report[3]); |
mbed_official | 0:a554658735bf | 88 | } |
mbed_official | 24:868cbfe611a7 | 89 | |
samux | 5:e48791a1ef18 | 90 | if (onButtonUpdate && (buttons != (report[0] & 0x07))) { |
samux | 5:e48791a1ef18 | 91 | (*onButtonUpdate)(report[0] & 0x07); |
samux | 5:e48791a1ef18 | 92 | } |
mbed_official | 24:868cbfe611a7 | 93 | |
samux | 5:e48791a1ef18 | 94 | if (onXUpdate && (x != report[1])) { |
samux | 5:e48791a1ef18 | 95 | (*onXUpdate)(report[1]); |
samux | 5:e48791a1ef18 | 96 | } |
mbed_official | 24:868cbfe611a7 | 97 | |
samux | 5:e48791a1ef18 | 98 | if (onYUpdate && (y != report[2])) { |
samux | 5:e48791a1ef18 | 99 | (*onYUpdate)(report[2]); |
samux | 5:e48791a1ef18 | 100 | } |
mbed_official | 24:868cbfe611a7 | 101 | |
samux | 5:e48791a1ef18 | 102 | if (onZUpdate && (z != report[3])) { |
samux | 5:e48791a1ef18 | 103 | (*onZUpdate)(report[3]); |
samux | 5:e48791a1ef18 | 104 | } |
mbed_official | 24:868cbfe611a7 | 105 | |
samux | 5:e48791a1ef18 | 106 | // update mouse state |
samux | 5:e48791a1ef18 | 107 | buttons = report[0] & 0x07; |
samux | 5:e48791a1ef18 | 108 | x = report[1]; |
samux | 5:e48791a1ef18 | 109 | y = report[2]; |
samux | 5:e48791a1ef18 | 110 | z = report[3]; |
mbed_official | 24:868cbfe611a7 | 111 | |
samux | 5:e48791a1ef18 | 112 | if (dev) |
mbed_official | 0:a554658735bf | 113 | host->interruptRead(dev, int_in, report, len_listen, false); |
mbed_official | 0:a554658735bf | 114 | } |
mbed_official | 0:a554658735bf | 115 | |
mbed_official | 0:a554658735bf | 116 | /*virtual*/ void USBHostMouse::setVidPid(uint16_t vid, uint16_t pid) |
mbed_official | 0:a554658735bf | 117 | { |
mbed_official | 0:a554658735bf | 118 | // we don't check VID/PID for mouse driver |
mbed_official | 0:a554658735bf | 119 | } |
mbed_official | 0:a554658735bf | 120 | |
mbed_official | 0:a554658735bf | 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 |
mbed_official | 0:a554658735bf | 122 | { |
mbed_official | 0:a554658735bf | 123 | if ((mouse_intf == -1) && |
mbed_official | 0:a554658735bf | 124 | (intf_class == HID_CLASS) && |
mbed_official | 0:a554658735bf | 125 | (intf_subclass == 0x01) && |
mbed_official | 0:a554658735bf | 126 | (intf_protocol == 0x02)) { |
mbed_official | 0:a554658735bf | 127 | mouse_intf = intf_nb; |
mbed_official | 0:a554658735bf | 128 | return true; |
mbed_official | 0:a554658735bf | 129 | } |
mbed_official | 0:a554658735bf | 130 | return false; |
mbed_official | 0:a554658735bf | 131 | } |
mbed_official | 0:a554658735bf | 132 | |
mbed_official | 0:a554658735bf | 133 | /*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 | 134 | { |
mbed_official | 0:a554658735bf | 135 | if (intf_nb == mouse_intf) { |
mbed_official | 0:a554658735bf | 136 | if (type == INTERRUPT_ENDPOINT && dir == IN) { |
mbed_official | 0:a554658735bf | 137 | mouse_device_found = true; |
mbed_official | 0:a554658735bf | 138 | return true; |
mbed_official | 0:a554658735bf | 139 | } |
mbed_official | 0:a554658735bf | 140 | } |
mbed_official | 0:a554658735bf | 141 | return false; |
mbed_official | 0:a554658735bf | 142 | } |
mbed_official | 0:a554658735bf | 143 | |
mbed_official | 0:a554658735bf | 144 | #endif |