-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstranger.py
executable file
·150 lines (117 loc) · 3.68 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
import colorsys
import os
import random
import time
from threading import Thread
from rpi_ws281x import *
from messages import messages
LED_COUNT = 50
GPIO_PIN = 10
LED_FREQ_HZ = 800000
LED_DMA = 5
LED_BRIGHTNESS = 127
LED_INVERT = False
CHAR_IDX = {'A': 0, 'B': 2, 'C': 3, 'D': 5, 'E': 7, 'F': 9, 'G': 11, 'H': 13, 'I': 32, 'J': 30, 'K': 28, 'L': 27,
'M': 25, 'N': 23, 'O': 21, 'P': 19, 'Q': 18, 'R': 36, 'S': 37, 'T': 39, 'U': 40, 'V': 42, 'W': 44, 'X': 46,
'Y': 48, 'Z': 49, ' ': "NONE", '!': "FLASH", '*': "CREEP"}
strip = Adafruit_NeoPixel(LED_COUNT, GPIO_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS)
strip.begin()
strip.show()
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 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
for c in msg:
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(5)
elif i == "CREEP":
creep(50)
else:
set_color(i, color_of(i))
strip.show()
time.sleep(1)
set_all((0, 0, 0))
strip.show()
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:
set_all((0, 0, 0))
strip.show()
time.sleep(2)
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()