USB Serial application

Fork of USBSerial_HelloWorld by Samuel Mokrani

Committer:
Zaitsev
Date:
Tue Jan 10 20:42:26 2017 +0000
Revision:
10:41552d038a69
USB Serial bi-directional bridge

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Zaitsev 10:41552d038a69 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
Zaitsev 10:41552d038a69 2 *
Zaitsev 10:41552d038a69 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Zaitsev 10:41552d038a69 4 * and associated documentation files (the "Software"), to deal in the Software without
Zaitsev 10:41552d038a69 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
Zaitsev 10:41552d038a69 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
Zaitsev 10:41552d038a69 7 * Software is furnished to do so, subject to the following conditions:
Zaitsev 10:41552d038a69 8 *
Zaitsev 10:41552d038a69 9 * The above copyright notice and this permission notice shall be included in all copies or
Zaitsev 10:41552d038a69 10 * substantial portions of the Software.
Zaitsev 10:41552d038a69 11 *
Zaitsev 10:41552d038a69 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
Zaitsev 10:41552d038a69 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Zaitsev 10:41552d038a69 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Zaitsev 10:41552d038a69 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Zaitsev 10:41552d038a69 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Zaitsev 10:41552d038a69 17 */
Zaitsev 10:41552d038a69 18
Zaitsev 10:41552d038a69 19 #include "stdint.h"
Zaitsev 10:41552d038a69 20
Zaitsev 10:41552d038a69 21 #include "USBKeyboard.h"
Zaitsev 10:41552d038a69 22
Zaitsev 10:41552d038a69 23 #define REPORT_ID_KEYBOARD 1
Zaitsev 10:41552d038a69 24 #define REPORT_ID_VOLUME 3
Zaitsev 10:41552d038a69 25
Zaitsev 10:41552d038a69 26
Zaitsev 10:41552d038a69 27 typedef struct {
Zaitsev 10:41552d038a69 28 unsigned char usage;
Zaitsev 10:41552d038a69 29 unsigned char modifier;
Zaitsev 10:41552d038a69 30 } KEYMAP;
Zaitsev 10:41552d038a69 31
Zaitsev 10:41552d038a69 32 #ifdef US_KEYBOARD
Zaitsev 10:41552d038a69 33 /* US keyboard (as HID standard) */
Zaitsev 10:41552d038a69 34 #define KEYMAP_SIZE (152)
Zaitsev 10:41552d038a69 35 const KEYMAP keymap[KEYMAP_SIZE] = {
Zaitsev 10:41552d038a69 36 {0, 0}, /* NUL */
Zaitsev 10:41552d038a69 37 {0, 0}, /* SOH */
Zaitsev 10:41552d038a69 38 {0, 0}, /* STX */
Zaitsev 10:41552d038a69 39 {0, 0}, /* ETX */
Zaitsev 10:41552d038a69 40 {0, 0}, /* EOT */
Zaitsev 10:41552d038a69 41 {0, 0}, /* ENQ */
Zaitsev 10:41552d038a69 42 {0, 0}, /* ACK */
Zaitsev 10:41552d038a69 43 {0, 0}, /* BEL */
Zaitsev 10:41552d038a69 44 {0x2a, 0}, /* BS */ /* Keyboard Delete (Backspace) */
Zaitsev 10:41552d038a69 45 {0x2b, 0}, /* TAB */ /* Keyboard Tab */
Zaitsev 10:41552d038a69 46 {0x28, 0}, /* LF */ /* Keyboard Return (Enter) */
Zaitsev 10:41552d038a69 47 {0, 0}, /* VT */
Zaitsev 10:41552d038a69 48 {0, 0}, /* FF */
Zaitsev 10:41552d038a69 49 {0, 0}, /* CR */
Zaitsev 10:41552d038a69 50 {0, 0}, /* SO */
Zaitsev 10:41552d038a69 51 {0, 0}, /* SI */
Zaitsev 10:41552d038a69 52 {0, 0}, /* DEL */
Zaitsev 10:41552d038a69 53 {0, 0}, /* DC1 */
Zaitsev 10:41552d038a69 54 {0, 0}, /* DC2 */
Zaitsev 10:41552d038a69 55 {0, 0}, /* DC3 */
Zaitsev 10:41552d038a69 56 {0, 0}, /* DC4 */
Zaitsev 10:41552d038a69 57 {0, 0}, /* NAK */
Zaitsev 10:41552d038a69 58 {0, 0}, /* SYN */
Zaitsev 10:41552d038a69 59 {0, 0}, /* ETB */
Zaitsev 10:41552d038a69 60 {0, 0}, /* CAN */
Zaitsev 10:41552d038a69 61 {0, 0}, /* EM */
Zaitsev 10:41552d038a69 62 {0, 0}, /* SUB */
Zaitsev 10:41552d038a69 63 {0, 0}, /* ESC */
Zaitsev 10:41552d038a69 64 {0, 0}, /* FS */
Zaitsev 10:41552d038a69 65 {0, 0}, /* GS */
Zaitsev 10:41552d038a69 66 {0, 0}, /* RS */
Zaitsev 10:41552d038a69 67 {0, 0}, /* US */
Zaitsev 10:41552d038a69 68 {0x2c, 0}, /* */
Zaitsev 10:41552d038a69 69 {0x1e, KEY_SHIFT}, /* ! */
Zaitsev 10:41552d038a69 70 {0x34, KEY_SHIFT}, /* " */
Zaitsev 10:41552d038a69 71 {0x20, KEY_SHIFT}, /* # */
Zaitsev 10:41552d038a69 72 {0x21, KEY_SHIFT}, /* $ */
Zaitsev 10:41552d038a69 73 {0x22, KEY_SHIFT}, /* % */
Zaitsev 10:41552d038a69 74 {0x24, KEY_SHIFT}, /* & */
Zaitsev 10:41552d038a69 75 {0x34, 0}, /* ' */
Zaitsev 10:41552d038a69 76 {0x26, KEY_SHIFT}, /* ( */
Zaitsev 10:41552d038a69 77 {0x27, KEY_SHIFT}, /* ) */
Zaitsev 10:41552d038a69 78 {0x25, KEY_SHIFT}, /* * */
Zaitsev 10:41552d038a69 79 {0x2e, KEY_SHIFT}, /* + */
Zaitsev 10:41552d038a69 80 {0x36, 0}, /* , */
Zaitsev 10:41552d038a69 81 {0x2d, 0}, /* - */
Zaitsev 10:41552d038a69 82 {0x37, 0}, /* . */
Zaitsev 10:41552d038a69 83 {0x38, 0}, /* / */
Zaitsev 10:41552d038a69 84 {0x27, 0}, /* 0 */
Zaitsev 10:41552d038a69 85 {0x1e, 0}, /* 1 */
Zaitsev 10:41552d038a69 86 {0x1f, 0}, /* 2 */
Zaitsev 10:41552d038a69 87 {0x20, 0}, /* 3 */
Zaitsev 10:41552d038a69 88 {0x21, 0}, /* 4 */
Zaitsev 10:41552d038a69 89 {0x22, 0}, /* 5 */
Zaitsev 10:41552d038a69 90 {0x23, 0}, /* 6 */
Zaitsev 10:41552d038a69 91 {0x24, 0}, /* 7 */
Zaitsev 10:41552d038a69 92 {0x25, 0}, /* 8 */
Zaitsev 10:41552d038a69 93 {0x26, 0}, /* 9 */
Zaitsev 10:41552d038a69 94 {0x33, KEY_SHIFT}, /* : */
Zaitsev 10:41552d038a69 95 {0x33, 0}, /* ; */
Zaitsev 10:41552d038a69 96 {0x36, KEY_SHIFT}, /* < */
Zaitsev 10:41552d038a69 97 {0x2e, 0}, /* = */
Zaitsev 10:41552d038a69 98 {0x37, KEY_SHIFT}, /* > */
Zaitsev 10:41552d038a69 99 {0x38, KEY_SHIFT}, /* ? */
Zaitsev 10:41552d038a69 100 {0x1f, KEY_SHIFT}, /* @ */
Zaitsev 10:41552d038a69 101 {0x04, KEY_SHIFT}, /* A */
Zaitsev 10:41552d038a69 102 {0x05, KEY_SHIFT}, /* B */
Zaitsev 10:41552d038a69 103 {0x06, KEY_SHIFT}, /* C */
Zaitsev 10:41552d038a69 104 {0x07, KEY_SHIFT}, /* D */
Zaitsev 10:41552d038a69 105 {0x08, KEY_SHIFT}, /* E */
Zaitsev 10:41552d038a69 106 {0x09, KEY_SHIFT}, /* F */
Zaitsev 10:41552d038a69 107 {0x0a, KEY_SHIFT}, /* G */
Zaitsev 10:41552d038a69 108 {0x0b, KEY_SHIFT}, /* H */
Zaitsev 10:41552d038a69 109 {0x0c, KEY_SHIFT}, /* I */
Zaitsev 10:41552d038a69 110 {0x0d, KEY_SHIFT}, /* J */
Zaitsev 10:41552d038a69 111 {0x0e, KEY_SHIFT}, /* K */
Zaitsev 10:41552d038a69 112 {0x0f, KEY_SHIFT}, /* L */
Zaitsev 10:41552d038a69 113 {0x10, KEY_SHIFT}, /* M */
Zaitsev 10:41552d038a69 114 {0x11, KEY_SHIFT}, /* N */
Zaitsev 10:41552d038a69 115 {0x12, KEY_SHIFT}, /* O */
Zaitsev 10:41552d038a69 116 {0x13, KEY_SHIFT}, /* P */
Zaitsev 10:41552d038a69 117 {0x14, KEY_SHIFT}, /* Q */
Zaitsev 10:41552d038a69 118 {0x15, KEY_SHIFT}, /* R */
Zaitsev 10:41552d038a69 119 {0x16, KEY_SHIFT}, /* S */
Zaitsev 10:41552d038a69 120 {0x17, KEY_SHIFT}, /* T */
Zaitsev 10:41552d038a69 121 {0x18, KEY_SHIFT}, /* U */
Zaitsev 10:41552d038a69 122 {0x19, KEY_SHIFT}, /* V */
Zaitsev 10:41552d038a69 123 {0x1a, KEY_SHIFT}, /* W */
Zaitsev 10:41552d038a69 124 {0x1b, KEY_SHIFT}, /* X */
Zaitsev 10:41552d038a69 125 {0x1c, KEY_SHIFT}, /* Y */
Zaitsev 10:41552d038a69 126 {0x1d, KEY_SHIFT}, /* Z */
Zaitsev 10:41552d038a69 127 {0x2f, 0}, /* [ */
Zaitsev 10:41552d038a69 128 {0x31, 0}, /* \ */
Zaitsev 10:41552d038a69 129 {0x30, 0}, /* ] */
Zaitsev 10:41552d038a69 130 {0x23, KEY_SHIFT}, /* ^ */
Zaitsev 10:41552d038a69 131 {0x2d, KEY_SHIFT}, /* _ */
Zaitsev 10:41552d038a69 132 {0x35, 0}, /* ` */
Zaitsev 10:41552d038a69 133 {0x04, 0}, /* a */
Zaitsev 10:41552d038a69 134 {0x05, 0}, /* b */
Zaitsev 10:41552d038a69 135 {0x06, 0}, /* c */
Zaitsev 10:41552d038a69 136 {0x07, 0}, /* d */
Zaitsev 10:41552d038a69 137 {0x08, 0}, /* e */
Zaitsev 10:41552d038a69 138 {0x09, 0}, /* f */
Zaitsev 10:41552d038a69 139 {0x0a, 0}, /* g */
Zaitsev 10:41552d038a69 140 {0x0b, 0}, /* h */
Zaitsev 10:41552d038a69 141 {0x0c, 0}, /* i */
Zaitsev 10:41552d038a69 142 {0x0d, 0}, /* j */
Zaitsev 10:41552d038a69 143 {0x0e, 0}, /* k */
Zaitsev 10:41552d038a69 144 {0x0f, 0}, /* l */
Zaitsev 10:41552d038a69 145 {0x10, 0}, /* m */
Zaitsev 10:41552d038a69 146 {0x11, 0}, /* n */
Zaitsev 10:41552d038a69 147 {0x12, 0}, /* o */
Zaitsev 10:41552d038a69 148 {0x13, 0}, /* p */
Zaitsev 10:41552d038a69 149 {0x14, 0}, /* q */
Zaitsev 10:41552d038a69 150 {0x15, 0}, /* r */
Zaitsev 10:41552d038a69 151 {0x16, 0}, /* s */
Zaitsev 10:41552d038a69 152 {0x17, 0}, /* t */
Zaitsev 10:41552d038a69 153 {0x18, 0}, /* u */
Zaitsev 10:41552d038a69 154 {0x19, 0}, /* v */
Zaitsev 10:41552d038a69 155 {0x1a, 0}, /* w */
Zaitsev 10:41552d038a69 156 {0x1b, 0}, /* x */
Zaitsev 10:41552d038a69 157 {0x1c, 0}, /* y */
Zaitsev 10:41552d038a69 158 {0x1d, 0}, /* z */
Zaitsev 10:41552d038a69 159 {0x2f, KEY_SHIFT}, /* { */
Zaitsev 10:41552d038a69 160 {0x31, KEY_SHIFT}, /* | */
Zaitsev 10:41552d038a69 161 {0x30, KEY_SHIFT}, /* } */
Zaitsev 10:41552d038a69 162 {0x35, KEY_SHIFT}, /* ~ */
Zaitsev 10:41552d038a69 163 {0,0}, /* DEL */
Zaitsev 10:41552d038a69 164
Zaitsev 10:41552d038a69 165 {0x3a, 0}, /* F1 */
Zaitsev 10:41552d038a69 166 {0x3b, 0}, /* F2 */
Zaitsev 10:41552d038a69 167 {0x3c, 0}, /* F3 */
Zaitsev 10:41552d038a69 168 {0x3d, 0}, /* F4 */
Zaitsev 10:41552d038a69 169 {0x3e, 0}, /* F5 */
Zaitsev 10:41552d038a69 170 {0x3f, 0}, /* F6 */
Zaitsev 10:41552d038a69 171 {0x40, 0}, /* F7 */
Zaitsev 10:41552d038a69 172 {0x41, 0}, /* F8 */
Zaitsev 10:41552d038a69 173 {0x42, 0}, /* F9 */
Zaitsev 10:41552d038a69 174 {0x43, 0}, /* F10 */
Zaitsev 10:41552d038a69 175 {0x44, 0}, /* F11 */
Zaitsev 10:41552d038a69 176 {0x45, 0}, /* F12 */
Zaitsev 10:41552d038a69 177
Zaitsev 10:41552d038a69 178 {0x46, 0}, /* PRINT_SCREEN */
Zaitsev 10:41552d038a69 179 {0x47, 0}, /* SCROLL_LOCK */
Zaitsev 10:41552d038a69 180 {0x39, 0}, /* CAPS_LOCK */
Zaitsev 10:41552d038a69 181 {0x53, 0}, /* NUM_LOCK */
Zaitsev 10:41552d038a69 182 {0x49, 0}, /* INSERT */
Zaitsev 10:41552d038a69 183 {0x4a, 0}, /* HOME */
Zaitsev 10:41552d038a69 184 {0x4b, 0}, /* PAGE_UP */
Zaitsev 10:41552d038a69 185 {0x4e, 0}, /* PAGE_DOWN */
Zaitsev 10:41552d038a69 186
Zaitsev 10:41552d038a69 187 {0x4f, 0}, /* RIGHT_ARROW */
Zaitsev 10:41552d038a69 188 {0x50, 0}, /* LEFT_ARROW */
Zaitsev 10:41552d038a69 189 {0x51, 0}, /* DOWN_ARROW */
Zaitsev 10:41552d038a69 190 {0x52, 0}, /* UP_ARROW */
Zaitsev 10:41552d038a69 191 };
Zaitsev 10:41552d038a69 192
Zaitsev 10:41552d038a69 193 #else
Zaitsev 10:41552d038a69 194 /* UK keyboard */
Zaitsev 10:41552d038a69 195 #define KEYMAP_SIZE (152)
Zaitsev 10:41552d038a69 196 const KEYMAP keymap[KEYMAP_SIZE] = {
Zaitsev 10:41552d038a69 197 {0, 0}, /* NUL */
Zaitsev 10:41552d038a69 198 {0, 0}, /* SOH */
Zaitsev 10:41552d038a69 199 {0, 0}, /* STX */
Zaitsev 10:41552d038a69 200 {0, 0}, /* ETX */
Zaitsev 10:41552d038a69 201 {0, 0}, /* EOT */
Zaitsev 10:41552d038a69 202 {0, 0}, /* ENQ */
Zaitsev 10:41552d038a69 203 {0, 0}, /* ACK */
Zaitsev 10:41552d038a69 204 {0, 0}, /* BEL */
Zaitsev 10:41552d038a69 205 {0x2a, 0}, /* BS */ /* Keyboard Delete (Backspace) */
Zaitsev 10:41552d038a69 206 {0x2b, 0}, /* TAB */ /* Keyboard Tab */
Zaitsev 10:41552d038a69 207 {0x28, 0}, /* LF */ /* Keyboard Return (Enter) */
Zaitsev 10:41552d038a69 208 {0, 0}, /* VT */
Zaitsev 10:41552d038a69 209 {0, 0}, /* FF */
Zaitsev 10:41552d038a69 210 {0, 0}, /* CR */
Zaitsev 10:41552d038a69 211 {0, 0}, /* SO */
Zaitsev 10:41552d038a69 212 {0, 0}, /* SI */
Zaitsev 10:41552d038a69 213 {0, 0}, /* DEL */
Zaitsev 10:41552d038a69 214 {0, 0}, /* DC1 */
Zaitsev 10:41552d038a69 215 {0, 0}, /* DC2 */
Zaitsev 10:41552d038a69 216 {0, 0}, /* DC3 */
Zaitsev 10:41552d038a69 217 {0, 0}, /* DC4 */
Zaitsev 10:41552d038a69 218 {0, 0}, /* NAK */
Zaitsev 10:41552d038a69 219 {0, 0}, /* SYN */
Zaitsev 10:41552d038a69 220 {0, 0}, /* ETB */
Zaitsev 10:41552d038a69 221 {0, 0}, /* CAN */
Zaitsev 10:41552d038a69 222 {0, 0}, /* EM */
Zaitsev 10:41552d038a69 223 {0, 0}, /* SUB */
Zaitsev 10:41552d038a69 224 {0, 0}, /* ESC */
Zaitsev 10:41552d038a69 225 {0, 0}, /* FS */
Zaitsev 10:41552d038a69 226 {0, 0}, /* GS */
Zaitsev 10:41552d038a69 227 {0, 0}, /* RS */
Zaitsev 10:41552d038a69 228 {0, 0}, /* US */
Zaitsev 10:41552d038a69 229 {0x2c, 0}, /* */
Zaitsev 10:41552d038a69 230 {0x1e, KEY_SHIFT}, /* ! */
Zaitsev 10:41552d038a69 231 {0x1f, KEY_SHIFT}, /* " */
Zaitsev 10:41552d038a69 232 {0x32, 0}, /* # */
Zaitsev 10:41552d038a69 233 {0x21, KEY_SHIFT}, /* $ */
Zaitsev 10:41552d038a69 234 {0x22, KEY_SHIFT}, /* % */
Zaitsev 10:41552d038a69 235 {0x24, KEY_SHIFT}, /* & */
Zaitsev 10:41552d038a69 236 {0x34, 0}, /* ' */
Zaitsev 10:41552d038a69 237 {0x26, KEY_SHIFT}, /* ( */
Zaitsev 10:41552d038a69 238 {0x27, KEY_SHIFT}, /* ) */
Zaitsev 10:41552d038a69 239 {0x25, KEY_SHIFT}, /* * */
Zaitsev 10:41552d038a69 240 {0x2e, KEY_SHIFT}, /* + */
Zaitsev 10:41552d038a69 241 {0x36, 0}, /* , */
Zaitsev 10:41552d038a69 242 {0x2d, 0}, /* - */
Zaitsev 10:41552d038a69 243 {0x37, 0}, /* . */
Zaitsev 10:41552d038a69 244 {0x38, 0}, /* / */
Zaitsev 10:41552d038a69 245 {0x27, 0}, /* 0 */
Zaitsev 10:41552d038a69 246 {0x1e, 0}, /* 1 */
Zaitsev 10:41552d038a69 247 {0x1f, 0}, /* 2 */
Zaitsev 10:41552d038a69 248 {0x20, 0}, /* 3 */
Zaitsev 10:41552d038a69 249 {0x21, 0}, /* 4 */
Zaitsev 10:41552d038a69 250 {0x22, 0}, /* 5 */
Zaitsev 10:41552d038a69 251 {0x23, 0}, /* 6 */
Zaitsev 10:41552d038a69 252 {0x24, 0}, /* 7 */
Zaitsev 10:41552d038a69 253 {0x25, 0}, /* 8 */
Zaitsev 10:41552d038a69 254 {0x26, 0}, /* 9 */
Zaitsev 10:41552d038a69 255 {0x33, KEY_SHIFT}, /* : */
Zaitsev 10:41552d038a69 256 {0x33, 0}, /* ; */
Zaitsev 10:41552d038a69 257 {0x36, KEY_SHIFT}, /* < */
Zaitsev 10:41552d038a69 258 {0x2e, 0}, /* = */
Zaitsev 10:41552d038a69 259 {0x37, KEY_SHIFT}, /* > */
Zaitsev 10:41552d038a69 260 {0x38, KEY_SHIFT}, /* ? */
Zaitsev 10:41552d038a69 261 {0x34, KEY_SHIFT}, /* @ */
Zaitsev 10:41552d038a69 262 {0x04, KEY_SHIFT}, /* A */
Zaitsev 10:41552d038a69 263 {0x05, KEY_SHIFT}, /* B */
Zaitsev 10:41552d038a69 264 {0x06, KEY_SHIFT}, /* C */
Zaitsev 10:41552d038a69 265 {0x07, KEY_SHIFT}, /* D */
Zaitsev 10:41552d038a69 266 {0x08, KEY_SHIFT}, /* E */
Zaitsev 10:41552d038a69 267 {0x09, KEY_SHIFT}, /* F */
Zaitsev 10:41552d038a69 268 {0x0a, KEY_SHIFT}, /* G */
Zaitsev 10:41552d038a69 269 {0x0b, KEY_SHIFT}, /* H */
Zaitsev 10:41552d038a69 270 {0x0c, KEY_SHIFT}, /* I */
Zaitsev 10:41552d038a69 271 {0x0d, KEY_SHIFT}, /* J */
Zaitsev 10:41552d038a69 272 {0x0e, KEY_SHIFT}, /* K */
Zaitsev 10:41552d038a69 273 {0x0f, KEY_SHIFT}, /* L */
Zaitsev 10:41552d038a69 274 {0x10, KEY_SHIFT}, /* M */
Zaitsev 10:41552d038a69 275 {0x11, KEY_SHIFT}, /* N */
Zaitsev 10:41552d038a69 276 {0x12, KEY_SHIFT}, /* O */
Zaitsev 10:41552d038a69 277 {0x13, KEY_SHIFT}, /* P */
Zaitsev 10:41552d038a69 278 {0x14, KEY_SHIFT}, /* Q */
Zaitsev 10:41552d038a69 279 {0x15, KEY_SHIFT}, /* R */
Zaitsev 10:41552d038a69 280 {0x16, KEY_SHIFT}, /* S */
Zaitsev 10:41552d038a69 281 {0x17, KEY_SHIFT}, /* T */
Zaitsev 10:41552d038a69 282 {0x18, KEY_SHIFT}, /* U */
Zaitsev 10:41552d038a69 283 {0x19, KEY_SHIFT}, /* V */
Zaitsev 10:41552d038a69 284 {0x1a, KEY_SHIFT}, /* W */
Zaitsev 10:41552d038a69 285 {0x1b, KEY_SHIFT}, /* X */
Zaitsev 10:41552d038a69 286 {0x1c, KEY_SHIFT}, /* Y */
Zaitsev 10:41552d038a69 287 {0x1d, KEY_SHIFT}, /* Z */
Zaitsev 10:41552d038a69 288 {0x2f, 0}, /* [ */
Zaitsev 10:41552d038a69 289 {0x64, 0}, /* \ */
Zaitsev 10:41552d038a69 290 {0x30, 0}, /* ] */
Zaitsev 10:41552d038a69 291 {0x23, KEY_SHIFT}, /* ^ */
Zaitsev 10:41552d038a69 292 {0x2d, KEY_SHIFT}, /* _ */
Zaitsev 10:41552d038a69 293 {0x35, 0}, /* ` */
Zaitsev 10:41552d038a69 294 {0x04, 0}, /* a */
Zaitsev 10:41552d038a69 295 {0x05, 0}, /* b */
Zaitsev 10:41552d038a69 296 {0x06, 0}, /* c */
Zaitsev 10:41552d038a69 297 {0x07, 0}, /* d */
Zaitsev 10:41552d038a69 298 {0x08, 0}, /* e */
Zaitsev 10:41552d038a69 299 {0x09, 0}, /* f */
Zaitsev 10:41552d038a69 300 {0x0a, 0}, /* g */
Zaitsev 10:41552d038a69 301 {0x0b, 0}, /* h */
Zaitsev 10:41552d038a69 302 {0x0c, 0}, /* i */
Zaitsev 10:41552d038a69 303 {0x0d, 0}, /* j */
Zaitsev 10:41552d038a69 304 {0x0e, 0}, /* k */
Zaitsev 10:41552d038a69 305 {0x0f, 0}, /* l */
Zaitsev 10:41552d038a69 306 {0x10, 0}, /* m */
Zaitsev 10:41552d038a69 307 {0x11, 0}, /* n */
Zaitsev 10:41552d038a69 308 {0x12, 0}, /* o */
Zaitsev 10:41552d038a69 309 {0x13, 0}, /* p */
Zaitsev 10:41552d038a69 310 {0x14, 0}, /* q */
Zaitsev 10:41552d038a69 311 {0x15, 0}, /* r */
Zaitsev 10:41552d038a69 312 {0x16, 0}, /* s */
Zaitsev 10:41552d038a69 313 {0x17, 0}, /* t */
Zaitsev 10:41552d038a69 314 {0x18, 0}, /* u */
Zaitsev 10:41552d038a69 315 {0x19, 0}, /* v */
Zaitsev 10:41552d038a69 316 {0x1a, 0}, /* w */
Zaitsev 10:41552d038a69 317 {0x1b, 0}, /* x */
Zaitsev 10:41552d038a69 318 {0x1c, 0}, /* y */
Zaitsev 10:41552d038a69 319 {0x1d, 0}, /* z */
Zaitsev 10:41552d038a69 320 {0x2f, KEY_SHIFT}, /* { */
Zaitsev 10:41552d038a69 321 {0x64, KEY_SHIFT}, /* | */
Zaitsev 10:41552d038a69 322 {0x30, KEY_SHIFT}, /* } */
Zaitsev 10:41552d038a69 323 {0x32, KEY_SHIFT}, /* ~ */
Zaitsev 10:41552d038a69 324 {0,0}, /* DEL */
Zaitsev 10:41552d038a69 325
Zaitsev 10:41552d038a69 326 {0x3a, 0}, /* F1 */
Zaitsev 10:41552d038a69 327 {0x3b, 0}, /* F2 */
Zaitsev 10:41552d038a69 328 {0x3c, 0}, /* F3 */
Zaitsev 10:41552d038a69 329 {0x3d, 0}, /* F4 */
Zaitsev 10:41552d038a69 330 {0x3e, 0}, /* F5 */
Zaitsev 10:41552d038a69 331 {0x3f, 0}, /* F6 */
Zaitsev 10:41552d038a69 332 {0x40, 0}, /* F7 */
Zaitsev 10:41552d038a69 333 {0x41, 0}, /* F8 */
Zaitsev 10:41552d038a69 334 {0x42, 0}, /* F9 */
Zaitsev 10:41552d038a69 335 {0x43, 0}, /* F10 */
Zaitsev 10:41552d038a69 336 {0x44, 0}, /* F11 */
Zaitsev 10:41552d038a69 337 {0x45, 0}, /* F12 */
Zaitsev 10:41552d038a69 338
Zaitsev 10:41552d038a69 339 {0x46, 0}, /* PRINT_SCREEN */
Zaitsev 10:41552d038a69 340 {0x47, 0}, /* SCROLL_LOCK */
Zaitsev 10:41552d038a69 341 {0x39, 0}, /* CAPS_LOCK */
Zaitsev 10:41552d038a69 342 {0x53, 0}, /* NUM_LOCK */
Zaitsev 10:41552d038a69 343 {0x49, 0}, /* INSERT */
Zaitsev 10:41552d038a69 344 {0x4a, 0}, /* HOME */
Zaitsev 10:41552d038a69 345 {0x4b, 0}, /* PAGE_UP */
Zaitsev 10:41552d038a69 346 {0x4e, 0}, /* PAGE_DOWN */
Zaitsev 10:41552d038a69 347
Zaitsev 10:41552d038a69 348 {0x4f, 0}, /* RIGHT_ARROW */
Zaitsev 10:41552d038a69 349 {0x50, 0}, /* LEFT_ARROW */
Zaitsev 10:41552d038a69 350 {0x51, 0}, /* DOWN_ARROW */
Zaitsev 10:41552d038a69 351 {0x52, 0}, /* UP_ARROW */
Zaitsev 10:41552d038a69 352 };
Zaitsev 10:41552d038a69 353 #endif
Zaitsev 10:41552d038a69 354
Zaitsev 10:41552d038a69 355 uint8_t * USBKeyboard::reportDesc() {
Zaitsev 10:41552d038a69 356 static uint8_t reportDescriptor[] = {
Zaitsev 10:41552d038a69 357 USAGE_PAGE(1), 0x01, // Generic Desktop
Zaitsev 10:41552d038a69 358 USAGE(1), 0x06, // Keyboard
Zaitsev 10:41552d038a69 359 COLLECTION(1), 0x01, // Application
Zaitsev 10:41552d038a69 360 REPORT_ID(1), REPORT_ID_KEYBOARD,
Zaitsev 10:41552d038a69 361
Zaitsev 10:41552d038a69 362 USAGE_PAGE(1), 0x07, // Key Codes
Zaitsev 10:41552d038a69 363 USAGE_MINIMUM(1), 0xE0,
Zaitsev 10:41552d038a69 364 USAGE_MAXIMUM(1), 0xE7,
Zaitsev 10:41552d038a69 365 LOGICAL_MINIMUM(1), 0x00,
Zaitsev 10:41552d038a69 366 LOGICAL_MAXIMUM(1), 0x01,
Zaitsev 10:41552d038a69 367 REPORT_SIZE(1), 0x01,
Zaitsev 10:41552d038a69 368 REPORT_COUNT(1), 0x08,
Zaitsev 10:41552d038a69 369 INPUT(1), 0x02, // Data, Variable, Absolute
Zaitsev 10:41552d038a69 370 REPORT_COUNT(1), 0x01,
Zaitsev 10:41552d038a69 371 REPORT_SIZE(1), 0x08,
Zaitsev 10:41552d038a69 372 INPUT(1), 0x01, // Constant
Zaitsev 10:41552d038a69 373
Zaitsev 10:41552d038a69 374
Zaitsev 10:41552d038a69 375 REPORT_COUNT(1), 0x05,
Zaitsev 10:41552d038a69 376 REPORT_SIZE(1), 0x01,
Zaitsev 10:41552d038a69 377 USAGE_PAGE(1), 0x08, // LEDs
Zaitsev 10:41552d038a69 378 USAGE_MINIMUM(1), 0x01,
Zaitsev 10:41552d038a69 379 USAGE_MAXIMUM(1), 0x05,
Zaitsev 10:41552d038a69 380 OUTPUT(1), 0x02, // Data, Variable, Absolute
Zaitsev 10:41552d038a69 381 REPORT_COUNT(1), 0x01,
Zaitsev 10:41552d038a69 382 REPORT_SIZE(1), 0x03,
Zaitsev 10:41552d038a69 383 OUTPUT(1), 0x01, // Constant
Zaitsev 10:41552d038a69 384
Zaitsev 10:41552d038a69 385
Zaitsev 10:41552d038a69 386 REPORT_COUNT(1), 0x06,
Zaitsev 10:41552d038a69 387 REPORT_SIZE(1), 0x08,
Zaitsev 10:41552d038a69 388 LOGICAL_MINIMUM(1), 0x00,
Zaitsev 10:41552d038a69 389 LOGICAL_MAXIMUM(1), 0x65,
Zaitsev 10:41552d038a69 390 USAGE_PAGE(1), 0x07, // Key Codes
Zaitsev 10:41552d038a69 391 USAGE_MINIMUM(1), 0x00,
Zaitsev 10:41552d038a69 392 USAGE_MAXIMUM(1), 0x65,
Zaitsev 10:41552d038a69 393 INPUT(1), 0x00, // Data, Array
Zaitsev 10:41552d038a69 394 END_COLLECTION(0),
Zaitsev 10:41552d038a69 395
Zaitsev 10:41552d038a69 396 // Media Control
Zaitsev 10:41552d038a69 397 USAGE_PAGE(1), 0x0C,
Zaitsev 10:41552d038a69 398 USAGE(1), 0x01,
Zaitsev 10:41552d038a69 399 COLLECTION(1), 0x01,
Zaitsev 10:41552d038a69 400 REPORT_ID(1), REPORT_ID_VOLUME,
Zaitsev 10:41552d038a69 401 USAGE_PAGE(1), 0x0C,
Zaitsev 10:41552d038a69 402 LOGICAL_MINIMUM(1), 0x00,
Zaitsev 10:41552d038a69 403 LOGICAL_MAXIMUM(1), 0x01,
Zaitsev 10:41552d038a69 404 REPORT_SIZE(1), 0x01,
Zaitsev 10:41552d038a69 405 REPORT_COUNT(1), 0x07,
Zaitsev 10:41552d038a69 406 USAGE(1), 0xB5, // Next Track
Zaitsev 10:41552d038a69 407 USAGE(1), 0xB6, // Previous Track
Zaitsev 10:41552d038a69 408 USAGE(1), 0xB7, // Stop
Zaitsev 10:41552d038a69 409 USAGE(1), 0xCD, // Play / Pause
Zaitsev 10:41552d038a69 410 USAGE(1), 0xE2, // Mute
Zaitsev 10:41552d038a69 411 USAGE(1), 0xE9, // Volume Up
Zaitsev 10:41552d038a69 412 USAGE(1), 0xEA, // Volume Down
Zaitsev 10:41552d038a69 413 INPUT(1), 0x02, // Input (Data, Variable, Absolute)
Zaitsev 10:41552d038a69 414 REPORT_COUNT(1), 0x01,
Zaitsev 10:41552d038a69 415 INPUT(1), 0x01,
Zaitsev 10:41552d038a69 416 END_COLLECTION(0),
Zaitsev 10:41552d038a69 417 };
Zaitsev 10:41552d038a69 418 reportLength = sizeof(reportDescriptor);
Zaitsev 10:41552d038a69 419 return reportDescriptor;
Zaitsev 10:41552d038a69 420 }
Zaitsev 10:41552d038a69 421
Zaitsev 10:41552d038a69 422
Zaitsev 10:41552d038a69 423 bool USBKeyboard::EPINT_OUT_callback() {
Zaitsev 10:41552d038a69 424 uint32_t bytesRead = 0;
Zaitsev 10:41552d038a69 425 uint8_t led[65];
Zaitsev 10:41552d038a69 426 USBDevice::readEP(EPINT_OUT, led, &bytesRead, MAX_HID_REPORT_SIZE);
Zaitsev 10:41552d038a69 427
Zaitsev 10:41552d038a69 428 // we take led[1] because led[0] is the report ID
Zaitsev 10:41552d038a69 429 lock_status = led[1] & 0x07;
Zaitsev 10:41552d038a69 430
Zaitsev 10:41552d038a69 431 // We activate the endpoint to be able to recceive data
Zaitsev 10:41552d038a69 432 if (!readStart(EPINT_OUT, MAX_HID_REPORT_SIZE))
Zaitsev 10:41552d038a69 433 return false;
Zaitsev 10:41552d038a69 434 return true;
Zaitsev 10:41552d038a69 435 }
Zaitsev 10:41552d038a69 436
Zaitsev 10:41552d038a69 437 uint8_t USBKeyboard::lockStatus() {
Zaitsev 10:41552d038a69 438 return lock_status;
Zaitsev 10:41552d038a69 439 }
Zaitsev 10:41552d038a69 440
Zaitsev 10:41552d038a69 441 int USBKeyboard::_putc(int c) {
Zaitsev 10:41552d038a69 442 return keyCode(c, keymap[c].modifier);
Zaitsev 10:41552d038a69 443 }
Zaitsev 10:41552d038a69 444
Zaitsev 10:41552d038a69 445 bool USBKeyboard::keyCode(uint8_t key, uint8_t modifier) {
Zaitsev 10:41552d038a69 446 // Send a simulated keyboard keypress. Returns true if successful.
Zaitsev 10:41552d038a69 447 HID_REPORT report;
Zaitsev 10:41552d038a69 448
Zaitsev 10:41552d038a69 449 report.data[0] = REPORT_ID_KEYBOARD;
Zaitsev 10:41552d038a69 450 report.data[1] = modifier;
Zaitsev 10:41552d038a69 451 report.data[2] = 0;
Zaitsev 10:41552d038a69 452 report.data[3] = keymap[key].usage;
Zaitsev 10:41552d038a69 453 report.data[4] = 0;
Zaitsev 10:41552d038a69 454 report.data[5] = 0;
Zaitsev 10:41552d038a69 455 report.data[6] = 0;
Zaitsev 10:41552d038a69 456 report.data[7] = 0;
Zaitsev 10:41552d038a69 457 report.data[8] = 0;
Zaitsev 10:41552d038a69 458
Zaitsev 10:41552d038a69 459 report.length = 9;
Zaitsev 10:41552d038a69 460
Zaitsev 10:41552d038a69 461 if (!send(&report)) {
Zaitsev 10:41552d038a69 462 return false;
Zaitsev 10:41552d038a69 463 }
Zaitsev 10:41552d038a69 464
Zaitsev 10:41552d038a69 465 report.data[1] = 0;
Zaitsev 10:41552d038a69 466 report.data[3] = 0;
Zaitsev 10:41552d038a69 467
Zaitsev 10:41552d038a69 468 if (!send(&report)) {
Zaitsev 10:41552d038a69 469 return false;
Zaitsev 10:41552d038a69 470 }
Zaitsev 10:41552d038a69 471
Zaitsev 10:41552d038a69 472 return true;
Zaitsev 10:41552d038a69 473
Zaitsev 10:41552d038a69 474 }
Zaitsev 10:41552d038a69 475
Zaitsev 10:41552d038a69 476
Zaitsev 10:41552d038a69 477 bool USBKeyboard::mediaControl(MEDIA_KEY key) {
Zaitsev 10:41552d038a69 478 HID_REPORT report;
Zaitsev 10:41552d038a69 479
Zaitsev 10:41552d038a69 480 report.data[0] = REPORT_ID_VOLUME;
Zaitsev 10:41552d038a69 481 report.data[1] = (1 << key) & 0x7f;
Zaitsev 10:41552d038a69 482
Zaitsev 10:41552d038a69 483 report.length = 2;
Zaitsev 10:41552d038a69 484
Zaitsev 10:41552d038a69 485 if (!send(&report)) {
Zaitsev 10:41552d038a69 486 return false;
Zaitsev 10:41552d038a69 487 }
Zaitsev 10:41552d038a69 488
Zaitsev 10:41552d038a69 489 report.data[0] = REPORT_ID_VOLUME;
Zaitsev 10:41552d038a69 490 report.data[1] = 0;
Zaitsev 10:41552d038a69 491
Zaitsev 10:41552d038a69 492 report.length = 2;
Zaitsev 10:41552d038a69 493
Zaitsev 10:41552d038a69 494 return send(&report);
Zaitsev 10:41552d038a69 495 }
Zaitsev 10:41552d038a69 496
Zaitsev 10:41552d038a69 497
Zaitsev 10:41552d038a69 498 #define DEFAULT_CONFIGURATION (1)
Zaitsev 10:41552d038a69 499 #define TOTAL_DESCRIPTOR_LENGTH ((1 * CONFIGURATION_DESCRIPTOR_LENGTH) \
Zaitsev 10:41552d038a69 500 + (1 * INTERFACE_DESCRIPTOR_LENGTH) \
Zaitsev 10:41552d038a69 501 + (1 * HID_DESCRIPTOR_LENGTH) \
Zaitsev 10:41552d038a69 502 + (2 * ENDPOINT_DESCRIPTOR_LENGTH))
Zaitsev 10:41552d038a69 503
Zaitsev 10:41552d038a69 504 uint8_t * USBKeyboard::configurationDesc() {
Zaitsev 10:41552d038a69 505 static uint8_t configurationDescriptor[] = {
Zaitsev 10:41552d038a69 506 CONFIGURATION_DESCRIPTOR_LENGTH,// bLength
Zaitsev 10:41552d038a69 507 CONFIGURATION_DESCRIPTOR, // bDescriptorType
Zaitsev 10:41552d038a69 508 LSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (LSB)
Zaitsev 10:41552d038a69 509 MSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (MSB)
Zaitsev 10:41552d038a69 510 0x01, // bNumInterfaces
Zaitsev 10:41552d038a69 511 DEFAULT_CONFIGURATION, // bConfigurationValue
Zaitsev 10:41552d038a69 512 0x00, // iConfiguration
Zaitsev 10:41552d038a69 513 C_RESERVED | C_SELF_POWERED, // bmAttributes
Zaitsev 10:41552d038a69 514 C_POWER(0), // bMaxPowerHello World from Mbed
Zaitsev 10:41552d038a69 515
Zaitsev 10:41552d038a69 516 INTERFACE_DESCRIPTOR_LENGTH, // bLength
Zaitsev 10:41552d038a69 517 INTERFACE_DESCRIPTOR, // bDescriptorType
Zaitsev 10:41552d038a69 518 0x00, // bInterfaceNumber
Zaitsev 10:41552d038a69 519 0x00, // bAlternateSetting
Zaitsev 10:41552d038a69 520 0x02, // bNumEndpoints
Zaitsev 10:41552d038a69 521 HID_CLASS, // bInterfaceClass
Zaitsev 10:41552d038a69 522 1, // bInterfaceSubClass
Zaitsev 10:41552d038a69 523 1, // bInterfaceProtocol (keyboard)
Zaitsev 10:41552d038a69 524 0x00, // iInterface
Zaitsev 10:41552d038a69 525
Zaitsev 10:41552d038a69 526 HID_DESCRIPTOR_LENGTH, // bLength
Zaitsev 10:41552d038a69 527 HID_DESCRIPTOR, // bDescriptorType
Zaitsev 10:41552d038a69 528 LSB(HID_VERSION_1_11), // bcdHID (LSB)
Zaitsev 10:41552d038a69 529 MSB(HID_VERSION_1_11), // bcdHID (MSB)
Zaitsev 10:41552d038a69 530 0x00, // bCountryCode
Zaitsev 10:41552d038a69 531 0x01, // bNumDescriptors
Zaitsev 10:41552d038a69 532 REPORT_DESCRIPTOR, // bDescriptorType
Zaitsev 10:41552d038a69 533 (uint8_t)(LSB(reportDescLength())), // wDescriptorLength (LSB)
Zaitsev 10:41552d038a69 534 (uint8_t)(MSB(reportDescLength())), // wDescriptorLength (MSB)
Zaitsev 10:41552d038a69 535
Zaitsev 10:41552d038a69 536 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
Zaitsev 10:41552d038a69 537 ENDPOINT_DESCRIPTOR, // bDescriptorType
Zaitsev 10:41552d038a69 538 PHY_TO_DESC(EPINT_IN), // bEndpointAddress
Zaitsev 10:41552d038a69 539 E_INTERRUPT, // bmAttributes
Zaitsev 10:41552d038a69 540 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)
Zaitsev 10:41552d038a69 541 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
Zaitsev 10:41552d038a69 542 1, // bInterval (milliseconds)
Zaitsev 10:41552d038a69 543
Zaitsev 10:41552d038a69 544 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
Zaitsev 10:41552d038a69 545 ENDPOINT_DESCRIPTOR, // bDescriptorType
Zaitsev 10:41552d038a69 546 PHY_TO_DESC(EPINT_OUT), // bEndpointAddress
Zaitsev 10:41552d038a69 547 E_INTERRUPT, // bmAttributes
Zaitsev 10:41552d038a69 548 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)
Zaitsev 10:41552d038a69 549 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
Zaitsev 10:41552d038a69 550 1, // bInterval (milliseconds)
Zaitsev 10:41552d038a69 551 };
Zaitsev 10:41552d038a69 552 return configurationDescriptor;
Zaitsev 10:41552d038a69 553 }