-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
sim.py
executable file
·239 lines (223 loc) · 7.45 KB
/
sim.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
#!/usr/bin/env python3
from os import environ
environ['PYGAME_HIDE_SUPPORT_PROMPT'] = '1'
import pygame
#import pygame.gfxdraw
import time
import modes
import math
# Dimensions measured from the mirror in mm
# The infinity mirror has 34 LEDs, 11 up each side and 6 on top and bottom
hleds = 6
hpadding = 37
width = 300
vleds = 11
vpadding = 34
height = 504
led_width = 5
update_freq_hz = 25 # how often we update the LEDs
class LED:
"""
One of the Infinity Mirror's LEDs
"""
def __init__(self, pos):
self.pos = pos
self.x, self.y = pos
center_x = width/2
center_y = height/2
self.dx = (center_x - self.x)/32
self.dy = (center_y - self.y)/32
#print(self.dx, self.dy)
# Print angle of each LED in degrees
# angle = 270-360*math.atan2(-self.dy, self.dx)/(2*math.pi)
# if angle < 0:
# angle += 360
# if angle >= 360:
# angle -= 360
# print("%.1f" % angle)
def circle(self, surface, col, width, offset_x=0, offset_y=0):
img = pygame.Surface((width, width))
pygame.draw.circle(img, col, (width/2, width/2), width/2)
w = int(width/2+0.5)
#pygame.gfxdraw.filled_circle(img, w, w, w-1, col)
#pygame.gfxdraw.aacircle(img, w, w, w, col)
surface.blit(img, (self.x - width/2 + offset_x, self.y - width/2 + offset_y), special_flags=pygame.BLEND_ADD)
def draw(self, surface, col):
size = 1.0
gap = 10*size
brightness = 1.0
r, g, b = col
diffuse_width = width
diffuse_brightness = 0.04
# Can see 24 LEDs deep
# Draw an LED and a diffuse beam for each LED
for i in range(24):
self.circle(surface, (brightness*r, brightness*g, brightness*b), led_width*size*1.4, offset_x=i*self.dx, offset_y=i*self.dy)
if i % 4 == 0:
self.circle(surface, (r*brightness*diffuse_brightness, g*brightness*diffuse_brightness, b*brightness*diffuse_brightness), diffuse_width*size, offset_x=i*self.dx, offset_y=i*self.dy)
#self.circle(surface, (r*diffuse_brightness, g*diffuse_brightness, b*diffuse_brightness), diffuse_width*size, offset_x=i*self.dx, offset_y=i*self.dy)
size *= 0.95
brightness *= 0.90
#self.circle(surface, (r*0.1, g*0.1, b*0.1), 30*led_width)
#self.circle(surface, (r*0.05, g*0.05, b*0.05), height/2)
class Mirror:
"""
Infinity Mirror with LEDs
"""
def __init__(self):
# LEDs in the mirror in the correct order
hspacing = (width - 2*hpadding)/(hleds-1)
vspacing = (height - 2*vpadding)/(vleds-1)
leds = []
for i in range(hleds):
leds.append(LED((hspacing * i + hpadding, height)))
for i in reversed(range(vleds)):
leds.append(LED((width, vspacing * i + vpadding)))
for i in reversed(range(hleds)):
leds.append(LED((hspacing * i + hpadding, 0)))
for i in range(vleds):
leds.append(LED((0, vspacing * i + vpadding)))
self._leds = leds
self.n = len(leds)
self._cols = [ (0,0,0) ] * self.n
self._knob = [0.5, 0.5, 0.5, 0.6]
self.mode = None
self.set_mode(0)
def __setitem__(self, index, value):
"""
We use mirror[0] = color to set colurs
"""
if index < 0 or index >= self.n:
return
self._cols[index] = value
def __getitem__(self, index):
"""
Read the value set back
"""
if index < 0 or index >= self.n:
return (0,0,0)
return self._cols[index]
def fill(self, col):
"""
Set all the LEDs to col
"""
for i in range(self.n):
self._cols[i] = col
def update(self, screen):
"""
Update the display
Write the state of the LEDs to the screen
Returns the desired delay
"""
delay = self.mode.update()
for i in range(self.n):
self._leds[i].draw(screen, self._cols[i])
return delay
def knob(self, i):
"""
Reads the value of knob i as 0..1
"""
return self._knob[i]
def knob_brightness(self):
"""
Brightness knob
"""
return self.knob(0)
def knob_hue(self):
"""
Hue knob
"""
return self.knob(1)
def knob_speed(self):
"""
Speed knob
"""
return self.knob(2)
def temperature(self):
"""
Return the temperature of the board in C as a floating point number
"""
return self.knob(3)*34
def add_knob(self, i, delta):
"""
Changes the value of knob i by delta
"""
self._knob[i] += delta
if self._knob[i] > 1.0:
self._knob[i] = 1.0
if self._knob[i] < 0.0:
self._knob[i] = 0.0
def set_mode(self, i):
"""
Runs the mode given
"""
i %= len(modes.MODES)
self.mode_number = i
self.mode = modes.MODES[i](self)
print(self.mode.NAME)
def press_button(self, i):
"""
Received a button press
"""
if i == 0:
self.set_mode(self.mode_number + 1)
elif i == 1:
self.set_mode(self.mode_number - 1)
def local_time(self):
"""
Returns the broken down local time
"""
return time.localtime()
def main():
print("Click LEFT, RIGHT to change mode")
print("Mouse wheel to change brightness (knob 0)")
print("SHIFT mouse wheel for hue (knob 1)")
print("CTRL mouse wheel for speed (knob 2)")
print("SHIFT+CTRL mouse wheel for temperature")
pygame.init()
size = (width, height)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Infinity Mirror Simulator")
mirror = Mirror()
quit = False
shift_pressed = False
control_pressed = False
while not quit:
start = time.time()
events = pygame.event.get()
for e in events:
if e.type == pygame.QUIT or (e.type == pygame.KEYDOWN and e.key == pygame.K_ESCAPE):
quit = True
if e.type == pygame.KEYDOWN or e.type == pygame.KEYUP:
if e.key == pygame.K_LSHIFT or e.key == pygame.K_RSHIFT:
shift_pressed = (e.type == pygame.KEYDOWN)
elif e.key == pygame.K_LCTRL or e.key == pygame.K_RCTRL:
control_pressed = (e.type == pygame.KEYDOWN)
if e.type == pygame.MOUSEBUTTONDOWN:
knob_number = 0
if shift_pressed and control_pressed:
knob_number = 3
elif shift_pressed:
knob_number = 1
elif control_pressed:
knob_number = 2
if e.button == 1:
mirror.press_button(0)
elif e.button == 3:
mirror.press_button(1)
elif e.button == 4:
mirror.add_knob(knob_number, 0.05)
elif e.button == 5:
mirror.add_knob(knob_number, -0.05)
screen.fill((0,0,0))
mirror.update(screen)
pygame.display.update()
dt = time.time() - start
delay = 1.0/update_freq_hz - dt
if delay < 0:
#print(f"Dropped frame by {-delay*1000:.2f}ms")
delay = 0
time.sleep(delay)
pygame.quit()
if __name__ == "__main__":
main()