|
@@ -0,0 +1,397 @@
|
|
1
|
+//
|
|
2
|
+// Created by robin on 1/8/16.
|
|
3
|
+//
|
|
4
|
+
|
|
5
|
+UsbKeyboardDevice UsbKeyboard = UsbKeyboardDevice("us");
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+/* We use a simplifed keyboard report descriptor which does not support the
|
|
9
|
+ * boot protocol. We don't allow setting status LEDs and but we do allow
|
|
10
|
+ * simultaneous key presses.
|
|
11
|
+ * The report descriptor has been created with usb.org's "HID Descriptor Tool"
|
|
12
|
+ * which can be downloaded from http://www.usb.org/developers/hidpage/.
|
|
13
|
+ * Redundant entries (such as LOGICAL_MINIMUM and USAGE_PAGE) have been omitted
|
|
14
|
+ * for the second INPUT item.
|
|
15
|
+ */
|
|
16
|
+PROGMEM const char usbHidReportDescriptor[35] = { /* USB report descriptor */
|
|
17
|
+ 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
|
|
18
|
+ 0x09, 0x06, // USAGE (Keyboard)
|
|
19
|
+ 0xa1, 0x01, // COLLECTION (Application)
|
|
20
|
+ 0x05, 0x07, // USAGE_PAGE (Keyboard)
|
|
21
|
+ 0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
|
|
22
|
+ 0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
|
|
23
|
+ 0x15, 0x00, // LOGICAL_MINIMUM (0)
|
|
24
|
+ 0x25, 0x01, // LOGICAL_MAXIMUM (1)
|
|
25
|
+ 0x75, 0x01, // REPORT_SIZE (1)
|
|
26
|
+ 0x95, 0x08, // REPORT_COUNT (8)
|
|
27
|
+ 0x81, 0x02, // INPUT (Data,Var,Abs)
|
|
28
|
+ 0x95, BUFFER_SIZE-1, // REPORT_COUNT (simultaneous keystrokes)
|
|
29
|
+ 0x75, 0x08, // REPORT_SIZE (8)
|
|
30
|
+ 0x25, 0x65, // LOGICAL_MAXIMUM (101)
|
|
31
|
+ 0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
|
|
32
|
+ 0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
|
|
33
|
+ 0x81, 0x00, // INPUT (Data,Ary,Abs)
|
|
34
|
+ 0xc0 // END_COLLECTION
|
|
35
|
+};
|
|
36
|
+
|
|
37
|
+UsbKeyboardDevice::UsbKeyboardDevice(const char* layout)
|
|
38
|
+ : _layout(0)
|
|
39
|
+{
|
|
40
|
+ PORTD = 0; // TODO: Only for USB pins?
|
|
41
|
+ DDRD |= ~USBMASK;
|
|
42
|
+
|
|
43
|
+ cli();
|
|
44
|
+ usbDeviceDisconnect();
|
|
45
|
+ usbDeviceConnect();
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+ usbInit();
|
|
49
|
+
|
|
50
|
+ sei();
|
|
51
|
+
|
|
52
|
+ // TODO: Remove the next two lines once we fix
|
|
53
|
+ // missing first keystroke bug properly.
|
|
54
|
+ memset(reportBuffer, 0, sizeof(reportBuffer));
|
|
55
|
+ usbSetInterrupt(reportBuffer, sizeof(reportBuffer));
|
|
56
|
+ setLayout("us");
|
|
57
|
+}
|
|
58
|
+
|
|
59
|
+bool UsbKeyboardDevice::isUsbReady()
|
|
60
|
+{
|
|
61
|
+ UsbKeyboard.update();
|
|
62
|
+ return usbInterruptIsReady();
|
|
63
|
+}
|
|
64
|
+
|
|
65
|
+void UsbKeyboardDevice::setLayout(const char* layout)
|
|
66
|
+{
|
|
67
|
+ delete _layout;
|
|
68
|
+ _layout = new char[strlen(layout)];
|
|
69
|
+ strcpy(_layout, layout);
|
|
70
|
+}
|
|
71
|
+
|
|
72
|
+int UsbKeyboardDevice::charModAltGr(char c, int* modifier)
|
|
73
|
+{
|
|
74
|
+ *modifier = MOD_ALT_RIGHT;
|
|
75
|
+ return c;
|
|
76
|
+}
|
|
77
|
+
|
|
78
|
+int UsbKeyboardDevice::charModShift(char c, int* modifier)
|
|
79
|
+{
|
|
80
|
+ *modifier = MOD_SHIFT_LEFT;
|
|
81
|
+ return c;
|
|
82
|
+}
|
|
83
|
+
|
|
84
|
+#ifdef ARD_USBKBD_AZERTY
|
|
85
|
+int UsbKeyboardDevice::charToKeyAzerty(int c, int* modifier)
|
|
86
|
+{
|
|
87
|
+ *modifier = 0;
|
|
88
|
+
|
|
89
|
+ if (c == '0')
|
|
90
|
+ return charModShift(KEY_0, modifier);
|
|
91
|
+ if (c >= '1' && c <= '9')
|
|
92
|
+ return charModShift(KEY_1 + c - '1', modifier);
|
|
93
|
+
|
|
94
|
+ if (c == '\n')
|
|
95
|
+ return KEY_ENTER;
|
|
96
|
+ if (c == ' ')
|
|
97
|
+ return KEY_SPACE;
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+ if (c >= 'a' && c <= 'z')
|
|
101
|
+ {
|
|
102
|
+ if (c == 'a')
|
|
103
|
+ c = 'q';
|
|
104
|
+ else if (c == 'm')
|
|
105
|
+ return KEY_SEMICOLON;
|
|
106
|
+ else if (c == 'q')
|
|
107
|
+ c = 'a';
|
|
108
|
+ else if (c == 'w')
|
|
109
|
+ c = 'z';
|
|
110
|
+ else if (c == 'z')
|
|
111
|
+ c = 'w';
|
|
112
|
+ return KEY_A + c - 'a';
|
|
113
|
+ }
|
|
114
|
+ if (c >= 'A' && c <= 'Z')
|
|
115
|
+ {
|
|
116
|
+ if (c == 'A')
|
|
117
|
+ c = 'Q';
|
|
118
|
+ else if (c == 'M')
|
|
119
|
+ return charModShift(KEY_SEMICOLON, modifier);
|
|
120
|
+ else if (c == 'Q')
|
|
121
|
+ c = 'A';
|
|
122
|
+ else if (c == 'W')
|
|
123
|
+ c = 'Z';
|
|
124
|
+ else if (c == 'Z')
|
|
125
|
+ c = 'W';
|
|
126
|
+ return charModShift(KEY_A + c - 'A', modifier);
|
|
127
|
+ }
|
|
128
|
+
|
|
129
|
+ if (c == '~')
|
|
130
|
+ return charModAltGr(KEY_2, modifier);
|
|
131
|
+ if (c == '#')
|
|
132
|
+ return charModAltGr(KEY_3, modifier);
|
|
133
|
+ if (c == '{')
|
|
134
|
+ return charModAltGr(KEY_4, modifier);
|
|
135
|
+ if (c == '[')
|
|
136
|
+ return charModAltGr(KEY_5, modifier);
|
|
137
|
+ if (c == '|')
|
|
138
|
+ return charModAltGr(KEY_6, modifier);
|
|
139
|
+ if (c == '`')
|
|
140
|
+ return charModAltGr(KEY_7, modifier);
|
|
141
|
+ if (c == '\\')
|
|
142
|
+ return charModAltGr(KEY_8, modifier);
|
|
143
|
+ if (c == '^')
|
|
144
|
+ return charModAltGr(KEY_9, modifier);
|
|
145
|
+ if (c == '@')
|
|
146
|
+ return charModAltGr(KEY_0, modifier);
|
|
147
|
+ if (c == ']')
|
|
148
|
+ return charModAltGr(KEY_HYPHEN, modifier);
|
|
149
|
+ if (c == '}')
|
|
150
|
+ return charModAltGr(KEY_EQUAL, modifier);
|
|
151
|
+ if (c == -75)//µ
|
|
152
|
+ return charModShift(KEY_BSLASH, modifier);
|
|
153
|
+ if (c == '*')
|
|
154
|
+ return KEY_BSLASH;
|
|
155
|
+
|
|
156
|
+ if (c == '&')
|
|
157
|
+ return KEY_1;
|
|
158
|
+ if (c == -23)//é
|
|
159
|
+ return KEY_2;
|
|
160
|
+ if (c == '\"')
|
|
161
|
+ return KEY_3;
|
|
162
|
+ if (c == '\'')
|
|
163
|
+ return KEY_4;
|
|
164
|
+ if (c == '(')
|
|
165
|
+ return KEY_5;
|
|
166
|
+ if (c == '-')
|
|
167
|
+ return KEY_6;
|
|
168
|
+ if (c == -24)//è
|
|
169
|
+ return KEY_7;
|
|
170
|
+ if (c == '_')
|
|
171
|
+ return KEY_8;
|
|
172
|
+ if (c == -25)//ç
|
|
173
|
+ return KEY_9;
|
|
174
|
+ if (c == -32)//à
|
|
175
|
+ return KEY_0;
|
|
176
|
+ if (c == ')')
|
|
177
|
+ return KEY_HYPHEN;
|
|
178
|
+ if (c == '=')
|
|
179
|
+ return KEY_EQUAL;
|
|
180
|
+ if (c == '+')
|
|
181
|
+ return charModShift(KEY_EQUAL, modifier);
|
|
182
|
+
|
|
183
|
+ if (c == ',')
|
|
184
|
+ return KEY_M;
|
|
185
|
+ if (c == ';')
|
|
186
|
+ return KEY_COMMA;
|
|
187
|
+ if (c == ':')
|
|
188
|
+ return KEY_DOT;
|
|
189
|
+ if (c == '!')
|
|
190
|
+ return KEY_SLASH;
|
|
191
|
+ if (c == -7)//ù
|
|
192
|
+ return KEY_QUOTE;
|
|
193
|
+ if (c == '<')
|
|
194
|
+ return KEYP_NUS_BSLASH;
|
|
195
|
+
|
|
196
|
+ if (c == '?')
|
|
197
|
+ return charModShift(KEY_M, modifier);
|
|
198
|
+ if (c == '.')
|
|
199
|
+ return charModShift(KEY_COMMA, modifier);
|
|
200
|
+ if (c == '/')
|
|
201
|
+ return charModShift(KEY_DOT, modifier);
|
|
202
|
+ if (c == -89)//§
|
|
203
|
+ return charModShift(KEY_SLASH, modifier);
|
|
204
|
+ if (c == '%')
|
|
205
|
+ return charModShift(KEY_QUOTE, modifier);
|
|
206
|
+ if (c == '>')
|
|
207
|
+ return charModShift(KEYP_NUS_BSLASH, modifier);
|
|
208
|
+
|
|
209
|
+ return 0;
|
|
210
|
+}
|
|
211
|
+#endif
|
|
212
|
+
|
|
213
|
+#ifdef ARD_USBKBD_AZERTY
|
|
214
|
+int UsbKeyboardDevice::charToKeyQwerty(int c, int* modifier)
|
|
215
|
+{
|
|
216
|
+ *modifier = 0;
|
|
217
|
+ if (c == '0')
|
|
218
|
+ return KEY_0;
|
|
219
|
+ if (c >= '1' && c <= '9')
|
|
220
|
+ return KEY_1 + c - '1';
|
|
221
|
+ if (c >= 'a' && c <= 'z')
|
|
222
|
+ return KEY_A + c - 'a';
|
|
223
|
+ if (c >= 'A' && c <= 'Z')
|
|
224
|
+ return charModShift(KEY_A + c - 'A', modifier);
|
|
225
|
+
|
|
226
|
+ if (c == '\n')
|
|
227
|
+ return KEY_ENTER;
|
|
228
|
+ if (c == ' ')
|
|
229
|
+ return KEY_SPACE;
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+ if (c == '.')
|
|
233
|
+ return KEY_DOT;
|
|
234
|
+ if (c == ',')
|
|
235
|
+ return KEY_COMMA;
|
|
236
|
+ if (c == '/')
|
|
237
|
+ return KEY_SLASH;
|
|
238
|
+ if (c == ';')
|
|
239
|
+ return KEY_SEMICOLON;
|
|
240
|
+ if (c == '`')
|
|
241
|
+ return KEY_TILDE;
|
|
242
|
+ if (c == '-')
|
|
243
|
+ return KEY_HYPHEN;
|
|
244
|
+ if (c == '=')
|
|
245
|
+ return KEY_EQUAL;
|
|
246
|
+ if (c == '\'')
|
|
247
|
+ return KEY_QUOTE;
|
|
248
|
+ if (c == '~')
|
|
249
|
+ return charModShift(KEY_TILDE, modifier);
|
|
250
|
+ if (c == '<')
|
|
251
|
+ return charModShift(KEY_COMMA, modifier);
|
|
252
|
+ if (c == '>')
|
|
253
|
+ return charModShift(KEY_DOT, modifier);
|
|
254
|
+ if (c == '?')
|
|
255
|
+ return charModShift(KEY_SLASH, modifier);
|
|
256
|
+ if (c == ':')
|
|
257
|
+ return charModShift(KEY_SEMICOLON, modifier);
|
|
258
|
+
|
|
259
|
+ if (c == '!')
|
|
260
|
+ return charModShift(KEY_1, modifier);
|
|
261
|
+ if (c == '@')
|
|
262
|
+ return charModShift(KEY_2, modifier);
|
|
263
|
+ if (c == '#')
|
|
264
|
+ return charModShift(KEY_3, modifier);
|
|
265
|
+ if (c == '$')
|
|
266
|
+ return charModShift(KEY_4, modifier);
|
|
267
|
+ if (c == '%')
|
|
268
|
+ return charModShift(KEY_5, modifier);
|
|
269
|
+ if (c == '^')
|
|
270
|
+ return charModShift(KEY_6, modifier);
|
|
271
|
+ if (c == '&')
|
|
272
|
+ return charModShift(KEY_7, modifier);
|
|
273
|
+ if (c == '*')
|
|
274
|
+ return charModShift(KEY_8, modifier);
|
|
275
|
+ if (c == '(')
|
|
276
|
+ return charModShift(KEY_9, modifier);
|
|
277
|
+ if (c == ')')
|
|
278
|
+ return charModShift(KEY_0, modifier);
|
|
279
|
+ if (c == '_')
|
|
280
|
+ return charModShift(KEY_HYPHEN, modifier);
|
|
281
|
+ if (c == '+')
|
|
282
|
+ return charModShift(KEY_EQUAL, modifier);
|
|
283
|
+ if (c == '"')
|
|
284
|
+ return charModShift(KEY_QUOTE, modifier);
|
|
285
|
+
|
|
286
|
+ if (c == '{')
|
|
287
|
+ return charModShift(KEY_LBRACKET, modifier);
|
|
288
|
+ if (c == '}')
|
|
289
|
+ return charModShift(KEY_RBRACKET, modifier);
|
|
290
|
+ if (c == '|')
|
|
291
|
+ return charModShift(KEY_BSLASH, modifier);
|
|
292
|
+
|
|
293
|
+ if (c == '[')
|
|
294
|
+ return KEY_LBRACKET;
|
|
295
|
+ if (c == ']')
|
|
296
|
+ return KEY_RBRACKET;
|
|
297
|
+ if (c == '\\')
|
|
298
|
+ return KEY_BSLASH;
|
|
299
|
+ return 0;
|
|
300
|
+}
|
|
301
|
+#endif
|
|
302
|
+
|
|
303
|
+void UsbKeyboardDevice::sendKeyStrokes(const char* str)
|
|
304
|
+{
|
|
305
|
+ int (*charToKey)(int,int*) = 0;
|
|
306
|
+#ifdef ARD_USBKBD_AZERTY
|
|
307
|
+ if (!strcmp(_layout, "fr"))
|
|
308
|
+ {
|
|
309
|
+ charToKey = charToKeyAzerty;
|
|
310
|
+ }
|
|
311
|
+#endif
|
|
312
|
+#ifdef ARD_USBKBD_QWERTY
|
|
313
|
+ if (!strcmp(_layout, "us"))
|
|
314
|
+ {
|
|
315
|
+ charToKey = charToKeyQwerty;
|
|
316
|
+ }
|
|
317
|
+#endif
|
|
318
|
+ if (!charToKey)
|
|
319
|
+ {
|
|
320
|
+ return;
|
|
321
|
+ }
|
|
322
|
+
|
|
323
|
+ while (*str)
|
|
324
|
+ {
|
|
325
|
+ int modifier = 0;
|
|
326
|
+ int k = charToKey(*str, &modifier);
|
|
327
|
+ /*if (!k)
|
|
328
|
+ {
|
|
329
|
+ k = KEY_SPACE;
|
|
330
|
+ char buf[12];
|
|
331
|
+ itoa((unsigned)*str, buf, 10);
|
|
332
|
+ sendKeyStrokes(buf);
|
|
333
|
+ }*/
|
|
334
|
+ if (k)
|
|
335
|
+ {
|
|
336
|
+ UsbKeyboard.sendKeyStroke(k, modifier);
|
|
337
|
+ }
|
|
338
|
+ ++str;
|
|
339
|
+ }
|
|
340
|
+}
|
|
341
|
+
|
|
342
|
+void UsbKeyboardDevice::sendKeyStroke(byte keyStroke, byte modifiers)
|
|
343
|
+{
|
|
344
|
+
|
|
345
|
+ while (!usbInterruptIsReady()) {
|
|
346
|
+ // Note: We wait until we can send keystroke
|
|
347
|
+ // so we know the previous keystroke was
|
|
348
|
+ // sent.
|
|
349
|
+ }
|
|
350
|
+
|
|
351
|
+ memset(reportBuffer, 0, sizeof(reportBuffer));
|
|
352
|
+
|
|
353
|
+ reportBuffer[0] = modifiers;
|
|
354
|
+ reportBuffer[1] = keyStroke;
|
|
355
|
+
|
|
356
|
+ usbSetInterrupt(reportBuffer, sizeof(reportBuffer));
|
|
357
|
+
|
|
358
|
+ while (!usbInterruptIsReady()) {
|
|
359
|
+ // Note: We wait until we can send keystroke
|
|
360
|
+ // so we know the previous keystroke was
|
|
361
|
+ // sent.
|
|
362
|
+ }
|
|
363
|
+
|
|
364
|
+ // This stops endlessly repeating keystrokes:
|
|
365
|
+ memset(reportBuffer, 0, sizeof(reportBuffer));
|
|
366
|
+ usbSetInterrupt(reportBuffer, sizeof(reportBuffer));
|
|
367
|
+
|
|
368
|
+}
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+uchar usbFunctionSetup(uchar data[8])
|
|
372
|
+{
|
|
373
|
+ usbRequest_t *rq = (usbRequest_t *)((void *)data);
|
|
374
|
+
|
|
375
|
+ usbMsgPtr = UsbKeyboard.reportBuffer; //
|
|
376
|
+ if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){
|
|
377
|
+ /* class request type */
|
|
378
|
+
|
|
379
|
+ if(rq->bRequest == USBRQ_HID_GET_REPORT){
|
|
380
|
+ /* wValue: ReportType (highbyte), ReportID (lowbyte) */
|
|
381
|
+
|
|
382
|
+ /* we only have one report type, so don't look at wValue */
|
|
383
|
+ // TODO: Ensure it's okay not to return anything here?
|
|
384
|
+ return 0;
|
|
385
|
+
|
|
386
|
+ }else if(rq->bRequest == USBRQ_HID_GET_IDLE){
|
|
387
|
+ // usbMsgPtr = &idleRate;
|
|
388
|
+ // return 1;
|
|
389
|
+ return 0;
|
|
390
|
+ }else if(rq->bRequest == USBRQ_HID_SET_IDLE){
|
|
391
|
+ idleRate = rq->wValue.bytes[1];
|
|
392
|
+ }
|
|
393
|
+ }else{
|
|
394
|
+ /* no vendor specific requests implemented */
|
|
395
|
+ }
|
|
396
|
+ return 0;
|
|
397
|
+}
|