keyboard->mbed->PC (mbed->SDcard)

Dependencies:   mbed-rtos

Fork of USBHost by mbed official

Committer:
Kojto
Date:
Thu Jul 20 10:13:56 2017 +0100
Revision:
37:f1e388e7b752
Parent:
27:4206883f4cb7
Update libraries (ed9d1da)

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
Kojto 37:f1e388e7b752 21 USBHostMouse::USBHostMouse()
Kojto 37:f1e388e7b752 22 {
mbed_official 0:a554658735bf 23 host = USBHost::getHostInst();
mbed_official 0:a554658735bf 24 init();
mbed_official 0:a554658735bf 25 }
mbed_official 0:a554658735bf 26
Kojto 37:f1e388e7b752 27 void USBHostMouse::init()
Kojto 37:f1e388e7b752 28 {
mbed_official 0:a554658735bf 29 dev = NULL;
mbed_official 0:a554658735bf 30 int_in = NULL;
mbed_official 0:a554658735bf 31 onUpdate = NULL;
samux 5:e48791a1ef18 32 onButtonUpdate = NULL;
samux 5:e48791a1ef18 33 onXUpdate = NULL;
samux 5:e48791a1ef18 34 onYUpdate = NULL;
samux 5:e48791a1ef18 35 onZUpdate = NULL;
mbed_official 0:a554658735bf 36 report_id = 0;
mbed_official 0:a554658735bf 37 dev_connected = false;
mbed_official 0:a554658735bf 38 mouse_device_found = false;
mbed_official 0:a554658735bf 39 mouse_intf = -1;
mbed_official 24:868cbfe611a7 40
samux 5:e48791a1ef18 41 buttons = 0;
samux 5:e48791a1ef18 42 x = 0;
samux 5:e48791a1ef18 43 y = 0;
samux 5:e48791a1ef18 44 z = 0;
mbed_official 0:a554658735bf 45 }
mbed_official 0:a554658735bf 46
Kojto 37:f1e388e7b752 47 bool USBHostMouse::connected()
Kojto 37:f1e388e7b752 48 {
mbed_official 0:a554658735bf 49 return dev_connected;
mbed_official 0:a554658735bf 50 }
mbed_official 0:a554658735bf 51
Kojto 37:f1e388e7b752 52 bool USBHostMouse::connect()
Kojto 37:f1e388e7b752 53 {
mbed_official 27:4206883f4cb7 54 int len_listen;
samux 5:e48791a1ef18 55
mbed_official 0:a554658735bf 56 if (dev_connected) {
mbed_official 0:a554658735bf 57 return true;
mbed_official 0:a554658735bf 58 }
mbed_official 24:868cbfe611a7 59
samux 5:e48791a1ef18 60 for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
mbed_official 0:a554658735bf 61 if ((dev = host->getDevice(i)) != NULL) {
mbed_official 0:a554658735bf 62
Kojto 37:f1e388e7b752 63 if(host->enumerate(dev, this)) {
mbed_official 0:a554658735bf 64 break;
Kojto 37:f1e388e7b752 65 }
mbed_official 0:a554658735bf 66 if (mouse_device_found) {
Kojto 37:f1e388e7b752 67 {
Kojto 37:f1e388e7b752 68 /* As this is done in a specific thread
Kojto 37:f1e388e7b752 69 * this lock is taken to avoid to process the device
Kojto 37:f1e388e7b752 70 * disconnect in usb process during the device registering */
Kojto 37:f1e388e7b752 71 USBHost::Lock Lock(host);
Kojto 37:f1e388e7b752 72 int_in = dev->getEndpoint(mouse_intf, INTERRUPT_ENDPOINT, IN);
Kojto 37:f1e388e7b752 73 if (!int_in) {
Kojto 37:f1e388e7b752 74 break;
Kojto 37:f1e388e7b752 75 }
mbed_official 24:868cbfe611a7 76
Kojto 37:f1e388e7b752 77 USB_INFO("New Mouse device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, mouse_intf);
Kojto 37:f1e388e7b752 78 dev->setName("Mouse", mouse_intf);
Kojto 37:f1e388e7b752 79 host->registerDriver(dev, mouse_intf, this, &USBHostMouse::init);
mbed_official 24:868cbfe611a7 80
Kojto 37:f1e388e7b752 81 int_in->attach(this, &USBHostMouse::rxHandler);
Kojto 37:f1e388e7b752 82 len_listen = int_in->getSize();
Kojto 37:f1e388e7b752 83 if (len_listen > sizeof(report)) {
Kojto 37:f1e388e7b752 84 len_listen = sizeof(report);
Kojto 37:f1e388e7b752 85 }
mbed_official 27:4206883f4cb7 86 }
Kojto 37:f1e388e7b752 87 int ret=host->interruptRead(dev, int_in, report, len_listen, false);
Kojto 37:f1e388e7b752 88 MBED_ASSERT((ret==USB_TYPE_OK) || (ret ==USB_TYPE_PROCESSING) || (ret == USB_TYPE_FREE));
Kojto 37:f1e388e7b752 89 if ((ret==USB_TYPE_OK) || (ret ==USB_TYPE_PROCESSING)) {
Kojto 37:f1e388e7b752 90 dev_connected = true;
Kojto 37:f1e388e7b752 91 }
Kojto 37:f1e388e7b752 92 if (ret == USB_TYPE_FREE) {
Kojto 37:f1e388e7b752 93 dev_connected = false;
Kojto 37:f1e388e7b752 94 }
mbed_official 0:a554658735bf 95 return true;
mbed_official 0:a554658735bf 96 }
mbed_official 0:a554658735bf 97 }
mbed_official 0:a554658735bf 98 }
mbed_official 0:a554658735bf 99 init();
mbed_official 0:a554658735bf 100 return false;
mbed_official 0:a554658735bf 101 }
mbed_official 0:a554658735bf 102
Kojto 37:f1e388e7b752 103 void USBHostMouse::rxHandler()
Kojto 37:f1e388e7b752 104 {
Kojto 37:f1e388e7b752 105 int len_listen = int_in->getLengthTransferred();
Kojto 37:f1e388e7b752 106 if (len_listen !=0) {
mbed_official 24:868cbfe611a7 107
Kojto 37:f1e388e7b752 108 if (onUpdate) {
Kojto 37:f1e388e7b752 109 (*onUpdate)(report[0] & 0x07, report[1], report[2], report[3]);
Kojto 37:f1e388e7b752 110 }
Kojto 37:f1e388e7b752 111
Kojto 37:f1e388e7b752 112 if (onButtonUpdate && (buttons != (report[0] & 0x07))) {
Kojto 37:f1e388e7b752 113 (*onButtonUpdate)(report[0] & 0x07);
Kojto 37:f1e388e7b752 114 }
mbed_official 24:868cbfe611a7 115
Kojto 37:f1e388e7b752 116 if (onXUpdate && (x != report[1])) {
Kojto 37:f1e388e7b752 117 (*onXUpdate)(report[1]);
Kojto 37:f1e388e7b752 118 }
Kojto 37:f1e388e7b752 119
Kojto 37:f1e388e7b752 120 if (onYUpdate && (y != report[2])) {
Kojto 37:f1e388e7b752 121 (*onYUpdate)(report[2]);
Kojto 37:f1e388e7b752 122 }
mbed_official 24:868cbfe611a7 123
Kojto 37:f1e388e7b752 124 if (onZUpdate && (z != report[3])) {
Kojto 37:f1e388e7b752 125 (*onZUpdate)(report[3]);
Kojto 37:f1e388e7b752 126 }
Kojto 37:f1e388e7b752 127
Kojto 37:f1e388e7b752 128 // update mouse state
Kojto 37:f1e388e7b752 129 buttons = report[0] & 0x07;
Kojto 37:f1e388e7b752 130 x = report[1];
Kojto 37:f1e388e7b752 131 y = report[2];
Kojto 37:f1e388e7b752 132 z = report[3];
samux 5:e48791a1ef18 133 }
Kojto 37:f1e388e7b752 134 /* set again the maximum value */
Kojto 37:f1e388e7b752 135 len_listen = int_in->getSize();
mbed_official 24:868cbfe611a7 136
mbed_official 27:4206883f4cb7 137 if (len_listen > sizeof(report)) {
mbed_official 27:4206883f4cb7 138 len_listen = sizeof(report);
mbed_official 27:4206883f4cb7 139 }
mbed_official 27:4206883f4cb7 140
Kojto 37:f1e388e7b752 141 if (dev) {
mbed_official 0:a554658735bf 142 host->interruptRead(dev, int_in, report, len_listen, false);
Kojto 37:f1e388e7b752 143 }
mbed_official 0:a554658735bf 144 }
mbed_official 0:a554658735bf 145
mbed_official 0:a554658735bf 146 /*virtual*/ void USBHostMouse::setVidPid(uint16_t vid, uint16_t pid)
mbed_official 0:a554658735bf 147 {
mbed_official 0:a554658735bf 148 // we don't check VID/PID for mouse driver
mbed_official 0:a554658735bf 149 }
mbed_official 0:a554658735bf 150
mbed_official 0:a554658735bf 151 /*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 152 {
mbed_official 0:a554658735bf 153 if ((mouse_intf == -1) &&
Kojto 37:f1e388e7b752 154 (intf_class == HID_CLASS) &&
Kojto 37:f1e388e7b752 155 (intf_subclass == 0x01) &&
Kojto 37:f1e388e7b752 156 (intf_protocol == 0x02)) {
mbed_official 0:a554658735bf 157 mouse_intf = intf_nb;
mbed_official 0:a554658735bf 158 return true;
mbed_official 0:a554658735bf 159 }
mbed_official 0:a554658735bf 160 return false;
mbed_official 0:a554658735bf 161 }
mbed_official 0:a554658735bf 162
mbed_official 0:a554658735bf 163 /*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 164 {
mbed_official 0:a554658735bf 165 if (intf_nb == mouse_intf) {
mbed_official 0:a554658735bf 166 if (type == INTERRUPT_ENDPOINT && dir == IN) {
mbed_official 0:a554658735bf 167 mouse_device_found = true;
mbed_official 0:a554658735bf 168 return true;
mbed_official 0:a554658735bf 169 }
mbed_official 0:a554658735bf 170 }
mbed_official 0:a554658735bf 171 return false;
mbed_official 0:a554658735bf 172 }
mbed_official 0:a554658735bf 173
mbed_official 0:a554658735bf 174 #endif