forked from openrazer/openrazer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_starlight.py
92 lines (64 loc) · 2.44 KB
/
custom_starlight.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
from collections import defaultdict
import colorsys
import random
import time
import threading
from openrazer.client import DeviceManager
from openrazer.client import constants as razer_constants
# Create a DeviceManager. This is used to get specific devices
device_manager = DeviceManager()
print("Found {} Razer devices".format(len(device_manager.devices)))
devices = device_manager.devices
for device in devices:
if not device.fx.advanced:
print("Skipping device " + device.name + " (" + device.serial + ")")
devices.remove(device)
print()
# Disable daemon effect syncing.
# Without this, the daemon will try to set the lighting effect to every device.
device_manager.sync_effects = False
# Helper function to generate interesting colors
def random_color():
rgb = colorsys.hsv_to_rgb(random.uniform(0, 1), random.uniform(0.5, 1), 1)
return tuple(map(lambda x: int(256 * x), rgb))
# Handle the startlight effect for a single key
def starlight_key(device, row, col, active):
color = random_color()
hue = random.uniform(0, 1)
start_time = time.time()
fade_time = 2
elapsed = 0
while elapsed < fade_time:
elapsed = time.time() - start_time
value = 1 - elapsed / fade_time
rgb = colorsys.hsv_to_rgb(hue, 1, value)
color = tuple(map(lambda x: int(256 * x), rgb))
device.fx.advanced.matrix[row, col] = color
value -= 0.01
# print(device, color)
time.sleep(1 / 60)
device.fx.advanced.matrix[row, col] = (0, 0, 0)
active[(row, col)] = False
# Handle the startlight effect for an entire device
def starlight_effect(device):
rows, cols = device.fx.advanced.rows, device.fx.advanced.cols
active = defaultdict(bool)
device.fx.advanced.matrix.reset()
device.fx.advanced.draw()
while True:
row, col = random.randrange(rows), random.randrange(cols)
if not active[(row, col)]:
active[(row, col)] = True
threading.Thread(target=starlight_key, args=(device, row, col, active)).start()
time.sleep(0.1)
# Spawn a manager thread for each device and wait on all of them.
threads = []
for device in devices:
t = threading.Thread(target=starlight_effect, args=(device,), daemon=True)
t.start()
threads.append(t)
# If there are still threads, update each device.
while any(t.isAlive() for t in threads):
for device in devices:
device.fx.advanced.draw()
time.sleep(1 / 60)