Modified for PS3 Joystick

Dependents:   NiseKabuto

Fork of USBDevice by Samuel Mokrani

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 
00146             USAGE_PAGE(1), 0x01,           // Generic Desktop
00147             USAGE(1), 0x02,                // Mouse
00148             COLLECTION(1), 0x01,           // Application
00149             USAGE(1), 0x01,                // Pointer
00150             COLLECTION(1), 0x00,           // Physical
00151 
00152             USAGE_PAGE(1), 0x01,            // Generic Desktop
00153             USAGE(1), 0x30,                 // X
00154             USAGE(1), 0x31,                 // Y
00155             LOGICAL_MINIMUM(1), 0x00,       // 0
00156             LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767
00157             REPORT_SIZE(1), 0x10,
00158             REPORT_COUNT(1), 0x02,
00159             INPUT(1), 0x02,                 // Data, Variable, Absolute
00160 
00161             USAGE_PAGE(1), 0x01,            // Generic Desktop
00162             USAGE(1), 0x38,                 // scroll
00163             LOGICAL_MINIMUM(1), 0x81,       // -127
00164             LOGICAL_MAXIMUM(1), 0x7f,       // 127
00165             REPORT_SIZE(1), 0x08,
00166             REPORT_COUNT(1), 0x01,
00167             INPUT(1), 0x06,                 // Data, Variable, Relative
00168 
00169             USAGE_PAGE(1), 0x09,            // Buttons
00170             USAGE_MINIMUM(1), 0x01,
00171             USAGE_MAXIMUM(1), 0x03,
00172             LOGICAL_MINIMUM(1), 0x00,       // 0
00173             LOGICAL_MAXIMUM(1), 0x01,       // 1
00174             REPORT_COUNT(1), 0x03,
00175             REPORT_SIZE(1), 0x01,
00176             INPUT(1), 0x02,                 // Data, Variable, Absolute
00177             REPORT_COUNT(1), 0x01,
00178             REPORT_SIZE(1), 0x05,
00179             INPUT(1), 0x01,                 // Constant
00180 
00181             END_COLLECTION(0),
00182             END_COLLECTION(0)
00183         };
00184         reportLength = sizeof(reportDescriptor);
00185         return reportDescriptor;
00186     }
00187     return NULL;
00188 }
00189 
00190 #define DEFAULT_CONFIGURATION (1)
00191 #define TOTAL_DESCRIPTOR_LENGTH ((1 * CONFIGURATION_DESCRIPTOR_LENGTH) \
00192                                + (1 * INTERFACE_DESCRIPTOR_LENGTH) \
00193                                + (1 * HID_DESCRIPTOR_LENGTH) \
00194                                + (2 * ENDPOINT_DESCRIPTOR_LENGTH))
00195 
00196 uint8_t * USBMouse::configurationDesc() {
00197     static uint8_t configurationDescriptor[] = {
00198         CONFIGURATION_DESCRIPTOR_LENGTH,// bLength
00199         CONFIGURATION_DESCRIPTOR,       // bDescriptorType
00200         LSB(TOTAL_DESCRIPTOR_LENGTH),   // wTotalLength (LSB)
00201         MSB(TOTAL_DESCRIPTOR_LENGTH),   // wTotalLength (MSB)
00202         0x01,                           // bNumInterfaces
00203         DEFAULT_CONFIGURATION,          // bConfigurationValue
00204         0x00,                           // iConfiguration
00205         C_RESERVED | C_SELF_POWERED,    // bmAttributes
00206         C_POWER(0),                     // bMaxPowerHello World from Mbed
00207 
00208         INTERFACE_DESCRIPTOR_LENGTH,    // bLength
00209         INTERFACE_DESCRIPTOR,           // bDescriptorType
00210         0x00,                           // bInterfaceNumber
00211         0x00,                           // bAlternateSetting
00212         0x02,                           // bNumEndpoints
00213         HID_CLASS,                      // bInterfaceClass
00214         1,                              // bInterfaceSubClass
00215         2,                              // bInterfaceProtocol (mouse)
00216         0x00,                           // iInterface
00217 
00218         HID_DESCRIPTOR_LENGTH,          // bLength
00219         HID_DESCRIPTOR,                 // bDescriptorType
00220         LSB(HID_VERSION_1_11),          // bcdHID (LSB)
00221         MSB(HID_VERSION_1_11),          // bcdHID (MSB)
00222         0x00,                           // bCountryCode
00223         0x01,                           // bNumDescriptors
00224         REPORT_DESCRIPTOR,              // bDescriptorType
00225         LSB(reportDescLength()),        // wDescriptorLength (LSB)
00226         MSB(reportDescLength()),        // wDescriptorLength (MSB)
00227 
00228         ENDPOINT_DESCRIPTOR_LENGTH,     // bLength
00229         ENDPOINT_DESCRIPTOR,            // bDescriptorType
00230         PHY_TO_DESC(EPINT_IN),          // bEndpointAddress
00231         E_INTERRUPT,                    // bmAttributes
00232         LSB(MAX_PACKET_SIZE_EPINT),     // wMaxPacketSize (LSB)
00233         MSB(MAX_PACKET_SIZE_EPINT),     // wMaxPacketSize (MSB)
00234         1,                              // bInterval (milliseconds)
00235 
00236         ENDPOINT_DESCRIPTOR_LENGTH,     // bLength
00237         ENDPOINT_DESCRIPTOR,            // bDescriptorType
00238         PHY_TO_DESC(EPINT_OUT),         // bEndpointAddress
00239         E_INTERRUPT,                    // bmAttributes
00240         LSB(MAX_PACKET_SIZE_EPINT),     // wMaxPacketSize (LSB)
00241         MSB(MAX_PACKET_SIZE_EPINT),     // wMaxPacketSize (MSB)
00242         1,                              // bInterval (milliseconds)
00243     };
00244     return configurationDescriptor;
00245 }