USB device stack

Dependents:   blinky_max32630fthr FTHR_USB_serial FTHR_OLED HSP_RPC_GUI_3_0_1 ... more

Fork of USBDevice by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBMouseKeyboard.cpp Source File

USBMouseKeyboard.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 "USBMouseKeyboard.h"
00021 
00022 typedef struct {
00023     unsigned char usage;
00024     unsigned char modifier;
00025 } KEYMAP;
00026 
00027 #ifdef US_KEYBOARD
00028 /* US keyboard (as HID standard) */
00029 #define KEYMAP_SIZE (152)
00030 const KEYMAP keymap[KEYMAP_SIZE] = {
00031     {0, 0},             /* NUL */
00032     {0, 0},             /* SOH */
00033     {0, 0},             /* STX */
00034     {0, 0},             /* ETX */
00035     {0, 0},             /* EOT */
00036     {0, 0},             /* ENQ */
00037     {0, 0},             /* ACK */
00038     {0, 0},             /* BEL */
00039     {0x2a, 0},          /* BS  */  /* Keyboard Delete (Backspace) */
00040     {0x2b, 0},          /* TAB */  /* Keyboard Tab */
00041     {0x28, 0},          /* LF  */  /* Keyboard Return (Enter) */
00042     {0, 0},             /* VT  */
00043     {0, 0},             /* FF  */
00044     {0, 0},             /* CR  */
00045     {0, 0},             /* SO  */
00046     {0, 0},             /* SI  */
00047     {0, 0},             /* DEL */
00048     {0, 0},             /* DC1 */
00049     {0, 0},             /* DC2 */
00050     {0, 0},             /* DC3 */
00051     {0, 0},             /* DC4 */
00052     {0, 0},             /* NAK */
00053     {0, 0},             /* SYN */
00054     {0, 0},             /* ETB */
00055     {0, 0},             /* CAN */
00056     {0, 0},             /* EM  */
00057     {0, 0},             /* SUB */
00058     {0, 0},             /* ESC */
00059     {0, 0},             /* FS  */
00060     {0, 0},             /* GS  */
00061     {0, 0},             /* RS  */
00062     {0, 0},             /* US  */
00063     {0x2c, 0},          /*   */
00064     {0x1e, KEY_SHIFT},      /* ! */
00065     {0x34, KEY_SHIFT},      /* " */
00066     {0x20, KEY_SHIFT},      /* # */
00067     {0x21, KEY_SHIFT},      /* $ */
00068     {0x22, KEY_SHIFT},      /* % */
00069     {0x24, KEY_SHIFT},      /* & */
00070     {0x34, 0},          /* ' */
00071     {0x26, KEY_SHIFT},      /* ( */
00072     {0x27, KEY_SHIFT},      /* ) */
00073     {0x25, KEY_SHIFT},      /* * */
00074     {0x2e, KEY_SHIFT},      /* + */
00075     {0x36, 0},          /* , */
00076     {0x2d, 0},          /* - */
00077     {0x37, 0},          /* . */
00078     {0x38, 0},          /* / */
00079     {0x27, 0},          /* 0 */
00080     {0x1e, 0},          /* 1 */
00081     {0x1f, 0},          /* 2 */
00082     {0x20, 0},          /* 3 */
00083     {0x21, 0},          /* 4 */
00084     {0x22, 0},          /* 5 */
00085     {0x23, 0},          /* 6 */
00086     {0x24, 0},          /* 7 */
00087     {0x25, 0},          /* 8 */
00088     {0x26, 0},          /* 9 */
00089     {0x33, KEY_SHIFT},      /* : */
00090     {0x33, 0},          /* ; */
00091     {0x36, KEY_SHIFT},      /* < */
00092     {0x2e, 0},          /* = */
00093     {0x37, KEY_SHIFT},      /* > */
00094     {0x38, KEY_SHIFT},      /* ? */
00095     {0x1f, KEY_SHIFT},      /* @ */
00096     {0x04, KEY_SHIFT},      /* A */
00097     {0x05, KEY_SHIFT},      /* B */
00098     {0x06, KEY_SHIFT},      /* C */
00099     {0x07, KEY_SHIFT},      /* D */
00100     {0x08, KEY_SHIFT},      /* E */
00101     {0x09, KEY_SHIFT},      /* F */
00102     {0x0a, KEY_SHIFT},      /* G */
00103     {0x0b, KEY_SHIFT},      /* H */
00104     {0x0c, KEY_SHIFT},      /* I */
00105     {0x0d, KEY_SHIFT},      /* J */
00106     {0x0e, KEY_SHIFT},      /* K */
00107     {0x0f, KEY_SHIFT},      /* L */
00108     {0x10, KEY_SHIFT},      /* M */
00109     {0x11, KEY_SHIFT},      /* N */
00110     {0x12, KEY_SHIFT},      /* O */
00111     {0x13, KEY_SHIFT},      /* P */
00112     {0x14, KEY_SHIFT},      /* Q */
00113     {0x15, KEY_SHIFT},      /* R */
00114     {0x16, KEY_SHIFT},      /* S */
00115     {0x17, KEY_SHIFT},      /* T */
00116     {0x18, KEY_SHIFT},      /* U */
00117     {0x19, KEY_SHIFT},      /* V */
00118     {0x1a, KEY_SHIFT},      /* W */
00119     {0x1b, KEY_SHIFT},      /* X */
00120     {0x1c, KEY_SHIFT},      /* Y */
00121     {0x1d, KEY_SHIFT},      /* Z */
00122     {0x2f, 0},          /* [ */
00123     {0x31, 0},          /* \ */
00124     {0x30, 0},          /* ] */
00125     {0x23, KEY_SHIFT},      /* ^ */
00126     {0x2d, KEY_SHIFT},      /* _ */
00127     {0x35, 0},          /* ` */
00128     {0x04, 0},          /* a */
00129     {0x05, 0},          /* b */
00130     {0x06, 0},          /* c */
00131     {0x07, 0},          /* d */
00132     {0x08, 0},          /* e */
00133     {0x09, 0},          /* f */
00134     {0x0a, 0},          /* g */
00135     {0x0b, 0},          /* h */
00136     {0x0c, 0},          /* i */
00137     {0x0d, 0},          /* j */
00138     {0x0e, 0},          /* k */
00139     {0x0f, 0},          /* l */
00140     {0x10, 0},          /* m */
00141     {0x11, 0},          /* n */
00142     {0x12, 0},          /* o */
00143     {0x13, 0},          /* p */
00144     {0x14, 0},          /* q */
00145     {0x15, 0},          /* r */
00146     {0x16, 0},          /* s */
00147     {0x17, 0},          /* t */
00148     {0x18, 0},          /* u */
00149     {0x19, 0},          /* v */
00150     {0x1a, 0},          /* w */
00151     {0x1b, 0},          /* x */
00152     {0x1c, 0},          /* y */
00153     {0x1d, 0},          /* z */
00154     {0x2f, KEY_SHIFT},      /* { */
00155     {0x31, KEY_SHIFT},      /* | */
00156     {0x30, KEY_SHIFT},      /* } */
00157     {0x35, KEY_SHIFT},      /* ~ */
00158     {0,0},              /* DEL */
00159 
00160     {0x3a, 0},          /* F1 */
00161     {0x3b, 0},          /* F2 */
00162     {0x3c, 0},          /* F3 */
00163     {0x3d, 0},          /* F4 */
00164     {0x3e, 0},          /* F5 */
00165     {0x3f, 0},          /* F6 */
00166     {0x40, 0},          /* F7 */
00167     {0x41, 0},          /* F8 */
00168     {0x42, 0},          /* F9 */
00169     {0x43, 0},          /* F10 */
00170     {0x44, 0},          /* F11 */
00171     {0x45, 0},          /* F12 */
00172 
00173     {0x46, 0},          /* PRINT_SCREEN */
00174     {0x47, 0},          /* SCROLL_LOCK */
00175     {0x39, 0},          /* CAPS_LOCK */
00176     {0x53, 0},          /* NUM_LOCK */
00177     {0x49, 0},          /* INSERT */
00178     {0x4a, 0},          /* HOME */
00179     {0x4b, 0},          /* PAGE_UP */
00180     {0x4e, 0},          /* PAGE_DOWN */
00181 
00182     {0x4f, 0},          /* RIGHT_ARROW */
00183     {0x50, 0},          /* LEFT_ARROW */
00184     {0x51, 0},          /* DOWN_ARROW */
00185     {0x52, 0},          /* UP_ARROW */
00186 };
00187 
00188 #else
00189 /* UK keyboard */
00190 #define KEYMAP_SIZE (152)
00191 const KEYMAP keymap[KEYMAP_SIZE] = {
00192     {0, 0},             /* NUL */
00193     {0, 0},             /* SOH */
00194     {0, 0},             /* STX */
00195     {0, 0},             /* ETX */
00196     {0, 0},             /* EOT */
00197     {0, 0},             /* ENQ */
00198     {0, 0},             /* ACK */
00199     {0, 0},             /* BEL */
00200     {0x2a, 0},          /* BS  */  /* Keyboard Delete (Backspace) */
00201     {0x2b, 0},          /* TAB */  /* Keyboard Tab */
00202     {0x28, 0},          /* LF  */  /* Keyboard Return (Enter) */
00203     {0, 0},             /* VT  */
00204     {0, 0},             /* FF  */
00205     {0, 0},             /* CR  */
00206     {0, 0},             /* SO  */
00207     {0, 0},             /* SI  */
00208     {0, 0},             /* DEL */
00209     {0, 0},             /* DC1 */
00210     {0, 0},             /* DC2 */
00211     {0, 0},             /* DC3 */
00212     {0, 0},             /* DC4 */
00213     {0, 0},             /* NAK */
00214     {0, 0},             /* SYN */
00215     {0, 0},             /* ETB */
00216     {0, 0},             /* CAN */
00217     {0, 0},             /* EM  */
00218     {0, 0},             /* SUB */
00219     {0, 0},             /* ESC */
00220     {0, 0},             /* FS  */
00221     {0, 0},             /* GS  */
00222     {0, 0},             /* RS  */
00223     {0, 0},             /* US  */
00224     {0x2c, 0},          /*   */
00225     {0x1e, KEY_SHIFT},      /* ! */
00226     {0x1f, KEY_SHIFT},      /* " */
00227     {0x32, 0},          /* # */
00228     {0x21, KEY_SHIFT},      /* $ */
00229     {0x22, KEY_SHIFT},      /* % */
00230     {0x24, KEY_SHIFT},      /* & */
00231     {0x34, 0},          /* ' */
00232     {0x26, KEY_SHIFT},      /* ( */
00233     {0x27, KEY_SHIFT},      /* ) */
00234     {0x25, KEY_SHIFT},      /* * */
00235     {0x2e, KEY_SHIFT},      /* + */
00236     {0x36, 0},          /* , */
00237     {0x2d, 0},          /* - */
00238     {0x37, 0},          /* . */
00239     {0x38, 0},          /* / */
00240     {0x27, 0},          /* 0 */
00241     {0x1e, 0},          /* 1 */
00242     {0x1f, 0},          /* 2 */
00243     {0x20, 0},          /* 3 */
00244     {0x21, 0},          /* 4 */
00245     {0x22, 0},          /* 5 */
00246     {0x23, 0},          /* 6 */
00247     {0x24, 0},          /* 7 */
00248     {0x25, 0},          /* 8 */
00249     {0x26, 0},          /* 9 */
00250     {0x33, KEY_SHIFT},      /* : */
00251     {0x33, 0},          /* ; */
00252     {0x36, KEY_SHIFT},      /* < */
00253     {0x2e, 0},          /* = */
00254     {0x37, KEY_SHIFT},      /* > */
00255     {0x38, KEY_SHIFT},      /* ? */
00256     {0x34, KEY_SHIFT},      /* @ */
00257     {0x04, KEY_SHIFT},      /* A */
00258     {0x05, KEY_SHIFT},      /* B */
00259     {0x06, KEY_SHIFT},      /* C */
00260     {0x07, KEY_SHIFT},      /* D */
00261     {0x08, KEY_SHIFT},      /* E */
00262     {0x09, KEY_SHIFT},      /* F */
00263     {0x0a, KEY_SHIFT},      /* G */
00264     {0x0b, KEY_SHIFT},      /* H */
00265     {0x0c, KEY_SHIFT},      /* I */
00266     {0x0d, KEY_SHIFT},      /* J */
00267     {0x0e, KEY_SHIFT},      /* K */
00268     {0x0f, KEY_SHIFT},      /* L */
00269     {0x10, KEY_SHIFT},      /* M */
00270     {0x11, KEY_SHIFT},      /* N */
00271     {0x12, KEY_SHIFT},      /* O */
00272     {0x13, KEY_SHIFT},      /* P */
00273     {0x14, KEY_SHIFT},      /* Q */
00274     {0x15, KEY_SHIFT},      /* R */
00275     {0x16, KEY_SHIFT},      /* S */
00276     {0x17, KEY_SHIFT},      /* T */
00277     {0x18, KEY_SHIFT},      /* U */
00278     {0x19, KEY_SHIFT},      /* V */
00279     {0x1a, KEY_SHIFT},      /* W */
00280     {0x1b, KEY_SHIFT},      /* X */
00281     {0x1c, KEY_SHIFT},      /* Y */
00282     {0x1d, KEY_SHIFT},      /* Z */
00283     {0x2f, 0},          /* [ */
00284     {0x64, 0},          /* \ */
00285     {0x30, 0},          /* ] */
00286     {0x23, KEY_SHIFT},      /* ^ */
00287     {0x2d, KEY_SHIFT},      /* _ */
00288     {0x35, 0},          /* ` */
00289     {0x04, 0},          /* a */
00290     {0x05, 0},          /* b */
00291     {0x06, 0},          /* c */
00292     {0x07, 0},          /* d */
00293     {0x08, 0},          /* e */
00294     {0x09, 0},          /* f */
00295     {0x0a, 0},          /* g */
00296     {0x0b, 0},          /* h */
00297     {0x0c, 0},          /* i */
00298     {0x0d, 0},          /* j */
00299     {0x0e, 0},          /* k */
00300     {0x0f, 0},          /* l */
00301     {0x10, 0},          /* m */
00302     {0x11, 0},          /* n */
00303     {0x12, 0},          /* o */
00304     {0x13, 0},          /* p */
00305     {0x14, 0},          /* q */
00306     {0x15, 0},          /* r */
00307     {0x16, 0},          /* s */
00308     {0x17, 0},          /* t */
00309     {0x18, 0},          /* u */
00310     {0x19, 0},          /* v */
00311     {0x1a, 0},          /* w */
00312     {0x1b, 0},          /* x */
00313     {0x1c, 0},          /* y */
00314     {0x1d, 0},          /* z */
00315     {0x2f, KEY_SHIFT},      /* { */
00316     {0x64, KEY_SHIFT},      /* | */
00317     {0x30, KEY_SHIFT},      /* } */
00318     {0x32, KEY_SHIFT},      /* ~ */
00319     {0,0},             /* DEL */
00320 
00321     {0x3a, 0},          /* F1 */
00322     {0x3b, 0},          /* F2 */
00323     {0x3c, 0},          /* F3 */
00324     {0x3d, 0},          /* F4 */
00325     {0x3e, 0},          /* F5 */
00326     {0x3f, 0},          /* F6 */
00327     {0x40, 0},          /* F7 */
00328     {0x41, 0},          /* F8 */
00329     {0x42, 0},          /* F9 */
00330     {0x43, 0},          /* F10 */
00331     {0x44, 0},          /* F11 */
00332     {0x45, 0},          /* F12 */
00333 
00334     {0x46, 0},          /* PRINT_SCREEN */
00335     {0x47, 0},          /* SCROLL_LOCK */
00336     {0x39, 0},          /* CAPS_LOCK */
00337     {0x53, 0},          /* NUM_LOCK */
00338     {0x49, 0},          /* INSERT */
00339     {0x4a, 0},          /* HOME */
00340     {0x4b, 0},          /* PAGE_UP */
00341     {0x4e, 0},          /* PAGE_DOWN */
00342 
00343     {0x4f, 0},          /* RIGHT_ARROW */
00344     {0x50, 0},          /* LEFT_ARROW */
00345     {0x51, 0},          /* DOWN_ARROW */
00346     {0x52, 0},          /* UP_ARROW */
00347 };
00348 #endif
00349 
00350 
00351 uint8_t * USBMouseKeyboard::reportDesc() {
00352     if (mouse_type == REL_MOUSE) {
00353         static uint8_t reportDescriptor[] = {
00354                 // Keyboard
00355                 USAGE_PAGE(1),      0x01,
00356                 USAGE(1),           0x06,
00357                 COLLECTION(1),      0x01,
00358                 REPORT_ID(1),       REPORT_ID_KEYBOARD,
00359                 USAGE_PAGE(1),      0x07,
00360                 USAGE_MINIMUM(1),       0xE0,
00361                 USAGE_MAXIMUM(1),       0xE7,
00362                 LOGICAL_MINIMUM(1),     0x00,
00363                 LOGICAL_MAXIMUM(1),     0x01,
00364                 REPORT_SIZE(1),     0x01,
00365                 REPORT_COUNT(1),    0x08,
00366                 INPUT(1),           0x02,
00367                 REPORT_COUNT(1),    0x01,
00368                 REPORT_SIZE(1),     0x08,
00369                 INPUT(1),           0x01,
00370                 REPORT_COUNT(1),    0x05,
00371                 REPORT_SIZE(1),     0x01,
00372                 USAGE_PAGE(1),      0x08,
00373                 USAGE_MINIMUM(1),       0x01,
00374                 USAGE_MAXIMUM(1),       0x05,
00375                 OUTPUT(1),          0x02,
00376                 REPORT_COUNT(1),    0x01,
00377                 REPORT_SIZE(1),     0x03,
00378                 OUTPUT(1),          0x01,
00379                 REPORT_COUNT(1),    0x06,
00380                 REPORT_SIZE(1),     0x08,
00381                 LOGICAL_MINIMUM(1),     0x00,
00382                 LOGICAL_MAXIMUM(2),     0xff, 0x00,
00383                 USAGE_PAGE(1),      0x07,
00384                 USAGE_MINIMUM(1),       0x00,
00385                 USAGE_MAXIMUM(2),       0xff, 0x00,
00386                 INPUT(1),           0x00,
00387                 END_COLLECTION(0),
00388 
00389                 // Mouse
00390                 USAGE_PAGE(1),      0x01,           // Generic Desktop
00391                 USAGE(1),           0x02,           // Mouse
00392                 COLLECTION(1),      0x01,           // Application
00393                 USAGE(1),           0x01,           // Pointer
00394                 COLLECTION(1),      0x00,           // Physical
00395                 REPORT_ID(1),       REPORT_ID_MOUSE,
00396                 REPORT_COUNT(1),    0x03,
00397                 REPORT_SIZE(1),     0x01,
00398                 USAGE_PAGE(1),      0x09,           // Buttons
00399                 USAGE_MINIMUM(1),       0x1,
00400                 USAGE_MAXIMUM(1),       0x3,
00401                 LOGICAL_MINIMUM(1),     0x00,
00402                 LOGICAL_MAXIMUM(1),     0x01,
00403                 INPUT(1),           0x02,
00404                 REPORT_COUNT(1),    0x01,
00405                 REPORT_SIZE(1),     0x05,
00406                 INPUT(1),           0x01,
00407                 REPORT_COUNT(1),    0x03,
00408                 REPORT_SIZE(1),     0x08,
00409                 USAGE_PAGE(1),      0x01,
00410                 USAGE(1),           0x30,           // X
00411                 USAGE(1),           0x31,           // Y
00412                 USAGE(1),           0x38,           // scroll
00413                 LOGICAL_MINIMUM(1),     0x81,
00414                 LOGICAL_MAXIMUM(1),     0x7f,
00415                 INPUT(1),           0x06,
00416                 END_COLLECTION(0),
00417                 END_COLLECTION(0),
00418 
00419 
00420                 // Media Control
00421                 USAGE_PAGE(1), 0x0C,
00422                 USAGE(1), 0x01,
00423                 COLLECTION(1), 0x01,
00424                 REPORT_ID(1), REPORT_ID_VOLUME,
00425                 USAGE_PAGE(1), 0x0C,
00426                 LOGICAL_MINIMUM(1), 0x00,
00427                 LOGICAL_MAXIMUM(1), 0x01,
00428                 REPORT_SIZE(1), 0x01,
00429                 REPORT_COUNT(1), 0x07,
00430                 USAGE(1), 0xB5,             // Next Track
00431                 USAGE(1), 0xB6,             // Previous Track
00432                 USAGE(1), 0xB7,             // Stop
00433                 USAGE(1), 0xCD,             // Play / Pause
00434                 USAGE(1), 0xE2,             // Mute
00435                 USAGE(1), 0xE9,             // Volume Up
00436                 USAGE(1), 0xEA,             // Volume Down
00437                 INPUT(1), 0x02,             // Input (Data, Variable, Absolute)
00438                 REPORT_COUNT(1), 0x01,
00439                 INPUT(1), 0x01,
00440                 END_COLLECTION(0),
00441         };
00442         reportLength = sizeof(reportDescriptor);
00443         return reportDescriptor;
00444     } else if (mouse_type == ABS_MOUSE) {
00445         static uint8_t reportDescriptor[] = {
00446 
00447                 // Keyboard
00448                 USAGE_PAGE(1),      0x01,
00449                 USAGE(1),           0x06,
00450                 COLLECTION(1),      0x01,
00451                 REPORT_ID(1),       REPORT_ID_KEYBOARD,
00452                 USAGE_PAGE(1),      0x07,
00453                 USAGE_MINIMUM(1),       0xE0,
00454                 USAGE_MAXIMUM(1),       0xE7,
00455                 LOGICAL_MINIMUM(1),     0x00,
00456                 LOGICAL_MAXIMUM(1),     0x01,
00457                 REPORT_SIZE(1),     0x01,
00458                 REPORT_COUNT(1),    0x08,
00459                 INPUT(1),           0x02,
00460                 REPORT_COUNT(1),    0x01,
00461                 REPORT_SIZE(1),     0x08,
00462                 INPUT(1),           0x01,
00463                 REPORT_COUNT(1),    0x05,
00464                 REPORT_SIZE(1),     0x01,
00465                 USAGE_PAGE(1),      0x08,
00466                 USAGE_MINIMUM(1),       0x01,
00467                 USAGE_MAXIMUM(1),       0x05,
00468                 OUTPUT(1),          0x02,
00469                 REPORT_COUNT(1),    0x01,
00470                 REPORT_SIZE(1),     0x03,
00471                 OUTPUT(1),          0x01,
00472                 REPORT_COUNT(1),    0x06,
00473                 REPORT_SIZE(1),     0x08,
00474                 LOGICAL_MINIMUM(1),     0x00,
00475                 LOGICAL_MAXIMUM(2),     0xff, 0x00,
00476                 USAGE_PAGE(1),      0x07,
00477                 USAGE_MINIMUM(1),       0x00,
00478                 USAGE_MAXIMUM(2),       0xff, 0x00,
00479                 INPUT(1),           0x00,
00480                 END_COLLECTION(0),
00481 
00482                 // Mouse
00483                 USAGE_PAGE(1), 0x01,               // Generic Desktop
00484                 USAGE(1), 0x02,                    // Mouse
00485                 COLLECTION(1), 0x01,               // Application
00486                 USAGE(1), 0x01,                    // Pointer
00487                 COLLECTION(1), 0x00,               // Physical
00488                 REPORT_ID(1),       REPORT_ID_MOUSE,
00489 
00490                 USAGE_PAGE(1), 0x01,                // Generic Desktop
00491                 USAGE(1), 0x30,                     // X
00492                 USAGE(1), 0x31,                     // Y
00493                 LOGICAL_MINIMUM(1), 0x00,           // 0
00494                 LOGICAL_MAXIMUM(2), 0xff, 0x7f,     // 32767
00495                 REPORT_SIZE(1), 0x10,
00496                 REPORT_COUNT(1), 0x02,
00497                 INPUT(1), 0x02,                     // Data, Variable, Absolute
00498 
00499                 USAGE_PAGE(1), 0x01,                // Generic Desktop
00500                 USAGE(1), 0x38,                     // scroll
00501                 LOGICAL_MINIMUM(1), 0x81,           // -127
00502                 LOGICAL_MAXIMUM(1), 0x7f,           // 127
00503                 REPORT_SIZE(1), 0x08,
00504                 REPORT_COUNT(1), 0x01,
00505                 INPUT(1), 0x06,                     // Data, Variable, Relative
00506 
00507                 USAGE_PAGE(1), 0x09,                // Buttons
00508                 USAGE_MINIMUM(1), 0x01,
00509                 USAGE_MAXIMUM(1), 0x03,
00510                 LOGICAL_MINIMUM(1), 0x00,           // 0
00511                 LOGICAL_MAXIMUM(1), 0x01,           // 1
00512                 REPORT_COUNT(1), 0x03,
00513                 REPORT_SIZE(1), 0x01,
00514                 INPUT(1), 0x02,                     // Data, Variable, Absolute
00515                 REPORT_COUNT(1), 0x01,
00516                 REPORT_SIZE(1), 0x05,
00517                 INPUT(1), 0x01,                     // Constant
00518 
00519                 END_COLLECTION(0),
00520                 END_COLLECTION(0),
00521 
00522                 // Media Control
00523                 USAGE_PAGE(1), 0x0C,
00524                 USAGE(1), 0x01,
00525                 COLLECTION(1), 0x01,
00526                 REPORT_ID(1), REPORT_ID_VOLUME,
00527                 USAGE_PAGE(1), 0x0C,
00528                 LOGICAL_MINIMUM(1), 0x00,
00529                 LOGICAL_MAXIMUM(1), 0x01,
00530                 REPORT_SIZE(1), 0x01,
00531                 REPORT_COUNT(1), 0x07,
00532                 USAGE(1), 0xB5,             // Next Track
00533                 USAGE(1), 0xB6,             // Previous Track
00534                 USAGE(1), 0xB7,             // Stop
00535                 USAGE(1), 0xCD,             // Play / Pause
00536                 USAGE(1), 0xE2,             // Mute
00537                 USAGE(1), 0xE9,             // Volume Up
00538                 USAGE(1), 0xEA,             // Volume Down
00539                 INPUT(1), 0x02,             // Input (Data, Variable, Absolute)
00540                 REPORT_COUNT(1), 0x01,
00541                 INPUT(1), 0x01,
00542                 END_COLLECTION(0),
00543         };
00544         reportLength = sizeof(reportDescriptor);
00545         return reportDescriptor;
00546     }
00547 
00548     return NULL;
00549 }
00550 
00551 bool USBMouseKeyboard::EPINT_OUT_callback() {
00552     uint32_t bytesRead = 0;
00553     uint8_t led[65];
00554     USBDevice::readEP(EPINT_OUT, led, &bytesRead, MAX_HID_REPORT_SIZE);
00555 
00556     // we take led[1] because led[0] is the report ID
00557     lock_status = led[1] & 0x07;
00558 
00559     // We activate the endpoint to be able to recceive data
00560     if (!readStart(EPINT_OUT, MAX_HID_REPORT_SIZE))
00561         return false;
00562     return true;
00563 }
00564 
00565 uint8_t USBMouseKeyboard::lockStatus() {
00566     return lock_status;
00567 }
00568 
00569 bool USBMouseKeyboard::update(int16_t x, int16_t y, uint8_t button, int8_t z) {
00570     switch (mouse_type) {
00571     case REL_MOUSE:
00572         while (x > 127) {
00573             if (!mouseSend(127, 0, button, z)) return false;
00574             x = x - 127;
00575         }
00576         while (x < -128) {
00577             if (!mouseSend(-128, 0, button, z)) return false;
00578             x = x + 128;
00579         }
00580         while (y > 127) {
00581             if (!mouseSend(0, 127, button, z)) return false;
00582             y = y - 127;
00583         }
00584         while (y < -128) {
00585             if (!mouseSend(0, -128, button, z)) return false;
00586             y = y + 128;
00587         }
00588         return mouseSend(x, y, button, z);
00589     case ABS_MOUSE:
00590         HID_REPORT report;
00591 
00592         report.data[0] = REPORT_ID_MOUSE;
00593         report.data[1] = x & 0xff;
00594         report.data[2] = (x >> 8) & 0xff;
00595         report.data[3] = y & 0xff;
00596         report.data[4] = (y >> 8) & 0xff;
00597         report.data[5] = -z;
00598         report.data[6] = button & 0x07;
00599 
00600         report.length = 7;
00601 
00602         return send(&report);
00603     default:
00604         return false;
00605     }
00606 }
00607 
00608 bool USBMouseKeyboard::mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z) {
00609     HID_REPORT report;
00610     report.data[0] = REPORT_ID_MOUSE;
00611     report.data[1] = buttons & 0x07;
00612     report.data[2] = x;
00613     report.data[3] = y;
00614     report.data[4] = -z; // >0 to scroll down, <0 to scroll up
00615 
00616     report.length = 5;
00617 
00618     return send(&report);
00619 }
00620 
00621 bool USBMouseKeyboard::move(int16_t x, int16_t y) {
00622     return update(x, y, button, 0);
00623 }
00624 
00625 bool USBMouseKeyboard::scroll(int8_t z) {
00626     return update(0, 0, button, z);
00627 }
00628 
00629 bool USBMouseKeyboard::doubleClick() {
00630     if (!click(MOUSE_LEFT))
00631         return false;
00632     wait(0.1);
00633     return click(MOUSE_LEFT);
00634 }
00635 
00636 bool USBMouseKeyboard::click(uint8_t button) {
00637     if (!update(0, 0, button, 0))
00638         return false;
00639     wait(0.01);
00640     return update(0, 0, 0, 0);
00641 }
00642 
00643 bool USBMouseKeyboard::press(uint8_t button_) {
00644     button = button_ & 0x07;
00645     return update(0, 0, button, 0);
00646 }
00647 
00648 bool USBMouseKeyboard::release(uint8_t button_) {
00649     button = (button & (~button_)) & 0x07;
00650     return update(0, 0, button, 0);
00651 }
00652 
00653 int USBMouseKeyboard::_putc(int c) {
00654     return keyCode(c, keymap[c].modifier);
00655 }
00656 
00657 bool USBMouseKeyboard::keyCode(uint8_t key, uint8_t modifier) {
00658     // Send a simulated keyboard keypress. Returns true if successful.
00659 
00660     HID_REPORT report;
00661 
00662     report.data[0] = REPORT_ID_KEYBOARD;
00663     report.data[1] = modifier;
00664     report.data[2] = 0;
00665     report.data[3] = keymap[key].usage;
00666     report.data[4] = 0;
00667     report.data[5] = 0;
00668     report.data[6] = 0;
00669     report.data[7] = 0;
00670     report.data[8] = 0;
00671 
00672     report.length = 9;
00673 
00674     if (!send(&report)) {
00675         return false;
00676     }
00677 
00678     report.data[1] = 0;
00679     report.data[3] = 0;
00680 
00681     if (!send(&report)) {
00682         return false;
00683     }
00684 
00685     return true;
00686 
00687 }
00688 
00689 
00690 bool USBMouseKeyboard::mediaControl(MEDIA_KEY key) {
00691     HID_REPORT report;
00692 
00693     report.data[0] = REPORT_ID_VOLUME;
00694     report.data[1] = (1 << key) & 0x7f;
00695 
00696     report.length = 2;
00697 
00698     send(&report);
00699 
00700     report.data[0] = REPORT_ID_VOLUME;
00701     report.data[1] = 0;
00702 
00703     report.length = 2;
00704 
00705     return send(&report);
00706 }