2018.07.26

Dependencies:   FATFileSystem3 mbed-rtos

Fork of USBHost by mbed official

Committer:
sayzyas
Date:
Thu Jul 26 00:20:32 2018 +0000
Revision:
43:1675750cca08
2018.07.26

Who changed what in which revision?

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