This repository has been archived by the owner on Jan 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
lighting.py
423 lines (336 loc) · 13.6 KB
/
lighting.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
"""
lighting.py
This file contains functions and constants related to controlling lights on the controller.
Author: Miguel Guthridge [[email protected]]
"""
IDLE_ANIMATION_SPEED = 1
IDLE_ANIMATION_STRETCH = 2
IDLE_ANIMATION_DO_TRAILS = True
IDLE_ANIMATION_TRAIL_SPEED_MODIFIER = 1
IDLE_ANIMATION_TRAIL_SPEED = 2
IDLE_ANIMATION_TRAIL_LENGTH = 6
IDLE_ANIMATION_TRAIL_INFREQUENCY = 37
IDLE_ANIMATION_TRAIL_Y_OFFSET = 13
import time
import internal
import eventconsts
import eventprocessor
import processorhelpers
import internal.consts
import config
import lightingconsts
class LightMap:
"""This object is sent through event processors to gather colours for UI redraws.
"""
def __init__(self):
"""Create instance
"""
self.reset()
def reset(self):
"""Resets all lights to transparent
"""
# indicates whether entire map is solidified
self.is_solid = False
# 0 = unfrozen, 1 = frozen
self.FrozenMap = [
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0]
]
# 0 = off, 1-127 = colour
self.PadColours = [
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0]
]
# 0 = off, 1 = on, 2 = pulse, negative = flash
self.PadStates = [
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0]
]
def setPadColour(self, x, y, colour, state = lightingconsts.MODE_DEFAULT, override = False):
"""Sets the colour of a pad
Args:
x (int): X coordinate
y (int): Y coordinate
colour (int): Colour (specified in lightingconsts)
state (int, optional): Lighting mode. Defaults to lightingconsts.MODE_DEFAULT. Can be:
- lightingconsts.MODE_OFF
- lightingconsts.MODE_ON
- lightingconsts.MODE_PULSE
- ColourL flash with that colour
- lightingconsts.MODE_DEFAULT
override (bool, optional): Whether to override the current pad option. Defaults to False.
Returns:
bool: Whether the assignment was successful.
"""
if self.FrozenMap[x][y] == 0 or override: # If pad available to map
self.PadColours[x][y] = colour
self.PadStates[x][y] = state
if colour != -1:
self.solidifyPad(x, y)
return True
else: return False
def setFromMatrix(self, map, state = lightingconsts.MODE_DEFAULT, override = False):
"""Set pad lights from a 2x8 matrix
Args:
map (matrix): 2D list structure containing light settings
state (int, optional): Lighting mode. Defaults to 3.
- 0 = off
- 1 = on
- 2 = pulse
- negative = flashing with abs(state)
- 3 = automatic
override (bool, optional): Whether to override the current pad option. Defaults to False.
"""
for x in range(len(self.FrozenMap) - 1): # Don't modify round pads
for y in range(len(self.FrozenMap[x])):
if self.FrozenMap[x][y] == 0 or override:
self.setPadColour(x, y, map[x][y], state, override)
return
def solidifyPad(self, x, y):
"""Prevent a pad from being overwritten
Args:
x (int): X coordinate
y (int): Y coordinate
"""
if self.FrozenMap[x][y] == 0: # If pad available to map
self.FrozenMap[x][y] = 1
def solidifyRow(self, y):
"""Prevent a row of lights from being overwritten (not including round pads)
Args:
y (int): Y coordinate
"""
for x in range(len(self.FrozenMap) - 1): # Don't solidify round pads
if self.FrozenMap[x][y] == 0: self.FrozenMap[x][y] = 1
def solidifyColumn(self, x):
"""Prevents a column from being overwritten
Args:
x (int): X coordinate
"""
for y in range(len(self.FrozenMap)):
if self.FrozenMap[x][y] == 0: self.FrozenMap[x][y] = 1
# Prevents all pads from being overwritten
def solidifyAll(self):
"""Prevents all pads from being overwritten (not including round pads). Call this at the end of your redraw functions
if you don't want other processors to possibly draw behind your UI.
"""
self.is_solid = True
for x in range(len(self.FrozenMap) - 1): # Don't solidify round pads
for y in range(len(self.FrozenMap[x])):
self.solidifyPad(x, y)
def isSolid(self):
"""Returns whether map has been solidified
Returns:
bool: Answer
"""
return self.is_solid
class Lights:
"""Manages the current state of lights.
"""
def __init__(self):
"""Create instance.
"""
# 0 = off, 1-127 = colour
self.PadColours = [
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0]
]
# 0 = off, 1 = on, 2 = pulse, negative = flash
self.PadStates = [
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0]
]
def reset(self):
"""Reset all lights to off
"""
internal.sendMidiMessage(0xBF, 0x00, 0x00)
internal.debugLog("Sent lighting reset signal", internal.consts.DEBUG.LIGHTING_RESET)
internal.window.resetAnimationTick()
self.__init__()
def setPadColour(self, x, y, colour, state = lightingconsts.MODE_DEFAULT, override = False):
"""Set the colour of a pad
Args:
x (int): X coordinate
y (int): Y coordinate
colour (int): Colour option
state (int, optional): Light mode. Defaults to lightingconsts.MODE_DEFAULT.
override (bool, optional): Whether the event should be sent regardless of the
current state of that pad. Defaults to False.
"""
if internal.window.getAbsoluteTick() % config.LIGHTS_FULL_REDRAW_FREQUENCY == 0:
full_redraw = True
else:
full_redraw = False
# Handle light offs
if colour == 0 and state == lightingconsts.MODE_DEFAULT:
state = lightingconsts.MODE_OFF
# Set legacy state
elif state == lightingconsts.MODE_DEFAULT:
state = lightingconsts.MODE_ON
if state == lightingconsts.MODE_OFF:
colour = 0
# Check if pad is already in that state - don't bother with event if so
if self.PadColours[x][y] == colour and self.PadStates[x][y] == state and not override and not full_redraw:
return
# Set state variables
self.PadColours[x][y] = colour
self.PadStates[x][y] = state
status_a = 0x9
status_b = 0xF
if state == lightingconsts.MODE_OFF:
status_a = 0x8
elif state == lightingconsts.MODE_PULSE:
status_b = 0x2
elif state > 0:
status_b = 0xF
# Round pads in basic mode
if x == 8 and not internal.extendedMode.query(eventconsts.INCONTROL_PADS):
status_a = 0xB
# Calculate Status
status = (status_a << 4) + status_b
if internal.extendedMode.query(eventconsts.INCONTROL_PADS):
internal.sendMidiMessage(status, eventconsts.Pads[x][y], colour)
internal.debugLog("Sent lighting command [" + str(x) + ", " + str(y) + "] (InControl Enabled)", internal.consts.DEBUG.LIGHTING_MESSAGE)
else:
internal.sendMidiMessage(status, processorhelpers.convertPadMapping(eventconsts.Pads[x][y]), colour)
internal.debugLog("Sent lighting command [" + str(x) + ", " + str(y) + "] (InControl Disabled)", internal.consts.DEBUG.LIGHTING_MESSAGE)
if state > 0: # Send extra event to trigger flashing
status_b = 0x1
status = (status_a << 4) + status_b
if internal.extendedMode.query(eventconsts.INCONTROL_PADS):
internal.sendMidiMessage(status, eventconsts.Pads[x][y], state)
internal.debugLog("Sent light flash command [" + str(x) + ", " + str(y) + "] (InControl Enabled)", internal.consts.DEBUG.LIGHTING_MESSAGE)
else:
internal.sendMidiMessage(status, processorhelpers.convertPadMapping(eventconsts.Pads[x][y]), state)
internal.debugLog("Sent light flash command [" + str(x) + ", " + str(y) + "] (InControl Disabled)", internal.consts.DEBUG.LIGHTING_MESSAGE)
def setFromMap(self, map):
"""Set light colours from LightMap object
Args:
map (LightMap): What to set the lights to
"""
map.solidifyAll()
for x in range(len(self.PadColours)):
for y in range(len(self.PadColours[x])):
self.setPadColour(x, y, map.PadColours[x][y], map.PadStates[x][y])
return
def redraw(self):
"""Resends all lighting options from current state.
"""
for x in range(len(self.PadColours)):
for y in range(len(self.PadColours[x])):
self.setPadColour(x, y, self.PadColours[x][y], override=True)
state = Lights()
def initLightShow(lights):
if internal.state.SHARED_INIT_STATE == internal.consts.INIT_OK:
rainbowColours = lightingconsts.PALLETE_NORMAL
elif internal.state.SHARED_INIT_STATE == internal.consts.INIT_API_OUTDATED or internal.state.SHARED_INIT_STATE == internal.consts.INIT_PORT_MISMATCH:
rainbowColours = lightingconsts.PALLETE_INIT_FAIL
elif internal.state.SHARED_INIT_STATE == internal.consts.INIT_UPDATE_AVAILABLE:
rainbowColours = lightingconsts.PALLETE_UPDATE
else:
rainbowColours = lightingconsts.PALLETE_INIT_FAIL
x = internal.window.getAnimationTick()
if x > len(rainbowColours) + 8: return
# Group 1
if (x >= 0) and (x < len(rainbowColours)):
lights.setPadColour(0, 1, rainbowColours[x])
# Group 2
if (x >= 1) and (x < len(rainbowColours) + 1):
lights.setPadColour(1, 1, rainbowColours[x - 1])
lights.setPadColour(0, 0, rainbowColours[x - 1])
# Group 3
if (x >= 2) and (x < len(rainbowColours) + 2):
lights.setPadColour(2, 1, rainbowColours[x - 2])
lights.setPadColour(1, 0, rainbowColours[x - 2])
# Group 4
if (x >= 3) and (x < len(rainbowColours) + 3):
lights.setPadColour(3, 1, rainbowColours[x - 3])
lights.setPadColour(2, 0, rainbowColours[x - 3])
# Group 5
if (x >= 4) and (x < len(rainbowColours) + 4):
lights.setPadColour(4, 1, rainbowColours[x - 4])
lights.setPadColour(3, 0, rainbowColours[x - 4])
# Group 6
if (x >= 5) and (x < len(rainbowColours) + 5):
lights.setPadColour(5, 1, rainbowColours[x - 5])
lights.setPadColour(4, 0, rainbowColours[x - 5])
# Group 7
if (x >= 6) and (x < len(rainbowColours) + 6):
lights.setPadColour(6, 1, rainbowColours[x - 6])
lights.setPadColour(5, 0, rainbowColours[x - 6])
# Group 8
if (x >= 7) and (x < len(rainbowColours) + 7):
lights.setPadColour(7, 1, rainbowColours[x - 7])
lights.setPadColour(6, 0, rainbowColours[x - 7])
# Group 9
if (x >= 8) and (x < len(rainbowColours) + 8):
lights.setPadColour(7, 0, rainbowColours[x - 8])
lights.solidifyAll()
def idleLightshow(lights):
"""EVEN PRETTIER!!!!!!!!!!
Args:
lights (LightMap): LightMap object to draw to
"""
if internal.window.getIdleTick() > config.IDLE_WAIT_TIME and config.IDLE_LIGHTS_ENABLED:
tick_num = internal.window.getIdleTick() - int(config.IDLE_WAIT_TIME) + IDLE_ANIMATION_TRAIL_LENGTH * IDLE_ANIMATION_TRAIL_SPEED
for x in range(9):
for y in range(2):
# Generate colours
if IDLE_ANIMATION_DO_TRAILS:
animation_speed = IDLE_ANIMATION_SPEED * IDLE_ANIMATION_TRAIL_SPEED_MODIFIER
else:
animation_speed = IDLE_ANIMATION_SPEED
colour = ((tick_num // animation_speed) + x - y) // IDLE_ANIMATION_STRETCH % 128
light_mode = lightingconsts.MODE_ON
# Set off to on
if colour == 0:
colour = 1
if IDLE_ANIMATION_DO_TRAILS:
if not (((tick_num // IDLE_ANIMATION_TRAIL_SPEED) + x + IDLE_ANIMATION_TRAIL_Y_OFFSET*y) % IDLE_ANIMATION_TRAIL_INFREQUENCY < IDLE_ANIMATION_TRAIL_LENGTH):
colour = lightingconsts.colours["OFF"]
lights.setPadColour(x, y, colour, light_mode)
def idleLightshowActive():
"""Whether the idle lightshow is currently active
Returns:
bool: Whether it's active
"""
return internal.window.getIdleTick() > config.IDLE_WAIT_TIME and config.IDLE_LIGHTS_ENABLED
def triggerIdleLightshow():
"""Set idle tick number to tick required for lightshow to begin.
Call this to get that sweet sweet RGB going.
"""
internal.window.idle_tick_number = config.IDLE_WAIT_TIME