Skip to content

Commit 2cb6a0a

Browse files
committed
make var names easy to understand
1 parent 9b80d90 commit 2cb6a0a

File tree

1 file changed

+52
-49
lines changed

1 file changed

+52
-49
lines changed

keyboard.py

Lines changed: 52 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ def get(self):
9494
def preview(self, n=0):
9595
return self.queue[(self.tail + n) % self.size]
9696

97+
def __getitem__(self, n):
98+
return self.queue[(self.tail + n) % self.size]
99+
97100
def __len__(self):
98101
length = self.head - self.tail
99102
return length if length >= 0 else length + self.size
@@ -119,19 +122,19 @@ def setup(self):
119122
self.keys = [0] * n
120123

121124
# convert pykey to pycode (unicode string)
122-
def pykeycode(x):
125+
def action_unicode(x):
123126
if type(x) is int:
124127
return chr(x) if x > 9 else ASCII_TO_KEYCODE[ord(str(x))]
125128
if type(x) is str and len(x) == 1:
126129
return ASCII_TO_KEYCODE[ord(str(x))]
127-
raise ValueError('Invalid keycode or keyname {}'.format(x))
130+
raise ValueError('Invalid keyname {}'.format(x))
128131

129-
concat = lambda *a: ''.join((pykeycode(x) for x in a))
132+
concat = lambda *a: ''.join((action_unicode(x) for x in a))
130133

131-
self._keymap = tuple(concat(*layer) for layer in self.keymap)
134+
self.unicode_keymap = tuple(concat(*layer) for layer in self.keymap)
132135

133136
self.pair_keys_code = tuple(
134-
map(lambda x: ord(pykeycode(x)), self.pair_keys.keys()))
137+
map(lambda x: ord(action_unicode(x)), self.pair_keys.keys()))
135138

136139
def get_coord(x): return self.coords[self.keymap[0].index(x)]
137140

@@ -142,20 +145,20 @@ def get_mask(x):
142145
self.pair_keys_mask = tuple(map(get_mask, self.pair_keys))
143146
# print([hex(x) for x in self.pair_keys_mask])
144147

145-
self.ro = [] # row as output
148+
self.rows_io = [] # row as output
146149
for pin in self.rows:
147150
io = digitalio.DigitalInOut(pin)
148151
io.direction = digitalio.Direction.OUTPUT
149152
io.drive_mode = digitalio.DriveMode.PUSH_PULL
150153
io.value = 0
151-
self.ro.append(io)
154+
self.rows_io.append(io)
152155

153-
self.ci = [] # col as input
156+
self.cols_io = [] # col as input
154157
for pin in self.cols:
155158
io = digitalio.DigitalInOut(pin)
156159
io.direction = digitalio.Direction.INPUT
157160
io.pull = digitalio.Pull.DOWN if self.row2col else digitalio.Pull.UP
158-
self.ci.append(io)
161+
self.cols_io.append(io)
159162

160163
# row selected value depends on diodes' direction
161164
self.selected_value = bool(self.row2col)
@@ -164,40 +167,40 @@ def scan(self):
164167
self.scan_time = time.monotonic_ns()
165168
pressed_mask = 0
166169
n_pressed = 0
167-
for r, o in enumerate(self.ro):
168-
o.value = self.selected_value # select row
169-
for c, i in enumerate(self.ci):
170-
key_index = r * len(self.ci) + c
170+
for row, row_io in enumerate(self.rows_io):
171+
row_io.value = self.selected_value # select row
172+
for col, col_io in enumerate(self.cols_io):
173+
key_index = row * len(self.cols_io) + col
171174
key_mask = 1 << key_index
172-
if i.value == self.selected_value:
175+
if col_io.value == self.selected_value:
173176
pressed_mask |= key_mask
174177
n_pressed += 1
175-
if not (self.pressed_mask & (1 << key_index)):
178+
if not (self.pressed_mask & key_mask):
176179
self.pressed_time[key_index] = self.scan_time
177180
self.queue.put(key_index)
178-
elif self.pressed_mask & (1 << key_index):
181+
elif self.pressed_mask & key_mask:
179182
self.queue.put(0x80 | key_index)
180183

181-
o.value = not self.selected_value
184+
row_io.value = not self.selected_value
182185
self.pressed_mask = pressed_mask
183186
self.pressed_count = n_pressed
184187

185188
return len(self.queue)
186189

187-
def wait(self, n_events=1, until=None):
190+
def wait(self, n_events=1, end_time=None):
188191
while True:
189192
n = len(self.queue)
190-
if n >= n_events or (until and self.scan_time > until):
193+
if n >= n_events or (end_time and self.scan_time > end_time):
191194
return n
192195

