Sensor reporting over USB CDC

Dependencies:   MAG3110 MMA8451Q SLCD- TSI USBDevice mbed

Committer:
wue
Date:
Wed Apr 16 12:20:12 2014 +0000
Revision:
0:7b58cdacf811
Sensor reporting over USB CDC

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wue 0:7b58cdacf811 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
wue 0:7b58cdacf811 2 *
wue 0:7b58cdacf811 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
wue 0:7b58cdacf811 4 * and associated documentation files (the "Software"), to deal in the Software without
wue 0:7b58cdacf811 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
wue 0:7b58cdacf811 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
wue 0:7b58cdacf811 7 * Software is furnished to do so, subject to the following conditions:
wue 0:7b58cdacf811 8 *
wue 0:7b58cdacf811 9 * The above copyright notice and this permission notice shall be included in all copies or
wue 0:7b58cdacf811 10 * substantial portions of the Software.
wue 0:7b58cdacf811 11 *
wue 0:7b58cdacf811 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
wue 0:7b58cdacf811 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
wue 0:7b58cdacf811 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
wue 0:7b58cdacf811 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
wue 0:7b58cdacf811 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
wue 0:7b58cdacf811 17 */
wue 0:7b58cdacf811 18
wue 0:7b58cdacf811 19 #include "stdint.h"
wue 0:7b58cdacf811 20 #include "USBMouse.h"
wue 0:7b58cdacf811 21
wue 0:7b58cdacf811 22 bool USBMouse::update(int16_t x, int16_t y, uint8_t button, int8_t z) {
wue 0:7b58cdacf811 23 switch (mouse_type) {
wue 0:7b58cdacf811 24 case REL_MOUSE:
wue 0:7b58cdacf811 25 while (x > 127) {
wue 0:7b58cdacf811 26 if (!mouseSend(127, 0, button, z)) return false;
wue 0:7b58cdacf811 27 x = x - 127;
wue 0:7b58cdacf811 28 }
wue 0:7b58cdacf811 29 while (x < -128) {
wue 0:7b58cdacf811 30 if (!mouseSend(-128, 0, button, z)) return false;
wue 0:7b58cdacf811 31 x = x + 128;
wue 0:7b58cdacf811 32 }
wue 0:7b58cdacf811 33 while (y > 127) {
wue 0:7b58cdacf811 34 if (!mouseSend(0, 127, button, z)) return false;
wue 0:7b58cdacf811 35 y = y - 127;
wue 0:7b58cdacf811 36 }
wue 0:7b58cdacf811 37 while (y < -128) {
wue 0:7b58cdacf811 38 if (!mouseSend(0, -128, button, z)) return false;
wue 0:7b58cdacf811 39 y = y + 128;
wue 0:7b58cdacf811 40 }
wue 0:7b58cdacf811 41 return mouseSend(x, y, button, z);
wue 0:7b58cdacf811 42 case ABS_MOUSE:
wue 0:7b58cdacf811 43 HID_REPORT report;
wue 0:7b58cdacf811 44
wue 0:7b58cdacf811 45 report.data[0] = x & 0xff;
wue 0:7b58cdacf811 46 report.data[1] = (x >> 8) & 0xff;
wue 0:7b58cdacf811 47 report.data[2] = y & 0xff;
wue 0:7b58cdacf811 48 report.data[3] = (y >> 8) & 0xff;
wue 0:7b58cdacf811 49 report.data[4] = -z;
wue 0:7b58cdacf811 50 report.data[5] = button & 0x07;
wue 0:7b58cdacf811 51
wue 0:7b58cdacf811 52 report.length = 6;
wue 0:7b58cdacf811 53
wue 0:7b58cdacf811 54 return send(&report);
wue 0:7b58cdacf811 55 default:
wue 0:7b58cdacf811 56 return false;
wue 0:7b58cdacf811 57 }
wue 0:7b58cdacf811 58 }
wue 0:7b58cdacf811 59
wue 0:7b58cdacf811 60 bool USBMouse::mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z) {
wue 0:7b58cdacf811 61 HID_REPORT report;
wue 0:7b58cdacf811 62 report.data[0] = buttons & 0x07;
wue 0:7b58cdacf811 63 report.data[1] = x;
wue 0:7b58cdacf811 64 report.data[2] = y;
wue 0:7b58cdacf811 65 report.data[3] = -z; // >0 to scroll down, <0 to scroll up
wue 0:7b58cdacf811 66
wue 0:7b58cdacf811 67 report.length = 4;
wue 0:7b58cdacf811 68
wue 0:7b58cdacf811 69 return send(&report);
wue 0:7b58cdacf811 70 }
wue 0:7b58cdacf811 71
wue 0:7b58cdacf811 72 bool USBMouse::move(int16_t x, int16_t y) {
wue 0:7b58cdacf811 73 return update(x, y, button, 0);
wue 0:7b58cdacf811 74 }
wue 0:7b58cdacf811 75
wue 0:7b58cdacf811 76 bool USBMouse::scroll(int8_t z) {
wue 0:7b58cdacf811 77 return update(0, 0, button, z);
wue 0:7b58cdacf811 78 }
wue 0:7b58cdacf811 79
wue 0:7b58cdacf811 80
wue 0:7b58cdacf811 81 bool USBMouse::doubleClick() {
wue 0:7b58cdacf811 82 if (!click(MOUSE_LEFT))
wue 0:7b58cdacf811 83 return false;
wue 0:7b58cdacf811 84 wait(0.1);
wue 0:7b58cdacf811 85 return click(MOUSE_LEFT);
wue 0:7b58cdacf811 86 }
wue 0:7b58cdacf811 87
wue 0:7b58cdacf811 88 bool USBMouse::click(uint8_t button) {
wue 0:7b58cdacf811 89 if (!update(0, 0, button, 0))
wue 0:7b58cdacf811 90 return false;
wue 0:7b58cdacf811 91 wait(0.01);
wue 0:7b58cdacf811 92 return update(0, 0, 0, 0);
wue 0:7b58cdacf811 93 }
wue 0:7b58cdacf811 94
wue 0:7b58cdacf811 95 bool USBMouse::press(uint8_t button_) {
wue 0:7b58cdacf811 96 button = button_ & 0x07;
wue 0:7b58cdacf811 97 return update(0, 0, button, 0);
wue 0:7b58cdacf811 98 }
wue 0:7b58cdacf811 99
wue 0:7b58cdacf811 100 bool USBMouse::release(uint8_t button_) {
wue 0:7b58cdacf811 101 button = (button & (~button_)) & 0x07;
wue 0:7b58cdacf811 102 return update(0, 0, button, 0);
wue 0:7b58cdacf811 103 }
wue 0:7b58cdacf811 104
wue 0:7b58cdacf811 105
wue 0:7b58cdacf811 106 uint8_t * USBMouse::reportDesc() {
wue 0:7b58cdacf811 107
wue 0:7b58cdacf811 108 if (mouse_type == REL_MOUSE) {
wue 0:7b58cdacf811 109 static uint8_t reportDescriptor[] = {
wue 0:7b58cdacf811 110 USAGE_PAGE(1), 0x01, // Genric Desktop
wue 0:7b58cdacf811 111 USAGE(1), 0x02, // Mouse
wue 0:7b58cdacf811 112 COLLECTION(1), 0x01, // Application
wue 0:7b58cdacf811 113 USAGE(1), 0x01, // Pointer
wue 0:7b58cdacf811 114 COLLECTION(1), 0x00, // Physical
wue 0:7b58cdacf811 115
wue 0:7b58cdacf811 116 REPORT_COUNT(1), 0x03,
wue 0:7b58cdacf811 117 REPORT_SIZE(1), 0x01,
wue 0:7b58cdacf811 118 USAGE_PAGE(1), 0x09, // Buttons
wue 0:7b58cdacf811 119 USAGE_MINIMUM(1), 0x1,
wue 0:7b58cdacf811 120 USAGE_MAXIMUM(1), 0x3,
wue 0:7b58cdacf811 121 LOGICAL_MINIMUM(1), 0x00,
wue 0:7b58cdacf811 122 LOGICAL_MAXIMUM(1), 0x01,
wue 0:7b58cdacf811 123 INPUT(1), 0x02,
wue 0:7b58cdacf811 124 REPORT_COUNT(1), 0x01,
wue 0:7b58cdacf811 125 REPORT_SIZE(1), 0x05,
wue 0:7b58cdacf811 126 INPUT(1), 0x01,
wue 0:7b58cdacf811 127
wue 0:7b58cdacf811 128 REPORT_COUNT(1), 0x03,
wue 0:7b58cdacf811 129 REPORT_SIZE(1), 0x08,
wue 0:7b58cdacf811 130 USAGE_PAGE(1), 0x01,
wue 0:7b58cdacf811 131 USAGE(1), 0x30, // X
wue 0:7b58cdacf811 132 USAGE(1), 0x31, // Y
wue 0:7b58cdacf811 133 USAGE(1), 0x38, // scroll
wue 0:7b58cdacf811 134 LOGICAL_MINIMUM(1), 0x81,
wue 0:7b58cdacf811 135 LOGICAL_MAXIMUM(1), 0x7f,
wue 0:7b58cdacf811 136 INPUT(1), 0x06, // Relative data
wue 0:7b58cdacf811 137
wue 0:7b58cdacf811 138 END_COLLECTION(0),
wue 0:7b58cdacf811 139 END_COLLECTION(0),
wue 0:7b58cdacf811 140 };
wue 0:7b58cdacf811 141 reportLength = sizeof(reportDescriptor);
wue 0:7b58cdacf811 142 return reportDescriptor;
wue 0:7b58cdacf811 143 } else if (mouse_type == ABS_MOUSE) {
wue 0:7b58cdacf811 144 static uint8_t reportDescriptor[] = {
wue 0:7b58cdacf811 145
wue 0:7b58cdacf811 146 USAGE_PAGE(1), 0x01, // Generic Desktop
wue 0:7b58cdacf811 147 USAGE(1), 0x02, // Mouse
wue 0:7b58cdacf811 148 COLLECTION(1), 0x01, // Application
wue 0:7b58cdacf811 149 USAGE(1), 0x01, // Pointer
wue 0:7b58cdacf811 150 COLLECTION(1), 0x00, // Physical
wue 0:7b58cdacf811 151
wue 0:7b58cdacf811 152 USAGE_PAGE(1), 0x01, // Generic Desktop
wue 0:7b58cdacf811 153 USAGE(1), 0x30, // X
wue 0:7b58cdacf811 154 USAGE(1), 0x31, // Y
wue 0:7b58cdacf811 155 LOGICAL_MINIMUM(1), 0x00, // 0
wue 0:7b58cdacf811 156 LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767
wue 0:7b58cdacf811 157 REPORT_SIZE(1), 0x10,
wue 0:7b58cdacf811 158 REPORT_COUNT(1), 0x02,
wue 0:7b58cdacf811 159 INPUT(1), 0x02, // Data, Variable, Absolute
wue 0:7b58cdacf811 160
wue 0:7b58cdacf811 161 USAGE_PAGE(1), 0x01, // Generic Desktop
wue 0:7b58cdacf811 162 USAGE(1), 0x38, // scroll
wue 0:7b58cdacf811 163 LOGICAL_MINIMUM(1), 0x81, // -127
wue 0:7b58cdacf811 164 LOGICAL_MAXIMUM(1), 0x7f, // 127
wue 0:7b58cdacf811 165 REPORT_SIZE(1), 0x08,
wue 0:7b58cdacf811 166 REPORT_COUNT(1), 0x01,
wue 0:7b58cdacf811 167 INPUT(1), 0x06, // Data, Variable, Relative
wue 0:7b58cdacf811 168
wue 0:7b58cdacf811 169 USAGE_PAGE(1), 0x09, // Buttons
wue 0:7b58cdacf811 170 USAGE_MINIMUM(1), 0x01,
wue 0:7b58cdacf811 171 USAGE_MAXIMUM(1), 0x03,
wue 0:7b58cdacf811 172 LOGICAL_MINIMUM(1), 0x00, // 0
wue 0:7b58cdacf811 173 LOGICAL_MAXIMUM(1), 0x01, // 1
wue 0:7b58cdacf811 174 REPORT_COUNT(1), 0x03,
wue 0:7b58cdacf811 175 REPORT_SIZE(1), 0x01,
wue 0:7b58cdacf811 176 INPUT(1), 0x02, // Data, Variable, Absolute
wue 0:7b58cdacf811 177 REPORT_COUNT(1), 0x01,
wue 0:7b58cdacf811 178 REPORT_SIZE(1), 0x05,
wue 0:7b58cdacf811 179 INPUT(1), 0x01, // Constant
wue 0:7b58cdacf811 180
wue 0:7b58cdacf811 181 END_COLLECTION(0),
wue 0:7b58cdacf811 182 END_COLLECTION(0)
wue 0:7b58cdacf811 183 };
wue 0:7b58cdacf811 184 reportLength = sizeof(reportDescriptor);
wue 0:7b58cdacf811 185 return reportDescriptor;
wue 0:7b58cdacf811 186 }
wue 0:7b58cdacf811 187 return NULL;
wue 0:7b58cdacf811 188 }
wue 0:7b58cdacf811 189
wue 0:7b58cdacf811 190 #define DEFAULT_CONFIGURATION (1)
wue 0:7b58cdacf811 191 #define TOTAL_DESCRIPTOR_LENGTH ((1 * CONFIGURATION_DESCRIPTOR_LENGTH) \
wue 0:7b58cdacf811 192 + (1 * INTERFACE_DESCRIPTOR_LENGTH) \
wue 0:7b58cdacf811 193 + (1 * HID_DESCRIPTOR_LENGTH) \
wue 0:7b58cdacf811 194 + (2 * ENDPOINT_DESCRIPTOR_LENGTH))
wue 0:7b58cdacf811 195
wue 0:7b58cdacf811 196 uint8_t * USBMouse::configurationDesc() {
wue 0:7b58cdacf811 197 static uint8_t configurationDescriptor[] = {
wue 0:7b58cdacf811 198 CONFIGURATION_DESCRIPTOR_LENGTH,// bLength
wue 0:7b58cdacf811 199 CONFIGURATION_DESCRIPTOR, // bDescriptorType
wue 0:7b58cdacf811 200 LSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (LSB)
wue 0:7b58cdacf811 201 MSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (MSB)
wue 0:7b58cdacf811 202 0x01, // bNumInterfaces
wue 0:7b58cdacf811 203 DEFAULT_CONFIGURATION, // bConfigurationValue
wue 0:7b58cdacf811 204 0x00, // iConfiguration
wue 0:7b58cdacf811 205 C_RESERVED | C_SELF_POWERED, // bmAttributes
wue 0:7b58cdacf811 206 C_POWER(0), // bMaxPowerHello World from Mbed
wue 0:7b58cdacf811 207
wue 0:7b58cdacf811 208 INTERFACE_DESCRIPTOR_LENGTH, // bLength
wue 0:7b58cdacf811 209 INTERFACE_DESCRIPTOR, // bDescriptorType
wue 0:7b58cdacf811 210 0x00, // bInterfaceNumber
wue 0:7b58cdacf811 211 0x00, // bAlternateSetting
wue 0:7b58cdacf811 212 0x02, // bNumEndpoints
wue 0:7b58cdacf811 213 HID_CLASS, // bInterfaceClass
wue 0:7b58cdacf811 214 1, // bInterfaceSubClass
wue 0:7b58cdacf811 215 2, // bInterfaceProtocol (mouse)
wue 0:7b58cdacf811 216 0x00, // iInterface
wue 0:7b58cdacf811 217
wue 0:7b58cdacf811 218 HID_DESCRIPTOR_LENGTH, // bLength
wue 0:7b58cdacf811 219 HID_DESCRIPTOR, // bDescriptorType
wue 0:7b58cdacf811 220 LSB(HID_VERSION_1_11), // bcdHID (LSB)
wue 0:7b58cdacf811 221 MSB(HID_VERSION_1_11), // bcdHID (MSB)
wue 0:7b58cdacf811 222 0x00, // bCountryCode
wue 0:7b58cdacf811 223 0x01, // bNumDescriptors
wue 0:7b58cdacf811 224 REPORT_DESCRIPTOR, // bDescriptorType
wue 0:7b58cdacf811 225 (uint8_t)(LSB(reportDescLength())), // wDescriptorLength (LSB)
wue 0:7b58cdacf811 226 (uint8_t)(MSB(reportDescLength())), // wDescriptorLength (MSB)
wue 0:7b58cdacf811 227
wue 0:7b58cdacf811 228 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
wue 0:7b58cdacf811 229 ENDPOINT_DESCRIPTOR, // bDescriptorType
wue 0:7b58cdacf811 230 PHY_TO_DESC(EPINT_IN), // bEndpointAddress
wue 0:7b58cdacf811 231 E_INTERRUPT, // bmAttributes
wue 0:7b58cdacf811 232 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)
wue 0:7b58cdacf811 233 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
wue 0:7b58cdacf811 234 1, // bInterval (milliseconds)
wue 0:7b58cdacf811 235
wue 0:7b58cdacf811 236 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
wue 0:7b58cdacf811 237 ENDPOINT_DESCRIPTOR, // bDescriptorType
wue 0:7b58cdacf811 238 PHY_TO_DESC(EPINT_OUT), // bEndpointAddress
wue 0:7b58cdacf811 239 E_INTERRUPT, // bmAttributes
wue 0:7b58cdacf811 240 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)
wue 0:7b58cdacf811 241 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
wue 0:7b58cdacf811 242 1, // bInterval (milliseconds)
wue 0:7b58cdacf811 243 };
wue 0:7b58cdacf811 244 return configurationDescriptor;
wue 0:7b58cdacf811 245 }