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