-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathPS2Keyboard.cpp
711 lines (666 loc) · 21.7 KB
/
PS2Keyboard.cpp
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
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
/*
PS2Keyboard.cpp - PS2Keyboard library
Copyright (c) 2007 Free Software Foundation. All right reserved.
Written by Christian Weichel <[email protected]>
** Mostly rewritten Paul Stoffregen <[email protected]> 2010, 2011
** Modified for use beginning with Arduino 13 by L. Abraham Smith, <[email protected]> *
** Modified for easy interrup pin assignement on method begin(datapin,irq_pin). Cuningan <[email protected]> **
for more information you can read the original wiki in arduino.cc
at http://www.arduino.cc/playground/Main/PS2Keyboard
or http://www.pjrc.com/teensy/td_libs_PS2Keyboard.html
Version 2.4 (March 2013)
- Support Teensy 3.0, Arduino Due, Arduino Leonardo & other boards
- French keyboard layout, David Chochoi, tchoyyfr at yahoo dot fr
Version 2.3 (October 2011)
- Minor bugs fixed
Version 2.2 (August 2011)
- Support non-US keyboards - thanks to Rainer Bruch for a German keyboard :)
Version 2.1 (May 2011)
- timeout to recover from misaligned input
- compatibility with Arduino "new-extension" branch
- TODO: send function, proposed by Scott Penrose, scooterda at me dot com
Version 2.0 (June 2010)
- Buffering added, many scan codes can be captured without data loss
if your sketch is busy doing other work
- Shift keys supported, completely rewritten scan code to ascii
- Slow linear search replaced with fast indexed table lookups
- Support for Teensy, Arduino Mega, and Sanguino added
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "PS2Keyboard.h"
#define BUFFER_SIZE 45
static volatile uint8_t buffer[BUFFER_SIZE];
static volatile uint8_t head, tail;
static uint8_t DataPin;
static uint8_t CharBuffer=0;
static uint8_t UTF8next=0;
static const PS2Keymap_t *keymap=NULL;
// The ISR for the external interrupt
void ps2interrupt(void)
{
static uint8_t bitcount=0;
static uint8_t incoming=0;
static uint32_t prev_ms=0;
uint32_t now_ms;
uint8_t n, val;
val = digitalRead(DataPin);
now_ms = millis();
if (now_ms - prev_ms > 250) {
bitcount = 0;
incoming = 0;
}
prev_ms = now_ms;
n = bitcount - 1;
if (n <= 7) {
incoming |= (val << n);
}
bitcount++;
if (bitcount == 11) {
uint8_t i = head + 1;
if (i >= BUFFER_SIZE) i = 0;
if (i != tail) {
buffer[i] = incoming;
head = i;
}
bitcount = 0;
incoming = 0;
}
}
static inline uint8_t get_scan_code(void)
{
uint8_t c, i;
i = tail;
if (i == head) return 0;
i++;
if (i >= BUFFER_SIZE) i = 0;
c = buffer[i];
tail = i;
return c;
}
// http://www.quadibloc.com/comp/scan.htm
// http://www.computer-engineering.org/ps2keyboard/scancodes2.html
// These arrays provide a simple key map, to turn scan codes into ISO8859
// output. If a non-US keyboard is used, these may need to be modified
// for the desired output.
//
const PROGMEM PS2Keymap_t PS2Keymap_US = {
// without shift
{0, PS2_F9, 0, PS2_F5, PS2_F3, PS2_F1, PS2_F2, PS2_F12,
0, PS2_F10, PS2_F8, PS2_F6, PS2_F4, PS2_TAB, '`', 0,
0, 0 /*Lalt*/, 0 /*Lshift*/, 0, 0 /*Lctrl*/, 'q', '1', 0,
0, 0, 'z', 's', 'a', 'w', '2', 0,
0, 'c', 'x', 'd', 'e', '4', '3', 0,
0, ' ', 'v', 'f', 't', 'r', '5', 0,
0, 'n', 'b', 'h', 'g', 'y', '6', 0,
0, 0, 'm', 'j', 'u', '7', '8', 0,
0, ',', 'k', 'i', 'o', '0', '9', 0,
0, '.', '/', 'l', ';', 'p', '-', 0,
0, 0, '\'', 0, '[', '=', 0, 0,
0 /*CapsLock*/, 0 /*Rshift*/, PS2_ENTER /*Enter*/, ']', 0, '\\', 0, 0,
0, 0, 0, 0, 0, 0, PS2_BACKSPACE, 0,
0, '1', 0, '4', '7', 0, 0, 0,
'0', '.', '2', '5', '6', '8', PS2_ESC, 0 /*NumLock*/,
PS2_F11, '+', '3', '-', '*', '9', PS2_SCROLL, 0,
0, 0, 0, PS2_F7 },
// with shift
{0, PS2_F9, 0, PS2_F5, PS2_F3, PS2_F1, PS2_F2, PS2_F12,
0, PS2_F10, PS2_F8, PS2_F6, PS2_F4, PS2_TAB, '~', 0,
0, 0 /*Lalt*/, 0 /*Lshift*/, 0, 0 /*Lctrl*/, 'Q', '!', 0,
0, 0, 'Z', 'S', 'A', 'W', '@', 0,
0, 'C', 'X', 'D', 'E', '$', '#', 0,
0, ' ', 'V', 'F', 'T', 'R', '%', 0,
0, 'N', 'B', 'H', 'G', 'Y', '^', 0,
0, 0, 'M', 'J', 'U', '&', '*', 0,
0, '<', 'K', 'I', 'O', ')', '(', 0,
0, '>', '?', 'L', ':', 'P', '_', 0,
0, 0, '"', 0, '{', '+', 0, 0,
0 /*CapsLock*/, 0 /*Rshift*/, PS2_ENTER /*Enter*/, '}', 0, '|', 0, 0,
0, 0, 0, 0, 0, 0, PS2_BACKSPACE, 0,
0, '1', 0, '4', '7', 0, 0, 0,
'0', '.', '2', '5', '6', '8', PS2_ESC, 0 /*NumLock*/,
PS2_F11, '+', '3', '-', '*', '9', PS2_SCROLL, 0,
0, 0, 0, PS2_F7 },
0
};
const PROGMEM PS2Keymap_t PS2Keymap_German = {
// without shift
{0, PS2_F9, 0, PS2_F5, PS2_F3, PS2_F1, PS2_F2, PS2_F12,
0, PS2_F10, PS2_F8, PS2_F6, PS2_F4, PS2_TAB, '^', 0,
0, 0 /*Lalt*/, 0 /*Lshift*/, 0, 0 /*Lctrl*/, 'q', '1', 0,
0, 0, 'y', 's', 'a', 'w', '2', 0,
0, 'c', 'x', 'd', 'e', '4', '3', 0,
0, ' ', 'v', 'f', 't', 'r', '5', 0,
0, 'n', 'b', 'h', 'g', 'z', '6', 0,
0, 0, 'm', 'j', 'u', '7', '8', 0,
0, ',', 'k', 'i', 'o', '0', '9', 0,
0, '.', '-', 'l', PS2_o_DIAERESIS, 'p', PS2_SHARP_S, 0,
0, 0, PS2_a_DIAERESIS, 0, PS2_u_DIAERESIS, '\'', 0, 0,
0 /*CapsLock*/, 0 /*Rshift*/, PS2_ENTER /*Enter*/, '+', 0, '#', 0, 0,
0, '<', 0, 0, 0, 0, PS2_BACKSPACE, 0,
0, '1', 0, '4', '7', 0, 0, 0,
'0', '.', '2', '5', '6', '8', PS2_ESC, 0 /*NumLock*/,
PS2_F11, '+', '3', '-', '*', '9', PS2_SCROLL, 0,
0, 0, 0, PS2_F7 },
// with shift
{0, PS2_F9, 0, PS2_F5, PS2_F3, PS2_F1, PS2_F2, PS2_F12,
0, PS2_F10, PS2_F8, PS2_F6, PS2_F4, PS2_TAB, PS2_DEGREE_SIGN, 0,
0, 0 /*Lalt*/, 0 /*Lshift*/, 0, 0 /*Lctrl*/, 'Q', '!', 0,
0, 0, 'Y', 'S', 'A', 'W', '"', 0,
0, 'C', 'X', 'D', 'E', '$', PS2_SECTION_SIGN, 0,
0, ' ', 'V', 'F', 'T', 'R', '%', 0,
0, 'N', 'B', 'H', 'G', 'Z', '&', 0,
0, 0, 'M', 'J', 'U', '/', '(', 0,
0, ';', 'K', 'I', 'O', '=', ')', 0,
0, ':', '_', 'L', PS2_O_DIAERESIS, 'P', '?', 0,
0, 0, PS2_A_DIAERESIS, 0, PS2_U_DIAERESIS, '`', 0, 0,
0 /*CapsLock*/, 0 /*Rshift*/, PS2_ENTER /*Enter*/, '*', 0, '\'', 0, 0,
0, '>', 0, 0, 0, 0, PS2_BACKSPACE, 0,
0, '1', 0, '4', '7', 0, 0, 0,
'0', '.', '2', '5', '6', '8', PS2_ESC, 0 /*NumLock*/,
PS2_F11, '+', '3', '-', '*', '9', PS2_SCROLL, 0,
0, 0, 0, PS2_F7 },
1,
// with altgr
{0, PS2_F9, 0, PS2_F5, PS2_F3, PS2_F1, PS2_F2, PS2_F12,
0, PS2_F10, PS2_F8, PS2_F6, PS2_F4, PS2_TAB, 0, 0,
0, 0 /*Lalt*/, 0 /*Lshift*/, 0, 0 /*Lctrl*/, '@', 0, 0,
0, 0, 0, 0, 0, 0, PS2_SUPERSCRIPT_TWO, 0,
0, 0, 0, 0, PS2_CURRENCY_SIGN, 0, PS2_SUPERSCRIPT_THREE, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, PS2_MICRO_SIGN, 0, 0, '{', '[', 0,
0, 0, 0, 0, 0, '}', ']', 0,
0, 0, 0, 0, 0, 0, '\\', 0,
0, 0, 0, 0, 0, 0, 0, 0,
0 /*CapsLock*/, 0 /*Rshift*/, PS2_ENTER /*Enter*/, '~', 0, '#', 0, 0,
0, '|', 0, 0, 0, 0, PS2_BACKSPACE, 0,
0, '1', 0, '4', '7', 0, 0, 0,
'0', '.', '2', '5', '6', '8', PS2_ESC, 0 /*NumLock*/,
PS2_F11, '+', '3', '-', '*', '9', PS2_SCROLL, 0,
0, 0, 0, PS2_F7 }
};
const PROGMEM PS2Keymap_t PS2Keymap_French = {
// without shift
{0, PS2_F9, 0, PS2_F5, PS2_F3, PS2_F1, PS2_F2, PS2_F12,
0, PS2_F10, PS2_F8, PS2_F6, PS2_F4, PS2_TAB, PS2_SUPERSCRIPT_TWO, 0,
0, 0 /*Lalt*/, 0 /*Lshift*/, 0, 0 /*Lctrl*/, 'a', '&', 0,
0, 0, 'w', 's', 'q', 'z', PS2_e_ACUTE, 0,
0, 'c', 'x', 'd', 'e', '\'', '"', 0,
0, ' ', 'v', 'f', 't', 'r', '(', 0,
0, 'n', 'b', 'h', 'g', 'y', '-', 0,
0, 0, ',', 'j', 'u', PS2_e_GRAVE, '_', 0,
0, ';', 'k', 'i', 'o', PS2_a_GRAVE, PS2_c_CEDILLA, 0,
0, ':', '!', 'l', 'm', 'p', ')', 0,
0, 0, PS2_u_GRAVE, 0, '^', '=', 0, 0,
0 /*CapsLock*/, 0 /*Rshift*/, PS2_ENTER /*Enter*/, '$', 0, '*', 0, 0,
0, '<', 0, 0, 0, 0, PS2_BACKSPACE, 0,
0, '1', 0, '4', '7', 0, 0, 0,
'0', '.', '2', '5', '6', '8', PS2_ESC, 0 /*NumLock*/,
PS2_F11, '+', '3', '-', '*', '9', PS2_SCROLL, 0,
0, 0, 0, PS2_F7 },
// with shift
{0, PS2_F9, 0, PS2_F5, PS2_F3, PS2_F1, PS2_F2, PS2_F12,
0, PS2_F10, PS2_F8, PS2_F6, PS2_F4, PS2_TAB, 0, 0,
0, 0 /*Lalt*/, 0 /*Lshift*/, 0, 0 /*Lctrl*/, 'A', '1', 0,
0, 0, 'W', 'S', 'Q', 'Z', '2', 0,
0, 'C', 'X', 'D', 'E', '4', '3', 0,
0, ' ', 'V', 'F', 'T', 'R', '5', 0,
0, 'N', 'B', 'H', 'G', 'Y', '6', 0,
0, 0, '?', 'J', 'U', '7', '8', 0,
0, '.', 'K', 'I', 'O', '0', '9', 0,
0, '/', PS2_SECTION_SIGN, 'L', 'M', 'P', PS2_DEGREE_SIGN, 0,
0, 0, '%', 0, PS2_DIAERESIS, '+', 0, 0,
0 /*CapsLock*/, 0 /*Rshift*/, PS2_ENTER /*Enter*/, PS2_POUND_SIGN, 0, PS2_MICRO_SIGN, 0, 0,
0, '>', 0, 0, 0, 0, PS2_BACKSPACE, 0,
0, '1', 0, '4', '7', 0, 0, 0,
'0', '.', '2', '5', '6', '8', PS2_ESC, 0 /*NumLock*/,
PS2_F11, '+', '3', '-', '*', '9', PS2_SCROLL, 0,
0, 0, 0, PS2_F7 },
1,
// with altgr
{0, PS2_F9, 0, PS2_F5, PS2_F3, PS2_F1, PS2_F2, PS2_F12,
0, PS2_F10, PS2_F8, PS2_F6, PS2_F4, PS2_TAB, 0, 0,
0, 0 /*Lalt*/, 0 /*Lshift*/, 0, 0 /*Lctrl*/, '@', 0, 0,
0, 0, 0, 0, 0, 0, '~', 0,
0, 0, 0, 0, 0 /*PS2_EURO_SIGN*/, '{', '#', 0,
0, 0, 0, 0, 0, 0, '[', 0,
0, 0, 0, 0, 0, 0, '|', 0,
0, 0, 0, 0, 0, '`', '\\', 0,
0, 0, 0, 0, 0, '@', '^', 0,
0, 0, 0, 0, 0, 0, ']', 0,
0, 0, 0, 0, 0, 0, '}', 0,
0 /*CapsLock*/, 0 /*Rshift*/, PS2_ENTER /*Enter*/, PS2_CURRENCY_SIGN, 0, '#', 0, 0,
0, '|', 0, 0, 0, 0, PS2_BACKSPACE, 0,
0, '1', 0, '4', '7', 0, 0, 0,
'0', '.', '2', '5', '6', '8', PS2_ESC, 0 /*NumLock*/,
PS2_F11, '+', '3', '-', '*', '9', PS2_SCROLL, 0,
0, 0, 0, PS2_F7 }
};
const PROGMEM PS2Keymap_t PS2Keymap_Spanish = {
// without shift
{0, PS2_F9, 0, PS2_F5, PS2_F3, PS2_F1, PS2_F2, PS2_F12,
0, PS2_F10, PS2_F8, PS2_F6, PS2_F4, PS2_TAB, PS2_MASCULINE_ORDINAL, 0,
0, 0 /*Lalt*/, 0 /*Lshift*/, 0, 0 /*Lctrl*/, 'q', '1', 0,
0, 0, 'z', 's', 'a', 'w', '2', 0,
0, 'c', 'x', 'd', 'e', '4', '3', 0,
0, ' ', 'v', 'f', 't', 'r', '5', 0,
0, 'n', 'b', 'h', 'g', 'y', '6', 0,
0, 0, 'm', 'j', 'u', '7', '8', 0,
0, ',', 'k', 'i', 'o', '0', '9', 0,
0, '.', '-', 'l', 'n', 'p', '\'', 0,
0, 0, PS2_ACUTE_ACCENT, 0, '`', PS2_INVERTED_EXCLAMATION, 0, 0,
0 /*CapsLock*/, 0 /*Rshift*/, PS2_ENTER /*Enter*/, '+', 0, PS2_c_CEDILLA, 0, 0,
0, '<', 0, 0, 0, 0, PS2_BACKSPACE, 0,
0, '1', 0, '4', '7', 0, 0, 0,
'0', '.', '2', '5', '6', '8', PS2_ESC, 0 /*NumLock*/,
PS2_F11, '+', '3', '-', '*', '9', PS2_SCROLL, 0,
0, 0, 0, PS2_F7 },
// with shift
{0, PS2_F9, 0, PS2_F5, PS2_F3, PS2_F1, PS2_F2, PS2_F12,
0, PS2_F10, PS2_F8, PS2_F6, PS2_F4, PS2_TAB, PS2_FEMININE_ORDINAL, 0,
0, 0 /*Lalt*/, 0 /*Lshift*/, 0, 0 /*Lctrl*/, 'Q', '!', 0,
0, 0, 'Z', 'S', 'A', 'W', '\"', 0,
0, 'C', 'X', 'D', 'E', '$', PS2_MIDDLE_DOT, 0,
0, ' ', 'V', 'F', 'T', 'R', '%', 0,
0, 'N', 'B', 'H', 'G', 'Y', '&', 0,
0, 0, 'M', 'J', 'U', '/', '(', 0,
0, ';', 'K', 'I', 'O', '=', ')', 0,
0, ':', '_', 'L', 'N', 'P', '?', 0,
0, 0, PS2_DIAERESIS, 0, '^', PS2_INVERTED_QUESTION_MARK, 0, 0,
0 /*CapsLock*/, 0 /*Rshift*/, PS2_ENTER /*Enter*/, '*', 0, PS2_C_CEDILLA, 0, 0,
0, '>', 0, 0, 0, 0, PS2_BACKSPACE, 0,
0, '1', 0, '4', '7', 0, 0, 0,
'0', '.', '2', '5', '6', '8', PS2_ESC, 0 /*NumLock*/,
PS2_F11, '+', '3', '-', '*', '9', PS2_SCROLL, 0,
0, 0, 0, PS2_F7 },
1,
// with altgr
{0, PS2_F9, 0, PS2_F5, PS2_F3, PS2_F1, PS2_F2, PS2_F12,
0, PS2_F10, PS2_F8, PS2_F6, PS2_F4, PS2_TAB, '\\', 0,
0, 0 /*Lalt*/, 0 /*Lshift*/, 0, 0 /*Lctrl*/, 'q', '|', 0,
0, 0, 'z', 's', 'a', 'w', '@', 0,
0, 'c', 'x', 'd', PS2_EURO_SIGN, '~', '#', 0,
0, ' ', 'v', 'f', 't', 'r', '5', 0,
0, 'n', 'b', 'h', 'g', 'y', PS2_NOT_SIGN, 0,
0, 0, 'm', 'j', 'u', '7', '8', 0,
0, ',', 'k', 'i', 'o', '0', '9', 0,
0, '.', '-', 'l', 'n', 'p', '\'', 0,
0, 0, '{', 0, '[', PS2_INVERTED_EXCLAMATION, 0, 0,
0 /*CapsLock*/, 0 /*Rshift*/, PS2_ENTER /*Enter*/, ']', 0, '}', 0, 0,
0, '|', 0, 0, 0, 0, PS2_BACKSPACE, 0,
0, '1', 0, '4', '7', 0, 0, 0,
'0', '.', '2', '5', '6', '8', PS2_ESC, 0 /*NumLock*/,
PS2_F11, '+', '3', '-', '*', '9', PS2_SCROLL, 0,
0, 0, 0, PS2_F7 }
};
const PROGMEM PS2Keymap_t PS2Keymap_Italian = {
// without shift
{0, PS2_F9, 0, PS2_F5, PS2_F3, PS2_F1, PS2_F2, PS2_F12,
0, PS2_F10, PS2_F8, PS2_F6, PS2_F4, PS2_TAB, '\\', 0,
0, 0 /*Lalt*/, 0 /*Lshift*/, 0, 0 /*Lctrl*/, 'q', '1', 0,
0, 0, 'z', 's', 'a', 'w', '2', 0,
0, 'c', 'x', 'd', 'e', '4', '3', 0,
0, ' ', 'v', 'f', 't', 'r', '5', 0,
0, 'n', 'b', 'h', 'g', 'y', '6', 0,
0, 0, 'm', 'j', 'u', '7', '8', 0,
0, ',', 'k', 'i', 'o', '0', '9', 0,
0, '.', '-', 'l', PS2_o_GRAVE, 'p', '\'', 0,
0, 0, PS2_a_GRAVE, 0, PS2_e_GRAVE, PS2_i_GRAVE, 0, 0,
0 /*CapsLock*/, 0 /*Rshift*/, PS2_ENTER /*Enter*/, '+', 0, PS2_u_GRAVE, 0, 0,
0, '<', 0, 0, 0, 0, PS2_BACKSPACE, 0,
0, '1', 0, '4', '7', 0, 0, 0,
'0', '.', '2', '5', '6', '8', PS2_ESC, 0 /*NumLock*/,
PS2_F11, '+', '3', '-', '*', '9', PS2_SCROLL, 0,
0, 0, 0, PS2_F7 },
// with shift
{0, PS2_F9, 0, PS2_F5, PS2_F3, PS2_F1, PS2_F2, PS2_F12,
0, PS2_F10, PS2_F8, PS2_F6, PS2_F4, PS2_TAB, '|', 0,
0, 0 /*Lalt*/, 0 /*Lshift*/, 0, 0 /*Lctrl*/, 'Q', '!', 0,
0, 0, 'Z', 'S', 'A', 'W', '\"', 0,
0, 'C', 'X', 'D', 'E', '$', PS2_POUND_SIGN, 0,
0, ' ', 'V', 'F', 'T', 'R', '%', 0,
0, 'N', 'B', 'H', 'G', 'Y', '&', 0,
0, 0, 'M', 'J', 'U', '/', '(', 0,
0, ';', 'K', 'I', 'O', '=', ')', 0,
0, ':', '_', 'L', PS2_c_CEDILLA, 0, '?', 0,
0, 0, PS2_DEGREE_SIGN, 0, PS2_e_ACUTE, '^', 0, 0,
0 /*CapsLock*/, 0 /*Rshift*/, PS2_ENTER /*Enter*/, '*', 0, PS2_SECTION_SIGN, 0, 0,
0, '>', 0, 0, 0, 0, PS2_BACKSPACE, 0,
0, '1', 0, '4', '7', 0, 0, 0,
'0', '.', '2', '5', '6', '8', PS2_ESC, 0 /*NumLock*/,
PS2_F11, '+', '3', '-', '*', '9', PS2_SCROLL, 0,
0, 0, 0, PS2_F7 },
1,
// with altgr
{0, PS2_F9, 0, PS2_F5, PS2_F3, PS2_F1, PS2_F2, PS2_F12,
0, PS2_F10, PS2_F8, PS2_F6, PS2_F4, PS2_TAB, PS2_NOT_SIGN, 0,
0, 0 /*Lalt*/, 0 /*Lshift*/, 0, 0 /*Lctrl*/, 'q', PS2_SUPERSCRIPT_ONE, 0,
0, 0, 'z', 's', 'a', 'w', PS2_SUPERSCRIPT_TWO, 0,
0, 'c', 'x', 'd', PS2_EURO_SIGN, PS2_FRACTION_ONE_QUARTER, PS2_SUPERSCRIPT_THREE, 0,
0, ' ', 'v', 'f', 't', 'r', PS2_FRACTION_ONE_HALF, 0,
0, 'n', 'b', 'h', 'g', 'y', PS2_NOT_SIGN, 0,
0, 0, 'm', 'j', 'u', '{', '[', 0,
0, ',', 'k', 'i', 'o', '}', ']', 0,
0, '.', '-', 'l', '@', 'p', '\'', 0,
0, 0, '#', 0, '[', '~', 0, 0,
0 /*CapsLock*/, 0 /*Rshift*/, PS2_ENTER /*Enter*/, ']', 0, 0, 0, 0,
0, PS2_LEFT_DOUBLE_ANGLE_QUOTE, 0, 0, 0, 0, PS2_BACKSPACE, 0,
0, '1', 0, '4', '7', 0, 0, 0,
'0', '.', '2', '5', '6', '8', PS2_ESC, 0 /*NumLock*/,
PS2_F11, '+', '3', '-', '*', '9', PS2_SCROLL, 0,
0, 0, 0, PS2_F7 }
};
const PROGMEM PS2Keymap_t PS2Keymap_UK = {
// https://github.com/PaulStoffregen/PS2Keyboard/issues/15
// without shift
{0, PS2_F9, 0, PS2_F5, PS2_F3, PS2_F1, PS2_F2, PS2_F12,
0, PS2_F10, PS2_F8, PS2_F6, PS2_F4, PS2_TAB, '`', 0,
0, 0 /*Lalt*/, 0 /*Lshift*/, 0, 0 /*Lctrl*/, 'q', '1', 0,
0, 0, 'z', 's', 'a', 'w', '2', 0,
0, 'c', 'x', 'd', 'e', '4', '3', 0,
0, ' ', 'v', 'f', 't', 'r', '5', 0,
0, 'n', 'b', 'h', 'g', 'y', '6', 0,
0, 0, 'm', 'j', 'u', '7', '8', 0,
0, ',', 'k', 'i', 'o', '0', '9', 0,
0, '.', '/', 'l', ';', 'p', '-', 0,
0, 0, '\'', 0, '[', '=', 0, 0,
0 /*CapsLock*/, 0 /*Rshift*/, PS2_ENTER /*Enter*/, ']', 0, '#', 0, 0,
0, '\\', 0, 0, 0, 0, PS2_BACKSPACE, 0,
0, '1', 0, '4', '7', 0, 0, 0,
'0', '.', '2', '5', '6', '8', PS2_ESC, 0 /*NumLock*/,
PS2_F11, '+', '3', '-', '*', '9', PS2_SCROLL, 0,
0, 0, 0, PS2_F7 },
// with shift
{0, PS2_F9, 0, PS2_F5, PS2_F3, PS2_F1, PS2_F2, PS2_F12,
0, PS2_F10, PS2_F8, PS2_F6, PS2_F4, PS2_TAB, 172 /* ¬ */, 0,
0, 0 /*Lalt*/, 0 /*Lshift*/, 0, 0 /*Lctrl*/, 'Q', '!', 0,
0, 0, 'Z', 'S', 'A', 'W', '"', 0,
0, 'C', 'X', 'D', 'E', '$', 163 /* £ */, 0,
0, ' ', 'V', 'F', 'T', 'R', '%', 0,
0, 'N', 'B', 'H', 'G', 'Y', '^', 0,
0, 0, 'M', 'J', 'U', '&', '*', 0,
0, '<', 'K', 'I', 'O', ')', '(', 0,
0, '>', '?', 'L', ':', 'P', '_', 0,
0, 0, '@', 0, '{', '+', 0, 0,
0 /*CapsLock*/, 0 /*Rshift*/, PS2_ENTER /*Enter*/, '}', 0, '~', 0, 0,
0, '|', 0, 0, 0, 0, PS2_BACKSPACE, 0,
0, '1', 0, '4', '7', 0, 0, 0,
'0', '.', '2', '5', '6', '8', PS2_ESC, 0 /*NumLock*/,
PS2_F11, '+', '3', '-', '*', '9', PS2_SCROLL, 0,
0, 0, 0, PS2_F7 },
0
};
#define BREAK 0x01
#define MODIFIER 0x02
#define SHIFT_L 0x04
#define SHIFT_R 0x08
#define ALTGR 0x10
static char get_iso8859_code(void)
{
static uint8_t state=0;
uint8_t s;
char c;
while (1) {
s = get_scan_code();
if (!s) return 0;
if (s == 0xF0) {
state |= BREAK;
} else if (s == 0xE0) {
state |= MODIFIER;
} else {
if (state & BREAK) {
if (s == 0x12) {
state &= ~SHIFT_L;
} else if (s == 0x59) {
state &= ~SHIFT_R;
} else if (s == 0x11 && (state & MODIFIER)) {
state &= ~ALTGR;
}
// CTRL, ALT & WIN keys could be added
// but is that really worth the overhead?
state &= ~(BREAK | MODIFIER);
continue;
}
if (s == 0x12) {
state |= SHIFT_L;
continue;
} else if (s == 0x59) {
state |= SHIFT_R;
continue;
} else if (s == 0x11 && (state & MODIFIER)) {
state |= ALTGR;
}
c = 0;
if (state & MODIFIER) {
switch (s) {
case 0x70: c = PS2_INSERT; break;
case 0x6C: c = PS2_HOME; break;
case 0x7D: c = PS2_PAGEUP; break;
case 0x71: c = PS2_DELETE; break;
case 0x69: c = PS2_END; break;
case 0x7A: c = PS2_PAGEDOWN; break;
case 0x75: c = PS2_UPARROW; break;
case 0x6B: c = PS2_LEFTARROW; break;
case 0x72: c = PS2_DOWNARROW; break;
case 0x74: c = PS2_RIGHTARROW; break;
case 0x4A: c = '/'; break;
case 0x5A: c = PS2_ENTER; break;
default: break;
}
} else if ((state & ALTGR) && pgm_read_byte(keymap->uses_altgr)) {
if (s < PS2_KEYMAP_SIZE)
c = pgm_read_byte(keymap->altgr + s);
} else if (state & (SHIFT_L | SHIFT_R)) {
if (s < PS2_KEYMAP_SIZE)
c = pgm_read_byte(keymap->shift + s);
} else {
if (s < PS2_KEYMAP_SIZE)
c = pgm_read_byte(keymap->noshift + s);
}
state &= ~(BREAK | MODIFIER);
if (c) return c;
}
}
}
bool PS2Keyboard::available() {
if (CharBuffer || UTF8next) return true;
CharBuffer = get_iso8859_code();
if (CharBuffer) return true;
return false;
}
void PS2Keyboard::clear() {
CharBuffer = 0;
UTF8next = 0;
}
uint8_t PS2Keyboard::readScanCode(void)
{
return get_scan_code();
}
int PS2Keyboard::read() {
uint8_t result;
result = UTF8next;
if (result) {
UTF8next = 0;
} else {
result = CharBuffer;
if (result) {
CharBuffer = 0;
} else {
result = get_iso8859_code();
}
if (result >= 128) {
UTF8next = (result & 0x3F) | 0x80;
result = ((result >> 6) & 0x1F) | 0xC0;
}
}
if (!result) return -1;
return result;
}
int PS2Keyboard::readUnicode() {
int result;
result = CharBuffer;
if (!result) result = get_iso8859_code();
if (!result) return -1;
UTF8next = 0;
CharBuffer = 0;
return result;
}
PS2Keyboard::PS2Keyboard() {
// nothing to do here, begin() does it all
}
void PS2Keyboard::begin(uint8_t data_pin, uint8_t irq_pin, const PS2Keymap_t &map) {
uint8_t irq_num=255;
DataPin = data_pin;
keymap = ↦
// initialize the pins
#ifdef INPUT_PULLUP
pinMode(irq_pin, INPUT_PULLUP);
pinMode(data_pin, INPUT_PULLUP);
#else
pinMode(irq_pin, INPUT);
digitalWrite(irq_pin, HIGH);
pinMode(data_pin, INPUT);
digitalWrite(data_pin, HIGH);
#endif
#ifdef CORE_INT_EVERY_PIN
irq_num = irq_pin;
#else
switch(irq_pin) {
#ifdef CORE_INT0_PIN
case CORE_INT0_PIN:
irq_num = 0;
break;
#endif
#ifdef CORE_INT1_PIN
case CORE_INT1_PIN:
irq_num = 1;
break;
#endif
#ifdef CORE_INT2_PIN
case CORE_INT2_PIN:
irq_num = 2;
break;
#endif
#ifdef CORE_INT3_PIN
case CORE_INT3_PIN:
irq_num = 3;
break;
#endif
#ifdef CORE_INT4_PIN
case CORE_INT4_PIN:
irq_num = 4;
break;
#endif
#ifdef CORE_INT5_PIN
case CORE_INT5_PIN:
irq_num = 5;
break;
#endif
#ifdef CORE_INT6_PIN
case CORE_INT6_PIN:
irq_num = 6;
break;
#endif
#ifdef CORE_INT7_PIN
case CORE_INT7_PIN:
irq_num = 7;
break;
#endif
#ifdef CORE_INT8_PIN
case CORE_INT8_PIN:
irq_num = 8;
break;
#endif
#ifdef CORE_INT9_PIN
case CORE_INT9_PIN:
irq_num = 9;
break;
#endif
#ifdef CORE_INT10_PIN
case CORE_INT10_PIN:
irq_num = 10;
break;
#endif
#ifdef CORE_INT11_PIN
case CORE_INT11_PIN:
irq_num = 11;
break;
#endif
#ifdef CORE_INT12_PIN
case CORE_INT12_PIN:
irq_num = 12;
break;
#endif
#ifdef CORE_INT13_PIN
case CORE_INT13_PIN:
irq_num = 13;
break;
#endif
#ifdef CORE_INT14_PIN
case CORE_INT14_PIN:
irq_num = 14;
break;
#endif
#ifdef CORE_INT15_PIN
case CORE_INT15_PIN:
irq_num = 15;
break;
#endif
#ifdef CORE_INT16_PIN
case CORE_INT16_PIN:
irq_num = 16;
break;
#endif
#ifdef CORE_INT17_PIN
case CORE_INT17_PIN:
irq_num = 17;
break;
#endif
#ifdef CORE_INT18_PIN
case CORE_INT18_PIN:
irq_num = 18;
break;
#endif
#ifdef CORE_INT19_PIN
case CORE_INT19_PIN:
irq_num = 19;
break;
#endif
#ifdef CORE_INT20_PIN
case CORE_INT20_PIN:
irq_num = 20;
break;
#endif
#ifdef CORE_INT21_PIN
case CORE_INT21_PIN:
irq_num = 21;
break;
#endif
#ifdef CORE_INT22_PIN
case CORE_INT22_PIN:
irq_num = 22;
break;
#endif
#ifdef CORE_INT23_PIN
case CORE_INT23_PIN:
irq_num = 23;
break;
#endif
}
#endif
head = 0;
tail = 0;
if (irq_num < 255) {
attachInterrupt(irq_num, ps2interrupt, FALLING);
}
}