USB CDC (serial) and USB MSC (strage) Composite Device. http://mbed.org/users/okini3939/notebook/USB_Device/

Dependencies:   ChaNFSSD mbed ChaNFS

Committer:
okini3939
Date:
Fri Dec 23 16:37:58 2011 +0000
Revision:
2:5db90410bb90
Parent:
0:9b1d17d54055

        

Who changed what in which revision?

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