This repository has been archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lights.py
493 lines (379 loc) · 15.1 KB
/
lights.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
import sys
import threading
from multiprocessing import Process, Value
from time import sleep, time
from edi2c import ads1x15
from edi2c import pca9685
FREQUENCY = 180
MAX_VALUE = 700
MAP = [6, 7, 4, 8, 5, 3, 10, 1, 9, 2, 11, 0]
LEVELS = [
[0, 4],
[1, 2],
[3, 5],
[6, 7],
[8, 9],
[10, 11],
]
class ProgramBuilder:
program = None
activations = None
duration = 0.0
def __init__(self):
self.clear()
def get(self):
if len(self.activations) > 0:
self.step()
return self.program
def duration(self, duration):
self.duration = float(duration)
return self
def clear(self):
self.program = []
self.activations = []
self.duration = 0.0
return self
def add(self, channel, value):
self.activations.append((channel, value))
return self
def step(self, duration=None):
if duration is not None:
self.duration = duration
self.program.append((self.duration, self.activations))
self.duration = 0.0
self.activations = []
return self
def lerp(self, duration=None, resolution=100):
"""Linear interpolation between program state and current activations.
"""
if duration is None:
duration = self.duration
else:
duration = float(duration)
if duration <= 0 or resolution <= 1:
raise ValueError, "Invalid duration/resolution"
if not len(self.program):
begin = {}
begin_duration = 0
else:
begin = dict(self.program[-1][1])
begin_duration, begin_activations = self.program.pop()
if not len(self.activations):
end = {}
else:
end = dict(self.activations)
channels = set(begin.keys() + end.keys())
t_step = duration / resolution
lerp_program = []
for i in range(resolution + 1):
p = float(i) / resolution
t_activations = {}
for channel in channels:
a1 = begin.get(channel, 0)
a2 = end.get(channel, 0)
t_activations[channel] = a1 + (a2 - a1) * p
if i == 0:
lerp_program.append((begin_duration, t_activations.items()))
else:
lerp_program.append((t_step, t_activations.items()))
self.program.extend(lerp_program)
self.activations = []
self.duration = 0.0
return self
class Lights:
PROGRAM_FULL_ON = ProgramBuilder().add(None, 1.0).get()
PROGRAM_FULL_OFF = ProgramBuilder().add(None, 0.0).get()
pwm = None
debug = False
def __init__(self, debug=False):
self.debug = debug
self.pwm = pca9685.PCA9685(debug=debug)
self.reset()
def reset(self):
self.pwm.reset(frequency=FREQUENCY)
def set_all(self, intensity):
self.run_program(ProgramBuilder().add(None, intensity).get())
def set_all_on(self):
self.run_program(Lights.PROGRAM_FULL_ON)
def set_all_off(self):
self.run_program(Lights.PROGRAM_FULL_OFF)
def run_program(self, program, debug=False):
"""Run a program given as a list of times and activations (float 0->1):
duration1, [(channel1A, value1A), (channel1B, value1B)]
Alternatively, use the ProgramBuilder:
ProgramBuilder().add(channel1A, value1A).add(channel1B, value1B).step(duration).get()
"""
pca_program = []
for duration, activations in program:
pca_activations = []
for channel, off in activations:
if off < 0 or off > 1:
raise ValueError, "Invalid activation value: must be in [0,1]"
off = int(off * MAX_VALUE)
if channel is None or channel < 0 or channel >= len(MAP):
pca_activations.extend((c, 0, off) for c in MAP)
else:
pca_activations.append((MAP[channel], 0, off))
pca_program.append((duration, pca_activations))
self.pwm.run_program(pca_program, debug=debug)
class ProximityLights(Lights):
program_on = None
program_off = None
builder_on = None
builder_off = None
current_space = None
def __init__(self, *args, **kwargs):
Lights.__init__(self, *args, **kwargs)
self.generate_program()
def generate_program(self):
self.builder_on = ProgramBuilder()
self.builder_off = ProgramBuilder()
intensity_on = 0
intensity_off = 1
for level in LEVELS:
intensity_on = intensity_on + 0.20
intensity_off = intensity_off - 0.20
for c in level:
self.builder_on.add(c, 1)
self.builder_off.add(c, 0)
self.builder_on.step()
self.builder_off.step()
self.program_on = self.builder_on.get()
self.program_off = self.builder_off.get()
def set_proximity(self, value):
on = 1
steps = len(self.program_on) - 1
step = min(steps, max(0, int(round(steps * on))))
if value == 0:
sleep_time = 0.100
elif value == 1:
sleep_time = 0.500
elif value == 2:
sleep_time = 1
while True:
intensity_on = 0
sleep(0.500)
for level in LEVELS:
intensity_on = intensity_on + 0.20
if self.current_space == 0:
self.set_all(intensity_on)
self.set_all_off()
elif self.current_space == 1:
self.set_all(intensity_on)
sleep(0.10)
elif self.current_space == 2:
self.set_all(intensity_on)
sleep(0.50)
else:
self.set_all_off()
class MindwaveLights(Lights):
program_on = None
program_off = None
def __init__(self, *args, **kwargs):
Lights.__init__(self, *args, **kwargs)
self.generate_program()
def generate_program(self):
builder_on = ProgramBuilder()
builder_off = ProgramBuilder()
for level in LEVELS:
for c in level:
builder_on.add(c, 1)
builder_off.add(c, 0)
builder_on.step()
builder_off.step()
self.program_on = builder_on.get()
self.program_off = builder_off.get()
def intensity_map(self, value):
return max(0.0, float(value - 50) / (90.0 - 50.0))
def set_mindwave(self, value):
on = self.intensity_map(value)
steps = len(self.program_on) - 1
step = min(steps, max(0, int(round(steps * on))))
if self.debug:
print "LIGHTS:", str(round(on * 100)) + "%"
self.run_program(self.program_on[:step+1] + self.program_off[step+1:])
class HeartrateLights(Lights):
program = None
bpm = 1
_running = False
def __init__(self, *args, **kwargs):
Lights.__init__(self, *args, **kwargs)
self.program = None
self.bpm = 1
self._running = False
self.generate_program()
def generate_program(self):
builder = ProgramBuilder()
step_duration = lambda: (60.0 / float(self.bpm)) / float(len(self.program))
self.program = builder.add(None, 1).lerp(1, 10).get()
program_rev = list(self.program)
program_rev.reverse()
self.program += program_rev
# HACK: lerp should allow a callable or not depend on duration if desired
for i, entry in enumerate(self.program):
self.program[i] = (step_duration, entry[1])
def generate_program_old(self):
builder = ProgramBuilder()
step_duration = lambda: (60.0 / float(self.bpm)) / float(len(self.program))
for level in LEVELS:
for channel in level:
builder.add(channel, 1)
for level2 in LEVELS:
if level2 != level:
for channel in level2:
builder.add(channel, 0)
builder.step(step_duration)
self.program = builder.get()
def start(self):
if not self._running:
self._running = True
task = threading.Thread(target=self._run)
task.setDaemon(True)
task.start()
def stop(self):
self._running = False
def _run(self):
while self._running:
self.run_program(self.program, debug=False)
def set_bpm(self, bpm):
if bpm <= 0:
raise ValueError, "Invalid BPM"
self.bpm = bpm
self.start()
class SynapseLights(Lights):
def __init__(self, *args, **kwargs):
Lights.__init__(self, *args, **kwargs)
self._running = False
self.heart = 60
self.mind = 0
self.space = -1
self.space_enable = False
self.task = None
def start(self):
if not self._running:
self.reset()
self._running = True
self.task = threading.Thread(target=self._run)
self.task.setDaemon(True)
self.task.start()
def stop(self):
self._running = False
def join(self, timeout=None):
if not self._running:
return False
self.task.join(timeout)
return self.task is None or not self.task.isAlive()
def set_intensity(self, channel, intensity):
self.pwm.set_off(MAP[channel], int(intensity * MAX_VALUE))
def set_heart(self, heart):
self.heart = heart
def set_mind(self, mind):
self.mind = mind
def set_space(self, space):
self.space = space
def set_space_enable(self, space_enable):
self.space_enable = space_enable
def _run(self):
from math import sin, cos, pi, log
SPACE_INTENSITY_MAP = [1, .4, .2, 0]
SPACE_RATE_MAP = [124, 78, 60, 0]
LEN_LEVELS = len(LEVELS)
ENUMERATED_LEVELS = tuple((level, level / float(LEN_LEVELS), channels) for (level, channels) in enumerate(LEVELS))
INTENSITY_LEVEL_MIN = 1.0 / LEN_LEVELS
start = time()
now = start
delta = 0
elapsed = 0
mind_intensity = 0
intensity_level = 0
intensity = 0
#imap = lambda x: (1 + sin((x - 0.25) * 2 * pi)) / 2
#imap = lambda x: abs(sin(2 * pi * x))
imap = lambda x: (2 + sin(4 * pi * x) - cos(2 * pi * x)) / 4.
try:
while self._running:
delta = time() - now
now += delta
elapsed = now - start
space_enable = self.space_enable
space = self.space
try: space_enable = space_enable.value
except: pass
try: space = space.value
except: pass
if space_enable and 0 <= space <= 3:
for level, level_scaled, channels in ENUMERATED_LEVELS:
for channel in channels:
intensity = SPACE_INTENSITY_MAP[space]
intensity *= imap(elapsed * (SPACE_RATE_MAP[space] / 60.0) - (0.05 * log(1 + level)))
self.set_intensity(channel, intensity)
sleep(0)
else:
mind = self.mind
heart = self.heart
try: mind = mind.value
except: pass
try: heart = heart.value
except: pass
mind_intensity = min(1.0, max(0.0, float(mind - 50) / (90.0 - 50.0)))
amplitude_ramp = max(0.0, 0.15 * (mind_intensity - 0.75) / (1 - 0.75))
for level, level_scaled, channels in ENUMERATED_LEVELS:
intensity_level = min(1.0, max(0.0, mind_intensity - level_scaled))
if level == 0:
intensity_level = 1
for channel in channels:
intensity = imap(elapsed * (heart / 60.0) - (0.05 * log(1 + level))) * intensity_level
intensity = amplitude_ramp + (1 - amplitude_ramp) * intensity
self.set_intensity(channel, intensity)
sleep(0)
finally:
self._running = False
self.reset()
class DemoLights(Lights):
program = None
bpm = 1
_running = False
def __init__(self, *args, **kwargs):
Lights.__init__(self, *args, **kwargs)
self.program = None
self.bpm = 1
self._running = False
self.generate_program()
def generate_program(self):
builder = ProgramBuilder()
step_duration = lambda: (60.0 / float(self.bpm)) / float(len(self.program))
self.program = builder.add(None, 1).lerp(1, 10).get()
program_rev = list(self.program)
program_rev.reverse()
self.program += program_rev
# HACK: lerp should allow a callable or not depend on duration if desired
for i, entry in enumerate(self.program):
self.program[i] = (step_duration, entry[1])
def generate_program_old(self):
builder = ProgramBuilder()
step_duration = lambda: (60.0 / float(self.bpm)) / float(len(self.program))
for level in LEVELS:
for channel in level:
builder.add(channel, 1)
for level2 in LEVELS:
if level2 != level:
for channel in level2:
builder.add(channel, 0)
builder.step(step_duration)
self.program = builder.get()
def start(self):
if not self._running:
self._running = True
task = threading.Thread(target=self._run)
task.setDaemon(True)
task.start()
def stop(self):
self._running = False
def _run(self):
while self._running:
self.run_program(self.program, debug=False)
def set_bpm(self, bpm):
if bpm <= 0:
raise ValueError, "Invalid BPM"
self.bpm = bpm
self.start()