-
Notifications
You must be signed in to change notification settings - Fork 0
/
mirror.py
115 lines (85 loc) · 2.89 KB
/
mirror.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
#!/usr/bin/python
import math
import time
from array import array
from random import random
from dotstar import Adafruit_DotStar
from lidar_lite import Lidar_Lite
import os
import select
# from evdev import InputDevice, ecodes
lidar = Lidar_Lite()
connected = lidar.connect(1)
if connected < -1:
print "Not Connected"
print lidar.getDistance()
numpixels = 204 # Number of LEDs in strip
pixelLength = numpixels / 2
es = array('f', [0.0] * pixelLength)
strip = Adafruit_DotStar(numpixels)
strip.begin() # Initialize pins for output
color = 0xFFFFFF # 'On' color
def ledForBrightness(norm):
total = int(math.floor(767 * norm))
return ~( (~(total % 256)) << (8 * (total / 256)) )
def candleBright(norm):
return ledForBrightness(max(0.0,min(1.0,norm))) # 2 * min(random() * norm,0.5))
def addEnergy(centrePixel):
for n in range(pixelLength):
es[n] = min(50.0,es[n] + 10/(1+math.pow((n-centrePixel)*2,4)))
def degrade():
for n in range(pixelLength):
es[n] *= 0.99
def showCandle():
for n in range(pixelLength):
setPixelBothSides(n,candleBright(es[n]))
strip.show()
def showThrobs(num, timeSinceInteractionStart, timeSinceInteractionStop):
interactionAge = timeSinceInteractionStart if timeSinceInteractionStop is None else (timeSinceInteractionStart - timeSinceInteractionStop)
print interactionAge
intensity = max(0,math.log1p((interactionAge/10.0)))
# print intensity
for n in range(pixelLength):
distIntensity = 1/(1+math.pow((n-num)/(1+4*timeSinceInteractionStart),2))
throbImportance = 1/(timeSinceInteractionStart/10.0+1)
throb = math.sin((15*timeSinceInteractionStart)-(math.fabs(n-num)*math.pi/16))
foo = intensity * distIntensity * (2+(throbImportance*throb))
setPixelBothSides(n,candleBright(foo))
strip.show()
def setPixelBothSides(n, cb):
strip.setPixelColor(n, cb)
strip.setPixelColor(numpixels - 1 - n, cb)
def sweepTo(c):
for n in range(numpixels):
strip.setPixelColor(n, c)
strip.show()
sweepTo(color)
lit = True
currentlyInteracting = False
interactionStartTime = None
interactionStopTime = None
previousInteractionNum = None
timeSinceInteractionStart = None
timeSinceInteractionStop = None
while True: # Loop forever
d = lidar.getDistance()
num = d / 1.6
t = time.time()
if num > 1 and num < (pixelLength):
if not currentlyInteracting:
interactionStartTime = t
interactionStopTime = None
currentlyInteracting = True
# print interactionAge
previousInteractionNum = num
else:
if currentlyInteracting:
interactionStopTime = t
currentlyInteracting = False
if interactionStartTime is not None:
timeSinceInteractionStart = t - interactionStartTime
if interactionStopTime is None:
timeSinceInteractionStop = None
else:
timeSinceInteractionStop = t - interactionStopTime
showThrobs(previousInteractionNum, timeSinceInteractionStart, timeSinceInteractionStop)