This repository was archived by the owner on May 25, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefault.py
More file actions
executable file
·91 lines (73 loc) · 2.89 KB
/
Copy pathdefault.py
File metadata and controls
executable file
·91 lines (73 loc) · 2.89 KB
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
#!/usr/bin/env python3
import os
import psutil
import pyautogui
import threading
import subprocess
from PIL import Image, ImageDraw, ImageFont
from src.StreamDeck.DeviceManager import DeviceManager
from src.StreamDeck.ImageHelpers import PILHelper
# Folder location of image assets used by this example.
ASSETS_PATH = os.path.join(os.path.dirname(__file__), "assets")
# Global state management
class State:
microphone = False
state = State()
# Generates a custom tile with run-time generated text and custom image via the
# PIL module.
def render_key_image(deck, icon_filename, label_text):
# Resize the source image asset to best-fit the dimensions of a single key,
# leaving a margin at the bottom so that we can draw the key title
# afterwards.
icon = Image.open(icon_filename)
margins = [0, 0, 20, 0] if label_text else [5, 5, 5, 5]
image = PILHelper.create_scaled_image(deck, icon, margins=margins)
# Load a custom TrueType font and use it to overlay the key index, draw key
# label onto the image a few pixels from the bottom of the key.
draw = ImageDraw.Draw(image)
font = ImageFont.truetype(os.path.join(ASSETS_PATH, 'roboto-regular.ttf'), 14)
if label_text:
draw.text((image.width / 2, image.height - 5), text=label_text, font=font, anchor="ms", fill="white")
return PILHelper.to_native_format(deck, image)
def key_image(deck, key, asset, title = None):
assetpath = os.path.join(ASSETS_PATH, asset)
image = render_key_image(deck, assetpath, title)
with deck:
deck.set_key_image(key, image)
def microphone_set_state_icon():
mic_on = subprocess.check_output(['pulsemixer', '--id', '3', '--get-mute']) == b'0\n'
file = 'microphone-on.png' if mic_on else 'microphone-off.png'
key_image(deck, 7, file)
def key_change(deck, key, direction):
# only operate on key down
if direction != 1:
return
if key == 0:
pyautogui.hotkey('winleft', '1')
elif key == 1:
pyautogui.hotkey('winleft', '4')
elif key == 7:
os.system('pulsemixer --id 3 --toggle-mute')
microphone_set_state_icon()
elif key == 4:
os.system('systemctl suspend')
if __name__ == "__main__":
for index, deck in enumerate(DeviceManager().enumerate()):
deck.open()
deck.reset()
# Set initial screen brightness to 30%.
deck.set_brightness(50)
# render keys
key_image(deck, 0, 'firefox.png')
key_image(deck, 1, 'vs-code.png')
key_image(deck, 4, 'power.png')
microphone_set_state_icon()
# actions
deck.set_key_callback(key_change)
# Wait until all application threads have terminated (for this example,
# this is when all deck handles are closed).
for t in threading.enumerate():
if t is threading.currentThread():
continue
if t.is_alive():
t.join()