-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUsbKeyboard.h
650 lines (582 loc) · 15.7 KB
/
UsbKeyboard.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
/**
* Based on Obdev's AVRUSB code and under the same license.
*
* usage:
* void setup() {
* TIMSK &= !(1 << TOIE0); // Disable timer0 as it can interfere with USB timing
* UsbKeyboard::init();
*
*/
#ifndef __UsbKeyboard_h__
#define __UsbKeyboard_h__
//#define _OSCCAL_ //Enable it id you want to use internal oscillator TODO buggy
/*
*
* Please Set USB_CFG_HAVE_MEASURE_FRAME_LENGTH to 1 in usbconfig.h
*
* #define USB_CFG_HAVE_MEASURE_FRAME_LENGTH 1
*
*/
#include <string.h>
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/eeprom.h> //Eeprom read
#include <avr/interrupt.h> // for sei()
#include <util/delay.h> // for _delay_ms()
#include <avr/pgmspace.h> // required by usbdrv.h
#ifdef _OSCCAL_
#include <calibrate.h>
#endif
#ifdef __cplusplus
extern "C"
{
#endif
#include "utility/usbdrv.h"
#ifdef __cplusplus
}
#endif
/* We use a simplifed keyboard report descriptor which does not support the
* boot protocol. We don't allow setting status LEDs and but we do allow
* simultaneous key presses.
* The report descriptor has been created with usb.org's "HID Descriptor Tool"
* which can be downloaded from http://www.usb.org/developers/hidpage/.
* Redundant entries (such as LOGICAL_MINIMUM and USAGE_PAGE) have been omitted
* for the second INPUT item.
* You need to modify USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH in usbconfig.h if you changed it.
*
* //http://www.microchip.com/forums/FindPost/878780
*/
#define KEYCODE_LENGTH 0x01
#define REPORT_ID_KEYBOARD 0x01
#define REPORT_ID_CONSUMER 0x2
PROGMEM const uchar usbHidReportDescriptor[USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH] = // USB report descriptor
{
//LENGTH=40
0x05, 0x01, //USAGE_PAGE (Generic Desktop)
0x09, 0x06, // USAGE (Keyboard)
0xa1, 0x01, //COLLECTION (Application)
0x85, REPORT_ID_KEYBOARD, // Report ID=1
//Modifiers
0x05, 0x07, // USAGE_PAGE (Keyboard)
0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x75, 0x01, // REPORT_SIZE (1)
0x95, 0x08, // REPORT_COUNT (8)
0x81, 0x02, // INPUT (Data,Var,Abs)
//Key byte
0x75, 0x08, // REPORT_SIZE(8)
0x95, KEYCODE_LENGTH, // REPORT_COUNT(KEYCODE_LENGTH) //TODO If we are using array we will set it to the length of that array 6
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x26, 0x86, 0x00, // LOGICAL_MAXIMUM (134)
0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
0x81, 0x00, // INPUT (Data,Ary,Abs)
0xc0, //END_COLLECTION
/* consumer */
0x05, 0x0c, // USAGE_PAGE (Consumer Devices)
0x09, 0x01, // USAGE (Consumer Control)
0xa1, 0x01, // COLLECTION (Application)
0x85, REPORT_ID_CONSUMER, // REPORT_ID (2)
0x15, 0x01, // LOGICAL_MINIMUM (0x1)
0x26, 0x9c, 0x02, // LOGICAL_MAXIMUM (0x29c)
0x19, 0x01, // USAGE_MINIMUM (0x1)
0x2a, 0x9c, 0x02, // USAGE_MAXIMUM (0x29c)
0x75, 0x10, // REPORT_SIZE (16)
0x95, 0x01, // REPORT_COUNT (1)
0x81, 0x00, // INPUT (Data,Array,Abs)
0xc0 // END_COLLECTION
};
/* for data
PROGMEM const char usbHidReportDescriptor[22] = { // USB report descriptor
0x06, 0x00, 0xff, // USAGE_PAGE (Generic Desktop)
0x09, 0x01, // USAGE (Vendor Usage 1)
0xa1, 0x01, // COLLECTION (Application)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x26, 0xff, 0x00, // LOGICAL_MAXIMUM (255)
0x75, 0x08, // REPORT_SIZE (8)
0x95, 0x01, // REPORT_COUNT (1)
0x09, 0x00, // USAGE (Undefined)
0xb2, 0x02, 0x01, // FEATURE (Data,Var,Abs,Buf)
0xc0 // END_COLLECTION
};
*/
/*
* Keyboard usage values, see usb.org's HID-usage-tables document, chapter
* or http://www.freebsddiary.org/APC/usb_hid_usages.php
*/
#define KEY_CTRL 0x01
#define KEY_SHIFT 0x02
#define KEY_ALT 0x04
#define KEY_GUI 0x08
#define KEY_LEFT_CTRL 0x01
#define KEY_LEFT_SHIFT 0x02
#define KEY_LEFT_ALT 0x04
#define KEY_LEFT_GUI 0x08
#define KEY_RIGHT_CTRL 0x10
#define KEY_RIGHT_SHIFT 0x20
#define KEY_RIGHT_ALT 0x40
#define KEY_RIGHT_GUI 0x80
#define KEY_A 4
#define KEY_B 5
#define KEY_C 6
#define KEY_D 7
#define KEY_E 8
#define KEY_F 9
#define KEY_G 10
#define KEY_H 11
#define KEY_I 12
#define KEY_J 13
#define KEY_K 14
#define KEY_L 15
#define KEY_M 16
#define KEY_N 17
#define KEY_O 18
#define KEY_P 19
#define KEY_Q 20
#define KEY_R 21
#define KEY_S 22
#define KEY_T 23
#define KEY_U 24
#define KEY_V 25
#define KEY_W 26
#define KEY_X 27
#define KEY_Y 28
#define KEY_Z 29
#define KEY_1 30
#define KEY_2 31
#define KEY_3 32
#define KEY_4 33
#define KEY_5 34
#define KEY_6 35
#define KEY_7 36
#define KEY_8 37
#define KEY_9 38
#define KEY_0 39
#define KEY_ENTER 40
#define KEY_ESC 41
#define KEY_BACKSPACE 42
#define KEY_TAB 43
#define KEY_SPACE 44
#define KEY_CAPS_LOCK 57
#define KEY_MINUS 45
#define KEY_EQUAL 46
#define KEY_LEFT_BRACE 47
#define KEY_RIGHT_BRACE 48
#define KEY_BACKSLASH 49
#define KEY_NUMBER 50
#define KEY_SEMICOLON 51
#define KEY_QUOTE 52
#define KEY_TILDE 53
#define KEY_COMMA 54
#define KEY_PERIOD 55
#define KEY_SLASH 56
#define KEY_F1 58
#define KEY_F2 59
#define KEY_F3 60
#define KEY_F4 61
#define KEY_F5 62
#define KEY_F6 63
#define KEY_F7 64
#define KEY_F8 65
#define KEY_F9 66
#define KEY_F10 67
#define KEY_F11 68
#define KEY_F12 69
#define KEY_PRINTSCREEN 70
#define KEY_SCROLL_LOCK 71
#define KEY_PAUSE 72
#define KEY_INSERT 73
#define KEY_HOME 74
#define KEY_PAGE_UP 75
#define KEY_DELETE 76
#define KEY_END 77
#define KEY_PAGE_DOWN 78
#define KEY_RIGHT 79
#define KEY_LEFT 80
#define KEY_DOWN 81
#define KEY_UP 82
#define KEY_NUM_LOCK 83
#define KEY_PAD_SLASH 84
#define KEY_PAD_ASTERIX 85
#define KEY_PAD_MINUS 86
#define KEY_PAD_PLUS 87
#define KEY_PAD_ENTER 88
#define KEY_PAD_1 89
#define KEY_PAD_2 90
#define KEY_PAD_3 91
#define KEY_PAD_4 92
#define KEY_PAD_5 93
#define KEY_PAD_6 94
#define KEY_PAD_7 95
#define KEY_PAD_8 96
#define KEY_PAD_9 97
#define KEY_PAD_0 98
#define KEY_PAD_PERIOD 99
#define KEY_KEYBOARD_NON_US_BACKSLASH 100
#define KEY_APPLICATION 101
#define KEY_POWER 102
#define KEY_EQUAL_ 103 //TODO Fix name
#define KEY_F13 104
#define KEY_F14 105
#define KEY_F15 106
#define KEY_F16 107
#define KEY_F17 108
#define KEY_F18 109
#define KEY_F19 110
#define KEY_F20 111
#define KEY_F21 112
#define KEY_F22 113
#define KEY_F23 114
#define KEY_F24 115
#define KEY_EXECUTE 116
#define KEY_HELP 117
#define KEY_MENU 118
#define KEY_SELECT 119
#define KEY_STOP 120
#define KEY_AGAIN 121
#define KEY_UNDO 122
#define KEY_CUT 123
#define KEY_COPY 124
#define KEY_PASTE 125
#define KEY_FIND 126
#define KEY_MUTE 127
#define KEY_VOLUME_UP 128
#define KEY_VOLUME_DOWN 129
#define KEY_LOCKING_CAPS LOCK 130
#define KEY_LOCKING_NUM LOCK 131
#define KEY_LOCKING_SCROLL_LOCK 132
#define KEY_COMMA_SIGN 133 //TODO Fix Name
#define KEY_EQUAL_SIGN 134
/*
* USB_Hut1_12v2 Page 75 chapter 15
*/
#define CONSUMER_CHANNEL_INCREMENT 0x9C
#define CONSUMER_CHANNEL_DECREMENT 0x9D
#define CONSUMER_PLAY 0xB0
#define CONSUMER_PAUSE 0xB1
#define CONSUMER_STOP 0xB1
#define CONSUMER_PLAY_PAUSE 0xE2
#define CONSUMER_MUTE 0xE2
#define CONSUMER_VOLUME_INCREMENT 0xE9
#define CONSUMER_VOLUME_DECREMENT 0xEA
#define KEY_ARROW_LEFT KEY_LEFT //deprecated
//ported from https://github.com/aduitsis/ardumultimedia/blob/master/HID.cpp
#define SHIFT 0x80
/*
* ref: http://www.avrfreaks.net/forum/tut-c-gcc-and-progmem-attribute?name=PNphpBB2&file=viewtopic&t=38003
*/
PROGMEM static const uint8_t asciimap[128] =
{
0x00, // NUL
0x00, // SOH
0x00, // STX
0x00, // ETX
0x00, // EOT
0x00, // ENQ
0x00, // ACK
0x00, // BEL
KEY_BACKSPACE, // BS Backspace
KEY_TAB, // TAB Tab
KEY_ENTER, // LF Enter
0x00, // VT
0x00, // FF
0x00, // CR
0x00, // SO
0x00, // SI
0x00, // DEL
0x00, // DC1
0x00, // DC2
0x00, // DC3
0x00, // DC4
0x00, // NAK
0x00, // SYN
0x00, // ETB
0x00, // CAN
0x00, // EM
0x00, // SUB
0x00, // ESC
0x00, // FS
0x00, // GS
0x00, // RS
0x00, // US
0x2c, // ' '
0x1e|SHIFT, // !
0x34|SHIFT, // "
0x20|SHIFT, // #
0x21|SHIFT, // $
0x22|SHIFT, // %
0x24|SHIFT, // &
0x34, // '
0x26|SHIFT, // (
0x27|SHIFT, // )
0x25|SHIFT, // *
0x2e|SHIFT, // +
0x36, // ,
0x2d, // -
0x37, // .
0x38, // /
0x27, // 0
0x1e, // 1
0x1f, // 2
0x20, // 3
0x21, // 4
0x22, // 5
0x23, // 6
0x24, // 7
0x25, // 8
0x26, // 9
0x33|SHIFT, // :
0x33, // ;
0x36|SHIFT, // <
0x2e, // =
0x37|SHIFT, // >
0x38|SHIFT, // ?
0x1f|SHIFT, // @
KEY_A|SHIFT, // A
KEY_B|SHIFT, // B
KEY_C|SHIFT, // C
KEY_D|SHIFT, // D
KEY_E|SHIFT, // E
KEY_F|SHIFT, // F
KEY_G|SHIFT, // G
KEY_H|SHIFT, // H
KEY_I|SHIFT, // I
KEY_J|SHIFT, // J
KEY_K|SHIFT, // K
KEY_L|SHIFT, // L
KEY_M|SHIFT, // M
KEY_N|SHIFT, // N
KEY_O|SHIFT, // O
KEY_P|SHIFT, // P
KEY_Q|SHIFT, // Q
KEY_R|SHIFT, // R
KEY_S|SHIFT, // S
KEY_T|SHIFT, // T
KEY_U|SHIFT, // U
KEY_V|SHIFT, // V
KEY_W|SHIFT, // W
KEY_X|SHIFT, // X
KEY_Y|SHIFT, // Y
KEY_Z|SHIFT, // Z
0x2f, // [
0x31, // bslash
0x30, // ]
0x23|SHIFT, // ^
0x2d|SHIFT, // _
0x35, // `
KEY_A, // a
KEY_B, // b
KEY_C, // c
KEY_D, // d
KEY_E, // e
KEY_F, // f
KEY_G, // g
KEY_H, // h
KEY_I, // i
KEY_J, // j
KEY_K, // k
KEY_L, // l
KEY_M, // m
KEY_N, // n
KEY_O, // o
KEY_P, // p
KEY_Q, // q
KEY_R, // r
KEY_S, // s
KEY_T, // t
KEY_U, // u
KEY_V, // v
KEY_W, // w
KEY_X, // x
KEY_Y, // y
KEY_Z, // z
0x2f|SHIFT, //
0x31|SHIFT, // |
0x30|SHIFT, // }
0x35|SHIFT, // ~
0 // DEL
};
#define CUSTOM_RQ_SET_STATUS 1
#define CUSTOM_RQ_GET_STATUS 2
typedef uint8_t byte;
typedef struct
{
uint8_t report_id;
uint8_t modifiers;
uint8_t keycode; //Maybe be array, but one code is good
} keyboard_data_t;
static uchar idleRate; // in 4 ms units
usbMsgLen_t usbFunctionSetup(uchar data[8])
{
usbRequest_t *rq = (usbRequest_t *)((void *)data);
/* Send and receive data from host
* Ref: https://github.com/obdev/v-usb/blob/master/examples/hid-custom-rq/firmware/main.c
*
*/
if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_VENDOR)
{
if(rq->bRequest == CUSTOM_RQ_SET_STATUS)
{
if(rq->wValue.bytes[0] == 1){
}else{
}
}
else if(rq->bRequest == CUSTOM_RQ_GET_STATUS)
{
static uchar dataBuffer[1]; // buffer must stay valid when usbFunctionSetup returns
dataBuffer[0] = 101; //for test
usbMsgPtr = dataBuffer; // tell the driver which data to return
//return sizeof(dataBuffer); // tell the driver to send 1 byte
return 1;
}
}
/* Report to the host */
else if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS)
{
/* class request type */
if(rq->bRequest == USBRQ_HID_GET_REPORT)
{
/* wValue: ReportType (highbyte), ReportID (lowbyte) */
//usbMsgPtr = (uchar *)&UsbKeyboard.keyboard_data;
/* we only have one report type, so don't look at wValue */
// TODO: Ensure it's okay not to return anything here?
//return sizeof(reportBuffer);
return 0;
} else if(rq->bRequest == USBRQ_HID_GET_IDLE){
usbMsgPtr = &idleRate;
return 1;
} else if(rq->bRequest == USBRQ_HID_SET_IDLE){
idleRate = rq->wValue.bytes[1];
}
} else {
/* no vendor specific requests implemented */
}
return 0;
}
/*
* UsbKeyboard namespace
*
*
*/
namespace UsbKeyboard {
PROGMEM void init()
{
USBOUT = 0; // TODO: Only for USB pins?
USBDDR |= ~USBMASK;
//wdt_enable(WDTO_1S);
#ifdef _OSCCAL_
uchar calibrationValue;
//Clock calibration setup
calibrationValue = eeprom_read_byte(0); // calibration value from last time
if(calibrationValue != 0xff){
OSCCAL = calibrationValue;
}
#endif
//MCUSR &= ~(1 << WDRF);
//wdt_disable();
cli();
usbDeviceDisconnect();
int i = 250;
while(--i){ // fake USB disconnect for > 250 ms
//wdt_reset();
_delay_ms(1);
}
usbDeviceConnect();
usbInit();
sei();
i = 100; //Delay to make usb comunicate
while(--i) {
_delay_ms(1);
usbPoll();
}
// TODO: Remove the next two lines once we fix
// missing first keystroke bug properly.
keyboard_data_t report = {
.report_id = REPORT_ID_KEYBOARD,
.modifiers = 0,
.keycode = 0
};
usbSetInterrupt((uchar *)&report, sizeof(report));
}
void delay(){
_delay_ms(1);
}
void update() {
usbPoll();
}
void wait(){
while (!usbInterruptIsReady()) {
//_delay_us(1);
usbPoll();
}
}
//Send key code down without sending up/release it, you need to send 0 to release it or use SendKeyStroke
void sendKey(byte keyStroke, byte modifiers)
{
wait();
// Note: We wait until we can send keystroke
// so we know the previous keystroke was
// sent.
keyboard_data_t report = {
.report_id = REPORT_ID_KEYBOARD,
.modifiers = modifiers,
.keycode = keyStroke
};
usbSetInterrupt((uchar *)&report, sizeof(report));
}
//Stroke send key down and up
void sendKeyStroke(byte keycode, byte modifiers = 0)
{
sendKey(keycode, modifiers);
sendKey(0, 0);
wait();
}
typedef struct {
uint8_t report_id;
uint16_t data;
} __attribute__ ((packed)) report_consumer_t;
void sendConsumerStroke(uint16_t data)
{
wait();
report_consumer_t report = {
.report_id = REPORT_ID_CONSUMER,
.data = data
};
usbSetInterrupt((uchar *)&report, sizeof(report));
wait();
report = {
.report_id = REPORT_ID_CONSUMER,
.data = 0
};
usbSetInterrupt((uchar *)&report, sizeof(report));
wait();
}
void sendAscii(const uchar c)
{
if (c < sizeof(asciimap)) {
byte keycode = pgm_read_byte(&asciimap[c]);
byte modifiers = 0;
if (keycode & 0x80) {
modifiers |= 0x02;
keycode &= 0x7F;
}
sendKeyStroke(keycode, modifiers);
//sendKeyStroke(KEY_6, 0);
} else
{
}
}
void sendString(const char str[])
{
unsigned int i = 0;
while (i < strlen(str))
{
sendAscii(str[i]);
delay();
i++;
}
}
}; //UsbKeyboard
#endif // __UsbKeyboard_h__