forked from CalebKussmaul/Stranger-Things-Integrated
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stranger.py
executable file
·234 lines (196 loc) · 5.19 KB
/
stranger.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
import colorsys
import os
import random
import time
from threading import Thread
import logging
from twilio import twiml
from rpi_ws281x import *
from messages import messages
LED_COUNT = 50
GPIO_PIN = 18
LED_FREQ_HZ = 800000
LED_DMA = 5
LED_BRIGHTNESS = 15
LED_INVERT = False
LED_CHANNEL = 0
LED_STRIP = ws.WS2811_STRIP_RGB # Strip type and colour ordering
build_iter = 1
CHAR_IDX = {
'A': 48,
'B': 47,
'C': 45,
'D': 43,
'E': 42,
'F': 40,
'G': 39,
'H': 37,
'I': 20,
'J': 22,
'K': 24,
'L': 25,
'M': 26,
'N': 27,
'O': 29,
'P': 31,
'Q': 33,
'R': 14,
'S': 13,
'T': 12,
'U': 10,
'V': 9,
'W': 7,
'X': 5,
'Y': 3,
'Z': 1,
' ': "NONE",
'!': "FLASH",
'*': "CREEP",
'@': "ALPHABET"
}
strip = Adafruit_NeoPixel(LED_COUNT, GPIO_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL, LED_STRIP)
strip.begin()
strip.show()
cancelled = False
level = logging.DEBUG
logging.basicConfig()
logging.getLogger().setLevel(level)
def rand_color():
return color_of(random.random())
def set_color(led, c):
strip.setPixelColor(led, Color(*c))
def set_all(color):
for i in range(0, LED_COUNT):
set_color(i, color)
def color_of(i):
"""
This function generates a color based on the index of an LED. This will always return the same color for a given
index. This allows the lights to function more like normal christmas lights where the color of one bulb wont change.
:param i: index of LED to get color of
:return: a pseudorandom color based on the index of the light
"""
random.seed(i)
rgb = colorsys.hsv_to_rgb(random.random(), 1, 1)
return int(rgb[0] * 255), int(rgb[1] * 255), int(rgb[2] * 255)
def set_all_color_of():
"""
Sets all LEDs to a pseudorandom color, that is the same for each LED every time this function is called
"""
for i in range(0, LED_COUNT):
set_color(i, color_of(i))
def creep(start=0, n=50):
"""
Sequentially illuminates each LED
:param start: Index to start creeping from
:param n: Number between 1 and LED_COUNT of lights to creep.
"""
for i in range(start, n):
set_color((i-1) % LED_COUNT, (0, 0, 0))
set_color(i % LED_COUNT, rand_color())
strip.show()
time.sleep(1)
def build():
global build_iter
clear_all()
strip.show()
if build_iter % 2 == 0:
num_list = range(LED_COUNT, -1, -1)
else:
num_list = range(0, LED_COUNT)
for i in num_list:
if displaying:
break
else:
new_color = rand_color()
strip.setPixelColorRGB(i, new_color[0], new_color[1], new_color[2])
strip.show()
time.sleep(.3)
build_iter += 1
def clear_all():
set_all((0, 0, 0))
strip.show()
def test_all():
clear_all()
for i in range(0, 50):
print("setting color for {}".format(i))
set_color(i, (0, 0, 0))
time.sleep(1)
set_color(i, (255, 0, 0))
strip.show()
time.sleep(2)
set_color(i, (0, 0, 0))
def flash(n):
for i in range(0, n):
set_all_color_of()
strip.show()
time.sleep(1)
set_all((0, 0, 0))
strip.show()
time.sleep(.5)
displaying = False
def display(msg):
global displaying
displaying = True
time.sleep(1)
for c in msg:
clear_all()
set_all((0, 0, 0))
if c.upper() in CHAR_IDX:
i = CHAR_IDX[c.upper()]
if i == "NONE":
"do nothing"
elif i == "FLASH":
flash(3)
elif i == "CREEP":
creep(50)
elif i == "ALPHABET":
alphabet()
else:
set_color(i, color_of(i))
strip.show()
time.sleep(1)
clear_all()
time.sleep(.2)
time.sleep(1)
displaying = False
def listen_on_console(prompt):
print("Enter messages here. To quit, enter \"\\exit\"")
while True:
msg = input(prompt)
if msg == "\\exit":
os._exit(1)
display(msg)
def check_for_message():
print("Enter messages here. To quit, enter \"\\exit\"")
global displaying
while True:
if not displaying:
msg = messages.next_message()
print("displaying: ", msg)
display(msg[:50])
time.sleep(1)
def clear_errors():
"""
Sometimes pixels will randomly turn themselves on. This fixes them by resetting the board every 2 seconds
"""
global displaying
while True:
if not displaying:
clear_all()
build()
time.sleep(2)
# borrowed from https://github.com/jgarff/rpi_ws281x/blob/master/python/examples/strandtest.py
def color_wipe(color, wait_ms=800):
for i in range(strip.numPixels()):
strip.setPixelColor(i, color)
strip.show()
time.sleep(wait_ms / 1000.0)
def alphabet():
display("abcdefghijklmnopqrstuvwxyz")
def start_client():
t0 = Thread(target=listen_on_console, args=("",))
t1 = Thread(target=check_for_message, args=())
t2 = Thread(target=clear_errors, args=())
t0.start()
t1.start()
t2.start()