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