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.
21 #include "USBKeyboard.h"
23 #define REPORT_ID_KEYBOARD 1
24 #define REPORT_ID_VOLUME 3
29 unsigned char modifier;
33 /* US keyboard (as HID standard) */
34 #define KEYMAP_SIZE (152)
35 const KEYMAP keymap[KEYMAP_SIZE] = {
44 {0x2a, 0}, /* BS */ /* Keyboard Delete (Backspace) */
45 {0x2b, 0}, /* TAB */ /* Keyboard Tab */
46 {0x28, 0}, /* LF */ /* Keyboard Return (Enter) */
69 {0x1e, KEY_SHIFT}, /* ! */
70 {0x34, KEY_SHIFT}, /* " */
71 {0x20, KEY_SHIFT}, /* # */
72 {0x21, KEY_SHIFT}, /* $ */
73 {0x22, KEY_SHIFT}, /* % */
74 {0x24, KEY_SHIFT}, /* & */
76 {0x26, KEY_SHIFT}, /* ( */
77 {0x27, KEY_SHIFT}, /* ) */
78 {0x25, KEY_SHIFT}, /* * */
79 {0x2e, KEY_SHIFT}, /* + */
94 {0x33, KEY_SHIFT}, /* : */
96 {0x36, KEY_SHIFT}, /* < */
98 {0x37, KEY_SHIFT}, /* > */
99 {0x38, KEY_SHIFT}, /* ? */
100 {0x1f, KEY_SHIFT}, /* @ */
101 {0x04, KEY_SHIFT}, /* A */
102 {0x05, KEY_SHIFT}, /* B */
103 {0x06, KEY_SHIFT}, /* C */
104 {0x07, KEY_SHIFT}, /* D */
105 {0x08, KEY_SHIFT}, /* E */
106 {0x09, KEY_SHIFT}, /* F */
107 {0x0a, KEY_SHIFT}, /* G */
108 {0x0b, KEY_SHIFT}, /* H */
109 {0x0c, KEY_SHIFT}, /* I */
110 {0x0d, KEY_SHIFT}, /* J */
111 {0x0e, KEY_SHIFT}, /* K */
112 {0x0f, KEY_SHIFT}, /* L */
113 {0x10, KEY_SHIFT}, /* M */
114 {0x11, KEY_SHIFT}, /* N */
115 {0x12, KEY_SHIFT}, /* O */
116 {0x13, KEY_SHIFT}, /* P */
117 {0x14, KEY_SHIFT}, /* Q */
118 {0x15, KEY_SHIFT}, /* R */
119 {0x16, KEY_SHIFT}, /* S */
120 {0x17, KEY_SHIFT}, /* T */
121 {0x18, KEY_SHIFT}, /* U */
122 {0x19, KEY_SHIFT}, /* V */
123 {0x1a, KEY_SHIFT}, /* W */
124 {0x1b, KEY_SHIFT}, /* X */
125 {0x1c, KEY_SHIFT}, /* Y */
126 {0x1d, KEY_SHIFT}, /* Z */
130 {0x23, KEY_SHIFT}, /* ^ */
131 {0x2d, KEY_SHIFT}, /* _ */
159 {0x2f, KEY_SHIFT}, /* { */
160 {0x31, KEY_SHIFT}, /* | */
161 {0x30, KEY_SHIFT}, /* } */
162 {0x35, KEY_SHIFT}, /* ~ */
178 {0x46, 0}, /* PRINT_SCREEN */
179 {0x47, 0}, /* SCROLL_LOCK */
180 {0x39, 0}, /* CAPS_LOCK */
181 {0x53, 0}, /* NUM_LOCK */
182 {0x49, 0}, /* INSERT */
183 {0x4a, 0}, /* HOME */
184 {0x4b, 0}, /* PAGE_UP */
185 {0x4e, 0}, /* PAGE_DOWN */
187 {0x4f, 0}, /* RIGHT_ARROW */
188 {0x50, 0}, /* LEFT_ARROW */
189 {0x51, 0}, /* DOWN_ARROW */
190 {0x52, 0}, /* UP_ARROW */
195 #define KEYMAP_SIZE (152)
196 const KEYMAP keymap[KEYMAP_SIZE] = {
205 {0x2a, 0}, /* BS */ /* Keyboard Delete (Backspace) */
206 {0x2b, 0}, /* TAB */ /* Keyboard Tab */
207 {0x28, 0}, /* LF */ /* Keyboard Return (Enter) */
230 {0x1e, KEY_SHIFT}, /* ! */
231 {0x1f, KEY_SHIFT}, /* " */
233 {0x21, KEY_SHIFT}, /* $ */
234 {0x22, KEY_SHIFT}, /* % */
235 {0x24, KEY_SHIFT}, /* & */
237 {0x26, KEY_SHIFT}, /* ( */
238 {0x27, KEY_SHIFT}, /* ) */
239 {0x25, KEY_SHIFT}, /* * */
240 {0x2e, KEY_SHIFT}, /* + */
255 {0x33, KEY_SHIFT}, /* : */
257 {0x36, KEY_SHIFT}, /* < */
259 {0x37, KEY_SHIFT}, /* > */
260 {0x38, KEY_SHIFT}, /* ? */
261 {0x34, KEY_SHIFT}, /* @ */
262 {0x04, KEY_SHIFT}, /* A */
263 {0x05, KEY_SHIFT}, /* B */
264 {0x06, KEY_SHIFT}, /* C */
265 {0x07, KEY_SHIFT}, /* D */
266 {0x08, KEY_SHIFT}, /* E */
267 {0x09, KEY_SHIFT}, /* F */
268 {0x0a, KEY_SHIFT}, /* G */
269 {0x0b, KEY_SHIFT}, /* H */
270 {0x0c, KEY_SHIFT}, /* I */
271 {0x0d, KEY_SHIFT}, /* J */
272 {0x0e, KEY_SHIFT}, /* K */
273 {0x0f, KEY_SHIFT}, /* L */
274 {0x10, KEY_SHIFT}, /* M */
275 {0x11, KEY_SHIFT}, /* N */
276 {0x12, KEY_SHIFT}, /* O */
277 {0x13, KEY_SHIFT}, /* P */
278 {0x14, KEY_SHIFT}, /* Q */
279 {0x15, KEY_SHIFT}, /* R */
280 {0x16, KEY_SHIFT}, /* S */
281 {0x17, KEY_SHIFT}, /* T */
282 {0x18, KEY_SHIFT}, /* U */
283 {0x19, KEY_SHIFT}, /* V */
284 {0x1a, KEY_SHIFT}, /* W */
285 {0x1b, KEY_SHIFT}, /* X */
286 {0x1c, KEY_SHIFT}, /* Y */
287 {0x1d, KEY_SHIFT}, /* Z */
291 {0x23, KEY_SHIFT}, /* ^ */
292 {0x2d, KEY_SHIFT}, /* _ */
320 {0x2f, KEY_SHIFT}, /* { */
321 {0x64, KEY_SHIFT}, /* | */
322 {0x30, KEY_SHIFT}, /* } */
323 {0x32, KEY_SHIFT}, /* ~ */
339 {0x46, 0}, /* PRINT_SCREEN */
340 {0x47, 0}, /* SCROLL_LOCK */
341 {0x39, 0}, /* CAPS_LOCK */
342 {0x53, 0}, /* NUM_LOCK */
343 {0x49, 0}, /* INSERT */
344 {0x4a, 0}, /* HOME */
345 {0x4b, 0}, /* PAGE_UP */
346 {0x4e, 0}, /* PAGE_DOWN */
348 {0x4f, 0}, /* RIGHT_ARROW */
349 {0x50, 0}, /* LEFT_ARROW */
350 {0x51, 0}, /* DOWN_ARROW */
351 {0x52, 0}, /* UP_ARROW */
355 uint8_t * USBKeyboard::reportDesc() {
356 static uint8_t reportDescriptor[] = {
357 USAGE_PAGE(1), 0x01, // Generic Desktop
358 USAGE(1), 0x06, // Keyboard
359 COLLECTION(1), 0x01, // Application
360 REPORT_ID(1), REPORT_ID_KEYBOARD,
362 USAGE_PAGE(1), 0x07, // Key Codes
363 USAGE_MINIMUM(1), 0xE0,
364 USAGE_MAXIMUM(1), 0xE7,
365 LOGICAL_MINIMUM(1), 0x00,
366 LOGICAL_MAXIMUM(1), 0x01,
367 REPORT_SIZE(1), 0x01,
368 REPORT_COUNT(1), 0x08,
369 INPUT(1), 0x02, // Data, Variable, Absolute
370 REPORT_COUNT(1), 0x01,
371 REPORT_SIZE(1), 0x08,
372 INPUT(1), 0x01, // Constant
375 REPORT_COUNT(1), 0x05,
376 REPORT_SIZE(1), 0x01,
377 USAGE_PAGE(1), 0x08, // LEDs
378 USAGE_MINIMUM(1), 0x01,
379 USAGE_MAXIMUM(1), 0x05,
380 OUTPUT(1), 0x02, // Data, Variable, Absolute
381 REPORT_COUNT(1), 0x01,
382 REPORT_SIZE(1), 0x03,
383 OUTPUT(1), 0x01, // Constant
386 REPORT_COUNT(1), 0x06,
387 REPORT_SIZE(1), 0x08,
388 LOGICAL_MINIMUM(1), 0x00,
389 LOGICAL_MAXIMUM(1), 0x65,
390 USAGE_PAGE(1), 0x07, // Key Codes
391 USAGE_MINIMUM(1), 0x00,
392 USAGE_MAXIMUM(1), 0x65,
393 INPUT(1), 0x00, // Data, Array
400 REPORT_ID(1), REPORT_ID_VOLUME,
402 LOGICAL_MINIMUM(1), 0x00,
403 LOGICAL_MAXIMUM(1), 0x01,
404 REPORT_SIZE(1), 0x01,
405 REPORT_COUNT(1), 0x07,
406 USAGE(1), 0xB5, // Next Track
407 USAGE(1), 0xB6, // Previous Track
408 USAGE(1), 0xB7, // Stop
409 USAGE(1), 0xCD, // Play / Pause
410 USAGE(1), 0xE2, // Mute
411 USAGE(1), 0xE9, // Volume Up
412 USAGE(1), 0xEA, // Volume Down
413 INPUT(1), 0x02, // Input (Data, Variable, Absolute)
414 REPORT_COUNT(1), 0x01,
418 reportLength = sizeof(reportDescriptor);
419 return reportDescriptor;
423 bool USBKeyboard::EPINT_OUT_callback() {
424 uint32_t bytesRead = 0;
426 USBDevice::readEP(EPINT_OUT, led, &bytesRead, MAX_HID_REPORT_SIZE);
428 // we take led[1] because led[0] is the report ID
429 lock_status = led[1] & 0x07;
431 // We activate the endpoint to be able to recceive data
432 if (!readStart(EPINT_OUT, MAX_HID_REPORT_SIZE))
437 uint8_t USBKeyboard::lockStatus() {
441 int USBKeyboard::_putc(int c) {
442 return keyCode(c, keymap[c].modifier);
445 bool USBKeyboard::keyCode(uint8_t key, uint8_t modifier) {
446 // Send a simulated keyboard keypress. Returns true if successful.
449 report.data[0] = REPORT_ID_KEYBOARD;
450 report.data[1] = modifier;
452 report.data[3] = keymap[key].usage;
461 if (!send(&report)) {
468 if (!send(&report)) {
477 bool USBKeyboard::mediaControl(MEDIA_KEY key) {
480 report.data[0] = REPORT_ID_VOLUME;
481 report.data[1] = (1 << key) & 0x7f;
485 if (!send(&report)) {
489 report.data[0] = REPORT_ID_VOLUME;
494 return send(&report);
498 #define DEFAULT_CONFIGURATION (1)
499 #define TOTAL_DESCRIPTOR_LENGTH ((1 * CONFIGURATION_DESCRIPTOR_LENGTH) \
500 + (1 * INTERFACE_DESCRIPTOR_LENGTH) \
501 + (1 * HID_DESCRIPTOR_LENGTH) \
502 + (2 * ENDPOINT_DESCRIPTOR_LENGTH))
504 uint8_t * USBKeyboard::configurationDesc() {
505 static uint8_t configurationDescriptor[] = {
506 CONFIGURATION_DESCRIPTOR_LENGTH,// bLength
507 CONFIGURATION_DESCRIPTOR, // bDescriptorType
508 LSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (LSB)
509 MSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (MSB)
510 0x01, // bNumInterfaces
511 DEFAULT_CONFIGURATION, // bConfigurationValue
512 0x00, // iConfiguration
513 C_RESERVED | C_SELF_POWERED, // bmAttributes
514 C_POWER(0), // bMaxPowerHello World from Mbed
516 INTERFACE_DESCRIPTOR_LENGTH, // bLength
517 INTERFACE_DESCRIPTOR, // bDescriptorType
518 0x00, // bInterfaceNumber
519 0x00, // bAlternateSetting
520 0x02, // bNumEndpoints
521 HID_CLASS, // bInterfaceClass
522 1, // bInterfaceSubClass
523 1, // bInterfaceProtocol (keyboard)
526 HID_DESCRIPTOR_LENGTH, // bLength
527 HID_DESCRIPTOR, // bDescriptorType
528 LSB(HID_VERSION_1_11), // bcdHID (LSB)
529 MSB(HID_VERSION_1_11), // bcdHID (MSB)
530 0x00, // bCountryCode
531 0x01, // bNumDescriptors
532 REPORT_DESCRIPTOR, // bDescriptorType
533 (uint8_t)(LSB(reportDescLength())), // wDescriptorLength (LSB)
534 (uint8_t)(MSB(reportDescLength())), // wDescriptorLength (MSB)
536 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
537 ENDPOINT_DESCRIPTOR, // bDescriptorType
538 PHY_TO_DESC(EPINT_IN), // bEndpointAddress
539 E_INTERRUPT, // bmAttributes
540 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)
541 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
542 1, // bInterval (milliseconds)
544 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
545 ENDPOINT_DESCRIPTOR, // bDescriptorType
546 PHY_TO_DESC(EPINT_OUT), // bEndpointAddress
547 E_INTERRUPT, // bmAttributes
548 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)
549 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
550 1, // bInterval (milliseconds)
552 return configurationDescriptor;