|
1 |
| - |
2 |
| -#from adafruit_hid.keyboard import Keyboard |
3 |
| -#from adafruit_hid.keycode import Keycode |
4 |
| -import Keys |
5 |
| -import Devices |
6 |
| -import keymap_simple as Keymap |
7 |
| -import time |
8 |
| -#import keymap_t9 as keymap |
9 |
| -#KMAP = keymap.KMAP |
10 |
| -#KEYS = keys.Keys() |
11 |
| -#SCROLL_ENCODER = Keymap.SCROLL_ENCODER |
12 |
| -#JOYSTICK_MOUSE = Keymap.JOYSTICK_MOUSE |
13 |
| - |
14 |
| -# If a dual function key (layer if hold, key if tap), is held longer than this, |
15 |
| -# then it is treated as a hold (layer change), not a key tap. |
16 |
| -HOLD_TIME = .2 |
17 |
| -LOOP_STEP = .05 # 5 ms |
18 |
| - |
19 |
| -def readLoop(): |
20 |
| - global HOLD_TIME |
21 |
| - global LOOP_STEP |
22 |
| - kmap = Keymap.KMAP |
23 |
| - keys = Keys.Keys() |
24 |
| - buttons = [Devices.Button(p, HOLD_TIME) for p in Keymap.PINS] |
25 |
| - scroll_encoder = Devices.Encoder(Keymap.SCROLL_ENCODER) |
26 |
| - joystick_mouse = Devices.Joystick(Keymap.JOYSTICK_MOUSE) |
27 |
| - queue = [None for b in buttons] |
28 |
| - layer = ['BASE', ] |
29 |
| - pending = False |
30 |
| - |
31 |
| - while True: |
32 |
| - time.sleep(LOOP_STEP) |
33 |
| - for n, button in enumerate(buttons): |
34 |
| - keys.move_mouse() |
35 |
| - enc_state = scroll_encoder.getState() |
36 |
| - if enc_state is not None: |
37 |
| - print(enc_state) |
38 |
| - joy_state = joystick_mouse.getState() |
39 |
| - if joy_state != (0, 0): |
40 |
| - print(joy_state) |
41 |
| - |
42 |
| - state = button.get_state() |
43 |
| - |
44 |
| - # Set the button hold/tap functions |
45 |
| - if state == 'new press': |
46 |
| - if pending: |
47 |
| - if pending.hold in kmap.keys(): |
48 |
| - layer.append(pending.hold) |
49 |
| - else: |
50 |
| - keys.press(pending.hold) |
51 |
| - pending.tap = False |
52 |
| - pending = False |
53 |
| - |
54 |
| - current_layer = layer[-1] |
55 |
| - funcs = kmap[current_layer][n] |
56 |
| - if isinstance(funcs, str): |
57 |
| - funcs = (funcs, False) |
58 |
| - print(funcs, state) |
59 |
| - button.tap, button.hold = funcs |
60 |
| - |
61 |
| - if 'continued' not in state: |
62 |
| - # FYI for button state transitions |
63 |
| - print(state, '- h:', button.hold, 't:', button.tap, button.pin) |
64 |
| - |
65 |
| - # Handle each state (change) |
66 |
| - if state == 'new press': |
67 |
| - if button.hold: |
68 |
| - pending = button |
69 |
| - else: |
70 |
| - keys.press(button.tap) |
71 |
| - |
72 |
| - elif state == 'continued press': |
73 |
| - pass |
74 |
| - |
75 |
| - elif state == 'new hold': |
76 |
| - if pending == button: |
77 |
| - if button.hold in kmap.keys(): |
78 |
| - layer.append(button.hold) |
79 |
| - else: |
80 |
| - keys.press(button.hold) # e.g. alt, shift, etc. |
81 |
| - button.tap = False |
82 |
| - pending = False |
83 |
| - |
84 |
| - elif state == 'continued hold': |
85 |
| - pass |
86 |
| - |
87 |
| - elif state == 'new release': |
88 |
| - if pending == button: |
89 |
| - pending = False |
90 |
| - button.hold = False |
91 |
| - keys.press(button.tap) |
92 |
| - |
93 |
| - elif button.hold: |
94 |
| - print(button.hold, kmap.keys()) |
95 |
| - if button.hold in kmap.keys(): |
96 |
| - layer.remove(button.hold) |
97 |
| - else: |
98 |
| - keys.release(button.hold) |
99 |
| - button.tap = False |
100 |
| - button.hold = False |
101 |
| - |
102 |
| - else: |
103 |
| - keys.release(button.tap) |
104 |
| - button.tap = False |
105 |
| - |
106 |
| - else: # state == 'condinued_release' |
107 |
| - if button.tap: |
108 |
| - print('continued release = tap release:', button.tap) |
109 |
| - keys.release(button.tap) |
110 |
| - button.tap = False |
111 |
| - |
112 |
| - |
113 |
| -readLoop() |
| 1 | + |
| 2 | +#from adafruit_hid.keyboard import Keyboard |
| 3 | +#from adafruit_hid.keycode import Keycode |
| 4 | +import Keys |
| 5 | +import Devices |
| 6 | +import keymap_simple as Keymap |
| 7 | +import time |
| 8 | +#import keymap_t9 as keymap |
| 9 | +#KMAP = keymap.KMAP |
| 10 | +#KEYS = keys.Keys() |
| 11 | +#SCROLL_ENCODER = Keymap.SCROLL_ENCODER |
| 12 | +#JOYSTICK_MOUSE = Keymap.JOYSTICK_MOUSE |
| 13 | + |
| 14 | +# If a dual function key (layer if hold, key if tap), is held longer than this, |
| 15 | +# then it is treated as a hold (layer change), not a key tap. |
| 16 | +HOLD_TIME = .2 |
| 17 | +LOOP_STEP = .05 # 5 ms |
| 18 | + |
| 19 | +def readLoop(): |
| 20 | + global HOLD_TIME |
| 21 | + global LOOP_STEP |
| 22 | + kmap = Keymap.KMAP |
| 23 | + keys = Keys.Keys() |
| 24 | + buttons = [Devices.Button(p, HOLD_TIME) for p in Keymap.PINS] |
| 25 | + scroll_encoder = Devices.Encoder(Keymap.SCROLL_ENCODER) |
| 26 | + joystick_mouse = Devices.Joystick(Keymap.JOYSTICK_MOUSE) |
| 27 | + queue = [None for b in buttons] |
| 28 | + layer = ['BASE', ] |
| 29 | + pending = False |
| 30 | + |
| 31 | + while True: |
| 32 | + time.sleep(LOOP_STEP) |
| 33 | + for n, button in enumerate(buttons): |
| 34 | + keys.move_mouse() |
| 35 | + enc_state = scroll_encoder.getState() |
| 36 | + if enc_state is not None: |
| 37 | + print(enc_state) |
| 38 | + joy_state = joystick_mouse.getState() |
| 39 | + if joy_state != (0, 0): |
| 40 | + print(joy_state) |
| 41 | + |
| 42 | + state = button.get_state() |
| 43 | + |
| 44 | + # Set the button hold/tap functions |
| 45 | + if state == 'new press': |
| 46 | + if pending: |
| 47 | + if pending.hold in kmap.keys(): |
| 48 | + layer.append(pending.hold) |
| 49 | + else: |
| 50 | + keys.press(pending.hold) |
| 51 | + pending.tap = False |
| 52 | + pending = False |
| 53 | + |
| 54 | + current_layer = layer[-1] |
| 55 | + funcs = kmap[current_layer][n] |
| 56 | + if isinstance(funcs, str): |
| 57 | + funcs = (funcs, False) |
| 58 | + print(funcs, state) |
| 59 | + button.tap, button.hold = funcs |
| 60 | + |
| 61 | + if 'continued' not in state: |
| 62 | + # FYI for button state transitions |
| 63 | + print(state, '- h:', button.hold, 't:', button.tap, button.pin) |
| 64 | + |
| 65 | + # Handle each state (change) |
| 66 | + if state == 'new press': |
| 67 | + if button.hold: |
| 68 | + pending = button |
| 69 | + else: |
| 70 | + keys.press(button.tap) |
| 71 | + |
| 72 | + elif state == 'continued press': |
| 73 | + pass |
| 74 | + |
| 75 | + elif state == 'new hold': |
| 76 | + if pending == button: |
| 77 | + if button.hold in kmap.keys(): |
| 78 | + layer.append(button.hold) |
| 79 | + else: |
| 80 | + keys.press(button.hold) # e.g. alt, shift, etc. |
| 81 | + button.tap = False |
| 82 | + pending = False |
| 83 | + |
| 84 | + elif state == 'continued hold': |
| 85 | + pass |
| 86 | + |
| 87 | + elif state == 'new release': |
| 88 | + if pending == button: |
| 89 | + pending = False |
| 90 | + button.hold = False |
| 91 | + keys.press(button.tap) |
| 92 | + |
| 93 | + elif button.hold: |
| 94 | + print(button.hold, kmap.keys()) |
| 95 | + if button.hold in kmap.keys(): |
| 96 | + layer.remove(button.hold) |
| 97 | + else: |
| 98 | + keys.release(button.hold) |
| 99 | + button.tap = False |
| 100 | + button.hold = False |
| 101 | + |
| 102 | + else: |
| 103 | + keys.release(button.tap) |
| 104 | + button.tap = False |
| 105 | + |
| 106 | + else: # state == 'condinued_release' |
| 107 | + if button.tap: |
| 108 | + print('continued release = tap release:', button.tap) |
| 109 | + keys.release(button.tap) |
| 110 | + button.tap = False |
| 111 | + |
| 112 | + |
| 113 | +readLoop() |
0 commit comments