This repository has been archived by the owner on Jun 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRave.py
168 lines (152 loc) · 6.31 KB
/
Rave.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
import colorsys, random, string, sys, threading, time, os, openrgb
from openrgb.utils import DeviceType, ModeData, RGBColor, ZoneType
# or the name could also be 'The Acid's Kickin In Hard' if you want (as per @Saint Mischievous on discord)
Black = RGBColor(0, 0, 0)
def UserInput():
"""It will always return 5 things;\n
Color1, Color2, Speed, Devices for reversal, Devices that are enables"""
Color1 = Color2 = Speed = ReversedDevice = OnlySet = None
for arg in sys.argv:
if arg == '--C1':
Pos = sys.argv.index(arg) + 1
R, G, B = sys.argv[Pos:(Pos + 3)]
Color1 = RGBColor(int(R),int(G),int(B))
elif arg == '--C2':
Pos = sys.argv.index(arg) + 1
R, G, B = sys.argv[Pos:(Pos + 3)]
Color2 = RGBColor(int(R),int(G),int(B))
elif arg == '--reversed':
ReversedDevices = (sys.argv.index(arg) + 1) # Will point to where the device(s) that need to be reversed are
ReversedDevice = []
if ' , ' in sys.argv[ReversedDevices]:
for i in sys.argv[ReversedDevices].split(' , '):
for D in client.devices:
if D.name.strip().casefold() == i.strip().casefold():
ReversedDevice += [D]
else:
for D in client.devices:
if D.name.strip().casefold() == sys.argv[ReversedDevices].strip().casefold():
ReversedDevice += [D]
elif arg == '--only-set':
AllowedDevices = (sys.argv.index(arg) + 1) # Will point to where the device(s) that are allowed are
OnlySet = []
if ' , ' in sys.argv[AllowedDevices]:
for i in sys.argv[AllowedDevices].split(' , '):
for D in client.devices:
if D.name.strip().casefold() == i.strip().casefold():
OnlySet += [D]
else:
for D in client.devices:
if D.name.strip().casefold() == sys.argv[AllowedDevices].strip().casefold():
OnlySet += [D]
elif arg == '--speed':
Speed = int(sys.argv[(sys.argv.index(arg) + 1)])
else:
pass
return(Color1, Color2, Speed, ReversedDevice, OnlySet)
class ColorDrop:
"""
Connects to an OpenRGB device and displays a rain effect.
"""
def __init__(self, client, device_index, surface_index, InstColor, ReverseBool):
self.Color = InstColor
self.device = None
self.device = client.devices[device_index]
self.surface = self.device.zones[surface_index]
if self.surface.type != ZoneType.LINEAR:
raise Exception("not a linear zone")
if ReverseBool == True:
self.surface.leds.reverse()
elif ReverseBool == False:
self.surface.leds
self.leds = self.surface.leds
self.set_mode()
def set_mode(self):
"""
Set in a direct / static mode.
"""
try:
self.device.set_mode('direct')
except:
try:
self.device.set_mode('static')
print("error setting %s\nfalling back to static" %
self.device.name)
except:
print(
"Critical error! couldn't set %s to static or direct" %
self.device.name)
self.device.set_color(Black)
@staticmethod
def transformer(state, ratio):
"""
Apply the rain transformation to this `state`
"""
transformed = []
for i in range(0, len(state)):
if i == 0:
# Mutation goes here
x = random.randint(0, len(state)*ratio) == 0
if state[0] and not state[1]:
x = True
else:
x = state[i-1]
transformed.append(x)
return transformed
def start(self, refresh=30, ratio=10):
"""
Start the effect on this surface.
"""
state = [False for _ in self.leds]
prev_state = state
while True:
for i, value in enumerate(state):
try:
if prev_state[i] != value:
self.leds[i].set_color(
{
True: self.Color,
False: Black
}[value]
)
except ValueError:
return
prev_state = state.copy()
state = ColorDrop.transformer(state, ratio)
time.sleep(1.0/refresh)
def Setup_Drop(Client, device_idx, surface_idx, InstColor, ReverseBool):
"""
Creates and instance of the SurfaceRain object and starts it.
Used by threads to provide a nice interface to do this.
"""
inst = ColorDrop(Client, device_idx, surface_idx, InstColor, ReverseBool)
inst.start(ratio=10)
if __name__ == "__main__":
Clist = [RGBColor(255,255,0),RGBColor(0, 255, 100),RGBColor(0, 255, 255),RGBColor(255,0,100),RGBColor(100,0,255)]
CName = ['Yellow','Aqua','Cyan','Red','DarkPurple']
# Get a list of surfaces
client = openrgb.OpenRGBClient()
_, _, _, Reversed, Enabled = UserInput()
Enable = []
if Enabled == None:
Enable += [i for i in client.devices]
elif Enabled != None:
Enable = Enabled
for C in Clist:
surfaces = []
for device_idx, device in enumerate(Enable):
if Reversed != None:
for R in Reversed:
if R == device:
ReverseBool = True
continue
else:
ReverseBool = False
else:
ReverseBool = False
for zone_idx, zone in enumerate(device.zones):
if zone.type == ZoneType.LINEAR:
surfaces.append((client, device_idx, zone_idx, C, ReverseBool))
for surface in surfaces:
t = threading.Thread(name="%s%s"%(zone.name,CName[Clist.index(C)]), target=Setup_Drop, args=surface)
t.start()