USB device stack, with KL25Z fixes for USB 3.0 hosts and sleep/resume interrupt handling

Dependents:   frdm_Slider_Keyboard idd_hw2_figlax_PanType idd_hw2_appachu_finger_chording idd_hw3_AngieWangAntonioDeLimaFernandesDanielLim_BladeSymphony ... more

Fork of USBDevice by mbed official

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 const uint8_t *USBMouse::reportDesc(int idx, uint16_t &len) 
00107 {
00108     // we only have one interface
00109     if (idx != 0) {
00110         len = 0;
00111         return 0;
00112     }
00113     
00114     if (mouse_type == REL_MOUSE) 
00115     {
00116         static const uint8_t reportDescriptor[] = {
00117             USAGE_PAGE(1),      0x01,       // Genric Desktop
00118             USAGE(1),           0x02,       // Mouse
00119             COLLECTION(1),      0x01,       // Application
00120             USAGE(1),           0x01,       // Pointer
00121             COLLECTION(1),      0x00,       // Physical
00122 
00123             REPORT_COUNT(1),    0x03,
00124             REPORT_SIZE(1),     0x01,
00125             USAGE_PAGE(1),      0x09,       // Buttons
00126             USAGE_MINIMUM(1),       0x1,
00127             USAGE_MAXIMUM(1),       0x3,
00128             LOGICAL_MINIMUM(1),     0x00,
00129             LOGICAL_MAXIMUM(1),     0x01,
00130             INPUT(1),           0x02,
00131             REPORT_COUNT(1),    0x01,
00132             REPORT_SIZE(1),     0x05,
00133             INPUT(1),           0x01,
00134 
00135             REPORT_COUNT(1),    0x03,
00136             REPORT_SIZE(1),     0x08,
00137             USAGE_PAGE(1),      0x01,
00138             USAGE(1),           0x30,       // X
00139             USAGE(1),           0x31,       // Y
00140             USAGE(1),           0x38,       // scroll
00141             LOGICAL_MINIMUM(1),     0x81,
00142             LOGICAL_MAXIMUM(1),     0x7f,
00143             INPUT(1),           0x06,       // Relative data
00144 
00145             END_COLLECTION(0),
00146             END_COLLECTION(0),
00147         };
00148 
00149         len = sizeof(reportDescriptor);
00150         return reportDescriptor;
00151     }
00152     else if (mouse_type == ABS_MOUSE) 
00153     {
00154         static const uint8_t reportDescriptor[] = {
00155 
00156             USAGE_PAGE(1), 0x01,           // Generic Desktop
00157             USAGE(1), 0x02,                // Mouse
00158             COLLECTION(1), 0x01,           // Application
00159             USAGE(1), 0x01,                // Pointer
00160             COLLECTION(1), 0x00,           // Physical
00161 
00162             USAGE_PAGE(1), 0x01,            // Generic Desktop
00163             USAGE(1), 0x30,                 // X
00164             USAGE(1), 0x31,                 // Y
00165             LOGICAL_MINIMUM(1), 0x00,       // 0
00166             LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767
00167             REPORT_SIZE(1), 0x10,
00168             REPORT_COUNT(1), 0x02,
00169             INPUT(1), 0x02,                 // Data, Variable, Absolute
00170 
00171             USAGE_PAGE(1), 0x01,            // Generic Desktop
00172             USAGE(1), 0x38,                 // scroll
00173             LOGICAL_MINIMUM(1), 0x81,       // -127
00174             LOGICAL_MAXIMUM(1), 0x7f,       // 127
00175             REPORT_SIZE(1), 0x08,
00176             REPORT_COUNT(1), 0x01,
00177             INPUT(1), 0x06,                 // Data, Variable, Relative
00178 
00179             USAGE_PAGE(1), 0x09,            // Buttons
00180             USAGE_MINIMUM(1), 0x01,
00181             USAGE_MAXIMUM(1), 0x03,
00182             LOGICAL_MINIMUM(1), 0x00,       // 0
00183             LOGICAL_MAXIMUM(1), 0x01,       // 1
00184             REPORT_COUNT(1), 0x03,
00185             REPORT_SIZE(1), 0x01,
00186             INPUT(1), 0x02,                 // Data, Variable, Absolute
00187             REPORT_COUNT(1), 0x01,
00188             REPORT_SIZE(1), 0x05,
00189             INPUT(1), 0x01,                 // Constant
00190 
00191             END_COLLECTION(0),
00192             END_COLLECTION(0)
00193         };
00194         
00195         len = sizeof(reportDescriptor);
00196         return reportDescriptor;
00197     }
00198     return NULL;
00199 }
00200 
00201 #define DEFAULT_CONFIGURATION (1)
00202 #define TOTAL_DESCRIPTOR_LENGTH ((1 * CONFIGURATION_DESCRIPTOR_LENGTH) \
00203                                + (1 * INTERFACE_DESCRIPTOR_LENGTH) \
00204                                + (1 * HID_DESCRIPTOR_LENGTH) \
00205                                + (2 * ENDPOINT_DESCRIPTOR_LENGTH))
00206 
00207 const uint8_t *USBMouse::configurationDesc() 
00208 {
00209     const uint16_t rdl = reportDescLength(0);
00210     static const uint8_t configurationDescriptor[] = {
00211         CONFIGURATION_DESCRIPTOR_LENGTH,// bLength
00212         CONFIGURATION_DESCRIPTOR,       // bDescriptorType
00213         LSB(TOTAL_DESCRIPTOR_LENGTH),   // wTotalLength (LSB)
00214         MSB(TOTAL_DESCRIPTOR_LENGTH),   // wTotalLength (MSB)
00215         0x01,                           // bNumInterfaces
00216         DEFAULT_CONFIGURATION,          // bConfigurationValue
00217         0x00,                           // iConfiguration
00218         C_RESERVED | C_SELF_POWERED,    // bmAttributes
00219         C_POWER(0),                     // bMaxPowerHello World from Mbed
00220 
00221         INTERFACE_DESCRIPTOR_LENGTH,    // bLength
00222         INTERFACE_DESCRIPTOR,           // bDescriptorType
00223         0x00,                           // bInterfaceNumber
00224         0x00,                           // bAlternateSetting
00225         0x02,                           // bNumEndpoints
00226         HID_CLASS,                      // bInterfaceClass
00227         1,                              // bInterfaceSubClass
00228         2,                              // bInterfaceProtocol (mouse)
00229         0x00,                           // iInterface
00230 
00231         HID_DESCRIPTOR_LENGTH,          // bLength
00232         HID_DESCRIPTOR,                 // bDescriptorType
00233         LSB(HID_VERSION_1_11),          // bcdHID (LSB)
00234         MSB(HID_VERSION_1_11),          // bcdHID (MSB)
00235         0x00,                           // bCountryCode
00236         0x01,                           // bNumDescriptors
00237         REPORT_DESCRIPTOR,              // bDescriptorType
00238         (uint8_t)(LSB(rdl)),            // wDescriptorLength (LSB)
00239         (uint8_t)(MSB(rdl)),            // wDescriptorLength (MSB)
00240 
00241         ENDPOINT_DESCRIPTOR_LENGTH,     // bLength
00242         ENDPOINT_DESCRIPTOR,            // bDescriptorType
00243         PHY_TO_DESC(EPINT_IN),          // bEndpointAddress
00244         E_INTERRUPT,                    // bmAttributes
00245         LSB(MAX_PACKET_SIZE_EPINT),     // wMaxPacketSize (LSB)
00246         MSB(MAX_PACKET_SIZE_EPINT),     // wMaxPacketSize (MSB)
00247         1,                              // bInterval (milliseconds)
00248 
00249         ENDPOINT_DESCRIPTOR_LENGTH,     // bLength
00250         ENDPOINT_DESCRIPTOR,            // bDescriptorType
00251         PHY_TO_DESC(EPINT_OUT),         // bEndpointAddress
00252         E_INTERRUPT,                    // bmAttributes
00253         LSB(MAX_PACKET_SIZE_EPINT),     // wMaxPacketSize (LSB)
00254         MSB(MAX_PACKET_SIZE_EPINT),     // wMaxPacketSize (MSB)
00255         1,                              // bInterval (milliseconds)
00256     };
00257     return configurationDescriptor;
00258 }