2018.07.26

Dependencies:   FATFileSystem2 mbed-rtos

Fork of USBHost by mbed official

Committer:
sayzyas
Date:
Thu Jul 26 00:29:30 2018 +0000
Revision:
44:e437b1c7c61e
Parent:
43:78f328f311dc
2018.07.26

Who changed what in which revision?

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