USBMOUSE
Dependents: ESD_Project_USBMouse
USBHID/USBMouse.cpp@0:78a4faee5b0f, 2015-12-13 (annotated)
- Committer:
- priyankapashte
- Date:
- Sun Dec 13 10:04:36 2015 +0000
- Revision:
- 0:78a4faee5b0f
USBMouse File has been modified;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
priyankapashte | 0:78a4faee5b0f | 1 | |
priyankapashte | 0:78a4faee5b0f | 2 | |
priyankapashte | 0:78a4faee5b0f | 3 | #include "stdint.h" |
priyankapashte | 0:78a4faee5b0f | 4 | #include "USBMouse.h" |
priyankapashte | 0:78a4faee5b0f | 5 | |
priyankapashte | 0:78a4faee5b0f | 6 | bool USBMouse::update(int16_t x, int16_t y, uint8_t button, int8_t z) { |
priyankapashte | 0:78a4faee5b0f | 7 | switch (mouse_type) { |
priyankapashte | 0:78a4faee5b0f | 8 | case REL_MOUSE: |
priyankapashte | 0:78a4faee5b0f | 9 | while (x > 127) { |
priyankapashte | 0:78a4faee5b0f | 10 | if (!mouseSend(127, 0, button, z)) return false; |
priyankapashte | 0:78a4faee5b0f | 11 | x = x - 127; |
priyankapashte | 0:78a4faee5b0f | 12 | } |
priyankapashte | 0:78a4faee5b0f | 13 | while (x < -128) { |
priyankapashte | 0:78a4faee5b0f | 14 | if (!mouseSend(-128, 0, button, z)) return false; |
priyankapashte | 0:78a4faee5b0f | 15 | x = x + 128; |
priyankapashte | 0:78a4faee5b0f | 16 | } |
priyankapashte | 0:78a4faee5b0f | 17 | while (y > 127) { |
priyankapashte | 0:78a4faee5b0f | 18 | if (!mouseSend(0, 127, button, z)) return false; |
priyankapashte | 0:78a4faee5b0f | 19 | y = y - 127; |
priyankapashte | 0:78a4faee5b0f | 20 | } |
priyankapashte | 0:78a4faee5b0f | 21 | while (y < -128) { |
priyankapashte | 0:78a4faee5b0f | 22 | if (!mouseSend(0, -128, button, z)) return false; |
priyankapashte | 0:78a4faee5b0f | 23 | y = y + 128; |
priyankapashte | 0:78a4faee5b0f | 24 | } |
priyankapashte | 0:78a4faee5b0f | 25 | return mouseSend(x, y, button, z); |
priyankapashte | 0:78a4faee5b0f | 26 | |
priyankapashte | 0:78a4faee5b0f | 27 | default: |
priyankapashte | 0:78a4faee5b0f | 28 | return false; |
priyankapashte | 0:78a4faee5b0f | 29 | } |
priyankapashte | 0:78a4faee5b0f | 30 | } |
priyankapashte | 0:78a4faee5b0f | 31 | |
priyankapashte | 0:78a4faee5b0f | 32 | bool USBMouse::mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z) { |
priyankapashte | 0:78a4faee5b0f | 33 | HID_REPORT report; |
priyankapashte | 0:78a4faee5b0f | 34 | report.data[0] = buttons & 0x07; |
priyankapashte | 0:78a4faee5b0f | 35 | report.data[1] = x; |
priyankapashte | 0:78a4faee5b0f | 36 | report.data[2] = y; |
priyankapashte | 0:78a4faee5b0f | 37 | report.data[3] = -z; // >0 to scroll down, <0 to scroll up |
priyankapashte | 0:78a4faee5b0f | 38 | |
priyankapashte | 0:78a4faee5b0f | 39 | report.length = 4; |
priyankapashte | 0:78a4faee5b0f | 40 | |
priyankapashte | 0:78a4faee5b0f | 41 | return send(&report); |
priyankapashte | 0:78a4faee5b0f | 42 | } |
priyankapashte | 0:78a4faee5b0f | 43 | |
priyankapashte | 0:78a4faee5b0f | 44 | bool USBMouse::move(int16_t x, int16_t y) { |
priyankapashte | 0:78a4faee5b0f | 45 | return update(x, y, button, 0); |
priyankapashte | 0:78a4faee5b0f | 46 | } |
priyankapashte | 0:78a4faee5b0f | 47 | |
priyankapashte | 0:78a4faee5b0f | 48 | bool USBMouse::scroll(int8_t z) { |
priyankapashte | 0:78a4faee5b0f | 49 | return update(0, 0, button, z); |
priyankapashte | 0:78a4faee5b0f | 50 | } |
priyankapashte | 0:78a4faee5b0f | 51 | |
priyankapashte | 0:78a4faee5b0f | 52 | |
priyankapashte | 0:78a4faee5b0f | 53 | bool USBMouse::doubleClick() { |
priyankapashte | 0:78a4faee5b0f | 54 | if (!click(MOUSE_LEFT)) |
priyankapashte | 0:78a4faee5b0f | 55 | return false; |
priyankapashte | 0:78a4faee5b0f | 56 | wait(0.1); |
priyankapashte | 0:78a4faee5b0f | 57 | return click(MOUSE_LEFT); |
priyankapashte | 0:78a4faee5b0f | 58 | } |
priyankapashte | 0:78a4faee5b0f | 59 | |
priyankapashte | 0:78a4faee5b0f | 60 | bool USBMouse::click(uint8_t button) { |
priyankapashte | 0:78a4faee5b0f | 61 | if (!update(0, 0, button, 0)) |
priyankapashte | 0:78a4faee5b0f | 62 | return false; |
priyankapashte | 0:78a4faee5b0f | 63 | wait(0.01); |
priyankapashte | 0:78a4faee5b0f | 64 | return update(0, 0, 0, 0); |
priyankapashte | 0:78a4faee5b0f | 65 | } |
priyankapashte | 0:78a4faee5b0f | 66 | |
priyankapashte | 0:78a4faee5b0f | 67 | bool USBMouse::press(uint8_t button_) { |
priyankapashte | 0:78a4faee5b0f | 68 | button = button_ & 0x07; |
priyankapashte | 0:78a4faee5b0f | 69 | return update(0, 0, button, 0); |
priyankapashte | 0:78a4faee5b0f | 70 | } |
priyankapashte | 0:78a4faee5b0f | 71 | |
priyankapashte | 0:78a4faee5b0f | 72 | bool USBMouse::release(uint8_t button_) { |
priyankapashte | 0:78a4faee5b0f | 73 | button = (button & (~button_)) & 0x07; |
priyankapashte | 0:78a4faee5b0f | 74 | return update(0, 0, button, 0); |
priyankapashte | 0:78a4faee5b0f | 75 | } |
priyankapashte | 0:78a4faee5b0f | 76 | |
priyankapashte | 0:78a4faee5b0f | 77 | |
priyankapashte | 0:78a4faee5b0f | 78 | uint8_t * USBMouse::reportDesc() { |
priyankapashte | 0:78a4faee5b0f | 79 | |
priyankapashte | 0:78a4faee5b0f | 80 | if (mouse_type == REL_MOUSE) { |
priyankapashte | 0:78a4faee5b0f | 81 | static uint8_t reportDescriptor[] = { |
priyankapashte | 0:78a4faee5b0f | 82 | USAGE_PAGE(1), 0x01, // Genric Desktop |
priyankapashte | 0:78a4faee5b0f | 83 | USAGE(1), 0x02, // Mouse |
priyankapashte | 0:78a4faee5b0f | 84 | COLLECTION(1), 0x01, // Application |
priyankapashte | 0:78a4faee5b0f | 85 | USAGE(1), 0x01, // Pointer |
priyankapashte | 0:78a4faee5b0f | 86 | COLLECTION(1), 0x00, // Physical |
priyankapashte | 0:78a4faee5b0f | 87 | |
priyankapashte | 0:78a4faee5b0f | 88 | REPORT_COUNT(1), 0x03, |
priyankapashte | 0:78a4faee5b0f | 89 | REPORT_SIZE(1), 0x01, |
priyankapashte | 0:78a4faee5b0f | 90 | USAGE_PAGE(1), 0x09, // Buttons |
priyankapashte | 0:78a4faee5b0f | 91 | USAGE_MINIMUM(1), 0x1, |
priyankapashte | 0:78a4faee5b0f | 92 | USAGE_MAXIMUM(1), 0x3, |
priyankapashte | 0:78a4faee5b0f | 93 | LOGICAL_MINIMUM(1), 0x00, |
priyankapashte | 0:78a4faee5b0f | 94 | LOGICAL_MAXIMUM(1), 0x01, |
priyankapashte | 0:78a4faee5b0f | 95 | INPUT(1), 0x02, |
priyankapashte | 0:78a4faee5b0f | 96 | REPORT_COUNT(1), 0x01, |
priyankapashte | 0:78a4faee5b0f | 97 | REPORT_SIZE(1), 0x05, |
priyankapashte | 0:78a4faee5b0f | 98 | INPUT(1), 0x01, |
priyankapashte | 0:78a4faee5b0f | 99 | |
priyankapashte | 0:78a4faee5b0f | 100 | REPORT_COUNT(1), 0x03, |
priyankapashte | 0:78a4faee5b0f | 101 | REPORT_SIZE(1), 0x08, |
priyankapashte | 0:78a4faee5b0f | 102 | USAGE_PAGE(1), 0x01, |
priyankapashte | 0:78a4faee5b0f | 103 | USAGE(1), 0x30, // X |
priyankapashte | 0:78a4faee5b0f | 104 | USAGE(1), 0x31, // Y |
priyankapashte | 0:78a4faee5b0f | 105 | USAGE(1), 0x38, // scroll |
priyankapashte | 0:78a4faee5b0f | 106 | LOGICAL_MINIMUM(1), 0x81, |
priyankapashte | 0:78a4faee5b0f | 107 | LOGICAL_MAXIMUM(1), 0x7f, |
priyankapashte | 0:78a4faee5b0f | 108 | INPUT(1), 0x06, // Relative data |
priyankapashte | 0:78a4faee5b0f | 109 | |
priyankapashte | 0:78a4faee5b0f | 110 | END_COLLECTION(0), |
priyankapashte | 0:78a4faee5b0f | 111 | END_COLLECTION(0), |
priyankapashte | 0:78a4faee5b0f | 112 | }; |
priyankapashte | 0:78a4faee5b0f | 113 | reportLength = sizeof(reportDescriptor); |
priyankapashte | 0:78a4faee5b0f | 114 | return reportDescriptor; |
priyankapashte | 0:78a4faee5b0f | 115 | } |
priyankapashte | 0:78a4faee5b0f | 116 | // reportLength = sizeof(reportDescriptor); |
priyankapashte | 0:78a4faee5b0f | 117 | //return reportDescriptor; |
priyankapashte | 0:78a4faee5b0f | 118 | |
priyankapashte | 0:78a4faee5b0f | 119 | return NULL; |
priyankapashte | 0:78a4faee5b0f | 120 | } |
priyankapashte | 0:78a4faee5b0f | 121 | |
priyankapashte | 0:78a4faee5b0f | 122 | #define DEFAULT_CONFIGURATION (1) |
priyankapashte | 0:78a4faee5b0f | 123 | #define TOTAL_DESCRIPTOR_LENGTH ((1 * CONFIGURATION_DESCRIPTOR_LENGTH) \ |
priyankapashte | 0:78a4faee5b0f | 124 | + (1 * INTERFACE_DESCRIPTOR_LENGTH) \ |
priyankapashte | 0:78a4faee5b0f | 125 | + (1 * HID_DESCRIPTOR_LENGTH) \ |
priyankapashte | 0:78a4faee5b0f | 126 | + (2 * ENDPOINT_DESCRIPTOR_LENGTH)) |
priyankapashte | 0:78a4faee5b0f | 127 | |
priyankapashte | 0:78a4faee5b0f | 128 | uint8_t * USBMouse::configurationDesc() { |
priyankapashte | 0:78a4faee5b0f | 129 | static uint8_t configurationDescriptor[] = { |
priyankapashte | 0:78a4faee5b0f | 130 | CONFIGURATION_DESCRIPTOR_LENGTH,// bLength |
priyankapashte | 0:78a4faee5b0f | 131 | CONFIGURATION_DESCRIPTOR, // bDescriptorType |
priyankapashte | 0:78a4faee5b0f | 132 | LSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (LSB) |
priyankapashte | 0:78a4faee5b0f | 133 | MSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (MSB) |
priyankapashte | 0:78a4faee5b0f | 134 | 0x01, // bNumInterfaces |
priyankapashte | 0:78a4faee5b0f | 135 | DEFAULT_CONFIGURATION, // bConfigurationValue |
priyankapashte | 0:78a4faee5b0f | 136 | 0x00, // iConfiguration |
priyankapashte | 0:78a4faee5b0f | 137 | C_RESERVED | C_SELF_POWERED, // bmAttributes |
priyankapashte | 0:78a4faee5b0f | 138 | C_POWER(0), // bMaxPowerHello World from Mbed |
priyankapashte | 0:78a4faee5b0f | 139 | |
priyankapashte | 0:78a4faee5b0f | 140 | INTERFACE_DESCRIPTOR_LENGTH, // bLength |
priyankapashte | 0:78a4faee5b0f | 141 | INTERFACE_DESCRIPTOR, // bDescriptorType |
priyankapashte | 0:78a4faee5b0f | 142 | 0x00, // bInterfaceNumber |
priyankapashte | 0:78a4faee5b0f | 143 | 0x00, // bAlternateSetting |
priyankapashte | 0:78a4faee5b0f | 144 | 0x02, // bNumEndpoints |
priyankapashte | 0:78a4faee5b0f | 145 | HID_CLASS, // bInterfaceClass |
priyankapashte | 0:78a4faee5b0f | 146 | 1, // bInterfaceSubClass |
priyankapashte | 0:78a4faee5b0f | 147 | 2, // bInterfaceProtocol (mouse) |
priyankapashte | 0:78a4faee5b0f | 148 | 0x00, // iInterface |
priyankapashte | 0:78a4faee5b0f | 149 | |
priyankapashte | 0:78a4faee5b0f | 150 | HID_DESCRIPTOR_LENGTH, // bLength |
priyankapashte | 0:78a4faee5b0f | 151 | HID_DESCRIPTOR, // bDescriptorType |
priyankapashte | 0:78a4faee5b0f | 152 | LSB(HID_VERSION_1_11), // bcdHID (LSB) |
priyankapashte | 0:78a4faee5b0f | 153 | MSB(HID_VERSION_1_11), // bcdHID (MSB) |
priyankapashte | 0:78a4faee5b0f | 154 | 0x00, // bCountryCode |
priyankapashte | 0:78a4faee5b0f | 155 | 0x01, // bNumDescriptors |
priyankapashte | 0:78a4faee5b0f | 156 | REPORT_DESCRIPTOR, // bDescriptorType |
priyankapashte | 0:78a4faee5b0f | 157 | (uint8_t)(LSB(reportDescLength())), // wDescriptorLength (LSB) |
priyankapashte | 0:78a4faee5b0f | 158 | (uint8_t)(MSB(reportDescLength())), // wDescriptorLength (MSB) |
priyankapashte | 0:78a4faee5b0f | 159 | |
priyankapashte | 0:78a4faee5b0f | 160 | ENDPOINT_DESCRIPTOR_LENGTH, // bLength |
priyankapashte | 0:78a4faee5b0f | 161 | ENDPOINT_DESCRIPTOR, // bDescriptorType |
priyankapashte | 0:78a4faee5b0f | 162 | PHY_TO_DESC(EPINT_IN), // bEndpointAddress |
priyankapashte | 0:78a4faee5b0f | 163 | E_INTERRUPT, // bmAttributes |
priyankapashte | 0:78a4faee5b0f | 164 | LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB) |
priyankapashte | 0:78a4faee5b0f | 165 | MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB) |
priyankapashte | 0:78a4faee5b0f | 166 | 1, // bInterval (milliseconds) |
priyankapashte | 0:78a4faee5b0f | 167 | |
priyankapashte | 0:78a4faee5b0f | 168 | ENDPOINT_DESCRIPTOR_LENGTH, // bLength |
priyankapashte | 0:78a4faee5b0f | 169 | ENDPOINT_DESCRIPTOR, // bDescriptorType |
priyankapashte | 0:78a4faee5b0f | 170 | PHY_TO_DESC(EPINT_OUT), // bEndpointAddress |
priyankapashte | 0:78a4faee5b0f | 171 | E_INTERRUPT, // bmAttributes |
priyankapashte | 0:78a4faee5b0f | 172 | LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB) |
priyankapashte | 0:78a4faee5b0f | 173 | MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB) |
priyankapashte | 0:78a4faee5b0f | 174 | 1, // bInterval (milliseconds) |
priyankapashte | 0:78a4faee5b0f | 175 | }; |
priyankapashte | 0:78a4faee5b0f | 176 | return configurationDescriptor; |
priyankapashte | 0:78a4faee5b0f | 177 | } |