-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.py
260 lines (195 loc) · 6.84 KB
/
code.py
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import board
import digitalio
import time
import usb_hid
import rotaryio
import busio
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode
from keys_dict.keys import key_lookup
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.consumer_control_code import ConsumerControlCode
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
BlueLed = digitalio.DigitalInOut(board.GP21)
BlueLed.direction = digitalio.Direction.OUTPUT
GreenLed = digitalio.DigitalInOut(board.GP20)
GreenLed.direction = digitalio.Direction.OUTPUT
YellowLed = digitalio.DigitalInOut(board.GP19)
YellowLed.direction = digitalio.Direction.OUTPUT
RedLed = digitalio.DigitalInOut(board.GP18)
RedLed.direction = digitalio.Direction.OUTPUT
button = digitalio.DigitalInOut(board.GP16)
button.direction = digitalio.Direction.INPUT
button.pull = digitalio.Pull.UP
kbd = Keyboard(usb_hid.devices)
cc = ConsumerControl(usb_hid.devices)
pins = [board.GP1, board.GP2, board.GP3, board.GP4, board.GP5, board.GP6, board.GP7, board.GP8, board.GP9, board.GP10, board.GP12]
KEY = 1
keymap = {
(0): (KEY, [Keycode.ONE]),
(1): (KEY, [Keycode.TWO]),
(7): (KEY, [Keycode.THREE]),
(8): (KEY, [Keycode.FOUR]),
(5): (KEY, [Keycode.FIVE]),
(4): (KEY, [Keycode.SIX]),
(9): (KEY, [Keycode.SEVEN]),
(3): (KEY, [Keycode.EIGHT]),
(2): (KEY, [Keycode.NINE]),
(6): (KEY, [Keycode.COMMAND, Keycode.Z]),
(10): (KEY, [Keycode.B]),
}
keymap2 = {
(0): (KEY, [Keycode.A]),
(1): (KEY, [Keycode.B]),
(7): (KEY, [Keycode.C]),
(8): (KEY, [Keycode.D]),
(5): (KEY, [Keycode.E]),
(4): (KEY, [Keycode.F]),
(9): (KEY, [Keycode.G]),
(3): (KEY, [Keycode.H]),
(2): (KEY, [Keycode.NINE]),
(6): (KEY, [Keycode.COMMAND, Keycode.Z]),
(10): (KEY, [Keycode.J]),
}
keymap3 = {
(0): (KEY, [Keycode.ONE]),
(1): (KEY, [Keycode.TWO]),
(7): (KEY, [Keycode.THREE]),
(8): (KEY, [Keycode.FOUR]),
(5): (KEY, [Keycode.FIVE]),
(4): (KEY, [Keycode.SIX]),
(9): (KEY, [Keycode.SEVEN]),
(3): (KEY, [Keycode.EIGHT]),
(2): (KEY, [Keycode.NINE]),
(6): (KEY, [Keycode.COMMAND, Keycode.Z]),
(10): (KEY, [Keycode.B]),
}
keymap4 = {
(0): (KEY, [Keycode.A]),
(1): (KEY, [Keycode.B]),
(7): (KEY, [Keycode.C]),
(8): (KEY, [Keycode.D]),
(5): (KEY, [Keycode.E]),
(4): (KEY, [Keycode.F]),
(9): (KEY, [Keycode.G]),
(3): (KEY, [Keycode.H]),
(2): (KEY, [Keycode.NINE]),
(6): (KEY, [Keycode.COMMAND, Keycode.Z]),
(10): (KEY, [Keycode.J]),
}
switches = []
for i in range(len(pins)):
switch = digitalio.DigitalInOut(pins[i])
switch.direction = digitalio.Direction.INPUT
switch.pull = digitalio.Pull.UP
switches.append(switch)
switch_state = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
switch_state2 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
switch_state3 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
switch_state4 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
""" Not using this int for now...
numStates = 4"""
button_state = None
while True:
if not button.value and button_state is None:
button_state = "pressed"
if button.value and button_state == "pressed":
BlueLed.value = True
button_state = "Blue"
if not button.value and button_state == "Blue":
BlueLed.value = False
button_state = "pressed2"
if button.value and button_state == "pressed2":
GreenLed.value = True
button_state = "Green"
if not button.value and button_state == "Green":
GreenLed.value = False
button_state = "pressed3"
if button.value and button_state == "pressed3":
YellowLed.value = True
button_state = "Yellow"
if not button.value and button_state == "Yellow":
YellowLed.value = False
button_state = "pressed4"
if button.value and button_state == "pressed4":
RedLed.value = True
button_state = "Red"
if not button.value and button_state == "Red":
RedLed.value = False
button_state = "pressed"
for i in range(11):
if switch_state[i] == 0 and button_state == "Blue":
if not switches[i].value:
try:
if keymap[i][0] == KEY:
kbd.press(*keymap[i][1])
else:
cc.send(keymap[i][1])
except ValueError: # deals w six key limit
pass
switch_state[i] = 1
if switch_state[i] == 1:
if switches[i].value:
try:
if keymap[i][0] == KEY:
kbd.release(*keymap[i][1])
except ValueError:
pass
switch_state[i] = 0
if switch_state2[i] == 0 and button_state == "Green":
if not switches[i].value:
try:
if keymap2[i][0] == KEY:
kbd.press(*keymap2[i][1])
else:
cc.send(keymap2[i][1])
except ValueError: # deals w six key limit
pass
switch_state2[i] = 1
if switch_state2[i] == 1:
if switches[i].value:
try:
if keymap2[i][0] == KEY:
kbd.release(*keymap2[i][1])
except ValueError:
pass
switch_state2[i] = 0
if switch_state3[i] == 0 and button_state == "Yellow":
if not switches[i].value:
try:
if keymap3[i][0] == KEY:
kbd.press(*keymap3[i][1])
else:
cc.send(keymap3[i][1])
except ValueError: # deals w six key limit
pass
switch_state3[i] = 1
if switch_state3[i] == 1:
if switches[i].value:
try:
if keymap3[i][0] == KEY:
kbd.release(*keymap3[i][1])
except ValueError:
pass
switch_state3[i] = 0
if switch_state4[i] == 0 and button_state == "Red":
if not switches[i].value:
try:
if keymap4[i][0] == KEY:
kbd.press(*keymap4[i][1])
else:
cc.send(keymap4[i][1])
except ValueError: # deals w six key limit
pass
switch_state4[i] = 1
if switch_state4[i] == 1:
if switches[i].value:
try:
if keymap4[i][0] == KEY:
kbd.release(*keymap4[i][1])
except ValueError:
pass
switch_state4[i] = 0
time.sleep(0.01) # debounce