USBDevice

Dependents:   QEI_X1_LCD_test3 macnica_test

Committer:
toucyy
Date:
Thu Apr 18 07:49:37 2013 +0000
Revision:
0:2d8d0b73e1ff
[mbed] converted /QEI_HelloWorld/USBDevice

Who changed what in which revision?

UserRevisionLine numberNew contents of line
toucyy 0:2d8d0b73e1ff 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
toucyy 0:2d8d0b73e1ff 2 *
toucyy 0:2d8d0b73e1ff 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
toucyy 0:2d8d0b73e1ff 4 * and associated documentation files (the "Software"), to deal in the Software without
toucyy 0:2d8d0b73e1ff 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
toucyy 0:2d8d0b73e1ff 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
toucyy 0:2d8d0b73e1ff 7 * Software is furnished to do so, subject to the following conditions:
toucyy 0:2d8d0b73e1ff 8 *
toucyy 0:2d8d0b73e1ff 9 * The above copyright notice and this permission notice shall be included in all copies or
toucyy 0:2d8d0b73e1ff 10 * substantial portions of the Software.
toucyy 0:2d8d0b73e1ff 11 *
toucyy 0:2d8d0b73e1ff 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
toucyy 0:2d8d0b73e1ff 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
toucyy 0:2d8d0b73e1ff 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
toucyy 0:2d8d0b73e1ff 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
toucyy 0:2d8d0b73e1ff 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
toucyy 0:2d8d0b73e1ff 17 */
toucyy 0:2d8d0b73e1ff 18
toucyy 0:2d8d0b73e1ff 19 #include "stdint.h"
toucyy 0:2d8d0b73e1ff 20 #include "USBMouse.h"
toucyy 0:2d8d0b73e1ff 21
toucyy 0:2d8d0b73e1ff 22 bool USBMouse::update(int16_t x, int16_t y, uint8_t button, int8_t z) {
toucyy 0:2d8d0b73e1ff 23 switch (mouse_type) {
toucyy 0:2d8d0b73e1ff 24 case REL_MOUSE:
toucyy 0:2d8d0b73e1ff 25 while (x > 127) {
toucyy 0:2d8d0b73e1ff 26 if (!mouseSend(127, 0, button, z)) return false;
toucyy 0:2d8d0b73e1ff 27 x = x - 127;
toucyy 0:2d8d0b73e1ff 28 }
toucyy 0:2d8d0b73e1ff 29 while (x < -128) {
toucyy 0:2d8d0b73e1ff 30 if (!mouseSend(-128, 0, button, z)) return false;
toucyy 0:2d8d0b73e1ff 31 x = x + 128;
toucyy 0:2d8d0b73e1ff 32 }
toucyy 0:2d8d0b73e1ff 33 while (y > 127) {
toucyy 0:2d8d0b73e1ff 34 if (!mouseSend(0, 127, button, z)) return false;
toucyy 0:2d8d0b73e1ff 35 y = y - 127;
toucyy 0:2d8d0b73e1ff 36 }
toucyy 0:2d8d0b73e1ff 37 while (y < -128) {
toucyy 0:2d8d0b73e1ff 38 if (!mouseSend(0, -128, button, z)) return false;
toucyy 0:2d8d0b73e1ff 39 y = y + 128;
toucyy 0:2d8d0b73e1ff 40 }
toucyy 0:2d8d0b73e1ff 41 return mouseSend(x, y, button, z);
toucyy 0:2d8d0b73e1ff 42 case ABS_MOUSE:
toucyy 0:2d8d0b73e1ff 43 HID_REPORT report;
toucyy 0:2d8d0b73e1ff 44
toucyy 0:2d8d0b73e1ff 45 report.data[0] = x & 0xff;
toucyy 0:2d8d0b73e1ff 46 report.data[1] = (x >> 8) & 0xff;
toucyy 0:2d8d0b73e1ff 47 report.data[2] = y & 0xff;
toucyy 0:2d8d0b73e1ff 48 report.data[3] = (y >> 8) & 0xff;
toucyy 0:2d8d0b73e1ff 49 report.data[4] = -z;
toucyy 0:2d8d0b73e1ff 50 report.data[5] = button & 0x07;
toucyy 0:2d8d0b73e1ff 51
toucyy 0:2d8d0b73e1ff 52 report.length = 6;
toucyy 0:2d8d0b73e1ff 53
toucyy 0:2d8d0b73e1ff 54 return send(&report);
toucyy 0:2d8d0b73e1ff 55 default:
toucyy 0:2d8d0b73e1ff 56 return false;
toucyy 0:2d8d0b73e1ff 57 }
toucyy 0:2d8d0b73e1ff 58 }
toucyy 0:2d8d0b73e1ff 59
toucyy 0:2d8d0b73e1ff 60 bool USBMouse::mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z) {
toucyy 0:2d8d0b73e1ff 61 HID_REPORT report;
toucyy 0:2d8d0b73e1ff 62 report.data[0] = buttons & 0x07;
toucyy 0:2d8d0b73e1ff 63 report.data[1] = x;
toucyy 0:2d8d0b73e1ff 64 report.data[2] = y;
toucyy 0:2d8d0b73e1ff 65 report.data[3] = -z; // >0 to scroll down, <0 to scroll up
toucyy 0:2d8d0b73e1ff 66
toucyy 0:2d8d0b73e1ff 67 report.length = 4;
toucyy 0:2d8d0b73e1ff 68
toucyy 0:2d8d0b73e1ff 69 return send(&report);
toucyy 0:2d8d0b73e1ff 70 }
toucyy 0:2d8d0b73e1ff 71
toucyy 0:2d8d0b73e1ff 72 bool USBMouse::move(int16_t x, int16_t y) {
toucyy 0:2d8d0b73e1ff 73 return update(x, y, button, 0);
toucyy 0:2d8d0b73e1ff 74 }
toucyy 0:2d8d0b73e1ff 75
toucyy 0:2d8d0b73e1ff 76 bool USBMouse::scroll(int8_t z) {
toucyy 0:2d8d0b73e1ff 77 return update(0, 0, button, z);
toucyy 0:2d8d0b73e1ff 78 }
toucyy 0:2d8d0b73e1ff 79
toucyy 0:2d8d0b73e1ff 80
toucyy 0:2d8d0b73e1ff 81 bool USBMouse::doubleClick() {
toucyy 0:2d8d0b73e1ff 82 if (!click(MOUSE_LEFT))
toucyy 0:2d8d0b73e1ff 83 return false;
toucyy 0:2d8d0b73e1ff 84 wait(0.1);
toucyy 0:2d8d0b73e1ff 85 return click(MOUSE_LEFT);
toucyy 0:2d8d0b73e1ff 86 }
toucyy 0:2d8d0b73e1ff 87
toucyy 0:2d8d0b73e1ff 88 bool USBMouse::click(uint8_t button) {
toucyy 0:2d8d0b73e1ff 89 if (!update(0, 0, button, 0))
toucyy 0:2d8d0b73e1ff 90 return false;
toucyy 0:2d8d0b73e1ff 91 wait(0.01);
toucyy 0:2d8d0b73e1ff 92 return update(0, 0, 0, 0);
toucyy 0:2d8d0b73e1ff 93 }
toucyy 0:2d8d0b73e1ff 94
toucyy 0:2d8d0b73e1ff 95 bool USBMouse::press(uint8_t button_) {
toucyy 0:2d8d0b73e1ff 96 button = button_ & 0x07;
toucyy 0:2d8d0b73e1ff 97 return update(0, 0, button, 0);
toucyy 0:2d8d0b73e1ff 98 }
toucyy 0:2d8d0b73e1ff 99
toucyy 0:2d8d0b73e1ff 100 bool USBMouse::release(uint8_t button_) {
toucyy 0:2d8d0b73e1ff 101 button = (button & (~button_)) & 0x07;
toucyy 0:2d8d0b73e1ff 102 return update(0, 0, button, 0);
toucyy 0:2d8d0b73e1ff 103 }
toucyy 0:2d8d0b73e1ff 104
toucyy 0:2d8d0b73e1ff 105
toucyy 0:2d8d0b73e1ff 106 uint8_t * USBMouse::reportDesc() {
toucyy 0:2d8d0b73e1ff 107
toucyy 0:2d8d0b73e1ff 108 if (mouse_type == REL_MOUSE) {
toucyy 0:2d8d0b73e1ff 109 static uint8_t reportDescriptor[] = {
toucyy 0:2d8d0b73e1ff 110 USAGE_PAGE(1), 0x01, // Genric Desktop
toucyy 0:2d8d0b73e1ff 111 USAGE(1), 0x02, // Mouse
toucyy 0:2d8d0b73e1ff 112 COLLECTION(1), 0x01, // Application
toucyy 0:2d8d0b73e1ff 113 USAGE(1), 0x01, // Pointer
toucyy 0:2d8d0b73e1ff 114 COLLECTION(1), 0x00, // Physical
toucyy 0:2d8d0b73e1ff 115
toucyy 0:2d8d0b73e1ff 116 REPORT_COUNT(1), 0x03,
toucyy 0:2d8d0b73e1ff 117 REPORT_SIZE(1), 0x01,
toucyy 0:2d8d0b73e1ff 118 USAGE_PAGE(1), 0x09, // Buttons
toucyy 0:2d8d0b73e1ff 119 USAGE_MINIMUM(1), 0x1,
toucyy 0:2d8d0b73e1ff 120 USAGE_MAXIMUM(1), 0x3,
toucyy 0:2d8d0b73e1ff 121 LOGICAL_MINIMUM(1), 0x00,
toucyy 0:2d8d0b73e1ff 122 LOGICAL_MAXIMUM(1), 0x01,
toucyy 0:2d8d0b73e1ff 123 INPUT(1), 0x02,
toucyy 0:2d8d0b73e1ff 124 REPORT_COUNT(1), 0x01,
toucyy 0:2d8d0b73e1ff 125 REPORT_SIZE(1), 0x05,
toucyy 0:2d8d0b73e1ff 126 INPUT(1), 0x01,
toucyy 0:2d8d0b73e1ff 127
toucyy 0:2d8d0b73e1ff 128 REPORT_COUNT(1), 0x03,
toucyy 0:2d8d0b73e1ff 129 REPORT_SIZE(1), 0x08,
toucyy 0:2d8d0b73e1ff 130 USAGE_PAGE(1), 0x01,
toucyy 0:2d8d0b73e1ff 131 USAGE(1), 0x30, // X
toucyy 0:2d8d0b73e1ff 132 USAGE(1), 0x31, // Y
toucyy 0:2d8d0b73e1ff 133 USAGE(1), 0x38, // scroll
toucyy 0:2d8d0b73e1ff 134 LOGICAL_MINIMUM(1), 0x81,
toucyy 0:2d8d0b73e1ff 135 LOGICAL_MAXIMUM(1), 0x7f,
toucyy 0:2d8d0b73e1ff 136 INPUT(1), 0x06, // Relative data
toucyy 0:2d8d0b73e1ff 137
toucyy 0:2d8d0b73e1ff 138 END_COLLECTION(0),
toucyy 0:2d8d0b73e1ff 139 END_COLLECTION(0),
toucyy 0:2d8d0b73e1ff 140 };
toucyy 0:2d8d0b73e1ff 141 reportLength = sizeof(reportDescriptor);
toucyy 0:2d8d0b73e1ff 142 return reportDescriptor;
toucyy 0:2d8d0b73e1ff 143 } else if (mouse_type == ABS_MOUSE) {
toucyy 0:2d8d0b73e1ff 144 static uint8_t reportDescriptor[] = {
toucyy 0:2d8d0b73e1ff 145
toucyy 0:2d8d0b73e1ff 146 USAGE_PAGE(1), 0x01, // Generic Desktop
toucyy 0:2d8d0b73e1ff 147 USAGE(1), 0x02, // Mouse
toucyy 0:2d8d0b73e1ff 148 COLLECTION(1), 0x01, // Application
toucyy 0:2d8d0b73e1ff 149 USAGE(1), 0x01, // Pointer
toucyy 0:2d8d0b73e1ff 150 COLLECTION(1), 0x00, // Physical
toucyy 0:2d8d0b73e1ff 151
toucyy 0:2d8d0b73e1ff 152 USAGE_PAGE(1), 0x01, // Generic Desktop
toucyy 0:2d8d0b73e1ff 153 USAGE(1), 0x30, // X
toucyy 0:2d8d0b73e1ff 154 USAGE(1), 0x31, // Y
toucyy 0:2d8d0b73e1ff 155 LOGICAL_MINIMUM(1), 0x00, // 0
toucyy 0:2d8d0b73e1ff 156 LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767
toucyy 0:2d8d0b73e1ff 157 REPORT_SIZE(1), 0x10,
toucyy 0:2d8d0b73e1ff 158 REPORT_COUNT(1), 0x02,
toucyy 0:2d8d0b73e1ff 159 INPUT(1), 0x02, // Data, Variable, Absolute
toucyy 0:2d8d0b73e1ff 160
toucyy 0:2d8d0b73e1ff 161 USAGE_PAGE(1), 0x01, // Generic Desktop
toucyy 0:2d8d0b73e1ff 162 USAGE(1), 0x38, // scroll
toucyy 0:2d8d0b73e1ff 163 LOGICAL_MINIMUM(1), 0x81, // -127
toucyy 0:2d8d0b73e1ff 164 LOGICAL_MAXIMUM(1), 0x7f, // 127
toucyy 0:2d8d0b73e1ff 165 REPORT_SIZE(1), 0x08,
toucyy 0:2d8d0b73e1ff 166 REPORT_COUNT(1), 0x01,
toucyy 0:2d8d0b73e1ff 167 INPUT(1), 0x06, // Data, Variable, Relative
toucyy 0:2d8d0b73e1ff 168
toucyy 0:2d8d0b73e1ff 169 USAGE_PAGE(1), 0x09, // Buttons
toucyy 0:2d8d0b73e1ff 170 USAGE_MINIMUM(1), 0x01,
toucyy 0:2d8d0b73e1ff 171 USAGE_MAXIMUM(1), 0x03,
toucyy 0:2d8d0b73e1ff 172 LOGICAL_MINIMUM(1), 0x00, // 0
toucyy 0:2d8d0b73e1ff 173 LOGICAL_MAXIMUM(1), 0x01, // 1
toucyy 0:2d8d0b73e1ff 174 REPORT_COUNT(1), 0x03,
toucyy 0:2d8d0b73e1ff 175 REPORT_SIZE(1), 0x01,
toucyy 0:2d8d0b73e1ff 176 INPUT(1), 0x02, // Data, Variable, Absolute
toucyy 0:2d8d0b73e1ff 177 REPORT_COUNT(1), 0x01,
toucyy 0:2d8d0b73e1ff 178 REPORT_SIZE(1), 0x05,
toucyy 0:2d8d0b73e1ff 179 INPUT(1), 0x01, // Constant
toucyy 0:2d8d0b73e1ff 180
toucyy 0:2d8d0b73e1ff 181 END_COLLECTION(0),
toucyy 0:2d8d0b73e1ff 182 END_COLLECTION(0)
toucyy 0:2d8d0b73e1ff 183 };
toucyy 0:2d8d0b73e1ff 184 reportLength = sizeof(reportDescriptor);
toucyy 0:2d8d0b73e1ff 185 return reportDescriptor;
toucyy 0:2d8d0b73e1ff 186 }
toucyy 0:2d8d0b73e1ff 187 return NULL;
toucyy 0:2d8d0b73e1ff 188 }
toucyy 0:2d8d0b73e1ff 189
toucyy 0:2d8d0b73e1ff 190