17
17
from .action_code import *
18
18
19
19
20
- ___ = TRANSPARENT
21
- BOOT = BOOTLOADER
22
- L1 = LAYER_TAP (1 )
23
- L2D = LAYER_TAP (2 , D )
24
- L3B = LAYER_TAP (3 , B )
25
-
26
- # Semicolon & Ctrl
27
- SCC = MODS_TAP (MODS (RCTRL ), ";" )
28
-
29
20
# fmt: off
30
- KEYMAP = (
31
- # layer 0
32
- (
33
- ESC , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 , '-' , '=' , BACKSPACE ,
34
- TAB , Q , W , E , R , T , Y , U , I , O , P , '[' , ']' , '|' ,
35
- CAPS , A , S , D , F , G , H , J , K , L , SCC , '"' , ENTER ,
36
- LSHIFT ,Z , X , C , V , L3B , N , M , ',' , '.' , '/' , RSHIFT ,
37
- LCTRL , LGUI , LALT , SPACE , RALT , MENU , L1 , RCTRL
38
- ),
39
-
40
- # layer 1
41
- (
42
- '`' , F1 , F2 , F3 , F4 , F5 , F6 , F7 , F8 , F9 , F10 , F11 , F12 , DEL ,
43
- ___ , ___ , UP , ___ , ___ , ___ , ___ , ___ , ___ , ___ , ___ , ___ , ___ , ___ ,
44
- ___ ,LEFT ,DOWN ,RIGHT ,___ , ___ , ___ , ___ , ___ , ___ , ___ , ___ , ___ ,
45
- ___ , ___ , ___ , ___ , ___ ,BOOT , ___ , ___ , ___ , ___ , ___ , ___ ,
46
- ___ , ___ , ___ , ___ , ___ , ___ , ___ , ___
47
- ),
48
-
49
- # layer 2
50
- (
51
- '`' , F1 , F2 , F3 , F4 , F5 , F6 , F7 , F8 , F9 , F10 , F11 , F12 , DEL ,
52
- ___ , ___ , ___ , ___ , ___ , ___ , ___ ,PGUP , ___ , ___ , ___ , ___ , ___ , ___ ,
53
- ___ , ___ , ___ , ___ , ___ , ___ ,LEFT ,DOWN , UP ,RIGHT , ___ , ___ , ___ ,
54
- ___ , ___ , ___ , ___ , ___ , ___ ,PGDN , ___ , ___ , ___ , ___ , ___ ,
55
- ___ , ___ , ___ , ___ , ___ , ___ , ___ , ___
56
- ),
57
-
58
- # layer 3
59
- (
60
- BT_TOGGLE ,BT1 ,BT2 , BT3 ,BT4 ,BT5 ,BT6 ,BT7 , BT8 , BT9 , BT0 , ___ , ___ , ___ ,
61
- ___ , ___ , ___ , ___ , ___ , ___ , ___ , ___ , ___ , ___ , ___ , ___ , ___ , ___ ,
62
- ___ , ___ , ___ , ___ , ___ , ___ , ___ , ___ , ___ , ___ , ___ , ___ , ___ ,
63
- ___ , ___ , ___ , ___ , ___ , ___ , ___ , ___ , ___ , ___ , ___ , ___ ,
64
- ___ , ___ , ___ , ___ , ___ , ___ , ___ , ___
65
- ),
66
- )
67
-
68
21
KEY_NAME = (
69
22
'ESC' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '0' , '-' , '=' , 'BACKSPACE' ,
70
23
'|' , ']' , '[' , 'P' , 'O' , 'I' , 'U' , 'Y' , 'T' , 'R' , 'E' , 'W' , 'Q' , 'TAB' , 'CAPS' ,
@@ -131,14 +84,14 @@ def send_text(self, text):
131
84
132
85
133
86
class Keyboard :
134
- def __init__ (self , keymap = KEYMAP , pairs = (), verbose = True ):
135
- self .keymap = KEYMAP
87
+ def __init__ (self , keymap = () , pairs = (), verbose = True ):
88
+ self .keymap = keymap
136
89
self .profiles = {}
137
90
self .pairs = pairs
138
91
self .verbose = verbose
139
- self .pairs_handler = None
92
+ self .pairs_handler = lambda dev , n : None
140
93
self .pair_keys = set ()
141
- self .macro_handler = None
94
+ self .macro_handler = lambda dev , key , pressed : None
142
95
self .layer_mask = 1
143
96
self .matrix = Matrix ()
144
97
self .backlight = Backlight ()
@@ -149,7 +102,7 @@ def __init__(self, keymap=KEYMAP, pairs=(), verbose=True):
149
102
self .fast_type_thresh = 200
150
103
self .pair_delay = 10
151
104
152
- self ._current_conn = ""
105
+ self ._connection = ""
153
106
154
107
self .data = array .array ("L" , microcontroller .nvm [:272 ])
155
108
if self .data [0 ] != 0x424B5950 :
@@ -171,18 +124,18 @@ def __init__(self, keymap=KEYMAP, pairs=(), verbose=True):
171
124
self .ble_hid = HID (ble_hid .devices )
172
125
self .usb_hid = HID (usb_hid .devices )
173
126
174
- def update_current_conn (self ):
127
+ def update_connection (self ):
175
128
if usb_is_connected () and self .usb_status == 3 :
176
129
conn = "USB"
177
130
else :
178
131
conn = "BT%d" % self .ble_id
179
- if conn != self ._current_conn :
180
- self ._current_conn = conn
132
+ if conn != self ._connection :
133
+ self ._connection = conn
181
134
if conn in self .action_maps :
182
- self .current_keymap = self .action_maps [self ._current_conn ]
135
+ self .current_keymap = self .action_maps [self ._connection ]
183
136
else :
184
137
self .current_keymap = self .actonmap
185
- print ("Current connection changed to %s" % self ._current_conn )
138
+ print ("Connection changed to %s" % self ._connection )
186
139
187
140
# reset `layer_mask` when keymap is changed
188
141
self .layer_mask = 1
@@ -220,7 +173,7 @@ def check(self):
220
173
self .leds = leds
221
174
self .backlight .set_hid_leds (leds )
222
175
self .log ("keyboard leds {}" .format (bin (leds )))
223
- self .update_current_conn ()
176
+ self .update_connection ()
224
177
225
178
# update battery level
226
179
if time .time () > self .battery_update_time :
@@ -240,7 +193,7 @@ def setup(self):
240
193
for pair in self .pairs :
241
194
for key in pair :
242
195
self .pair_keys .add (key )
243
- self .update_current_conn ()
196
+ self .update_connection ()
244
197
245
198
def start_advertising (self ):
246
199
self .ble .start_advertising (self .advertisement )
@@ -262,22 +215,22 @@ def get_key_sequence_info(self, start, end):
262
215
key = event & 0x7F
263
216
desc = KEY_NAME [key ]
264
217
if event < 0x80 :
265
- desc += ' \\ '
218
+ desc += " \\ "
266
219
t0 = matrix .get_keydown_time (key )
267
220
else :
268
- desc += ' / '
221
+ desc += " / "
269
222
t0 = matrix .get_keyup_time (key )
270
-
223
+
271
224
t = []
272
225
for i in range (start , end ):
273
226
event = matrix .view (i )
274
227
key = event & 0x7F
275
228
desc += KEY_NAME [key ]
276
229
if event < 0x80 :
277
- desc += ' \\ '
230
+ desc += " \\ "
278
231
t1 = matrix .get_keydown_time (key )
279
232
else :
280
- desc += ' / '
233
+ desc += " / "
281
234
t1 = matrix .get_keyup_time (key )
282
235
dt = matrix .ms (t1 - t0 )
283
236
t0 = t1
@@ -372,15 +325,15 @@ def toggle_bt(self):
372
325
self .stop_advertising ()
373
326
else :
374
327
self .start_advertising ()
375
- self .update_current_conn ()
328
+ self .update_connection ()
376
329
377
330
def toggle_usb (self ):
378
331
if usb_is_connected ():
379
332
if self .usb_status == 1 :
380
333
self .usb_status = 3
381
334
else :
382
335
self .usb_status = 1
383
- self .update_current_conn ()
336
+ self .update_connection ()
384
337
385
338
def action_code (self , position ):
386
339
position = COORDS [position ]
@@ -485,12 +438,10 @@ def run(self):
485
438
matrix .get_keydown_time (key2 ) - matrix .get_keydown_time (key1 )
486
439
)
487
440
log ("pair keys {} {}, dt = {}" .format (pair_index , pair , dt ))
488
- if callable (self .pairs_handler ):
489
- try :
490
- self .pairs_handler (dev , pair_index )
491
- except Exception as e :
492
- print (e )
493
- pass
441
+ try :
442
+ self .pairs_handler (dev , pair_index )
443
+ except Exception as e :
444
+ print (e )
494
445
495
446
while len (matrix ):
496
447
event = self .get ()
@@ -500,12 +451,6 @@ def run(self):
500
451
keys [key ] = action_code
501
452
if action_code < 0xFF :
502
453
self .press (action_code )
503
- if self .verbose :
504
- keydown_time = matrix .get_keydown_time (key )
505
- dt = ms (matrix .time () - keydown_time )
506
- dt2 = ms (keydown_time - last_time )
507
- last_time = keydown_time
508
- print ("{} {} \\ {} latency {} | {}" .format (key , KEY_NAME [key ], hex (action_code ), dt , dt2 ))
509
454
else :
510
455
kind = action_code >> 12
511
456
if kind < ACT_MODS_TAP :
@@ -585,12 +530,16 @@ def run(self):
585
530
log ("switch to bt {}" .format (i ))
586
531
self .change_bt (i )
587
532
588
- if self .verbose :
589
- keydown_time = matrix .get_keydown_time (key )
590
- dt = ms (matrix .time () - keydown_time )
591
- dt2 = ms (keydown_time - last_time )
592
- last_time = keydown_time
593
- print ("{} {} \\ {} latency {} | {}" .format (key , KEY_NAME [key ], hex (action_code ), dt , dt2 ))
533
+ if self .verbose :
534
+ keydown_time = matrix .get_keydown_time (key )
535
+ dt = ms (matrix .time () - keydown_time )
536
+ dt2 = ms (keydown_time - last_time )
537
+ last_time = keydown_time
538
+ print (
539
+ "{} {} \\ {} latency {} | {}" .format (
540
+ key , KEY_NAME [key ], hex (action_code ), dt , dt2
541
+ )
542
+ )
594
543
else :
595
544
action_code = keys [key ]
596
545
if action_code < 0xFF :
@@ -624,16 +573,19 @@ def run(self):
624
573
self .layer_mask &= ~ (1 << layer )
625
574
log ("layers {}" .format (self .layer_mask ))
626
575
elif kind == ACT_MACRO :
627
- if callable (self .macro_handler ):
628
- i = action_code & 0xFFF
629
- try :
630
- self .macro_handler (dev , i , False )
631
- except Exception as e :
632
- print (e )
576
+ i = action_code & 0xFFF
577
+ try :
578
+ self .macro_handler (dev , i , False )
579
+ except Exception as e :
580
+ print (e )
633
581
634
582
if self .verbose :
635
583
keyup_time = matrix .get_keyup_time (key )
636
584
dt = ms (matrix .time () - keyup_time )
637
585
dt2 = ms (keyup_time - last_time )
638
586
last_time = keyup_time
639
- print ("{} {} / {} latency {} | {}" .format (key , KEY_NAME [key ], hex (action_code ), dt , dt2 ))
587
+ print (
588
+ "{} {} / {} latency {} | {}" .format (
589
+ key , KEY_NAME [key ], hex (action_code ), dt , dt2
590
+ )
591
+ )
0 commit comments