2018.07.26

Dependencies:   FATFileSystem3 mbed-rtos

Fork of USBHost by mbed official

Committer:
sayzyas
Date:
Tue Jun 02 05:57:44 2015 +0000
Revision:
32:e6717a485577
Parent:
27:4206883f4cb7
20150602

Who changed what in which revision?

UserRevisionLine numberNew 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() {
mbed_official 27:4206883f4cb7 50 int len_listen;
sayzyas 32:e6717a485577 51
mbed_official 0:a554658735bf 52 if (dev_connected) {
mbed_official 0:a554658735bf 53 return true;
mbed_official 0:a554658735bf 54 }
mbed_official 24:868cbfe611a7 55
samux 5:e48791a1ef18 56 for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
mbed_official 0:a554658735bf 57 if ((dev = host->getDevice(i)) != NULL) {
sayzyas 32:e6717a485577 58 if(host->enumerate(dev, this)){
mbed_official 0:a554658735bf 59 break;
sayzyas 32:e6717a485577 60 }
mbed_official 0:a554658735bf 61 if (mouse_device_found) {
mbed_official 0:a554658735bf 62 int_in = dev->getEndpoint(mouse_intf, INTERRUPT_ENDPOINT, IN);
sayzyas 32:e6717a485577 63 if (!int_in){
sayzyas 32:e6717a485577 64 USB_INFO("b");
mbed_official 0:a554658735bf 65 break;
sayzyas 32:e6717a485577 66 }
mbed_official 24:868cbfe611a7 67
samux 4:b320d68e98e7 68 USB_INFO("New Mouse device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, mouse_intf);
samux 4:b320d68e98e7 69 dev->setName("Mouse", mouse_intf);
mbed_official 0:a554658735bf 70 host->registerDriver(dev, mouse_intf, this, &USBHostMouse::init);
mbed_official 24:868cbfe611a7 71
mbed_official 0:a554658735bf 72 int_in->attach(this, &USBHostMouse::rxHandler);
mbed_official 27:4206883f4cb7 73 len_listen = int_in->getSize();
mbed_official 27:4206883f4cb7 74 if (len_listen > sizeof(report)) {
mbed_official 27:4206883f4cb7 75 len_listen = sizeof(report);
mbed_official 27:4206883f4cb7 76 }
mbed_official 27:4206883f4cb7 77 host->interruptRead(dev, int_in, report, len_listen, false);
mbed_official 24:868cbfe611a7 78
mbed_official 0:a554658735bf 79 dev_connected = true;
mbed_official 0:a554658735bf 80 return true;
mbed_official 0:a554658735bf 81 }
mbed_official 0:a554658735bf 82 }
mbed_official 0:a554658735bf 83 }
mbed_official 0:a554658735bf 84 init();
mbed_official 0:a554658735bf 85 return false;
mbed_official 0:a554658735bf 86 }
mbed_official 0:a554658735bf 87
mbed_official 0:a554658735bf 88 void USBHostMouse::rxHandler() {
sayzyas 32:e6717a485577 89 // このハンドラーでマウスイベントを取得している。
mbed_official 0:a554658735bf 90 int len_listen = int_in->getSize();
mbed_official 24:868cbfe611a7 91
mbed_official 0:a554658735bf 92 if (onUpdate) {
mbed_official 0:a554658735bf 93 (*onUpdate)(report[0] & 0x07, report[1], report[2], report[3]);
mbed_official 0:a554658735bf 94 }
mbed_official 24:868cbfe611a7 95
samux 5:e48791a1ef18 96 if (onButtonUpdate && (buttons != (report[0] & 0x07))) {
samux 5:e48791a1ef18 97 (*onButtonUpdate)(report[0] & 0x07);
samux 5:e48791a1ef18 98 }
mbed_official 24:868cbfe611a7 99
samux 5:e48791a1ef18 100 if (onXUpdate && (x != report[1])) {
samux 5:e48791a1ef18 101 (*onXUpdate)(report[1]);
samux 5:e48791a1ef18 102 }
mbed_official 24:868cbfe611a7 103
samux 5:e48791a1ef18 104 if (onYUpdate && (y != report[2])) {
samux 5:e48791a1ef18 105 (*onYUpdate)(report[2]);
samux 5:e48791a1ef18 106 }
mbed_official 24:868cbfe611a7 107
samux 5:e48791a1ef18 108 if (onZUpdate && (z != report[3])) {
samux 5:e48791a1ef18 109 (*onZUpdate)(report[3]);
samux 5:e48791a1ef18 110 }
mbed_official 24:868cbfe611a7 111
samux 5:e48791a1ef18 112 // update mouse state
samux 5:e48791a1ef18 113 buttons = report[0] & 0x07;
samux 5:e48791a1ef18 114 x = report[1];
samux 5:e48791a1ef18 115 y = report[2];
samux 5:e48791a1ef18 116 z = report[3];
mbed_official 24:868cbfe611a7 117
mbed_official 27:4206883f4cb7 118 if (len_listen > sizeof(report)) {
mbed_official 27:4206883f4cb7 119 len_listen = sizeof(report);
mbed_official 27:4206883f4cb7 120 }
mbed_official 27:4206883f4cb7 121
samux 5:e48791a1ef18 122 if (dev)
mbed_official 0:a554658735bf 123 host->interruptRead(dev, int_in, report, len_listen, false);
mbed_official 0:a554658735bf 124 }
mbed_official 0:a554658735bf 125
mbed_official 0:a554658735bf 126 /*virtual*/ void USBHostMouse::setVidPid(uint16_t vid, uint16_t pid)
mbed_official 0:a554658735bf 127 {
mbed_official 0:a554658735bf 128 // we don't check VID/PID for mouse driver
mbed_official 0:a554658735bf 129 }
mbed_official 0:a554658735bf 130
mbed_official 0:a554658735bf 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
mbed_official 0:a554658735bf 132 {
mbed_official 0:a554658735bf 133 if ((mouse_intf == -1) &&
mbed_official 0:a554658735bf 134 (intf_class == HID_CLASS) &&
mbed_official 0:a554658735bf 135 (intf_subclass == 0x01) &&
mbed_official 0:a554658735bf 136 (intf_protocol == 0x02)) {
mbed_official 0:a554658735bf 137 mouse_intf = intf_nb;
mbed_official 0:a554658735bf 138 return true;
mbed_official 0:a554658735bf 139 }
mbed_official 0:a554658735bf 140 return false;
mbed_official 0:a554658735bf 141 }
mbed_official 0:a554658735bf 142
mbed_official 0:a554658735bf 143 /*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 144 {
mbed_official 0:a554658735bf 145 if (intf_nb == mouse_intf) {
mbed_official 0:a554658735bf 146 if (type == INTERRUPT_ENDPOINT && dir == IN) {
mbed_official 0:a554658735bf 147 mouse_device_found = true;
mbed_official 0:a554658735bf 148 return true;
mbed_official 0:a554658735bf 149 }
mbed_official 0:a554658735bf 150 }
mbed_official 0:a554658735bf 151 return false;
mbed_official 0:a554658735bf 152 }
mbed_official 0:a554658735bf 153
mbed_official 0:a554658735bf 154 #endif