-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathkeyboard_i2c.py
157 lines (127 loc) · 3.86 KB
/
keyboard_i2c.py
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
from slime_os.keycode import Keycode
from machine import Pin, I2C
import slime_os.mcp23017 as mcp23017
kbd_map = {
"13x12": Keycode.Q,
"10x4": Keycode.W,
"9x4": Keycode.E,
"4x1": Keycode.T,
"4x2": Keycode.R,
"4x0": Keycode.Y,
"15x4": Keycode.U,
"14x4": Keycode.I,
"13x4": Keycode.O,
"10x9": Keycode.P,
"12x4": Keycode.UP_ARROW,
"10x2": Keycode.A,
"10x1": Keycode.S,
"10x0": Keycode.D,
"15x10": Keycode.F,
"14x10": Keycode.G,
"13x10": Keycode.H,
"9x2": Keycode.J,
"9x1": Keycode.K,
"9x0": Keycode.L,
"15x9": Keycode.BACKSPACE,
"12x2": Keycode.DOWN_ARROW,
"14x9": Keycode.TAB,
"13x9": Keycode.Z,
"2x1": Keycode.X,
"2x0": Keycode.C,
"15x2": Keycode.V,
"14x2": Keycode.B,
"13x2": Keycode.N,
"1x0": Keycode.M,
"15x1": Keycode.KEYPAD_FORWARD_SLASH,
"14x1": Keycode.ENTER,
"12x10": Keycode.LEFT_ARROW,
#weird shift // any 11
#weird alt // any 3 alt
"13x1": 206, #"@",
"15x0": 203,
"14x0": Keycode.SPACE,
"13x0": Keycode.COMMA,
"15x14": Keycode.PERIOD,
#"15x13": [Keycode.PERIOD, Keycode.C, Keycode.O, Keycode.M],
"12x9": Keycode.RIGHT_ARROW,
}
kbd_map_inv = {}
for key,value in kbd_map.items():
r,c = key.split("x")
kbd_map_inv[value] = [int(r), int(c)]
class Keyboard:
def __init__(self, i2c):
self.is_ready = False
try:
mcp = mcp23017.MCP23017(i2c, 0x20)
self.is_ready=True
except OSError as e:
print("[KBD].error:", e)
self.is_ready=False
self.held = {}
for key in kbd_map:
self.held[key] = False
self.rows = {}
used = {}
for key in kbd_map:
[row, col] = key.split("x")
row = int(row)
col = int(col)
cols = []
if row in self.rows:
cols = self.rows[row]
else:
self.rows[row] = cols
cols.append(col)
used[row] = True
used[col] = True
if self.is_ready:
self.pins = {}
for p in used.keys():
self.pins[p] = mcp[p]
self.pins[p].input(pull=1)
self.row_keys = list(self.rows.keys())
self.row_keys.sort()
for row in self.rows:
self.rows[row].sort()
print("[KBD].ready")
else:
print("[KBD].not_ready")
def get_key(self, key):
row,col = kbd_map_inv[key]
high_pin = self.pins[row]
high_pin.output(0)
read_pin = self.pins[col]
key = "{0}x{1}".format(row, col)
pressed = False
if read_pin.value() == False:
if not self.held[key]:
pressed = True
self.held[key] = True
else:
self.held[key] = False
high_pin.input(pull=1)
return pressed
def get_keys(self, keys):
results = {}
for key in keys:
results[key] = self.get_key(key)
return results
def get_all(self):
keys = []
if not self.is_ready:
return keys
for pidx in self.row_keys:
high_pin = self.pins[pidx]
high_pin.output(0)
for rpidx in self.rows[pidx]:
read_pin = self.pins[rpidx]
key = "{0}x{1}".format(pidx, rpidx)
if read_pin.value() == False:
if not self.held[key]:
keys.append(kbd_map[key])
self.held[key] = True
else:
self.held[key] = False
high_pin.input(pull=1)
return keys