-
Notifications
You must be signed in to change notification settings - Fork 0
/
now_playing.py
212 lines (157 loc) · 4.64 KB
/
now_playing.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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import string
import time
import pytz
from datetime import datetime
from astral import Astral
from PIL import Image, ImageDraw, ImageFont, ImageFile
from pcd8544 import lcd
from fontdemo import Font
from pi_logo import demo
VOLUME_LEVELS = {
'-144.000000': 0,
'-28.125000': 1,
'-26.250000': 2,
'-24.375000': 3,
'-22.500000': 4,
'-20.625000': 5,
'-18.750000': 6,
'-16.875000': 7,
'-15.000000': 8,
'-13.125000': 9,
'-11.250000': 10,
'-9.375000': 11,
'-7.500000': 12,
'-5.625000': 13,
'-3.750000': 14,
'-1.875000': 15,
'0.000000': 16,
}
VOLUME_FULL_BLOCK = [0x41, 0x41, 0x41, 0x41, 0x7F]
VOLUME_BLANK_BLOCK = [0x7F, 0x7F, 0x7F, 0x7F, 0x7F]
VOLUME_BAR_HEAD = [0x7F, 0x7F]
VOLUME_BAR_TAIL = [0x7F, 0x7F]
def get_volume_bar_data(volume):
volume_level = VOLUME_LEVELS.get(volume, 0)
return VOLUME_BAR_HEAD + VOLUME_FULL_BLOCK * volume_level + VOLUME_BLANK_BLOCK * (16 - volume_level) + VOLUME_BAR_TAIL
def display_banner():
lcd.locate(0, 5)
lcd.text(' @wolfg1969 ')
def display_volume_bar(volume='-24.375000'):
lcd.locate(0, 4)
lcd.data(get_volume_bar_data(volume))
def is_night():
a = Astral()
city = a['Beijing'] # Replace with your city
now = datetime.now(pytz.utc)
sun = city.sun(date=now, local=True)
return now >= sun['dusk'] or now <= sun['dawn'] # "It's dark outside"
def get_bitmap(text, **kwargs):
fnt = kwargs.get('font')
font = kwargs.get('imageFont1')
font2 = kwargs.get('imageFont2')
im = Image.new('L', (84, 32))
draw = ImageDraw.Draw(im)
width, height, baseline = fnt.text_dimensions(text)
previous_char = None
pos_x = 0
pos_y = 0
line_break = False
line_start = 0
line = 1
for char in text:
char_left = line_start
glyph = fnt.glyph_for_character(char)
# Take kerning information into account before we render the
# glyph to the output bitmap.
kerning_offset = fnt.kerning_offset(previous_char, char)
line_start += kerning_offset
previous_char = char
line_start += glyph.advance_width
if line_start > 84 or char == '\n':
line_start = kerning_offset + glyph.advance_width
line += 1
char_left = 0
if char == '\n':
line_start = 0
continue
pos_x = char_left
pos_y = (line - 1) * (height+1)
if (pos_y + height) > 32:
break
# print line, line_start, pos_x, pos_y, char
if char in string.printable:
draw.text((pos_x, pos_y), char, font=font2, fill=1)
else:
draw.text((pos_x, pos_y), char, font=font, fill=1)
img_bytes = im.tobytes()
# print(len(img_bytes), [ord(x) for x in img_bytes])
cols = []
for x in range(84):
col = []
for y in range(32):
p = ord(img_bytes[y*84 + x])
if (p > 1):
p = 1
col.append(str(p))
cols.append(col)
# print(len(cols))
bitmap = []
x = 1
for i in range(4):
# print col
j = i * 8
for col in cols:
b = col[j:j+8]
b.reverse()
bitmap.append(int(''.join(b), 2))
del im
return bitmap
def display_song_meta(text, **kwargs):
lcd.locate(0, 0)
bitmap = get_bitmap(text, **kwargs)
lcd.data(bitmap)
def send_to_display(fields, **kwargs):
artist=fields[0]
title=fields[1]
volume = fields[6]
text = u"{artist} {title}".format(artist=artist, title=title, )
lcd.backlight(not is_night())
# lcd.backlight(1)
display_song_meta(text, **kwargs)
display_volume_bar(volume)
display_banner()
def read_now_playing(**kwargs):
bufferSize = 1024
PATH = "/home/pi/shairport/now_playing"
fifo = os.open(PATH, os.O_RDONLY)
fd = os.fdopen(fifo, 'r', bufferSize)
fields = []
while True:
line = fd.readline()
if not line or line == '\n':
# print(u', '.join(fields))
send_to_display(fields, **kwargs)
fields = []
continue
field = line.strip().split('=')
# print(field)
fields.append(field[1].decode('utf-8'))
fd.close()
os.close(fifo)
lcd.cls()
if __name__ == '__main__':
lcd.init(contrast=0xBB)
demo()
time.sleep(2)
lcd.cls()
display_volume_bar()
display_banner()
font_info = {
'font': Font('Zpix.ttf', 12),
'imageFont1': ImageFont.truetype("Zpix.ttf", 12),
'imageFont2': ImageFont.truetype("DejaVuSansMono.ttf", 12),
}
read_now_playing(**font_info)