USB device stack

Dependents:   mbed-mX-USB-TEST1 USBMSD_SD_HID_HelloWorld HidTest MIDI_usb_bridge ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBMouse.cpp Source File

USBMouse.cpp

00001 /* Copyright (c) 2010-2011 mbed.org, MIT License
00002 *
00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004 * and associated documentation files (the "Software"), to deal in the Software without
00005 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
00006 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
00007 * Software is furnished to do so, subject to the following conditions:
00008 *
00009 * The above copyright notice and this permission notice shall be included in all copies or
00010 * substantial portions of the Software.
00011 *
00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017 */
00018 
00019 #include "stdint.h"
00020 #include "USBMouse.h"
00021 
00022 bool USBMouse::update(int16_t x, int16_t y, uint8_t button, int8_t z) {
00023     switch (mouse_type) {
00024         case REL_MOUSE:
00025             while (x > 127) {
00026                 if (!mouseSend(127, 0, button, z)) return false;
00027                 x = x - 127;
00028             }
00029             while (x < -128) {
00030                 if (!mouseSend(-128, 0, button, z)) return false;
00031                 x = x + 128;
00032             }
00033             while (y > 127) {
00034                 if (!mouseSend(0, 127, button, z)) return false;
00035                 y = y - 127;
00036             }
00037             while (y < -128) {
00038                 if (!mouseSend(0, -128, button, z)) return false;
00039                 y = y + 128;
00040             }
00041             return mouseSend(x, y, button, z);
00042         case ABS_MOUSE:
00043             HID_REPORT report;
00044 
00045             report.data[0] = x & 0xff;
00046             report.data[1] = (x >> 8) & 0xff;
00047             report.data[2] = y & 0xff;
00048             report.data[3] = (y >> 8) & 0xff;
00049             report.data[4] = -z;
00050             report.data[5] = button & 0x07;
00051 
00052             report.length = 6;
00053 
00054             return send(&report);
00055         default:
00056             return false;
00057     }
00058 }
00059 
00060 bool USBMouse::mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z) {
00061     HID_REPORT report;
00062     report.data[0] = buttons & 0x07;
00063     report.data[1] = x;
00064     report.data[2] = y;
00065     report.data[3] = -z; // >0 to scroll down, <0 to scroll up
00066 
00067     report.length = 4;
00068 
00069     return send(&report);
00070 }
00071 
00072 bool USBMouse::move(int16_t x, int16_t y) {
00073     return update(x, y, button, 0);
00074 }
00075 
00076 bool USBMouse::scroll(int8_t z) {
00077     return update(0, 0, button, z);
00078 }
00079 
00080 
00081 bool USBMouse::doubleClick() {
00082     if (!click(MOUSE_LEFT))
00083         return false;
00084     wait(0.1);
00085     return click(MOUSE_LEFT);
00086 }
00087 
00088 bool USBMouse::click(uint8_t button) {
00089     if (!update(0, 0, button, 0))
00090         return false;
00091     wait(0.01);
00092     return update(0, 0, 0, 0);
00093 }
00094 
00095 bool USBMouse::press(uint8_t button_) {
00096     button = button_ & 0x07;
00097     return update(0, 0, button, 0);
00098 }
00099 
00100 bool USBMouse::release(uint8_t button_) {
00101     button = (button & (~button_)) & 0x07;
00102     return update(0, 0, button, 0);
00103 }
00104 
00105 
00106 uint8_t * USBMouse::reportDesc() {
00107 
00108     if (mouse_type == REL_MOUSE) {
00109         static uint8_t reportDescriptor[] = {
00110             USAGE_PAGE(1),      0x01,       // Genric Desktop
00111             USAGE(1),           0x02,       // Mouse
00112             COLLECTION(1),      0x01,       // Application
00113             USAGE(1),           0x01,       // Pointer
00114             COLLECTION(1),      0x00,       // Physical
00115 
00116             REPORT_COUNT(1),    0x03,
00117             REPORT_SIZE(1),     0x01,
00118             USAGE_PAGE(1),      0x09,       // Buttons
00119             USAGE_MINIMUM(1),       0x1,
00120             USAGE_MAXIMUM(1),       0x3,
00121             LOGICAL_MINIMUM(1),     0x00,
00122             LOGICAL_MAXIMUM(1),     0x01,
00123             INPUT(1),           0x02,
00124             REPORT_COUNT(1),    0x01,
00125             REPORT_SIZE(1),     0x05,
00126             INPUT(1),           0x01,
00127 
00128             REPORT_COUNT(1),    0x03,
00129             REPORT_SIZE(1),     0x08,
00130             USAGE_PAGE(1),      0x01,
00131             USAGE(1),           0x30,       // X
00132             USAGE(1),           0x31,       // Y
00133             USAGE(1),           0x38,       // scroll
00134             LOGICAL_MINIMUM(1),     0x81,
00135             LOGICAL_MAXIMUM(1),     0x7f,
00136             INPUT(1),           0x06,       // Relative data
00137 
00138             END_COLLECTION(0),
00139             END_COLLECTION(0),
00140         };
00141         reportLength = sizeof(reportDescriptor);
00142         return reportDescriptor;
00143     } else if (mouse_type == ABS_MOUSE) {
00144         static uint8_t reportDescriptor[] = {
00145             USAGE_PAGE(1), 0x01,           // Generic Desktop
00146             USAGE(1), 0x02,                // Mouse
00147             COLLECTION(1), 0x01,           // Application
00148             USAGE(1), 0x01,                // Pointer
00149             COLLECTION(1), 0x00,           // Physical
00150 
00151             USAGE_PAGE(1), 0x01,            // Generic Desktop
00152             USAGE(1), 0x30,                 // X
00153             USAGE(1), 0x31,                 // Y
00154             LOGICAL_MINIMUM(1), 0x00,       // 0
00155             LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767
00156             REPORT_SIZE(1), 0x10,
00157             REPORT_COUNT(1), 0x02,
00158             INPUT(1), 0x02,                 // Data, Variable, Absolute
00159 
00160             USAGE_PAGE(1), 0x01,            // Generic Desktop
00161             USAGE(1), 0x38,                 // scroll
00162             LOGICAL_MINIMUM(1), 0x81,       // -127
00163             LOGICAL_MAXIMUM(1), 0x7f,       // 127
00164             REPORT_SIZE(1), 0x08,
00165             REPORT_COUNT(1), 0x01,
00166             INPUT(1), 0x06,                 // Data, Variable, Relative
00167 
00168             USAGE_PAGE(1), 0x09,            // Buttons
00169             USAGE_MINIMUM(1), 0x01,
00170             USAGE_MAXIMUM(1), 0x03,
00171             LOGICAL_MINIMUM(1), 0x00,       // 0
00172             LOGICAL_MAXIMUM(1), 0x01,       // 1
00173             REPORT_COUNT(1), 0x03,
00174             REPORT_SIZE(1), 0x01,
00175             INPUT(1), 0x02,                 // Data, Variable, Absolute
00176             REPORT_COUNT(1), 0x01,
00177             REPORT_SIZE(1), 0x05,
00178             INPUT(1), 0x01,                 // Constant
00179 
00180             END_COLLECTION(0),
00181             END_COLLECTION(0)
00182         };
00183         reportLength = sizeof(reportDescriptor);
00184         return reportDescriptor;
00185     }
00186     return NULL;
00187 }
00188 
00189 #define DEFAULT_CONFIGURATION (1)
00190 #define TOTAL_DESCRIPTOR_LENGTH ((1 * CONFIGURATION_DESCRIPTOR_LENGTH) \
00191                                + (1 * INTERFACE_DESCRIPTOR_LENGTH) \
00192                                + (1 * HID_DESCRIPTOR_LENGTH) \
00193                                + (2 * ENDPOINT_DESCRIPTOR_LENGTH))
00194 
00195 uint8_t * USBMouse::configurationDesc() {
00196     static uint8_t configurationDescriptor[] = {
00197         CONFIGURATION_DESCRIPTOR_LENGTH,    // bLength
00198         CONFIGURATION_DESCRIPTOR,           // bDescriptorType
00199         LSB(TOTAL_DESCRIPTOR_LENGTH),       // wTotalLength (LSB)
00200         MSB(TOTAL_DESCRIPTOR_LENGTH),       // wTotalLength (MSB)
00201         0x01,                               // bNumInterfaces
00202         DEFAULT_CONFIGURATION,              // bConfigurationValue
00203         0x00,                               // iConfiguration
00204         C_RESERVED | C_SELF_POWERED,        // bmAttributes
00205         C_POWER(0),                         // bMaxPower
00206 
00207         INTERFACE_DESCRIPTOR_LENGTH,        // bLength
00208         INTERFACE_DESCRIPTOR,               // bDescriptorType
00209         0x00,                               // bInterfaceNumber
00210         0x00,                               // bAlternateSetting
00211         0x02,                               // bNumEndpoints
00212         HID_CLASS,                          // bInterfaceClass
00213         HID_SUBCLASS_BOOT,                  // bInterfaceSubClass
00214         HID_PROTOCOL_MOUSE,                 // bInterfaceProtocol
00215         0x00,                               // iInterface
00216 
00217         HID_DESCRIPTOR_LENGTH,              // bLength
00218         HID_DESCRIPTOR,                     // bDescriptorType
00219         LSB(HID_VERSION_1_11),              // bcdHID (LSB)
00220         MSB(HID_VERSION_1_11),              // bcdHID (MSB)
00221         0x00,                               // bCountryCode
00222         0x01,                               // bNumDescriptors
00223         REPORT_DESCRIPTOR,                  // bDescriptorType
00224         (uint8_t)(LSB(reportDescLength())), // wDescriptorLength (LSB)
00225         (uint8_t)(MSB(reportDescLength())), // wDescriptorLength (MSB)
00226 
00227         ENDPOINT_DESCRIPTOR_LENGTH,         // bLength
00228         ENDPOINT_DESCRIPTOR,                // bDescriptorType
00229         PHY_TO_DESC(EPINT_IN),              // bEndpointAddress
00230         E_INTERRUPT,                        // bmAttributes
00231         LSB(MAX_PACKET_SIZE_EPINT),         // wMaxPacketSize (LSB)
00232         MSB(MAX_PACKET_SIZE_EPINT),         // wMaxPacketSize (MSB)
00233         1,                                  // bInterval (milliseconds)
00234 
00235         ENDPOINT_DESCRIPTOR_LENGTH,         // bLength
00236         ENDPOINT_DESCRIPTOR,                // bDescriptorType
00237         PHY_TO_DESC(EPINT_OUT),             // bEndpointAddress
00238         E_INTERRUPT,                        // bmAttributes
00239         LSB(MAX_PACKET_SIZE_EPINT),         // wMaxPacketSize (LSB)
00240         MSB(MAX_PACKET_SIZE_EPINT),         // wMaxPacketSize (MSB)
00241         1,                                  // bInterval (milliseconds)
00242     };
00243     return configurationDescriptor;
00244 }