This repository was archived by the owner on May 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisplay.py
More file actions
226 lines (190 loc) · 7.6 KB
/
Display.py
File metadata and controls
226 lines (190 loc) · 7.6 KB
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
import rpi_ws281x as npx
import RPi.GPIO as GPIO
# import RPIO
import colorsys
import random
import time
import multiprocessing
import queue
import threading
import logging
ADDR_MAP = {'A': 24, 'B': 23, 'C': 22, 'D': 21, 'E': 20,
'F': 15, 'G': 16, 'H': 17, 'I': 18, 'J': 19,
'K': 10, 'L': 11, 'M': 12, 'N': 13, 'O': 14,
'P': 5, 'R': 6, 'S': 7, 'T': 8, 'U': 9,
'V': 4, 'W': 3, 'X': 2, 'Y': 1, 'Z': 0}
# LED strip configuration:
LED_COUNT = 25 # Number of LED pixels.
LED_PIN = 18 # GPIO pin connected to the pixels (18 uses PWM!).
LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz)
LED_DMA = 9 # DMA channel to use for genera ting signal (try 10)
LED_BRIGHTNESS = 255 # Set to 0 for darkest aand 255 for brightest
LED_INVERT = False # True to invert the signal (when using NPN transistor level shift)
LED_CHANNEL = 0 # set to '1' for GPIOs 13, 19, 41, 45 or 53
CHAR_ON_PERIOD = 1.3
DELAY_BETWEEN_CHARS = 0.2
MESSAGE_DELAY = 1.5
BEAT_PIN = 25
TEST_PIN = 16
GPIO.setmode(GPIO.BCM)
GPIO.setup(BEAT_PIN, GPIO.IN)
# GPIO.setup(TEST_PIN, GPIO.OUT)
# GPIO.output(TEST_PIN, GPIO.LOW)
def random_color():
h = random.uniform(0.0, 1.0)
s = random.uniform(0.5, 1.0)
v = random.uniform(0.7, 1.0)
color = colorsys.hsv_to_rgb(h, s, v)
rgb255 = [int(x * 255) for x in color]
return npx.Color(*rgb255)
def wheel(pos):
"""Generate rainbow colors across 0-255 positions."""
if pos < 85:
return npx.Color(pos * 3, 255 - pos * 3, 0)
elif pos < 170:
pos -= 85
return npx.Color(255 - pos * 3, 0, pos * 3)
else:
pos -= 170
return npx.Color(0, pos * 3, 255 - pos * 3)
class Display:
def __init__(self):
self.log = logging.getLogger("client")
self.strip = npx.Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL)
self.strip.begin()
self.in_queue = queue.PriorityQueue()
self.out_queue = queue.Queue()
self.beat_flag = threading.Event()
self.animation_process = None
self.brightness = multiprocessing.Event()
self.beat_flag.set()
GPIO.add_event_detect(
BEAT_PIN,
GPIO.RISING,
callback=self.beat_callback,
bouncetime=10
)
self.strip.setBrightness(0)
time.sleep(1)
self.strip.setBrightness(1)
def show_char(self, c, color=None):
led = ADDR_MAP.get(c, 26) # 26 is out of range, nothing will light up
self.strip.setPixelColor(led, color if color else random_color())
self.strip.show()
time.sleep(CHAR_ON_PERIOD)
self.strip.setPixelColor(led, npx.Color(0, 0, 0))
self.strip.show()
def show_message(self, msg):
for c in msg.replace(' ', ''):
self.show_char(c)
time.sleep(DELAY_BETWEEN_CHARS)
def start_random_animations(self):
self.strip.setBrightness(8)
self.animation_process = multiprocessing.Process(target=self.random_forever)
self.animation_process.start()
def run_forever(self):
while True:
if self.in_queue.empty():
self.start_random_animations()
_, msg, uuid = self.in_queue.get()
if self.animation_process is not None and self.animation_process.is_alive():
self.animation_process.terminate()
self.clear_strip()
self.strip.setBrightness(255)
if msg == "ANIMATION":
self.random_animation()
self.clear_strip()
self.strip.setBrightness(255)
continue
self.show_message(msg)
self.out_queue.put(uuid)
time.sleep(MESSAGE_DELAY)
def clear_strip(self):
for i in range(self.strip.numPixels()):
self.strip.setPixelColor(i, npx.Color(0, 0, 0))
self.strip.show()
def flash(self):
for i in range(0, self.strip.numPixels()):
self.strip.setPixelColor(i, random_color())
self.strip.show()
while 1:
if self.brightness.is_set():
self.strip.setBrightness(50)
else:
self.strip.setBrightness(3)
self.strip.show()
def theater_chase(self, color=random_color(), wait_ms=50, iterations=20):
"""Movie theater light style chaser animation."""
for j in range(iterations):
for q in range(3):
for i in range(0, self.strip.numPixels(), 3):
self.strip.setPixelColor(i + q, color)
self.strip.show()
time.sleep(wait_ms / 1000.0)
for i in range(0, self.strip.numPixels(), 3):
self.strip.setPixelColor(i + q, 0)
def rainbow(self, wait_ms=20, iterations=1):
"""Draw rainbow that fades across all pixels at once."""
for j in range(256*iterations):
for i in range(self.strip.numPixels()):
self.strip.setPixelColor(i, wheel((i+j) & 255))
self.strip.show()
time.sleep(wait_ms/1000.0)
def rainbow_cycle(self, wait_ms=20, iterations=5):
"""Draw rainbow that uniformly distributes itself across all pixels."""
for j in range(256*iterations):
for i in range(self.strip.numPixels()):
self.strip.setPixelColor(i, wheel((int(i * 256 / self.strip.numPixels()) + j) & 255))
self.strip.show()
time.sleep(wait_ms/1000.0)
def theater_chase_rainbow(self, wait_ms=50):
"""Rainbow movie theater light style chaser animation."""
for j in range(256):
for q in range(3):
for i in range(0, self.strip.numPixels(), 3):
self.strip.setPixelColor(i+q, wheel((i+j) % 255))
self.strip.show()
time.sleep(wait_ms/1000.0)
for i in range(0, self.strip.numPixels(), 3):
self.strip.setPixelColor(i+q, 0)
def wills_speech(self):
self.show_message("RIGHTHERE")
time.sleep(2)
self.show_message("RUN")
def dun_dun(self, delay=1, scalar=0.85):
addr = [x for x in range(len(ADDR_MAP))]
for i in range(len(addr)):
rand_addr = random.choice(addr)
self.strip.setPixelColor(rand_addr, random_color())
self.strip.show()
addr.remove(rand_addr)
time.sleep(delay*scalar**i)
time.sleep(3)
addr = [x for x in range(len(ADDR_MAP))]
for _ in range(len(addr)):
a = random.choice(addr)
addr.remove(a)
self.strip.setPixelColor(a, npx.Color(0, 0, 0))
self.strip.show()
time.sleep(0.03)
time.sleep(0.3)
def random_animation(self):
self.clear_strip()
animation_list = [
# self.theater_chase,
# self.rainbow,
# self.rainbow_cycle,
# self.dun_dun
self.flash
]
random.choice(animation_list)()
def beat_callback(self, _):
self.brightness.set()
time.sleep(0.01)
self.brightness.clear()
def mock_brightness(self, val):
self.log.info("brightness changed to {}".format(val))
GPIO.output(TEST_PIN, val)
def random_forever(self):
while True:
self.random_animation()