X-TOUCH to djay bridge

Dependencies:   mbed mbed-rtos FATFileSystem

Committer:
okini3939
Date:
Wed Jun 05 04:54:37 2019 +0000
Revision:
1:0dac72ab5910
sample

Who changed what in which revision?

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