-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathKeyboard.c
More file actions
1240 lines (1191 loc) · 46.8 KB
/
Keyboard.c
File metadata and controls
1240 lines (1191 loc) · 46.8 KB
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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***********************************************************************************************************************
PicoMite MMBasic
keyboard.c
<COPYRIGHT HOLDERS> Geoff Graham, Peter Mather
Copyright (c) 2021, <COPYRIGHT HOLDERS> All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the distribution.
3. The name MMBasic be used when referring to the interpreter in any documentation and promotional material and the original copyright message be displayed
on the console at startup (additional copyright messages may be added).
4. All advertising materials mentioning features or use of this software must display the following acknowledgement: This product includes software developed
by the <copyright holder>.
5. Neither the name of the <copyright holder> nor the names of its contributors may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDERS> AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDERS> BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
************************************************************************************************************************/
/*
* Keyboard.c
*
* Created on: 20 Apr 2019
* Author: Peter
*/
/***************************************************************************************************************************
MMBasic
keyboard.c
Does all the hard work in getting data from the PS2 keyboard
Copyright 2011 - 2018 Geoff Graham. All Rights Reserved.
This file and modified versions of this file are supplied to specific individuals or organisations under the following
provisions:
- This file, or any files that comprise the MMBasic source (modified or not), may not be distributed or copied to any other
person or organisation without written permission.
- Object files (.o and .hex files) generated using this file (modified or not) may not be distributed or copied to any other
person or organisation without written permission.
- This file is provided 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.
****************************************************************************************************************************
This routine is based on a technique and code presented by Lucio Di Jasio in his excellent book
"Programming 32-bit Microcontrollers in C - Exploring the PIC32".
Thanks to Muller Fabrice (France), Alberto Leibovich (Argentina) and the other contributors who provided the code for
the non US keyboard layouts
****************************************************************************************************************************/
#include "MMBasic_Includes.h"
#include "Hardware_Includes.h"
void setLEDs(int num, int caps, int scroll);
bool sendCommand(int cmd, int clock, int data);
volatile char CapsLock;
volatile char NumLock;
volatile int setleds = 0;
// PS2 KBD state machine and buffer
static volatile int PS2State;
unsigned char KBDBuf;
static int KCount, KParity;
extern volatile int ConsoleRxBufHead;
extern volatile int ConsoleRxBufTail;
int justset = 0;
// extern char ConsoleRxBuf[];
volatile int KeyDownRegister;
volatile int KeyDownCode;
volatile int PS2code = 0;
volatile bool PS2int = false;
// key codes that must be tracked for up/down state
#define CTRL 0x14 // left and right generate the same code
#define L_SHFT 0x12
#define R_SHFT 0x59
#define CAPS 0x58
#define NUML 0x77
static char LShift = 0;
static char RShift = 0;
static char PS2Ctrl = 0;
static char AltGrDown = 0;
static char PS2Alt = 0;
static char KeyUpCode = false;
static char KeyE0 = false;
// PS2 KEYDOWN() tracking - up to 6 simultaneous keys + modifier bitmask
int PS2KeyDown[7] = {0};
static uint16_t PS2ActiveScancodes[6] = {0};
// this is a map of the keycode characters and the character to be returned for the keycode
const char keyCodes[8][128] =
// US Layout
{
{
// Base 10 Hex
0, F9, 0, F5, F3, F1, F2, F12, // 00-07 00-07
0, F10, F8, F6, F4, TAB, '`', 0, // 08-15 08-0F
0, ALT, L_SHFT, 0, CTRL, 'q', '1', 0, // 16-23 10-07
0, 0, 'z', 's', 'a', 'w', '2', 0, // 24-31 18-1F
0, 'c', 'x', 'd', 'e', '4', '3', 0, // 32-39 20-27
0, ' ', 'v', 'f', 't', 'r', '5', 0, // 40-48 28-2F
0, 'n', 'b', 'h', 'g', 'y', '6', 0, // 48-56 30-37
0, 0, 'm', 'j', 'u', '7', '8', 0, // 56-63 38-3F
0, ',', 'k', 'i', 'o', '0', '9', 0, // 64-71 40-47
0, '.', '/', 'l', ';', 'p', '-', 0, // 72-79 48-4F
0, 0, '\'', 0, '[', '=', 0, 0, // 80-87 50-57
CAPS, R_SHFT, ENTER, ']', 0, 0x5c, 0, 0, // 88-95 58-5F
0, 0, 0, 0, 0, 0, BKSP, 0, // 96-103 60-67
0, '1', 0, '4', '7', 0, 0, 0, // 104-111 68-6F
'0', '.', '2', '5', '6', '8', ESC, NUML, // 112-119 70-77
F11, '+', '3', '-', '*', '9', 0, 0 // 120-127 78-7F
},
// FR Layout
{
// Base 10 Hex
0, F9, 0, F5, F3, F1, F2, F12, // 00-07 00-07
0, F10, F8, F6, F4, TAB, 0, 0, // 08-15 08-0F
0, ALT, L_SHFT, 0, CTRL, 'a', '&', 0, // 16-23 10-07
0, 0, 'w', 's', 'q', 'z', 0, 0, // 24-31 18-1F
0, 'c', 'x', 'd', 'e', '\'', '"', 0, // 32-39 20-27
0, ' ', 'v', 'f', 't', 'r', '(', 0, // 40-48 28-2F
0, 'n', 'b', 'h', 'g', 'y', '-', 0, // 48-56 30-37
0, 0, ',', 'j', 'u', 0, '_', 0, // 56-63 38-3F
0, ';', 'k', 'i', 'o', 0, 0, 0, // 64-71 40-47
0, ':', '!', 'l', 'm', 'p', ')', 0, // 72-79 48-4F
0, 0, 0, 0, '^', '=', 0, 0, // 80-87 50-57
CAPS, R_SHFT, ENTER, '$', 0, '*', 0, 0, // 88-95 58-5F
0, '<', 0, 0, 0, 0, BKSP, 0, // 96-103 60-67
0, '1', 0, '4', '7', 0, 0, 0, // 104-111 68-6F
'0', '.', '2', '5', '6', '8', ESC, NUML, // 112-119 70-77
F11, '+', '3', '-', '*', '9', 0, 0 // 120-127 78-7F
},
// GR Layout
{
// Base 10 Hex
0, F9, 0, F5, F3, F1, F2, F12, // 00-07 00-07
0, F10, F8, F6, F4, TAB, '^', 0, // 08-15 08-0F
0, ALT, L_SHFT, 0, CTRL, 'q', '1', 0, // 16-23 10-07
0, 0, 'y', 's', 'a', 'w', '2', 0, // 24-31 18-1F
0, 'c', 'x', 'd', 'e', '4', '3', 0, // 32-39 20-27
0, ' ', 'v', 'f', 't', 'r', '5', 0, // 40-47 28-2F
0, 'n', 'b', 'h', 'g', 'z', '6', 0, // 48-55 30-37
0, 0, 'm', 'j', 'u', '7', '8', 0, // 56-63 38-3F
0, ',', 'k', 'i', 'o', '0', '9', 0, // 64-71 40-47
0, '.', '-', 'l', 0, 'p', 0, 0, // 72-79 48-4F
0, 0, 0, 0, 0, '\'', 0, 0, // 80-87 50-57
CAPS, R_SHFT, ENTER, '+', 0, '#', 0, 0, // 88-95 58-5F
0, '<', 0, 0, 0, 0, BKSP, 0, // 96-103 60-67
0, '1', 0, '4', '7', 0, 0, 0, // 104-111 68-6F
'0', '.', '2', '5', '6', '8', ESC, NUML, // 112-119 70-77
F11, '+', '3', '-', '*', '9', 0, 0 // 120-127 78-7F
},
// IT Layout
{
// Base 10 Hex
0, F9, 0, F5, F3, F1, F2, F12, // 00-07 00-07
0, F10, F8, F6, F4, TAB, 0x5c, 0, // 08-15 08-0F
0, ALT, L_SHFT, 0, CTRL, 'q', '1', 0, // 16-23 10-07
0, 0, 'z', 's', 'a', 'w', '2', 0, // 24-31 18-1F
0, 'c', 'x', 'd', 'e', '4', '3', 0, // 32-39 20-27
0, ' ', 'v', 'f', 't', 'r', '5', 0, // 40-48 28-2F
0, 'n', 'b', 'h', 'g', 'y', '6', 0, // 48-56 30-37
0, 0, 'm', 'j', 'u', '7', '8', 0, // 56-63 38-3F
0, ',', 'k', 'i', 'o', '0', '9', 0, // 64-71 40-47
0, '.', '-', 'l', 0, 'p', '\'', 0, // 72-79 48-4F
0, 0, 0, 0, 0, 0, 0, 0, // 80-87 50-57
CAPS, R_SHFT, ENTER, '+', 0, 0, 0, 0, // 88-95 58-5F
0, '<', 0, 0, 0, 0, BKSP, 0, // 96-103 60-67
0, '1', 0, '4', '7', 0, 0, 0, // 104-111 68-6F
'0', '.', '2', '5', '6', '8', ESC, NUML, // 112-119 70-77
F11, '+', '3', '-', '*', '9', 0, 0 // 120-127 78-7F
},
// BE Layout
{
// Base 10 Hex
0, F9, 0, F5, F3, F1, F2, F12, // 00-07 00-07
0, F10, F8, F6, F4, TAB, 0, 0, // 08-15 08-0F
0, ALT, L_SHFT, 0, CTRL, 'a', '&', 0, // 16-23 10-07
0, 0, 'w', 's', 'q', 'z', 0, 0, // 24-31 18-1F
0, 'c', 'x', 'd', 'e', '\'', '"', 0, // 32-39 20-27
0, ' ', 'v', 'f', 't', 'r', '(', 0, // 40-48 28-2F
0, 'n', 'b', 'h', 'g', 'y', 0, 0, // 48-56 30-37
0, 0, ',', 'j', 'u', 0, '!', 0, // 56-63 38-3F
0, ';', 'k', 'i', 'o', 0, 0, 0, // 64-71 40-47
0, ':', '=', 'l', 'm', 'p', ')', 0, // 72-79 48-4F
0, 0, 0, 0, '^', '-', 0, 0, // 80-87 50-57
CAPS, R_SHFT, ENTER, '$', 0, 0, 0, 0, // 88-95 58-5F
0, '<', 0, 0, 0, 0, BKSP, 0, // 96-103 60-67
0, '1', 0, '4', '7', 0, 0, 0, // 104-111 68-6F
'0', '.', '2', '5', '6', '8', ESC, NUML, // 112-119 70-77
F11, '+', '3', '-', '*', '9', 0, 0 // 120-127 78-7F
},
// UK Layout
{
// Base 10 Hex
0, F9, 0, F5, F3, F1, F2, F12, // 00-07 00-07
0, F10, F8, F6, F4, TAB, '`', 0, // 08-15 08-0F
0, ALT, L_SHFT, 0x5C, CTRL, 'q', '1', 0, // 16-23 10-07
0, 0, 'z', 's', 'a', 'w', '2', 0, // 24-31 18-1F
0, 'c', 'x', 'd', 'e', '4', '3', 0, // 32-39 20-27
0, ' ', 'v', 'f', 't', 'r', '5', 0, // 40-48 28-2F
0, 'n', 'b', 'h', 'g', 'y', '6', 0, // 48-56 30-37
0, 0, 'm', 'j', 'u', '7', '8', 0, // 56-63 38-3F
0, ',', 'k', 'i', 'o', '0', '9', 0, // 64-71 40-47
0, '.', '/', 'l', ';', 'p', '-', 0, // 72-79 48-4F
0, 0, '\'', 0, '[', '=', 0x5C, 0, // 80-87 50-57
CAPS, R_SHFT, ENTER, ']', '#', '#', 0, 0, // 88-95 58-5F
0, 0x5C, 0, 0, 0, 0, BKSP, 0, // 96-103 60-67
0, '1', 0, '4', '7', 0, 0, 0, // 104-111 68-6F
'0', '.', '2', '5', '6', '8', ESC, NUML, // 112-119 70-77
F11, '+', '3', '-', '*', '9', 0, 0 // 120-127 78-7F
},
// ES Layout
{
// Base 10 Hex
0, F9, 0, F5, F3, F1, F2, F12, // 00-07 00-07
0, F10, F8, F6, F4, TAB, 0x5c, 0, // 08-15 08-0F 0x5C is backslash
0, ALT, L_SHFT, 0, CTRL, 'q', '1', 0, // 16-23 10-07
0, 0, 'z', 's', 'a', 'w', '2', 0, // 24-31 18-1F
0, 'c', 'x', 'd', 'e', '4', '3', 0, // 32-39 20-27
0, ' ', 'v', 'f', 't', 'r', '5', 0, // 40-47 28-2F
0, 'n', 'b', 'h', 'g', 'y', '6', 0, // 48-55 30-37
0, 0, 'm', 'j', 'u', '7', '8', 0, // 56-63 38-3F
0, ',', 'k', 'i', 'o', '0', '9', 0, // 64-71 40-47
0, '.', '-', 'l', 0, 'p', '\'', 0, // 72-79 48-4F
0, 0, '\'', 0, 0x60, 0, 0, 0, // 80-87 50-57 0x60 a single quote
CAPS, R_SHFT, ENTER, '+', 0, 0, 0, 0, // 88-95 58-5F
0, '<', 0, 0, 0, 0, BKSP, 0, // 96-103 60-67
0, '1', 0, '4', '7', 0, 0, 0, // 104-111 68-6F
'0', '.', '2', '5', '6', '8', ESC, NUML, // 112-119 70-77
F11, '+', '3', '-', '*', '9', 0, 0 // 120-127 78-7F
},
// BR Layout
{
// Base 10 Hex
0, F9, 0, F5, F3, F1, F2, F12, // 00-07 00-07
0, F10, F8, F6, F4, TAB, '\'', 0, // 08-15 08-0F
0, ALT, L_SHFT, 0, CTRL, 'q', '1', 0, // 16-23 10-17
0, 0, 'z', 's', 'a', 'w', '2', 0, // 24-31 18-1F
0, 'c', 'x', 'd', 'e', '4', '3', 0, // 32-39 20-27
0, ' ', 'v', 'f', 't', 'r', '5', 0, // 40-48 28-2F
0, 'n', 'b', 'h', 'g', 'y', '6', 0, // 48-56 30-37
0, 0, 'm', 'j', 'u', '7', '8', 0, // 56-63 38-3F
0, ',', 'k', 'i', 'o', '0', '9', 0, // 64-71 40-47
0, '.', ';', 'l', 'c', 'p', '-', 0, // 72-79 48-4F
0, '/', '~', 0, 0, '=', 0, 0, // 80-87 50-57
CAPS, R_SHFT, ENTER, '[', 0, ']', 0, 0, // 88-95 58-5F
0, '\\', 0, 0, 0, 0, BKSP, 0, // 96-103 60-67
0, '1', 0, '4', '7', '.', 0, 0, // 104-111 68-6F
'0', '.', '2', '5', '6', '8', ESC, NUML, // 112-119 70-77
F11, '+', '3', '-', '*', '9', 0, 0 // 120-127 78-7F
}};
// this map is with the shift key pressed
const char keySCodes[8][128] =
// US Layout
{
{
// Base 10 Hex
0, F9, 0, F5, F3, F1, F2, F12, // 00-07 00-07
0, F10, F8, F6, F4, TAB, '~', 0, // 08-15 08-0F
0, ALT, L_SHFT, 0, CTRL, 'Q', '!', 0, // 16-23 10-17
0, 0, 'Z', 'S', 'A', 'W', '@', 0, // 24-31 18-1F
0, 'C', 'X', 'D', 'E', '$', '#', 0, // 32-39 20-27
0, ' ', 'V', 'F', 'T', 'R', '%', 0, // 40-47 28-2F
0, 'N', 'B', 'H', 'G', 'Y', '^', 0, // 48-55 30-37
0, 0, 'M', 'J', 'U', '&', '*', 0, // 56-63 38-3F
0, '<', 'K', 'I', 'O', ')', '(', 0, // 64-71 40-47
0, '>', '?', 'L', ':', 'P', '_', 0, // 72-79 48-4F
0, 0, '\"', 0, '{', '+', 0, 0, // 80-87 50-57
CAPS, R_SHFT, ENTER, '}', 0, '|', 0, 0, // 88-95 58-5F
0, 0, 0, 0, 0, 0, BKSP, 0, // 96-103 60-67
0, '1', 0, '4', '7', 0, 0, 0, // 104-111 68-6F
'0', '.', '2', '5', '6', '8', ESC, NUML, // 112-119 70-77
F11, '+', '3', '-', '*', '9', 0, 0 // 120-127 78-7F
},
// FR Layout
{
// Base 10 Hex
0, F9, 0, F5, F3, F1, F2, F12, // 00-07 00-07
0, F10, F8, F6, F4, TAB, 0, 0, // 08-15 08-0F
0, ALT, L_SHFT, 0, CTRL, 'A', '1', 0, // 16-23 10-17
0, 0, 'W', 'S', 'Q', 'Z', '2', 0, // 24-31 18-1F
0, 'C', 'X', 'D', 'E', '4', '3', 0, // 32-39 20-27
0, ' ', 'V', 'F', 'T', 'R', '5', 0, // 40-47 28-2F
0, 'N', 'B', 'H', 'G', 'Y', '6', 0, // 48-55 30-37
0, 0, '?', 'J', 'U', '7', '8', 0, // 56-63 38-3F
0, '.', 'K', 'I', 'O', '0', '9', 0, // 64-71 40-47
0, '/', 0, 'L', 'M', 'P', 0, 0, // 72-79 48-4F
0, 0, '%', 0, 0, '+', 0, 0, // 80-87 50-57
CAPS, R_SHFT, ENTER, 0, 0, 0, 0, 0, // 88-95 58-5F
0, '>', 0, 0, 0, 0, BKSP, 0, // 96-103 60-67
0, '1', 0, '4', '7', 0, 0, 0, // 104-111 68-6F
'0', '.', '2', '5', '6', '8', ESC, NUML, // 112-119 70-77
F11, '+', '3', '-', '*', '9', 0, 0 // 120-127 78-7F
},
// GR Layout
{
// Base 10 Hex
0, F9, 0, F5, F3, F1, F2, F12, // 00-07 00-07
0, F10, F8, F6, F4, TAB, 0, 0, // 08-15 08-0F
0, ALT, L_SHFT, 0, CTRL, 'Q', '!', 0, // 16-23 10-07
0, 0, 'Y', 'S', 'A', 'W', '\"', 0, // 24-31 18-1F
0, 'C', 'X', 'D', 'E', '$', 0, 0, // 32-39 20-27
0, '\'', 'V', 'F', 'T', 'R', '%', 0, // 40-47 28-2F
0, 'N', 'B', 'H', 'G', 'Z', '&', 0, // 48-55 30-37
0, 0, 'M', 'J', 'U', '/', '(', 0, // 56-63 38-3F
0, ';', 'K', 'I', 'O', '=', ')', 0, // 64-71 40-47
0, ':', '_', 'L', 0, 'P', '?', 0, // 72-79 48-4F
0, 0, 0, 0, 0, 0, 0, 0, // 80-87 50-57
CAPS, R_SHFT, ENTER, '*', 0, 0, 0, 0, // 88-95 58-5F
0, '>', 0, 0, 0, 0, BKSP, 0, // 96-103 60-67
0, '1', 0, '4', '7', 0, 0, 0, // 104-111 68-6F
'0', '.', '2', '5', '6', '8', ESC, NUML, // 112-119 70-77
F11, '+', '3', '-', '*', '9', 0, 0 // 120-127 78-7F
},
// IT Layout
{
// Base 10 Hex
0, F9, 0, F5, F3, F1, F2, F12, // 00-07 00-07
0, F10, F8, F6, F4, TAB, '|', 0, // 08-15 08-0F
0, ALT, L_SHFT, 0, CTRL, 'Q', '!', 0, // 16-23 10-17
0, 0, 'Z', 'S', 'A', 'W', '\"', 0, // 24-31 18-1F
0, 'C', 'X', 'D', 'E', '$', 0, 0, // 32-39 20-27
0, ' ', 'V', 'F', 'T', 'R', '%', 0, // 40-47 28-2F
0, 'N', 'B', 'H', 'G', 'Y', '&', 0, // 48-55 30-37
0, 0, 'M', 'J', 'U', '/', '(', 0, // 56-63 38-3F
0, ';', 'K', 'I', 'O', '=', ')', 0, // 64-71 40-47
0, ':', '_', 'L', 0, 'P', '?', 0, // 72-79 48-4F
0, 0, 0, 0, 0, '^', 0, 0, // 80-87 50-57
CAPS, R_SHFT, ENTER, '*', 0, 0, 0, 0, // 88-95 58-5F
0, '>', 0, 0, 0, 0, BKSP, 0, // 96-103 60-67
0, '1', 0, '4', '7', 0, 0, 0, // 104-111 68-6F
'0', '.', '2', '5', '6', '8', ESC, NUML, // 112-119 70-77
F11, '+', '3', '-', '*', '9', 0, 0 // 120-127 78-7F
},
// BE Layout
{
// Base 10 Hex
0, F9, 0, F5, F3, F1, F2, F12, // 00-07 00-07
0, F10, F8, F6, F4, TAB, 0, 0, // 08-15 08-0F
0, ALT, L_SHFT, 0, CTRL, 'A', '1', 0, // 16-23 10-17
0, 0, 'W', 'S', 'Q', 'Z', '2', 0, // 24-31 18-1F
0, 'C', 'X', 'D', 'E', '4', '3', 0, // 32-39 20-27
0, ' ', 'V', 'F', 'T', 'R', '5', 0, // 40-47 28-2F
0, 'N', 'B', 'H', 'G', 'Y', '6', 0, // 48-55 30-37
0, 0, '?', 'J', 'U', '7', '8', 0, // 56-63 38-3F
0, '.', 'K', 'I', 'O', '0', '9', 0, // 64-71 40-47
0, '/', '+', 'L', 'M', 'P', 0, 0, // 72-79 48-4F
0, 0, '%', 0, 0, '_', 0, 0, // 80-87 50-57
CAPS, R_SHFT, ENTER, '*', 0, 0, 0, 0, // 88-95 58-5F
0, '>', 0, 0, 0, 0, BKSP, 0, // 96-103 60-67
0, '1', 0, '4', '7', 0, 0, 0, // 104-111 68-6F
'0', '.', '2', '5', '6', '8', ESC, NUML, // 112-119 70-77
F11, '+', '3', '-', '*', '9', 0, 0 // 120-127 78-7F
},
// UK Layout
{
// Base 10 Hex
0, F9, 0, F5, F3, F1, F2, F12, // 00-07 00-07
0, F10, F8, F6, F4, TAB, '~', 0, // 08-15 08-0F
0, ALT, L_SHFT, '|', CTRL, 'Q', '!', 0, // 16-23 10-17
0, 0, 'Z', 'S', 'A', 'W', '\"', 0, // 24-31 18-1F
0, 'C', 'X', 'D', 'E', '$', '#', 0, // 32-39 20-27
0, ' ', 'V', 'F', 'T', 'R', '%', 0, // 40-47 28-2F
0, 'N', 'B', 'H', 'G', 'Y', '^', 0, // 48-55 30-37
0, 0, 'M', 'J', 'U', '&', '*', 0, // 56-63 38-3F
0, '<', 'K', 'I', 'O', ')', '(', 0, // 64-71 40-47
0, '>', '?', 'L', ':', 'P', '_', 0, // 72-79 48-4F
0, 0, '@', 0, '{', '+', '|', 0, // 80-87 50-57
CAPS, R_SHFT, ENTER, '}', '~', '~', 0, 0, // 88-95 58-5F
0, '|', 0, 0, 0, 0, BKSP, 0, // 96-103 60-67
0, '1', 0, '4', '7', 0, 0, 0, // 104-111 68-6F
'0', '.', '2', '5', '6', '8', ESC, NUML, // 112-119 70-77
F11, '+', '3', '-', '*', '9', 0, 0 // 120-127 78-7F
},
// ES Layout
{
// Base 10 Hex
0, F9, 0, F5, F3, F1, F2, F12, // 00-07 00-07
0, F10, F8, F6, F4, TAB, 0x5C, 0, // 08-15 08-0F 0x5C is backslash
0, ALT, L_SHFT, 0, CTRL, 'Q', '!', 0, // 16-23 10-17
0, 0, 'Z', 'S', 'A', 'W', '\"', 0, // 24-31 18-1F
0, 'C', 'X', 'D', 'E', '$', 0, 0, // 32-39 20-27
0, ' ', 'V', 'F', 'T', 'R', '%', 0, // 40-47 28-2F
0, 'N', 'B', 'H', 'G', 'Y', '&', 0, // 48-55 30-37
0, 0, 'M', 'J', 'U', '/', '(', 0, // 56-63 38-3F
0, ';', 'K', 'I', 'O', '=', ')', 0, // 64-71 40-47
0, ':', '_', 'L', 0, 'P', '?', 0, // 72-79 48-4F
0, 0, 0, 0, '^', 0, 0, 0, // 80-87 50-57
CAPS, R_SHFT, ENTER, '*', 0, 0, 0, 0, // 88-95 58-5F
0, '>', 0, 0, 0, 0, BKSP, 0, // 96-103 60-67
0, '1', 0, '4', '7', 0, 0, 0, // 104-111 68-6F
'0', '.', '2', '5', '6', '8', ESC, NUML, // 112-119 70-77
F11, '+', '3', '-', '*', '9', 0, 0 // 120-127 78-7F
},
// BR Layout
{
// Base 10 Hex
0, F9, 0, F5, F3, F1, F2, F12, // 00-07 00-07
0, F10, F8, F6, F4, TAB, '\"', 0, // 08-15 08-0F
0, ALT, L_SHFT, CTRL, 'Q', 0, '!', 0, // 16-23 10-17
0, 0, 'Z', 'S', 'A', 'W', '@', 0, // 24-31 18-1F
0, 'C', 'X', 'D', 'E', '$', '#', 0, // 32-39 20-27
0, ' ', 'V', 'F', 'T', 'R', '%', 0, // 40-47 28-2F
0, 'N', 'B', 'H', 'G', 'Y', 0, 0, // 48-55 30-37
0, 0, 'M', 'J', 'U', '&', '*', 0, // 56-63 38-3F
0, '<', 'K', 'I', 'O', ')', '(', 0, // 64-71 40-47
0, '>', ':', 'L', 'C', 'P', '_', 0, // 72-79 48-4F
0, '?', '^', 0, 0, '+', 0, 0, // 80-87 50-57
CAPS, R_SHFT, ENTER, '{', 0, '}', 0, 0, // 88-95 58-5F
0, '|', 0, 0, 0, 0, BKSP, 0, // 96-103 60-67
0, '1', 0, '4', '7', '.', 0, 0, // 104-111 68-6F
'0', '.', '2', '5', '6', '8', ESC, NUML, // 112-119 70-77
F11, '+', '3', '-', '*', '9', 0, 0 // 120-127 78-7F
}};
// this map is for when the keycode preceeded by 0xe0
const char keyE0Codes[56] =
// General Layout on all Keyboard for the Keypad
{
// Base 10
0,
END,
0,
LEFT,
HOME,
0,
0,
0, // 104-111
INSERT,
DEL,
DOWN,
0,
RIGHT,
UP,
ESC,
NUML, // 112-119
F11,
'+',
PDOWN,
'-',
'*',
PUP,
SLOCK,
0, // 120-127
};
const char keyE0Codes_ES[56] =
// Layout for spanish Keyboard for the Keypad
{
// Base 10
0,
END,
0,
LEFT,
HOME,
0,
0,
0, // 104-111
INSERT,
DEL,
DOWN,
PUP,
RIGHT,
UP,
ESC,
NUML, // 112-119
F11,
0,
PDOWN,
0,
0,
0,
SLOCK,
0, // 120-127
};
void KBDIntEnable(int status)
{
PinSetBit(Option.KEYBOARD_CLOCK, TRISSET); // if tris = 1 then it is an input
PinSetBit(Option.KEYBOARD_DATA, TRISSET); // if tris = 1 then it is an input
PinSetBit(Option.KEYBOARD_CLOCK, CNPUSET); // if tris = 1 then it is an input
PinSetBit(Option.KEYBOARD_DATA, CNPUSET); // if tris = 1 then it is an input
if (status)
{
if (!CallBackEnabled)
{
CallBackEnabled = 32;
gpio_set_irq_enabled_with_callback(PinDef[Option.KEYBOARD_CLOCK].GPno, GPIO_IRQ_EDGE_FALL, true, &gpio_callback);
}
else
{
CallBackEnabled |= 32;
gpio_set_irq_enabled(PinDef[Option.KEYBOARD_CLOCK].GPno, GPIO_IRQ_EDGE_FALL, true);
}
}
else
{
if (CallBackEnabled == 32)
gpio_set_irq_enabled_with_callback(PinDef[Option.KEYBOARD_CLOCK].GPno, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, false, &gpio_callback);
else
gpio_set_irq_enabled(PinDef[Option.KEYBOARD_CLOCK].GPno, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, false);
CallBackEnabled &= (~32);
}
}
/***************************************************************************************************
initKeyboard
Initialise the keyboard routine.
****************************************************************************************************/
void initKeyboard(void)
{
// GPIO_InitTypeDef GPIO_InitDef;
if (Option.KeyboardConfig == NO_KEYBOARD || Option.KeyboardConfig >= CONFIG_I2C)
return;
KBDIntEnable(0); // disable interrupt in case called from within CNInterrupt()
// enable pullups on the clock and data lines.
// sendCommand(0xFF);
// HAL_Delay(100);
// setup Change Notification interrupt
KBDIntEnable(1); // enable interrupt
PS2State = PS2START;
CapsLock = Option.capslock;
NumLock = Option.numlock;
uSec(100000);
setLEDs(CapsLock, NumLock, 0);
// Clear any phantom keys from keyboard ACK responses during LED setup
{
int i;
for (i = 0; i < 6; i++)
PS2ActiveScancodes[i] = 0;
for (i = 0; i < 7; i++)
PS2KeyDown[i] = 0;
}
}
/***************************************************************************************************
sendCommand - Send a command to to keyboard.
****************************************************************************************************/
bool sendCommand(int cmd, int clock, int data)
{
int i, j;
// calculate the parity and add to the command as the 9th bit
for (j = i = 0; i < 8; i++)
j += ((cmd >> i) & 1);
cmd = (cmd & 0xff) | (((j + 1) & 1) << 8);
InkeyTimer = 0;
/* while (!PinRead(clock)) //wait for clock high
if (InkeyTimer >= 500)
{ // wait for the keyboard to pull the clock low
return false; // wait for the keyboard to pull the clock low
}*/
PinSetBit(clock, TRISCLR);
PinSetBit(clock, LATCLR);
uSec(250);
PinSetBit(data, TRISCLR);
PinSetBit(data, LATCLR);
uSec(2);
PinSetBit(clock, TRISSET);
uSec(2);
while (PinRead(clock))
if (InkeyTimer >= 500)
{ // wait for the keyboard to pull the clock low
return false; // wait for the keyboard to pull the clock low
}
// send each bit including parity
for (i = 0; i < 9; i++)
{
if (cmd & 1)
{
PinSetBit(data, LATSET);
}
else
{
PinSetBit(data, LATCLR);
}
while (!PinRead(clock))
if (InkeyTimer >= 500)
{ // wait for the keyboard to pull the clock low
return false; // wait for the keyboard to pull the clock low
}
while (PinRead(clock))
if (InkeyTimer >= 500)
{ // wait for the keyboard to pull the clock low
return false; // wait for the keyboard to pull the clock low
}
cmd >>= 1;
}
// PinSetBit(clock, TRISSET);
PinSetBit(data, TRISSET);
while (PinRead(data))
if (InkeyTimer >= 500)
{ // wait for the keyboard to pull the clock low
return false; // wait for the keyboard to pull the clock low
} // wait for the keyboard to pull data low (ACK)
while (PinRead(clock))
if (InkeyTimer >= 500)
{ // wait for the keyboard to pull the clock low
return false; // wait for the keyboard to pull the clock low
} // wait for the clock to go low
while (!(PinRead(clock)) || !(PinRead(data)))
if (InkeyTimer >= 500)
{ // wait for the keyboard to pull the clock low
return false; // wait for the keyboard to pull the clock low
}
return true;
}
// set the keyboard LEDs
void setLEDs(int caps, int num, int scroll)
{
setleds = 0;
KBDIntEnable(0); // disable interrupt while we play
PS2State = PS2START;
sendCommand(0xED, Option.KEYBOARD_CLOCK, Option.KEYBOARD_DATA); // Set/Reset Status Indicators Command
uSec(50000);
sendCommand(((caps & 1) << 2) | ((num & 1) << 1) | (scroll & 1), Option.KEYBOARD_CLOCK, Option.KEYBOARD_DATA); // set the various LEDs
uSec(50000);
sendCommand(0xF3, Option.KEYBOARD_CLOCK, Option.KEYBOARD_DATA); // Set/Reset Status Indicators Command
uSec(50000);
sendCommand(Option.repeat, Option.KEYBOARD_CLOCK, Option.KEYBOARD_DATA);
KBDIntEnable(1); // re enable interrupt
justset = 1;
}
void clearrepeat(void)
{
int i;
for (i = 0; i < 6; i++)
PS2ActiveScancodes[i] = 0;
for (i = 0; i < 7; i++)
PS2KeyDown[i] = 0;
}
void __not_in_flash_func(CheckKeyboard)(void)
{
if (setleds)
{
setLEDs(CapsLock, NumLock, 0);
}
}
void processcode(unsigned char Code)
{
unsigned char c = 0;
// unsigned int dly;
if (!(Code == 0xe0 || Code == 0xf0 || (Code == 0x12 && KeyE0) || (Code == 0x12 && KeyUpCode)) && Code)
{
PS2int = true;
PS2code = Code;
}
if (KeyUpCode)
PS2code |= 0xf000;
if (KeyE0)
PS2code |= 0xe000;
if (KeyE0 && KeyUpCode)
PS2code |= 0xe0f000;
if (Code == 0xaa)
{ // self test code (a keyboard must have just been plugged in)
CapsLock = 0;
NumLock = 1;
} // so initialise it
else if (Code == 0xf0) // a key has been released
KeyUpCode = true;
else if (Code == 0xe0) // extended keycode prefix
KeyE0 = true;
else
{
// Process a scan code from the keyboard into an ASCII character.
// It then inserts the char into the keyboard queue.
// for the US keyboard and the right Alt key we need to make it the same as the left Alt key
if (Option.KeyboardConfig == CONFIG_US && KeyE0 && Code == 0x11)
KeyE0 = false;
// if a key has been released we are only interested in resetting state keys
if (KeyUpCode)
{
if (Code == L_SHFT)
LShift = 0; // left shift button is released
else if (Code == R_SHFT)
RShift = 0; // right shift button is released
else if (Code == CTRL)
PS2Ctrl = 0; // left control button is released
else if (KeyE0 && Code == 0x11)
AltGrDown = 0; // release the AltGr key on non US keyboards
else if (!KeyE0 && Code == 0x11)
PS2Alt = 0; // left alt released
// Remove released key from PS2 active key tracking and compact the array
{
uint16_t encoded = (uint16_t)Code | (KeyE0 ? 0x100 : 0);
for (int i = 0; i < 6; i++)
{
if (PS2ActiveScancodes[i] == encoded)
{
// Shift remaining entries left to fill the gap
for (int j = i; j < 5; j++)
{
PS2ActiveScancodes[j] = PS2ActiveScancodes[j + 1];
PS2KeyDown[j] = PS2KeyDown[j + 1];
}
PS2ActiveScancodes[5] = 0;
PS2KeyDown[5] = 0;
break;
}
}
}
goto SkipOut;
}
// we are only here if the key has been pressed (NOT released)
if (Code == L_SHFT)
{
LShift = 1;
goto SkipOut;
} // left shift button is pressed
if (Code == R_SHFT)
{
RShift = 1;
goto SkipOut;
} // right shift button is pressed
if (Code == CTRL)
{
PS2Ctrl = 1;
goto SkipOut;
} // left control button is pressed
if (Code == CAPS)
{
CapsLock = !CapsLock;
setleds = 1;
// setLEDs(CapsLock, NumLock, 3);
goto SkipOut;
}
if (Code == NUML)
{ // caps or num lock pressed
NumLock = !NumLock;
setleds = 1;
// setLEDs(CapsLock, NumLock, 7);
goto SkipOut;
}
if (KeyE0 && Code == 0x11)
{
AltGrDown = 1;
goto SkipOut;
} // AltGr key pressed on non US Keyboard
if (!KeyE0 && Code == 0x11)
{
PS2Alt = 1;
goto SkipOut;
} // Left Alt key pressed
// now get the character into c. Why, oh why, are scan codes so random?
if (!KeyE0 && Code == 0x83)
c = 0x97; // a special case, function key F7
else if (KeyE0 && Code == 0x4A)
c = '/'; // another special case, this time the keypad forward slash
else if (KeyE0 && Code == 0x5A)
c = NUM_ENT; // yet another special case, this time the keypad enter key
else if ((KeyE0 || !NumLock) && Code >= 104 && Code < 0x80 && !AltGrDown)
{ // a keycode from the numeric keypad
LShift = 0; // when num lock LED is on codes are preceeded by left shift
if (Option.KeyboardConfig == CONFIG_ES)
c = keyE0Codes_ES[Code - 104];
else
c = keyE0Codes[Code - 104];
if (PS2Ctrl)
{ // special for PB
if (c == UP)
c = PUP; // CTRL-UP to page up
if (c == DOWN)
c = PDOWN; // CTRL-DOWN to page down
if (c == LEFT)
c = HOME; // CTRL-LEFT to home
if (c == RIGHT)
c = END; // CTRL-RIGHT to end
}
}
else if ((Code >= 0x15 && Code < 0x62) && AltGrDown != 0) // a keycode preceeded by Alt-Gr
switch (Option.KeyboardConfig)
{ // an international keycode pressed with
case CONFIG_US: // the AltGr key
break; // no code for US keyboard
case CONFIG_FR: // French Keyboard
switch (Code)
{
case 0x45:
c = 0x40; // @
AltGrDown = 0;
break;
case 0x25:
c = 0x7b; // {
AltGrDown = 0;
break;
case 0x2e:
c = 0x5b; // [
AltGrDown = 0;
break;
case 0x55:
c = 0x7d; // }
AltGrDown = 0;
break;
case 0x4e:
c = 0x5d; // ]
AltGrDown = 0;
break;
case 0x3e:
c = 0x5c; // '\'
AltGrDown = 0;
break;
case 0x1e:
c = 0x7e; // ~
AltGrDown = 0;
break;
case 0x36:
c = 0x7c; // |
AltGrDown = 0;
break;
case 0x26:
c = 0x23; // #
AltGrDown = 0;
break;
default:
c = 0; // invalid code
AltGrDown = 0;
break;
}
break;
case CONFIG_GR: // German Keyboard
switch (Code)
{
case 0x15:
c = 0x40; // @
AltGrDown = 0;
break;
case 0x3d:
c = 0x7b; // {
AltGrDown = 0;
break;
case 0x3e:
c = 0x5b; // [
AltGrDown = 0;
break;
case 0x45:
c = 0x7d; // }
AltGrDown = 0;
break;
case 0x46:
c = 0x5d; // ]
AltGrDown = 0;
break;
case 0x4e:
c = 0x5c; // '\'
AltGrDown = 0;
break;
case 0x5b:
c = 0x7e; // ~
AltGrDown = 0;
break;
case 0x61:
c = 0x7c; // |
AltGrDown = 0;
break;
default:
c = 0; // invalid code
AltGrDown = 0;
break;
}
break;
case CONFIG_IT: // Italian Keyboard
switch (Code)
{
case 0x4C:
c = 0x40; // @
AltGrDown = 0;
break;
case 0x54:
c = 0x5b; // [
AltGrDown = 0;
break;
case 0x5b:
c = 0x5d; // ]
AltGrDown = 0;
break;
case 0x52:
c = 0x23; // #
AltGrDown = 0;
break;
default:
c = 0; // invalid code
AltGrDown = 0;
break;
}
break;
case CONFIG_BE: // Belgian Keyboard
switch (Code)
{
case 0x1e:
c = 0x40; // @
AltGrDown = 0;
break;
case 0x46:
c = 0x7b; // {
AltGrDown = 0;
break;
case 0x54:
c = 0x5b; // [
AltGrDown = 0;
break;
case 0x45:
c = 0x7d; // }
AltGrDown = 0;
break;
case 0x5b:
c = 0x5d; // ]
AltGrDown = 0;
break;
case 0x1a:
c = 0x5c; // '\'
AltGrDown = 0;
break;
case 0x4a:
c = 0x7e; // ~
AltGrDown = 0;
break;
case 0x16:
c = 0x7c; // |
AltGrDown = 0;
break;
case 0x26:
c = 0x23; // #
AltGrDown = 0;
break;
default:
c = 0; // invalid code
AltGrDown = 0;
break;
}
break;
case CONFIG_ES: // Spanish Keyboard
switch (Code)
{
case 0x1E:
c = 0x40; // @
AltGrDown = 0;
break;
case 0x54:
c = 0x5b; // [
AltGrDown = 0;
break;
case 0x5b:
c = 0x5d; // ]
AltGrDown = 0;
break;
case 0x26:
c = 0x23; // #
AltGrDown = 0;
break;
case 0x52:
c = 0x7b; // {
AltGrDown = 0;
break;
case 0x5d:
c = 0x7d; // }
AltGrDown = 0;
break;
case 0x16:
c = 0x7c; // |
AltGrDown = 0;
break;
case 0x0e:
c = 0x5c; // '\'
AltGrDown = 0;
break;
case 0x25:
c = 0x7e; // ~
AltGrDown = 0;
break;
default:
c = 0; // invalid code
AltGrDown = 0;
break;
}