-
Notifications
You must be signed in to change notification settings - Fork 4
/
ukkbd.cpp
131 lines (119 loc) · 3.89 KB
/
ukkbd.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
#if defined(ARDUINO)
#include <Arduino.h>
#endif
#include <stdint.h>
#include <memory.h>
#include <ps2_raw_kbd.h>
#include "ukkbd.h"
// maps scan codes to uk101 rows/cols + left-shift (where applicable)
static const uint16_t scanmap[128] = {
0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, // 0x00
0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, // 0x08
0xffff, 0xffff, 0x0002, 0xffff, 0x0006, 0x0017, 0x0077, 0xffff, // 0x10
0xffff, 0xffff, 0x0015, 0x0037, 0x0016, 0x0047, 0x0076, 0xffff, // 0x18
0xffff, 0x0026, 0x0027, 0x0036, 0x0046, 0x0074, 0x0075, 0xffff, // 0x20
0xffff, 0x0014, 0x0025, 0x0035, 0x0044, 0x0045, 0x0073, 0xffff, // 0x28
0xffff, 0x0023, 0x0024, 0x0033, 0x0034, 0x0043, 0x0072, 0xffff, // 0x30
0xffff, 0xffff, 0x0022, 0x0032, 0x0042, 0x0071, 0x0067, 0xffff, // 0x38
0xffff, 0x0021, 0x0031, 0x0041, 0x0055, 0x0065, 0x0066, 0xffff, // 0x40
0xffff, 0x0057, 0x0013, 0x0056, 0x0012, 0x0011, 0x0063, 0xffff, // 0x48
0xffff, 0xffff, 0x0271, 0xffff, 0x0231, 0x0263, 0xffff, 0xffff, // 0x50
0x0000, 0x0001, 0x0053, 0x0222, 0xffff, 0x0275, 0xffff, 0xffff, // 0x58
0xffff, 0x0256, 0xffff, 0xffff, 0xffff, 0xffff, 0x0062, 0xffff, // 0x60
0xffff, 0xffff, 0xffff, 0x0616, 0xffff, 0xffff, 0xffff, 0xffff, // 0x68
0x0617, 0xffff, 0x0635, 0xffff, 0x0636, 0x0637, 0x0005, 0x0646, // 0x70
0xffff, 0x0212, 0xffff, 0x0063, 0x0264, 0x0013, 0xffff, 0xffff, // 0x78
};
static const uint16_t shiftmap[128] = {
0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, // 0x00
0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, // 0x08
0xffff, 0xffff, 0x0002, 0xffff, 0xffff, 0xffff, 0x0277, 0xffff, // 0x10
0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0x0276, 0xffff, // 0x18
0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0x0274, 0x0000, 0xffff, // 0x20
0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0x0273, 0xffff, // 0x28
0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0x0054, 0xffff, // 0x30
0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0x0272, 0x0264, 0xffff, // 0x38
0xffff, 0x0221, 0xffff, 0xffff, 0xffff, 0x0266, 0x0267, 0xffff, // 0x40
0xffff, 0x0257, 0x0213, 0xffff, 0x0064, 0xffff, 0x0000, 0xffff, // 0x48
0xffff, 0xffff, 0x0211, 0xffff, 0xffff, 0x0212, 0xffff, 0xffff, // 0x50
0xffff, 0x0001, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, // 0x58
0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, // 0x60
0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, // 0x68
0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, // 0x70
0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, // 0x78
};
uint16_t ukkbd::_map(uint8_t scan)
{
if (_shifted) {
uint16_t sh = shiftmap[scan];
if (sh == 0x0000)
return 0xffff;
if (sh != 0xffff)
return sh;
}
return scanmap[scan];
}
void ukkbd::reset() {
for (int i=8; i--; )
_rows[i] = 0;
// shift-lock on
_rows[0] = 1;
_shifted = false;
}
void ukkbd::operator=(uint8_t row) {
_last = row;
}
uint8_t ukkbd::pattern() {
int r = 255-_last;
uint8_t pattern = 0;
for (int i=8; i--; r>>=1)
if (r & 1)
pattern |= _rows[7-i];
return pattern ^ 0xff;
}
void ukkbd::_reset(uint8_t k) {
_rows[(k & 0xf0) >> 4] &= ~(1 << (k & 0x0f));
}
void ukkbd::up(uint8_t scan) {
if (scan > 127) {
reset();
return;
}
if (is_ps2_shift(scan))
_shifted = false;
uint16_t k = _map(scan);
if (k != 0xffff) {
if (!_shifted && k > 0xff)
_reset (k / 0xff);
if (k > 0)
_reset(k & 0xff);
}
}
void ukkbd::_set(uint8_t k) {
_rows[(k & 0xf0) >> 4] |= 1 << (k & 0x0f);
}
void ukkbd::down(uint8_t scan) {
if (scan > 127) {
reset();
return;
}
if (is_ps2_shift(scan)) {
// reset any depressed keys so they don't get stuck
reset();
_shifted = true;
}
uint16_t k = _map(scan);
if (k == 0xffff) {
reset();
return;
}
if (_shifted) {
if (k < 0xff)
_reset(0x02);
} else if (k > 0xff)
_set(k / 0xff);
if (k > 0)
_set(k & 0xff);
else // toggle caps lock
_rows[0] ^= 1;
}