6
6
key sequences.
7
7
"""
8
8
9
- # pylint: disable=import-error, unused-import, too-few-public-methods, eval-used
9
+ # pylint: disable=import-error, unused-import, too-few-public-methods
10
10
11
11
import os
12
- import time
13
12
import board
14
13
import digitalio
15
14
import displayio
16
15
import neopixel
17
16
import rotaryio
17
+ import keypad
18
18
import terminalio
19
19
import usb_hid
20
20
from adafruit_display_shapes .rect import Rect
31
31
32
32
# CLASSES AND FUNCTIONS ----------------
33
33
34
- class Key :
35
- """ Class representing the physical hardware of each MACROPAD key. """
36
- DEBOUNCE_TIME = 1 / 50
37
-
38
- def __init__ (self , keyname ):
39
- self .pin = digitalio .DigitalInOut (keyname )
40
- self .pin .direction = digitalio .Direction .INPUT
41
- self .pin .pull = digitalio .Pull .UP
42
- self .last_value = self .pin .value # Initial state
43
- self .last_time = time .monotonic ()
44
-
45
- def debounce (self ):
46
- """ Read a key's current state (hardware pin value), filtering out
47
- any "bounce" noise. This function needs to be called frequently,
48
- once for each key on pad, plus encoder switch. """
49
- value = self .pin .value
50
- if value != self .last_value :
51
- now = time .monotonic ()
52
- elapsed = now - self .last_time
53
- if elapsed >= self .DEBOUNCE_TIME :
54
- self .last_value = value
55
- self .last_time = now
56
- return value
57
- return None
58
-
59
34
class App :
60
35
""" Class representing a host-side application, for which we have a set
61
36
of macro sequences. """
@@ -86,7 +61,12 @@ def switch(self):
86
61
PIXELS = neopixel .NeoPixel (board .NEOPIXEL , 12 , auto_write = False )
87
62
KEYBOARD = Keyboard (usb_hid .devices )
88
63
LAYOUT = KeyboardLayoutUS (KEYBOARD )
64
+ KEYS = keypad .Keys ((board .KEY1 , board .KEY2 , board .KEY3 , board .KEY4 , board .KEY5 ,
65
+ board .KEY6 , board .KEY7 , board .KEY8 , board .KEY9 , board .KEY10 ,
66
+ board .KEY11 , board .KEY12 , board .ENCODER_SWITCH ),
67
+ value_when_pressed = False , pull = True )
89
68
69
+ # Set up displayio group with all labels
90
70
GROUP = displayio .Group (max_size = 14 )
91
71
for KEY_INDEX in range (12 ):
92
72
x = KEY_INDEX % 3
@@ -102,12 +82,6 @@ def switch(self):
102
82
anchor_point = (0.5 , 0.0 ), max_glyphs = 30 ))
103
83
DISPLAY .show (GROUP )
104
84
105
- KEYS = []
106
- for pin in (board .KEY1 , board .KEY2 , board .KEY3 , board .KEY4 , board .KEY5 ,
107
- board .KEY6 , board .KEY7 , board .KEY8 , board .KEY9 , board .KEY10 ,
108
- board .KEY11 , board .KEY12 , board .ENCODER_SWITCH ):
109
- KEYS .append (Key (pin ))
110
-
111
85
# Load all the macro key setups from .py files in MACRO_FOLDER
112
86
APPS = []
113
87
FILES = os .listdir (MACRO_FOLDER )
@@ -118,7 +92,8 @@ def switch(self):
118
92
APPS .append (App (module .app ))
119
93
120
94
if not APPS :
121
- print ('No valid macro files found' )
95
+ GROUP [13 ].text = 'NO MACRO FILES FOUND'
96
+ DISPLAY .refresh ()
122
97
while True :
123
98
pass
124
99
@@ -136,27 +111,27 @@ def switch(self):
136
111
APPS [APP_INDEX ].switch ()
137
112
LAST_POSITION = POSITION
138
113
139
- for KEY_INDEX , KEY in enumerate (KEYS [0 : len (APPS [APP_INDEX ].macros )]):
140
- action = KEY .debounce ()
141
- if action is not None :
142
- sequence = APPS [APP_INDEX ].macros [KEY_INDEX ][2 ]
143
- if action is False : # Macro key pressed
144
- if KEY_INDEX < 12 :
145
- PIXELS [KEY_INDEX ] = 0xFFFFFF
146
- PIXELS .show ()
147
- for item in sequence :
148
- if isinstance (item , int ):
149
- if item >= 0 :
150
- KEYBOARD .press (item )
151
- else :
152
- KEYBOARD .release (item )
114
+ EVENT = KEYS .events .get ()
115
+ if EVENT and EVENT .key_number < len (APPS [APP_INDEX ].macros ):
116
+ SEQUENCE = APPS [APP_INDEX ].macros [EVENT .key_number ][2 ]
117
+ if EVENT .pressed :
118
+ if EVENT .key_number < 12 :
119
+ PIXELS [EVENT .key_number ] = 0xFFFFFF
120
+ PIXELS .show ()
121
+ for item in SEQUENCE :
122
+ if isinstance (item , int ):
123
+ if item >= 0 :
124
+ KEYBOARD .press (item )
153
125
else :
154
- LAYOUT .write (item )
155
- elif action is True : # Macro key released
156
- # Release any still-pressed modifier keys
157
- for item in sequence :
158
- if isinstance (item , int ) and item >= 0 :
159
126
KEYBOARD .release (item )
160
- if KEY_INDEX < 12 :
161
- PIXELS [KEY_INDEX ] = APPS [APP_INDEX ].macros [KEY_INDEX ][0 ]
162
- PIXELS .show ()
127
+ else :
128
+ LAYOUT .write (item )
129
+ else :
130
+ # Release any still-pressed modifier keys
131
+ for item in SEQUENCE :
132
+ if isinstance (item , int ) and item >= 0 :
133
+ KEYBOARD .release (item )
134
+ if EVENT .key_number < 12 :
135
+ PIXELS [EVENT .key_number ] = APPS [APP_INDEX ].macros [
136
+ EVENT .key_number ][0 ]
137
+ PIXELS .show ()
0 commit comments