1 /* Copyright (c) 2010-2011 mbed.org, MIT License
3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
4 * and associated documentation files (the "Software"), to deal in the Software without
5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
7 * Software is furnished to do so, subject to the following conditions:
9 * The above copyright notice and this permission notice shall be included in all copies or
10 * substantial portions of the Software.
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 #include "USBMouseKeyboard.h"
24 unsigned char modifier;
28 /* US keyboard (as HID standard) */
29 #define KEYMAP_SIZE (152)
30 const KEYMAP keymap[KEYMAP_SIZE] = {
39 {0x2a, 0}, /* BS */ /* Keyboard Delete (Backspace) */
40 {0x2b, 0}, /* TAB */ /* Keyboard Tab */
41 {0x28, 0}, /* LF */ /* Keyboard Return (Enter) */
64 {0x1e, KEY_SHIFT}, /* ! */
65 {0x34, KEY_SHIFT}, /* " */
66 {0x20, KEY_SHIFT}, /* # */
67 {0x21, KEY_SHIFT}, /* $ */
68 {0x22, KEY_SHIFT}, /* % */
69 {0x24, KEY_SHIFT}, /* & */
71 {0x26, KEY_SHIFT}, /* ( */
72 {0x27, KEY_SHIFT}, /* ) */
73 {0x25, KEY_SHIFT}, /* * */
74 {0x2e, KEY_SHIFT}, /* + */
89 {0x33, KEY_SHIFT}, /* : */
91 {0x36, KEY_SHIFT}, /* < */
93 {0x37, KEY_SHIFT}, /* > */
94 {0x38, KEY_SHIFT}, /* ? */
95 {0x1f, KEY_SHIFT}, /* @ */
96 {0x04, KEY_SHIFT}, /* A */
97 {0x05, KEY_SHIFT}, /* B */
98 {0x06, KEY_SHIFT}, /* C */
99 {0x07, KEY_SHIFT}, /* D */
100 {0x08, KEY_SHIFT}, /* E */
101 {0x09, KEY_SHIFT}, /* F */
102 {0x0a, KEY_SHIFT}, /* G */
103 {0x0b, KEY_SHIFT}, /* H */
104 {0x0c, KEY_SHIFT}, /* I */
105 {0x0d, KEY_SHIFT}, /* J */
106 {0x0e, KEY_SHIFT}, /* K */
107 {0x0f, KEY_SHIFT}, /* L */
108 {0x10, KEY_SHIFT}, /* M */
109 {0x11, KEY_SHIFT}, /* N */
110 {0x12, KEY_SHIFT}, /* O */
111 {0x13, KEY_SHIFT}, /* P */
112 {0x14, KEY_SHIFT}, /* Q */
113 {0x15, KEY_SHIFT}, /* R */
114 {0x16, KEY_SHIFT}, /* S */
115 {0x17, KEY_SHIFT}, /* T */
116 {0x18, KEY_SHIFT}, /* U */
117 {0x19, KEY_SHIFT}, /* V */
118 {0x1a, KEY_SHIFT}, /* W */
119 {0x1b, KEY_SHIFT}, /* X */
120 {0x1c, KEY_SHIFT}, /* Y */
121 {0x1d, KEY_SHIFT}, /* Z */
125 {0x23, KEY_SHIFT}, /* ^ */
126 {0x2d, KEY_SHIFT}, /* _ */
154 {0x2f, KEY_SHIFT}, /* { */
155 {0x31, KEY_SHIFT}, /* | */
156 {0x30, KEY_SHIFT}, /* } */
157 {0x35, KEY_SHIFT}, /* ~ */
173 {0x46, 0}, /* PRINT_SCREEN */
174 {0x47, 0}, /* SCROLL_LOCK */
175 {0x39, 0}, /* CAPS_LOCK */
176 {0x53, 0}, /* NUM_LOCK */
177 {0x49, 0}, /* INSERT */
178 {0x4a, 0}, /* HOME */
179 {0x4b, 0}, /* PAGE_UP */
180 {0x4e, 0}, /* PAGE_DOWN */
182 {0x4f, 0}, /* RIGHT_ARROW */
183 {0x50, 0}, /* LEFT_ARROW */
184 {0x51, 0}, /* DOWN_ARROW */
185 {0x52, 0}, /* UP_ARROW */
190 #define KEYMAP_SIZE (152)
191 const KEYMAP keymap[KEYMAP_SIZE] = {
200 {0x2a, 0}, /* BS */ /* Keyboard Delete (Backspace) */
201 {0x2b, 0}, /* TAB */ /* Keyboard Tab */
202 {0x28, 0}, /* LF */ /* Keyboard Return (Enter) */
225 {0x1e, KEY_SHIFT}, /* ! */
226 {0x1f, KEY_SHIFT}, /* " */
228 {0x21, KEY_SHIFT}, /* $ */
229 {0x22, KEY_SHIFT}, /* % */
230 {0x24, KEY_SHIFT}, /* & */
232 {0x26, KEY_SHIFT}, /* ( */
233 {0x27, KEY_SHIFT}, /* ) */
234 {0x25, KEY_SHIFT}, /* * */
235 {0x2e, KEY_SHIFT}, /* + */
250 {0x33, KEY_SHIFT}, /* : */
252 {0x36, KEY_SHIFT}, /* < */
254 {0x37, KEY_SHIFT}, /* > */
255 {0x38, KEY_SHIFT}, /* ? */
256 {0x34, KEY_SHIFT}, /* @ */
257 {0x04, KEY_SHIFT}, /* A */
258 {0x05, KEY_SHIFT}, /* B */
259 {0x06, KEY_SHIFT}, /* C */
260 {0x07, KEY_SHIFT}, /* D */
261 {0x08, KEY_SHIFT}, /* E */
262 {0x09, KEY_SHIFT}, /* F */
263 {0x0a, KEY_SHIFT}, /* G */
264 {0x0b, KEY_SHIFT}, /* H */
265 {0x0c, KEY_SHIFT}, /* I */
266 {0x0d, KEY_SHIFT}, /* J */
267 {0x0e, KEY_SHIFT}, /* K */
268 {0x0f, KEY_SHIFT}, /* L */
269 {0x10, KEY_SHIFT}, /* M */
270 {0x11, KEY_SHIFT}, /* N */
271 {0x12, KEY_SHIFT}, /* O */
272 {0x13, KEY_SHIFT}, /* P */
273 {0x14, KEY_SHIFT}, /* Q */
274 {0x15, KEY_SHIFT}, /* R */
275 {0x16, KEY_SHIFT}, /* S */
276 {0x17, KEY_SHIFT}, /* T */
277 {0x18, KEY_SHIFT}, /* U */
278 {0x19, KEY_SHIFT}, /* V */
279 {0x1a, KEY_SHIFT}, /* W */
280 {0x1b, KEY_SHIFT}, /* X */
281 {0x1c, KEY_SHIFT}, /* Y */
282 {0x1d, KEY_SHIFT}, /* Z */
286 {0x23, KEY_SHIFT}, /* ^ */
287 {0x2d, KEY_SHIFT}, /* _ */
315 {0x2f, KEY_SHIFT}, /* { */
316 {0x64, KEY_SHIFT}, /* | */
317 {0x30, KEY_SHIFT}, /* } */
318 {0x32, KEY_SHIFT}, /* ~ */
334 {0x46, 0}, /* PRINT_SCREEN */
335 {0x47, 0}, /* SCROLL_LOCK */
336 {0x39, 0}, /* CAPS_LOCK */
337 {0x53, 0}, /* NUM_LOCK */
338 {0x49, 0}, /* INSERT */
339 {0x4a, 0}, /* HOME */
340 {0x4b, 0}, /* PAGE_UP */
341 {0x4e, 0}, /* PAGE_DOWN */
343 {0x4f, 0}, /* RIGHT_ARROW */
344 {0x50, 0}, /* LEFT_ARROW */
345 {0x51, 0}, /* DOWN_ARROW */
346 {0x52, 0}, /* UP_ARROW */
351 uint8_t * USBMouseKeyboard::reportDesc() {
352 if (mouse_type == REL_MOUSE) {
353 static uint8_t reportDescriptor[] = {
358 REPORT_ID(1), REPORT_ID_KEYBOARD,
360 USAGE_MINIMUM(1), 0xE0,
361 USAGE_MAXIMUM(1), 0xE7,
362 LOGICAL_MINIMUM(1), 0x00,
363 LOGICAL_MAXIMUM(1), 0x01,
364 REPORT_SIZE(1), 0x01,
365 REPORT_COUNT(1), 0x08,
367 REPORT_COUNT(1), 0x01,
368 REPORT_SIZE(1), 0x08,
370 REPORT_COUNT(1), 0x05,
371 REPORT_SIZE(1), 0x01,
373 USAGE_MINIMUM(1), 0x01,
374 USAGE_MAXIMUM(1), 0x05,
376 REPORT_COUNT(1), 0x01,
377 REPORT_SIZE(1), 0x03,
379 REPORT_COUNT(1), 0x06,
380 REPORT_SIZE(1), 0x08,
381 LOGICAL_MINIMUM(1), 0x00,
382 LOGICAL_MAXIMUM(2), 0xff, 0x00,
384 USAGE_MINIMUM(1), 0x00,
385 USAGE_MAXIMUM(2), 0xff, 0x00,
390 USAGE_PAGE(1), 0x01, // Generic Desktop
391 USAGE(1), 0x02, // Mouse
392 COLLECTION(1), 0x01, // Application
393 USAGE(1), 0x01, // Pointer
394 COLLECTION(1), 0x00, // Physical
395 REPORT_ID(1), REPORT_ID_MOUSE,
396 REPORT_COUNT(1), 0x03,
397 REPORT_SIZE(1), 0x01,
398 USAGE_PAGE(1), 0x09, // Buttons
399 USAGE_MINIMUM(1), 0x1,
400 USAGE_MAXIMUM(1), 0x3,
401 LOGICAL_MINIMUM(1), 0x00,
402 LOGICAL_MAXIMUM(1), 0x01,
404 REPORT_COUNT(1), 0x01,
405 REPORT_SIZE(1), 0x05,
407 REPORT_COUNT(1), 0x03,
408 REPORT_SIZE(1), 0x08,
412 USAGE(1), 0x38, // scroll
413 LOGICAL_MINIMUM(1), 0x81,
414 LOGICAL_MAXIMUM(1), 0x7f,
424 REPORT_ID(1), REPORT_ID_VOLUME,
426 LOGICAL_MINIMUM(1), 0x00,
427 LOGICAL_MAXIMUM(1), 0x01,
428 REPORT_SIZE(1), 0x01,
429 REPORT_COUNT(1), 0x07,
430 USAGE(1), 0xB5, // Next Track
431 USAGE(1), 0xB6, // Previous Track
432 USAGE(1), 0xB7, // Stop
433 USAGE(1), 0xCD, // Play / Pause
434 USAGE(1), 0xE2, // Mute
435 USAGE(1), 0xE9, // Volume Up
436 USAGE(1), 0xEA, // Volume Down
437 INPUT(1), 0x02, // Input (Data, Variable, Absolute)
438 REPORT_COUNT(1), 0x01,
442 reportLength = sizeof(reportDescriptor);
443 return reportDescriptor;
444 } else if (mouse_type == ABS_MOUSE) {
445 static uint8_t reportDescriptor[] = {
451 REPORT_ID(1), REPORT_ID_KEYBOARD,
453 USAGE_MINIMUM(1), 0xE0,
454 USAGE_MAXIMUM(1), 0xE7,
455 LOGICAL_MINIMUM(1), 0x00,
456 LOGICAL_MAXIMUM(1), 0x01,
457 REPORT_SIZE(1), 0x01,
458 REPORT_COUNT(1), 0x08,
460 REPORT_COUNT(1), 0x01,
461 REPORT_SIZE(1), 0x08,
463 REPORT_COUNT(1), 0x05,
464 REPORT_SIZE(1), 0x01,
466 USAGE_MINIMUM(1), 0x01,
467 USAGE_MAXIMUM(1), 0x05,
469 REPORT_COUNT(1), 0x01,
470 REPORT_SIZE(1), 0x03,
472 REPORT_COUNT(1), 0x06,
473 REPORT_SIZE(1), 0x08,
474 LOGICAL_MINIMUM(1), 0x00,
475 LOGICAL_MAXIMUM(2), 0xff, 0x00,
477 USAGE_MINIMUM(1), 0x00,
478 USAGE_MAXIMUM(2), 0xff, 0x00,
483 USAGE_PAGE(1), 0x01, // Generic Desktop
484 USAGE(1), 0x02, // Mouse
485 COLLECTION(1), 0x01, // Application
486 USAGE(1), 0x01, // Pointer
487 COLLECTION(1), 0x00, // Physical
488 REPORT_ID(1), REPORT_ID_MOUSE,
490 USAGE_PAGE(1), 0x01, // Generic Desktop
493 LOGICAL_MINIMUM(1), 0x00, // 0
494 LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767
495 REPORT_SIZE(1), 0x10,
496 REPORT_COUNT(1), 0x02,
497 INPUT(1), 0x02, // Data, Variable, Absolute
499 USAGE_PAGE(1), 0x01, // Generic Desktop
500 USAGE(1), 0x38, // scroll
501 LOGICAL_MINIMUM(1), 0x81, // -127
502 LOGICAL_MAXIMUM(1), 0x7f, // 127
503 REPORT_SIZE(1), 0x08,
504 REPORT_COUNT(1), 0x01,
505 INPUT(1), 0x06, // Data, Variable, Relative
507 USAGE_PAGE(1), 0x09, // Buttons
508 USAGE_MINIMUM(1), 0x01,
509 USAGE_MAXIMUM(1), 0x03,
510 LOGICAL_MINIMUM(1), 0x00, // 0
511 LOGICAL_MAXIMUM(1), 0x01, // 1
512 REPORT_COUNT(1), 0x03,
513 REPORT_SIZE(1), 0x01,
514 INPUT(1), 0x02, // Data, Variable, Absolute
515 REPORT_COUNT(1), 0x01,
516 REPORT_SIZE(1), 0x05,
517 INPUT(1), 0x01, // Constant
526 REPORT_ID(1), REPORT_ID_VOLUME,
528 LOGICAL_MINIMUM(1), 0x00,
529 LOGICAL_MAXIMUM(1), 0x01,
530 REPORT_SIZE(1), 0x01,
531 REPORT_COUNT(1), 0x07,
532 USAGE(1), 0xB5, // Next Track
533 USAGE(1), 0xB6, // Previous Track
534 USAGE(1), 0xB7, // Stop
535 USAGE(1), 0xCD, // Play / Pause
536 USAGE(1), 0xE2, // Mute
537 USAGE(1), 0xE9, // Volume Up
538 USAGE(1), 0xEA, // Volume Down
539 INPUT(1), 0x02, // Input (Data, Variable, Absolute)
540 REPORT_COUNT(1), 0x01,
544 reportLength = sizeof(reportDescriptor);
545 return reportDescriptor;
551 bool USBMouseKeyboard::EPINT_OUT_callback() {
552 uint32_t bytesRead = 0;
554 USBDevice::readEP(EPINT_OUT, led, &bytesRead, MAX_HID_REPORT_SIZE);
556 // we take led[1] because led[0] is the report ID
557 lock_status = led[1] & 0x07;
559 // We activate the endpoint to be able to recceive data
560 if (!readStart(EPINT_OUT, MAX_HID_REPORT_SIZE))
565 uint8_t USBMouseKeyboard::lockStatus() {
569 bool USBMouseKeyboard::update(int16_t x, int16_t y, uint8_t button, int8_t z) {
570 switch (mouse_type) {
573 if (!mouseSend(127, 0, button, z)) return false;
577 if (!mouseSend(-128, 0, button, z)) return false;
581 if (!mouseSend(0, 127, button, z)) return false;
585 if (!mouseSend(0, -128, button, z)) return false;
588 return mouseSend(x, y, button, z);
592 report.data[0] = REPORT_ID_MOUSE;
593 report.data[1] = x & 0xff;
594 report.data[2] = (x >> 8) & 0xff;
595 report.data[3] = y & 0xff;
596 report.data[4] = (y >> 8) & 0xff;
598 report.data[6] = button & 0x07;
602 return send(&report);
608 bool USBMouseKeyboard::mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z) {
610 report.data[0] = REPORT_ID_MOUSE;
611 report.data[1] = buttons & 0x07;
614 report.data[4] = -z; // >0 to scroll down, <0 to scroll up
618 return send(&report);
621 bool USBMouseKeyboard::move(int16_t x, int16_t y) {
622 return update(x, y, button, 0);
625 bool USBMouseKeyboard::scroll(int8_t z) {
626 return update(0, 0, button, z);
629 bool USBMouseKeyboard::doubleClick() {
630 if (!click(MOUSE_LEFT))
633 return click(MOUSE_LEFT);
636 bool USBMouseKeyboard::click(uint8_t button) {
637 if (!update(0, 0, button, 0))
640 return update(0, 0, 0, 0);
643 bool USBMouseKeyboard::press(uint8_t button_) {
644 button = button_ & 0x07;
645 return update(0, 0, button, 0);
648 bool USBMouseKeyboard::release(uint8_t button_) {
649 button = (button & (~button_)) & 0x07;
650 return update(0, 0, button, 0);
653 int USBMouseKeyboard::_putc(int c) {
654 return keyCode(c, keymap[c].modifier);
657 bool USBMouseKeyboard::keyCode(uint8_t key, uint8_t modifier) {
658 // Send a simulated keyboard keypress. Returns true if successful.
662 report.data[0] = REPORT_ID_KEYBOARD;
663 report.data[1] = modifier;
665 report.data[3] = keymap[key].usage;
674 if (!send(&report)) {
681 if (!send(&report)) {
690 bool USBMouseKeyboard::mediaControl(MEDIA_KEY key) {
693 report.data[0] = REPORT_ID_VOLUME;
694 report.data[1] = (1 << key) & 0x7f;
700 report.data[0] = REPORT_ID_VOLUME;
705 return send(&report);