@@ -94,6 +94,9 @@ def get(self):
94
94
def preview (self , n = 0 ):
95
95
return self .queue [(self .tail + n ) % self .size ]
96
96
97
+ def __getitem__ (self , n ):
98
+ return self .queue [(self .tail + n ) % self .size ]
99
+
97
100
def __len__ (self ):
98
101
length = self .head - self .tail
99
102
return length if length >= 0 else length + self .size
@@ -119,19 +122,19 @@ def setup(self):
119
122
self .keys = [0 ] * n
120
123
121
124
# convert pykey to pycode (unicode string)
122
- def pykeycode (x ):
125
+ def action_unicode (x ):
123
126
if type (x ) is int :
124
127
return chr (x ) if x > 9 else ASCII_TO_KEYCODE [ord (str (x ))]
125
128
if type (x ) is str and len (x ) == 1 :
126
129
return ASCII_TO_KEYCODE [ord (str (x ))]
127
- raise ValueError ('Invalid keycode or keyname {}' .format (x ))
130
+ raise ValueError ('Invalid keyname {}' .format (x ))
128
131
129
- concat = lambda * a : '' .join ((pykeycode (x ) for x in a ))
132
+ concat = lambda * a : '' .join ((action_unicode (x ) for x in a ))
130
133
131
- self ._keymap = tuple (concat (* layer ) for layer in self .keymap )
134
+ self .unicode_keymap = tuple (concat (* layer ) for layer in self .keymap )
132
135
133
136
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 ()))
135
138
136
139
def get_coord (x ): return self .coords [self .keymap [0 ].index (x )]
137
140
@@ -142,20 +145,20 @@ def get_mask(x):
142
145
self .pair_keys_mask = tuple (map (get_mask , self .pair_keys ))
143
146
# print([hex(x) for x in self.pair_keys_mask])
144
147
145
- self .ro = [] # row as output
148
+ self .rows_io = [] # row as output
146
149
for pin in self .rows :
147
150
io = digitalio .DigitalInOut (pin )
148
151
io .direction = digitalio .Direction .OUTPUT
149
152
io .drive_mode = digitalio .DriveMode .PUSH_PULL
150
153
io .value = 0
151
- self .ro .append (io )
154
+ self .rows_io .append (io )
152
155
153
- self .ci = [] # col as input
156
+ self .cols_io = [] # col as input
154
157
for pin in self .cols :
155
158
io = digitalio .DigitalInOut (pin )
156
159
io .direction = digitalio .Direction .INPUT
157
160
io .pull = digitalio .Pull .DOWN if self .row2col else digitalio .Pull .UP
158
- self .ci .append (io )
161
+ self .cols_io .append (io )
159
162
160
163
# row selected value depends on diodes' direction
161
164
self .selected_value = bool (self .row2col )
@@ -164,40 +167,40 @@ def scan(self):
164
167
self .scan_time = time .monotonic_ns ()
165
168
pressed_mask = 0
166
169
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
171
174
key_mask = 1 << key_index
172
- if i .value == self .selected_value :
175
+ if col_io .value == self .selected_value :
173
176
pressed_mask |= key_mask
174
177
n_pressed += 1
175
- if not (self .pressed_mask & ( 1 << key_index ) ):
178
+ if not (self .pressed_mask & key_mask ):
176
179
self .pressed_time [key_index ] = self .scan_time
177
180
self .queue .put (key_index )
178
- elif self .pressed_mask & ( 1 << key_index ) :
181
+ elif self .pressed_mask & key_mask :
179
182
self .queue .put (0x80 | key_index )
180
183
181
- o .value = not self .selected_value
184
+ row_io .value = not self .selected_value
182
185
self .pressed_mask = pressed_mask
183
186
self .pressed_count = n_pressed
184
187
185
188
return len (self .queue )
186
189
187
- def wait (self , n_events = 1 , until = None ):
190
+ def wait (self , n_events = 1 , end_time = None ):
188
191
while True :
189
192
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 ):
191
194
return n
192
195
193
196
self .scan ()
194
197
195
- def keycode (self , position ):
198
+ def action_code (self , position ):
196
199
position = self .coords [position ]
197
200
198
- for layer in range (len (self ._keymap ) - 1 , - 1 , - 1 ):
201
+ for layer in range (len (self .unicode_keymap ) - 1 , - 1 , - 1 ):
199
202
if (self .layers >> layer ) & 1 :
200
- code = self ._keymap [layer ][position ]
203
+ code = self .unicode_keymap [layer ][position ]
201
204
if code == TRANSPARENT :
202
205
continue
203
206
return ord (code )
@@ -253,7 +256,7 @@ def release(code):
253
256
mask = 1 << self .queue .preview (0 ) | 1 << self .queue .preview (1 )
254
257
if mask in self .pair_keys_mask :
255
258
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 ]
257
260
key1 = self .queue .get ()
258
261
key2 = self .queue .get ()
259
262
dt = self .pressed_time [key2 ] - self .pressed_time [key1 ]
@@ -264,19 +267,19 @@ def release(code):
264
267
dt // 1000000 ))
265
268
266
269
# only one action
267
- self .keys [key1 ] = keycode
270
+ self .keys [key1 ] = action_code
268
271
self .keys [key2 ] = 0
269
272
270
- if keycode < 2 :
273
+ if action_code < 2 :
271
274
pass
272
- elif keycode < 0xFF :
273
- press (keycode )
275
+ elif action_code < 0xFF :
276
+ press (action_code )
274
277
else :
275
- kind = keycode >> 12
276
- layer = ((keycode >> 8 ) & 0xF )
278
+ kind = action_code >> 12
279
+ layer = ((action_code >> 8 ) & 0xF )
277
280
if kind < (ACT_MODS_TAP + 1 ):
278
281
# todo
279
- mods = (keycode >> 8 ) & 0x1F
282
+ mods = (action_code >> 8 ) & 0x1F
280
283
elif kind == ACT_LAYER_TAP :
281
284
self .layers |= 1 << layer
282
285
print ('layers {}' .format (self .layers ))
@@ -285,38 +288,38 @@ def release(code):
285
288
event = self .queue .get ()
286
289
key = event & 0x7F
287
290
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 :
292
295
pass
293
- elif keycode < 0xFF :
294
- press (keycode )
296
+ elif action_code < 0xFF :
297
+ press (action_code )
295
298
else :
296
- kind = keycode >> 12
297
- layer = ((keycode >> 8 ) & 0xF )
299
+ kind = action_code >> 12
300
+ layer = ((action_code >> 8 ) & 0xF )
298
301
if kind == ACT_LAYER_TAP :
299
302
self .layers |= 1 << layer
300
303
print ('layers {}' .format (self .layers ))
301
- elif keycode == BOOTLOADER :
304
+ elif action_code == BOOTLOADER :
302
305
reset_into_bootloader ()
303
306
else :
304
- keycode = self .keys [key ]
307
+ action_code = self .keys [key ]
305
308
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 :
308
311
pass
309
- if keycode < 0xFF :
310
- release (keycode )
312
+ elif action_code < 0xFF :
313
+ release (action_code )
311
314
else :
312
- kind = keycode >> 12
313
- layer = ((keycode >> 8 ) & 0xF )
315
+ kind = action_code >> 12
316
+ layer = ((action_code >> 8 ) & 0xF )
314
317
if kind == ACT_LAYER_TAP :
315
318
self .layers &= ~ (1 << layer )
316
319
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 )
320
323
321
324
if not ble .connected and not ble .advertising :
322
325
ble .start_advertising (advertisement )
0 commit comments