forked from goofmint/isaax-unicorn-hat-hd-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
72 lines (49 loc) · 1.7 KB
/
app.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
#!/usr/bin/env python
import colorsys
import signal
import time
import os
import re
from sys import exit
try:
from PIL import Image, ImageDraw, ImageFont
except ImportError:
exit("This script requires the pillow module\nInstall with: sudo pip install pillow")
import unicornhathd
def show():
message = os.environ.get('MESSAGE')
lines = message.split(',')
colours = [tuple([int(n * 255) for n in colorsys.hsv_to_rgb(x/float(len(lines)), 1.0, 1.0)]) for x in range(len(lines))]
FONT = ("/usr/share/fonts/truetype/roboto/Roboto-Bold.ttf", 10)
unicornhathd.rotation(0)
unicornhathd.brightness(1.0)
width, height = unicornhathd.get_shape()
text_x = width
text_y = 2
font_file, font_size = FONT
font = ImageFont.truetype(font_file, font_size)
text_width, text_height = width, 0
for line in lines:
w, h = font.getsize(line)
text_width += w + width
text_height = max(text_height,h)
text_width += width + text_x + 1
image = Image.new("RGB", (text_width,max(16, text_height)), (0,0,0))
draw = ImageDraw.Draw(image)
offset_left = 0
for index, line in enumerate(lines):
draw.text((text_x + offset_left, text_y), line, colours[index], font=font)
offset_left += font.getsize(line)[0] + width
for scroll in range(text_width - width):
for x in range(width):
for y in range(height):
pixel = image.getpixel((x+scroll, y))
r, g, b = [int(n) for n in pixel]
unicornhathd.set_pixel(width-1-x, y, r, g, b)
unicornhathd.show()
time.sleep(0.01)
unicornhathd.off()
return True
while True:
show()
time.sleep(3)