-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.py.1
96 lines (81 loc) · 3.01 KB
/
code.py.1
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
#from adafruit_hid.keyboard import Keyboard
#from adafruit_hid.keycode import Keycode
import Keys
import Button
import keymap_simple as keymap
import time
#import keymap_t9 as keymap
KMAP = keymap.KMAP
PINS = keymap.PINS
KEYS = Keys.Keys()
# If a dual function key (layer if hold, key if tap), is held longer than this,
# then it is treated as a hold (layer change), not a key tap.
HOLD_TIME = .2
LOOP_STEP = .05 # 5 ms
def readLoop():
global PINS
global KMAP
global HOLD_TIME
#kmap = getKeymap()
#keys = Keys()
buttons = [Button.Button(p, HOLD_TIME) for p in PINS]
queue = [None for b in buttons]
layer = ['BASE', ]
pending = False
while True:
time.sleep(0.05)
for n, button in enumerate(buttons):
state = button.get_state()
current_layer = layer[-1]
# Set the button hold/tap functions
if state == 'new press':
funcs = KMAP[current_layer][n]
if isinstance(funcs, str):
funcs = (funcs, False)
print(funcs, state)
button.tap, button.hold = funcs
if 'continued' not in state:
# FYI for button state transitions
print(state, 'hold:', button.hold, 'tap:', button.tap, button.pin)
# Handle each state (change)
if state == 'new press':
if pending:
KEYS.press(pending.hold)
pending.tap = False
pending = False
if button.hold:
if button.hold in KMAP.keys():
layer.append(button.hold)
else:
pending = button
else:
KEYS.press(button.tap)
elif state == 'continued press':
pass
elif state == 'new hold':
if pending == button:
KEYS.press(button.hold) # e.g. alt, shift, etc.
pending = False
button.tap = False
elif state == 'continued hold':
pass
elif state == 'new release':
if button.hold:
print(button.hold, KMAP.keys())
if button.hold in KMAP.keys():
layer.remove(button.hold)
elif pending == button:
pending = False
KEYS.press(button.tap)
else:
KEYS.release(button.hold)
button.tap = False
button.hold = False
else:
KEYS.release(button.tap)
button.tap = False
else: # state == 'condinued_release'
if button.tap:
KEYS.release(button.tap)
button.tap = False
readLoop()