Skip to content

Commit fbfd558

Browse files
committed
Add display-text.py
just a simple script to scroll some text across the UnicornHAT HD
1 parent a92c0b1 commit fbfd558

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

display-text.py

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/usr/bin/env python3
2+
3+
import colorsys
4+
import time
5+
import sys
6+
import getopt
7+
from sys import exit
8+
import signal
9+
import os
10+
11+
try:
12+
from PIL import Image, ImageDraw, ImageFont
13+
except ImportError:
14+
exit('This script requires the pillow module\nInstall with: sudo pip install pillow')
15+
16+
import unicornhathd
17+
18+
19+
def sig_handler(signum, frame):
20+
unicornhathd.off()
21+
sys.exit(0)
22+
23+
def run_ticker(bg_color, text):
24+
FONT = ("DejaVuSans-Bold.ttf", 10)
25+
unicornhathd.rotation(90)
26+
unicornhathd.brightness(0.6)
27+
width, height = unicornhathd.get_shape()
28+
text_x = width
29+
text_y = 2
30+
font_file, font_size = FONT
31+
font = ImageFont.truetype(font_file, font_size)
32+
text_width, text_height = width, 0
33+
w, h = font.getsize(text)
34+
text_width += w + width
35+
text_height = max(text_height, h)
36+
text_width += width + text_x + 1
37+
38+
image = Image.new('RGB', (text_width, max(16, text_height)), color)
39+
draw = ImageDraw.Draw(image)
40+
offset_left = 0
41+
draw.text((text_x + offset_left, text_y), text, fill="white", font=font)
42+
offset_left += font.getsize(text)[0] + width
43+
while True:
44+
for scroll in range(text_width - width):
45+
for x in range(width):
46+
for y in range(height):
47+
pixel = image.getpixel((x + scroll, y))
48+
r, g, b = [int(n) for n in pixel]
49+
unicornhathd.set_pixel(width - 1 - x, y, r, g, b)
50+
51+
unicornhathd.show()
52+
time.sleep(0.01)
53+
54+
signal.signal(signal.SIGTERM, sig_handler)
55+
56+
try:
57+
opts, args = getopt.getopt(sys.argv[1:], "c:", ["color"])
58+
color = "black"
59+
60+
for opt, arg in opts:
61+
if opt in ("-c", "--color"):
62+
color = arg
63+
64+
text = args[0]
65+
run_ticker(color, text)
66+
67+
except getopt.GetoptError:
68+
print("display-text.py -c <hex color|color name> <text>")
69+
sys.exit(2)
70+
except KeyboardInterrupt:
71+
unicornhathd.off()
72+
73+
finally:
74+
unicornhathd.off()

0 commit comments

Comments
 (0)