USBHost library

Dependencies:   FATFileSystem

Dependents:   x4180_Tank Totaleprogramma Handmatig Totaleprogramma1

Fork of USBHost by mbed official

Committer:
samux
Date:
Thu Mar 14 11:49:53 2013 +0000
Revision:
8:93da8ea2708b
Parent:
5:e48791a1ef18
Child:
13:b58a2204422f
update doc + license

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;
samux 5:e48791a1ef18 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 0:a554658735bf 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 0:a554658735bf 60
mbed_official 0:a554658735bf 61 if (mouse_device_found) {
mbed_official 0:a554658735bf 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 0:a554658735bf 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 0:a554658735bf 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 0:a554658735bf 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 0:a554658735bf 85 int len = int_in->getLengthTransferred();
samux 5:e48791a1ef18 86
mbed_official 0:a554658735bf 87 if (onUpdate) {
mbed_official 0:a554658735bf 88 (*onUpdate)(report[0] & 0x07, report[1], report[2], report[3]);
mbed_official 0:a554658735bf 89 }
samux 5:e48791a1ef18 90
samux 5:e48791a1ef18 91 if (onButtonUpdate && (buttons != (report[0] & 0x07))) {
samux 5:e48791a1ef18 92 (*onButtonUpdate)(report[0] & 0x07);
samux 5:e48791a1ef18 93 }
samux 5:e48791a1ef18 94
samux 5:e48791a1ef18 95 if (onXUpdate && (x != report[1])) {
samux 5:e48791a1ef18 96 (*onXUpdate)(report[1]);
samux 5:e48791a1ef18 97 }
samux 5:e48791a1ef18 98
samux 5:e48791a1ef18 99 if (onYUpdate && (y != report[2])) {
samux 5:e48791a1ef18 100 (*onYUpdate)(report[2]);
samux 5:e48791a1ef18 101 }
samux 5:e48791a1ef18 102
samux 5:e48791a1ef18 103 if (onZUpdate && (z != report[3])) {
samux 5:e48791a1ef18 104 (*onZUpdate)(report[3]);
samux 5:e48791a1ef18 105 }
samux 5:e48791a1ef18 106
samux 5:e48791a1ef18 107 // update mouse state
samux 5:e48791a1ef18 108 buttons = report[0] & 0x07;
samux 5:e48791a1ef18 109 x = report[1];
samux 5:e48791a1ef18 110 y = report[2];
samux 5:e48791a1ef18 111 z = report[3];
samux 5:e48791a1ef18 112
samux 5:e48791a1ef18 113 if (dev)
mbed_official 0:a554658735bf 114 host->interruptRead(dev, int_in, report, len_listen, false);
mbed_official 0:a554658735bf 115 }
mbed_official 0:a554658735bf 116
mbed_official 0:a554658735bf 117 /*virtual*/ void USBHostMouse::setVidPid(uint16_t vid, uint16_t pid)
mbed_official 0:a554658735bf 118 {
mbed_official 0:a554658735bf 119 // we don't check VID/PID for mouse driver
mbed_official 0:a554658735bf 120 }
mbed_official 0:a554658735bf 121
mbed_official 0:a554658735bf 122 /*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 123 {
mbed_official 0:a554658735bf 124 if ((mouse_intf == -1) &&
mbed_official 0:a554658735bf 125 (intf_class == HID_CLASS) &&
mbed_official 0:a554658735bf 126 (intf_subclass == 0x01) &&
mbed_official 0:a554658735bf 127 (intf_protocol == 0x02)) {
mbed_official 0:a554658735bf 128 mouse_intf = intf_nb;
mbed_official 0:a554658735bf 129 return true;
mbed_official 0:a554658735bf 130 }
mbed_official 0:a554658735bf 131 return false;
mbed_official 0:a554658735bf 132 }
mbed_official 0:a554658735bf 133
mbed_official 0:a554658735bf 134 /*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 135 {
mbed_official 0:a554658735bf 136 if (intf_nb == mouse_intf) {
mbed_official 0:a554658735bf 137 if (type == INTERRUPT_ENDPOINT && dir == IN) {
mbed_official 0:a554658735bf 138 mouse_device_found = true;
mbed_official 0:a554658735bf 139 return true;
mbed_official 0:a554658735bf 140 }
mbed_official 0:a554658735bf 141 }
mbed_official 0:a554658735bf 142 return false;
mbed_official 0:a554658735bf 143 }
mbed_official 0:a554658735bf 144
mbed_official 0:a554658735bf 145 #endif