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
|
#!/usr/bin/python
"""Simple demo of oscillating colors."""
from __future__ import division
import colorsys
import lumiversepython
import math
import random
import time
FRAME_RATE = 44
MIN_VALUE = 0
MAX_VALUE = 1
MAX_TIME = 4
MIN_TIME = 0.3
MAX_HUE = 169 / 360
MIN_HUE = 250 / 360
class Vector(object):
"""N-dimensional vector class."""
def __init__(self, *components):
"""Create a vector from its components."""
self.components = components
def __add__(self, other):
"""Add two vectors."""
return Vector(
*map(lambda x, y: x + y, zip(self.components, other.components))
)
def __sub__(self, other):
"""Subtract two vectors."""
return Vector(
*map(lambda x, y: x - y, zip(self.components, other.components))
)
def __mul__(self, other):
"""Multiply a vector by a constant."""
return Vector(*map(lambda x: x * other, self.components))
def __rmul__(self, other):
"""Multiply a constant by a vector."""
return self.__mul__(other)
def __abs__(self):
"""Return the 2-norm of the vector."""
return math.sqrt(sum(map(lambda x: x * x, self.components)))
def HSV(Vector):
"""HSV 3-vector."""
def __init__(self, hue, saturation, value)
super().__init__(hue, saturation, value)
def h(self):
"""Return hue."""
return self.components[0]
def s(self):
"""Return saturation."""
return self.components[1]
def v(self):
"""Return value."""
return self.components[2]
def get_all_lights(rig):
"""Return a list of all panels."""
return [
rig.select('$side={}[$sequence={}]'.format(j, i))
for j in ['top', 'bot'] for i in range(1, 200)
]
def pick_HSV():
"""Return a random color in an appropriate interval."""
hue = random.random() * (MAX_HUE - MIN_HUE) + MIN_HUE
saturation = random.random()
value = random.random()
return HSV(hue, saturation, value)
def transition(start, end, step):
"""Return a vector equal to (end - start) * step + start."""
return (end - start) * step + start
def random_change():
"""Return a random amount that a light should change each frame."""
min_time = int(MIN_TIME * FRAME_RATE)
max_time = int(MAX_TIME * FRAME_RATE)
return 1 / random.randint(min_time, max_time)
def HSV_to_RGB(h):
"""Convert HSV to a 3-tuple of RGB."""
return colorsys.hsv_to_rgb(h.h(), h.s(), h.v())
def demo(lights, rig):
"""Do some light things."""
light_status = [
{
'change': 0,
'light': light,
'current value': Vector(0, 0, 0),
'transitioning to': Vector(0, 0, 0),
}
for light in lights
]
while True:
for light_s in light_status:
change = light_s['change']
light = light_s['light']
current_value = light_s['current value']
transitioning_to = light_s['transitioning to']
if abs(transitioning_to - current_value) < change:
change = random_change()
light_s['change'] = change
transitioning_to = pick_HSV()
light_s['transitioning to'] = transitioning_to
new_color = transition(current_value, transitioning_to, change)
rgb = HSV_to_RGB(new_color)
light.setRGBRaw(*rgb)
light_s['current value'] = new_color
rig.updateOnce()
time.sleep(1 / FRAME_RATE)
def main():
"""Run show."""
rig = lumiversepython.Rig('/home/teacher/Lumiverse/PBridge.rig.json')
rig.init()
lights = get_all_lights(rig)
demo(lights, rig)
if __name__ == '__main__':
main()
|