-
Notifications
You must be signed in to change notification settings - Fork 0
/
unicorn_tools.py
150 lines (115 loc) · 3.98 KB
/
unicorn_tools.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
# unicorn_tools.py
from PIL import Image, ImageDraw, ImageFont
from time import sleep
import unicornhathd as uni
_WIDTH, _HEIGHT = uni.get_shape()
_FONT = "/usr/share/fonts/truetype/roboto/Roboto-Bold.ttf"
_FONTSIZE = 12
# Functions for unicornhathd
def set_pixel(x, y, r, g, b) -> None:
uni.set_pixel(x, y, r, g, b)
def set_pixel_hsv(x, y, h, s=1.0, v=1.0) -> None:
uni.set_pixel_hsv(x, y, h, s, v)
def brightness(b: float) -> None:
uni.brightness(b)
def rotation(r: int) -> None:
uni.rotation(r)
def get_rotation() -> int:
return uni.get_rotation()
def show() -> None:
uni.show()
def clear() -> None:
uni.clear()
def off() -> None:
uni.off()
def get_shape() -> (int, int):
return uni.get_shape()
# Undocumented functions
def set_all(r, g, b) -> None:
uni.set_all(r, g, b)
def get_pixels() -> [[[int, int, int]]]:
return uni.get_pixels()
def shade_pixels(shader) -> None:
uni.shade_pixels(shader)
# Additional functions
def set_pixels(pixel_list: [[[int, int, int]]]) -> None:
"""Updates the unicorn given the array of colors"""
for row in range(_HEIGHT):
for col in range(_WIDTH):
r, g, b = pixel_list[row][col]
set_pixel(row, col, r, g, b)
def set_pixel_big(x, y, r, g, b) -> None:
"""Sets a 2x2 area of pixels"""
x *= 2
y *= 2
set_pixel(x, y, r, g, b)
set_pixel(x+1, y, r, g, b)
set_pixel(x, y+1, r, g, b)
set_pixel(x+1, y+1, r, g, b)
def line_h(x, r, g, b) -> None:
"""Draws a horizontal line at x position"""
for y in range(_WIDTH):
set_pixel(x, y, r, g, b)
def line_v(y, r, g, b) -> None:
"""Draws a vertical line at y position"""
for x in range(_HEIGHT):
set_pixel(x, y, r, g, b)
def show_letter(s: str, fg: (int)=(255,255,255), bg: (int)=(0,0,0)) -> None:
"""Displays a single character (or more)"""
font = ImageFont.truetype(_FONT, _FONTSIZE)
w, h = font.getsize(s)
image = Image.new("RGB", (_WIDTH, _HEIGHT), bg)
ImageDraw.Draw(image).text(((_WIDTH-w)/2, (_HEIGHT-h*1.5)/2), s, fg, font)
image = image.rotate(270) # rotate the image
for x in range(_WIDTH):
for y in range(_HEIGHT):
pixel = image.getpixel((x, y))
r, g, b = (int(n) for n in pixel)
set_pixel(-x-1, y, r, g, b)
show()
def show_message(text: str, speed: float=0.02,
fg: (int)=(255,255,255), bg: (int)=(0,0,0)) -> None:
"""Displays a scrolling text"""
old_rotation = get_rotation()
rotation(old_rotation + 270) # rotate
font = ImageFont.truetype(_FONT, _FONTSIZE)
w, h = font.getsize(text)
text_x, text_y = _WIDTH+1, 0
text_width = w + _WIDTH*3 # padding
image = Image.new("RGB", (text_width, _HEIGHT), bg)
ImageDraw.Draw(image).text((text_x, text_y), text, fg, font)
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)
set_pixel(_WIDTH - 1 - x, y, r, g, b)
show()
sleep(speed)
rotation(old_rotation)
clear()
show()
def char_sequence(text: str, speed: float=0.3,
fg: (int)=(255,255,255), bg: (int)=(0,0,0)) -> None:
"""Displays text character by character"""
for c in tuple(text):
show_letter(c, fg, bg)
sleep(speed)
clear()
show()
sleep(speed/3)
def show_image(filename: str):
"""Opens an image file and displays it"""
try:
image = Image.open(filename).resize((_WIDTH, _HEIGHT))
image = image.rotate(270)
if image.mode != "RGB":
image = image.convert("RGB")
for x in range(_WIDTH):
for y in range(_HEIGHT):
pixel = image.getpixel((x, y))
r, g, b = (int(n) for n in pixel)
set_pixel(-x-1, y, r, g, b)
show()
finally:
image.close()