193196
self.scan()
194197

195-
def keycode(self, position):
198+
def action_code(self, position):
196199
position = self.coords[position]
197200

198-
for layer in range(len(self._keymap) - 1, -1, -1):
201+
for layer in range(len(self.unicode_keymap) - 1, -1, -1):
199202
if (self.layers >> layer) & 1:
200-
code = self._keymap[layer][position]
203+
code = self.unicode_keymap[layer][position]
201204
if code == TRANSPARENT:
202205
continue
203206
return ord(code)
@@ -253,7 +256,7 @@ def release(code):
253256
mask = 1 << self.queue.preview(0) | 1 << self.queue.preview(1)
254257
if mask in self.pair_keys_mask:
255258
pair_keys_index = self.pair_keys_mask.index(mask)
256-
keycode = self.pair_keys_code[pair_keys_index]
259+
action_code = self.pair_keys_code[pair_keys_index]
257260
key1 = self.queue.get()
258261
key2 = self.queue.get()
259262
dt = self.pressed_time[key2] - self.pressed_time[key1]
@@ -264,19 +267,19 @@ def release(code):
264267
dt // 1000000))
265268

266269
# only one action
267-
self.keys[key1] = keycode
270+
self.keys[key1] = action_code
268271
self.keys[key2] = 0
269272

270-
if keycode < 2:
273+
if action_code < 2:
271274
pass
272-
elif keycode < 0xFF:
273-
press(keycode)
275+
elif action_code < 0xFF:
276+
press(action_code)
274277
else:
275-
kind = keycode >> 12
276-
layer = ((keycode >> 8) & 0xF)
278+
kind = action_code >> 12
279+
layer = ((action_code >> 8) & 0xF)
277280
if kind < (ACT_MODS_TAP + 1):
278281
# todo
279-
mods = (keycode >> 8) & 0x1F
282+
mods = (action_code >> 8) & 0x1F
280283
elif kind == ACT_LAYER_TAP:
281284
self.layers |= 1 << layer
282285
print('layers {}'.format(self.layers))
@@ -285,38 +288,38 @@ def release(code):
285288
event = self.queue.get()
286289
key = event & 0x7F
287290
if event & 0x80 == 0:
288-
keycode = self.keycode(key)
289-
self.keys[key] = keycode
290-
print('{} / keycode = {}'.format(key, keycode))
291-
if keycode < 2:
291+
action_code = self.action_code(key)
292+
self.keys[key] = action_code
293+
print('{} / action_code = {}'.format(key, action_code))
294+
if action_code < 2:
292295
pass
293-
elif keycode < 0xFF:
294-
press(keycode)
296+
elif action_code < 0xFF:
297+
press(action_code)
295298
else:
296-
kind = keycode >> 12
297-
layer = ((keycode >> 8) & 0xF)
299+
kind = action_code >> 12
300+
layer = ((action_code >> 8) & 0xF)
298301
if kind == ACT_LAYER_TAP:
299302
self.layers |= 1 << layer
300303
print('layers {}'.format(self.layers))
301-
elif keycode == BOOTLOADER:
304+
elif action_code == BOOTLOADER:
302305
reset_into_bootloader()
303306
else:
304-
keycode = self.keys[key]
307+
action_code = self.keys[key]
305308
dt = (self.scan_time - self.pressed_time[key]) // 1000000
306-
print('{} \\ keycode = {}, dt = {}'.format(key, keycode, dt))
307-
if keycode < 2:
309+
print('{} \\ action_code = {}, dt = {}'.format(key, action_code, dt))
310+
if action_code < 2:
308311
pass
309-
if keycode < 0xFF:
310-
release(keycode)
312+
elif action_code < 0xFF:
313+
release(action_code)
311314
else:
312-
kind = keycode >> 12
313-
layer = ((keycode >> 8) & 0xF)
315+
kind = action_code >> 12
316+
layer = ((action_code >> 8) & 0xF)
314317
if kind == ACT_LAYER_TAP:
315318
self.layers &= ~(1 << layer)
316319
print('layers {}'.format(self.layers))
317-
code = keycode & 0xFF
318-
if dt < 500 and code:
319-
send(code)
320+
keycode = action_code & 0xFF
321+
if dt < 500 and keycode:
322+
send(keycode)
320323

321324
if not ble.connected and not ble.advertising:
322325
ble.start_advertising(advertisement)

0 commit comments

Comments
 (0)