-
Notifications
You must be signed in to change notification settings - Fork 1
/
code.py
350 lines (334 loc) · 14.6 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# rpico_rgb_keypad_obs v1.0.1
#
# SPDX-FileCopyrightText: 2023 Martin Looker
#
# SPDX-License-Identifier: MIT
#
# DESCRIPTION
#
# This code provides a controller for OBS studio acting as a USB keyboard
#
# Keys are as follows:
#
# Green:
# 11 scene keys, only one scene can be active at a time
# Cyan, Blue:
# 2 general keys
# Red, Yellow, Magenta:
# 3 toggle keys different key combos are sent when toggling on or off, so map these to start/stop
# hotkeys for start/stop streaming etc
#
# HARDWARE
#
# https://www.raspberrypi.com/documentation/microcontrollers/raspberry-pi-pico.html
# https://shop.pimoroni.com/products/pico-rgb-keypad-base
#
# LIBRARIES
#
# adafruit:
# https://circuitpython.org/board/raspberry_pi_pico/
# https://github.com/adafruit/Adafruit_DotStar
# https://github.com/adafruit/Adafruit_CircuitPython_HID
#
# pimoroni:
# https://github.com/pimoroni/pmk-circuitpython
#
# SOFTWARE
#
# Inspired by:
# https://github.com/pimoroni/pmk-circuitpython/blob/main/examples/obs-studio-toggle-and-mutex.py
import math
from pmk import PMK, number_to_xy, hsv_to_rgb
# from pmk.platform.keybow2040 import Keybow2040 as Hardware # for Keybow 2040
from pmk.platform.rgbkeypadbase import RGBKeypadBase as Hardware # for Pico RGB Keypad Base
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode
# When true keycodes are sent
KC_LIVE = True
# LED Hues
HUE_SPLIT = (1.0/24.0)
hue = {
"red" : (HUE_SPLIT * 0.0),
"rry" : (HUE_SPLIT * 1.0),
"ry" : (HUE_SPLIT * 2.0),
"ryy" : (HUE_SPLIT * 3.0),
"yellow" : (HUE_SPLIT * 4.0),
"yyg" : (HUE_SPLIT * 5.0),
"yg" : (HUE_SPLIT * 6.0),
"ygg" : (HUE_SPLIT * 7.0),
"green" : (HUE_SPLIT * 8.0),
"ggc" : (HUE_SPLIT * 9.0),
"gc" : (HUE_SPLIT * 10.0),
"gcc" : (HUE_SPLIT * 11.0),
"cyan" : (HUE_SPLIT * 12.0),
"ccb" : (HUE_SPLIT * 13.0),
"cb" : (HUE_SPLIT * 14.0),
"cbb" : (HUE_SPLIT * 15.0),
"blue" : (HUE_SPLIT * 16.0),
"bbm" : (HUE_SPLIT * 17.0),
"bm" : (HUE_SPLIT * 18.0),
"bmm" : (HUE_SPLIT * 19.0),
"magenta" : (HUE_SPLIT * 20.0),
"mmr" : (HUE_SPLIT * 21.0),
"mr" : (HUE_SPLIT * 22.0),
"mrr" : (HUE_SPLIT * 23.0),
}
# Hue:
# Set this for the pad color.
#
# Group:
# Set this to group pads together to operate like radio buttons (good for
# scene selection). You can have many separate groups of keys as set by the
# string set for the group
#
# Keycodes On:
# These are the keyboard codes to be sent for normal, grouped and toggle on
# pads.
#
# Keycodes Off:
# These are the keyboard codes to be sent for toggle off pads, setting this
# makes a toggle button, good for start/stop streaming
#
# Note:
# Pads configured as toggles will be removed from any groups
#
config = [
{"hue": hue["magenta"], "group": "scene", "keycodes_on": [Keycode.F13], "keycodes_off": None }, # 0
{"hue": hue["magenta"], "group": "scene", "keycodes_on": [Keycode.F14], "keycodes_off": None }, # 1
{"hue": hue["magenta"], "group": "scene", "keycodes_on": [Keycode.F15], "keycodes_off": None }, # 2
{"hue": hue["magenta"], "group": "scene", "keycodes_on": [Keycode.F16], "keycodes_off": None }, # 3
{"hue": hue["magenta"], "group": "scene", "keycodes_on": [Keycode.F17], "keycodes_off": None }, # 4
{"hue": hue["magenta"], "group": "scene", "keycodes_on": [Keycode.F18], "keycodes_off": None }, # 5
{"hue": hue["magenta"], "group": "scene", "keycodes_on": [Keycode.F19], "keycodes_off": None }, # 6
{"hue": hue["magenta"], "group": "scene", "keycodes_on": [Keycode.F20], "keycodes_off": None }, # 7
{"hue": hue["magenta"], "group": "scene", "keycodes_on": [Keycode.F21], "keycodes_off": None }, # 8
{"hue": hue["magenta"], "group": "scene", "keycodes_on": [Keycode.F22], "keycodes_off": None }, # 9
{"hue": hue["magenta"], "group": "scene", "keycodes_on": [Keycode.F23], "keycodes_off": None }, # A
{"hue": hue["magenta"], "group": "scene", "keycodes_on": [Keycode.F24], "keycodes_off": None }, # B
{"hue": hue["cyan"] , "group": None, "keycodes_on": [Keycode.SHIFT, Keycode.F13], "keycodes_off": [Keycode.SHIFT, Keycode.F13]}, # C
{"hue": hue["cyan"] , "group": None, "keycodes_on": [Keycode.SHIFT, Keycode.F14], "keycodes_off": [Keycode.SHIFT, Keycode.F14]}, # D
{"hue": hue["green"] , "group": None, "keycodes_on": [Keycode.CONTROL, Keycode.F13], "keycodes_off": [Keycode.ALT, Keycode.F13]}, # E
{"hue": hue["red"] , "group": None , "keycodes_on": [Keycode.CONTROL, Keycode.F14], "keycodes_off": [Keycode.ALT, Keycode.F14]} # F
]
#config = [
# {"hue": hue["yg"] , "group": "scene", "keycodes_on": [Keycode.F13], "keycodes_off": None }, # 0
# {"hue": hue["yellow"] , "group": "scene", "keycodes_on": [Keycode.F14], "keycodes_off": None }, # 1
# {"hue": hue["yg"] , "group": "scene", "keycodes_on": [Keycode.F15], "keycodes_off": None }, # 2
# {"hue": hue["red"] , "group": None , "keycodes_on": [Keycode.CONTROL, Keycode.F13], "keycodes_off": [Keycode.ALT, Keycode.F13]}, # 3
# {"hue": hue["gc"] , "group": "scene", "keycodes_on": [Keycode.F16], "keycodes_off": None }, # 4
# {"hue": hue["green"] , "group": "scene", "keycodes_on": [Keycode.F17], "keycodes_off": None }, # 5
# {"hue": hue["gc"] , "group": "scene", "keycodes_on": [Keycode.F18], "keycodes_off": None }, # 6
# {"hue": hue["rry"] , "group": None , "keycodes_on": [Keycode.CONTROL, Keycode.F14], "keycodes_off": [Keycode.ALT, Keycode.F14]}, # 7
# {"hue": hue["cb"] , "group": "scene", "keycodes_on": [Keycode.F19], "keycodes_off": None }, # 8
# {"hue": hue["cyan"] , "group": "scene", "keycodes_on": [Keycode.F20], "keycodes_off": None }, # 9
# {"hue": hue["cb"] , "group": "scene", "keycodes_on": [Keycode.F21], "keycodes_off": None }, # A
# {"hue": hue["ry"] , "group": None, "keycodes_on": [Keycode.SHIFT, Keycode.F13] , "keycodes_off": None }, # B
# {"hue": hue["bm"] , "group": "scene", "keycodes_on": [Keycode.F22], "keycodes_off": None }, # C
# {"hue": hue["blue"] , "group": "scene", "keycodes_on": [Keycode.F23], "keycodes_off": None }, # D
# {"hue": hue["bm"] , "group": "scene", "keycodes_on": [Keycode.F24], "keycodes_off": None }, # E
# {"hue": hue["ryy"] , "group": None , "keycodes_on": [Keycode.CONTROL, Keycode.F16], "keycodes_off": [Keycode.ALT, Keycode.F16]} # F
#]
# LED Values (brightness)
VAL_SPLIT = (1.0/32.0)
VAL_MIN = (VAL_SPLIT * 0.0)
VAL_OFF = (VAL_SPLIT * 2.0)
VAL_ON = (VAL_SPLIT * 30.0)
VAL_MAX = (VAL_SPLIT * 32.0)
VAL_STEP = 0.01
# Set up the keyboard and layout
keyboard = Keyboard(usb_hid.devices)
layout = KeyboardLayoutUS(keyboard)
# Set up Keybow
keybow = PMK(Hardware())
keys = keybow.keys
# Add runtime data to config
for i in range(16):
# Defaults
# Mode is toggle
config[i]["mode"] = None
# Set LED value to max
config[i]["val"] = VAL_MAX
# Not down
config[i]["down"] = False
# Not on
config[i]["on"] = False
# This is a toggle pad ?
if config[i]["keycodes_off"] != None and len(config[i]["keycodes_off"]) and len(config[i]["keycodes_on"]):
# Mode is toggle
config[i]["mode"] = "toggle"
# Can't be in a group
config[i]["group"] = None
# This is a grouped pad ?
if config[i]["group"] != None and len(config[i]["keycodes_on"]):
# Mode is group
config[i]["mode"] = "group"
# This is a key pad ?
if config[i]["mode"] == None and len(config[i]["keycodes_on"]):
# Mode is key
config[i]["mode"] = "key"
# This key has not got a mode ?
if config[i]["mode"] == None:
# Set LED value to min (not lit)
config[i]["val"] = VAL_MIN
# Presses a list of keycodes
def press_kcs(kcs):
print(f'keycode press {kcs} {KC_LIVE}')
if KC_LIVE:
if len(kcs) == 1:
keyboard.press(kcs[0])
elif len(kcs) == 2:
keyboard.press(kcs[0], kcs[1])
elif len(kcs) == 3:
keyboard.press(kcs[0], kcs[1], kcs[2])
# Releases a list of keycodes
def release_kcs(kcs):
print(f'keycode release {kcs} {KC_LIVE}')
if KC_LIVE:
if len(kcs) == 1:
keyboard.release(kcs[0])
elif len(kcs) == 2:
keyboard.release(kcs[0], kcs[1])
elif len(kcs) == 3:
keyboard.release(kcs[0], kcs[1], kcs[2])
# Process key presses/releases
for key in keys:
# Pad pressed?
@keybow.on_press(key)
def press_handler(key):
print(f'keypad press {key.number}')
# Pad is now down
config[key.number]["down"] = True
# Normal pad ?
if config[key.number]["mode"] == "key":
# Press the on keycodes
press_kcs(config[key.number]["keycodes_on"])
# Toggle pad ?
elif config[key.number]["mode"] == "toggle":
# Toggle is currently on ?
if config[key.number]["on"]:
# Turn off
config[key.number]["on"] = False
# Press the off keycodes
press_kcs(config[key.number]["keycodes_off"])
# Toggle is currently off ?
else:
# Turn on
config[key.number]["on"] = True
# Press the on keycodes
press_kcs(config[key.number]["keycodes_on"])
# Grouped pad ?
elif config[key.number]["mode"] == "group":
# Turn on the pressed pad
config[key.number]["on"] = True
# Press the on keycodes
press_kcs(config[key.number]["keycodes_on"])
# Loop through pads
for i in range(16):
# Not the pad that has just been pressed ?
if i != key.number:
# This pad is in the same group as the pad that has just been pressed ?
if config[i]["mode"] == "group" and config[i]["group"] == config[key.number]["group"]:
# The pad is on ?
if config[i]["on"]:
# Turn it off
config[i]["on"] = False
# Set val to minimum
config[i]["val"] = VAL_MIN
# Pad released ?
@keybow.on_release(key)
def release_handler(key):
print(f'keypad release {key.number}')
# Pad is not down
config[key.number]["down"] = False
# Normal pad ?
if config[key.number]["mode"] == "key":
# Release on keycodes
release_kcs(config[key.number]["keycodes_on"])
# Toggle pad ?
elif config[key.number]["mode"] == "toggle":
# Pad has been toggled on ?
if config[key.number]["on"]:
# Release on keycodes
release_kcs(config[key.number]["keycodes_on"])
# Pad has just been turned off ?
else:
# Release off keycodes
release_kcs(config[key.number]["keycodes_off"])
# Grouped pad
elif config[key.number]["mode"] == "group":
# Release on keycodes
release_kcs(config[key.number]["keycodes_on"])
# Pad held ?
@keybow.on_hold(key)
def hold_handler(key):
pass
# Main loop
while True:
# Always remember to call keybow.update() on every iteration of your loop!
keybow.update()
# Loop through pads
for i in range(16):
# Start with LED off
h = 0.0
s = 0.0
v = 0.0
# No mode ?
if config[i]["mode"] == None:
# Turn off LED
keys[i].set_led(0, 0, 0)
# Pad has a mode ?
else:
# Pad is down ?
if config[i]["down"]:
# Normal or grouped pad
if config[i]["mode"] == "key" or config[i]["mode"] == "group":
# Go to full brightness
config[i]["val"] = v = VAL_MAX
# Toggle pad?
elif config[i]["mode"] == "toggle":
# Toggled on ?
if config[i]["on"]:
# Go to full brightness
config[i]["val"] = v = VAL_MAX
# Toggled off ?
else:
# Go to min brightness
config[i]["val"] = v = VAL_MIN
# Pad is not down
else:
# Pad is on
if config[i]["on"]:
# Set target on brightness
v = VAL_ON
# Pad is off ?
else:
# Set target off brightness
v = VAL_OFF
# Target value above current value ?
if v > config[i]["val"]:
# Move towards target
if v - config[i]["val"] > VAL_STEP:
config[i]["val"] += VAL_STEP
else:
config[i]["val"] = v
# Target value below current value
elif v < config[i]["val"]:
# Move towards target
if config[i]["val"] - v > VAL_STEP:
config[i]["val"] -= VAL_STEP
else:
config[i]["val"] = v
# Pad has a hue ?
if config[i]["hue"] is not None:
# Set full saturation
s = 1.0
# Set hue
h = config[i]["hue"]
# Convert the hue to RGB values.
r, g, b = hsv_to_rgb(h, s, config[i]["val"])
# if i == 0:
# print(f'{h} {s} {config[i]["val"]} {r} {g} {b} rgb')
# Finally set the LED
keys[i].set_led(r, g, b)