-
Notifications
You must be signed in to change notification settings - Fork 7
/
moode-oled.py
executable file
·227 lines (186 loc) · 6.26 KB
/
moode-oled.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/python
# Author: Suwat Saisema
# Date: 5-Oct-2017
import sys
import time
import os
from socket import error as socket_error
# Adafruit Library
import Adafruit_GPIO.SPI as SPI
import Adafruit_SSD1306
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
# MPD Clint
from mpd import MPDClient, MPDError, CommandError, ConnectionError
# System UTF-8
reload(sys)
sys.setdefaultencoding('utf-8')
# Raspberry Pi pin configuration:
RST = 24
# Note the following are only used with SPI:
DC = 23
SPI_PORT = 0
SPI_DEVICE = 0
# 128x64 display with hardware I2C:
disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST)
# MPD Client
class MPDConnect(object):
def __init__(self, host='localhost', port=6600):
self._mpd_client = MPDClient()
self._mpd_client.timeout = 10
self._mpd_connected = False
self._host = host
self._port = port
def connect(self):
if not self._mpd_connected:
try:
self._mpd_client.ping()
except(socket_error, ConnectionError):
try:
self._mpd_client.connect(self._host, self._port)
self._mpd_connected = True
except(socket_error, ConnectionError, CommandError):
self._mpd_connected = False
def disconnect(self):
self._mpd_client.close()
self._mpd_client.disconnect()
def _play_pause(self):
self._mpd_client.pause()
#return False
def _next_track(self):
self._mpd_client.next()
#return False
def _prev_track(self):
self._mpd_client.previous()
#return False
def fetch(self):
# MPD current song
song_info = self._mpd_client.currentsong()
# Artist Name
if 'artist' in song_info:
artist = song_info['artist']
else:
artist = 'Unknown Artist'
# Song Name
if 'title' in song_info:
title = song_info['title']
else:
title = 'Unknown Title'
# MPD Status
song_stats = self._mpd_client.status()
# State
state = song_stats['state']
# Song time
if 'elapsed' in song_stats:
elapsed = song_stats['elapsed']
m,s = divmod(float(elapsed), 60)
h,m = divmod(m, 60)
eltime = "%d:%02d:%02d" % (h, m, s)
else:
eltime ="0:00:00"
# Audio
if 'audio' in song_stats:
bit = song_stats['audio'].split(':')[1]
frequency = song_stats['audio'].split(':')[0]
z, f = divmod( int(frequency), 1000 )
if ( f == 0 ):
frequency = str(z)
else:
frequency = str( float(frequency) / 1000 )
bitrate = song_stats['bitrate']
audio_info = bit + "bit " + frequency + "kHz " + bitrate + "kbps"
else:
audio_info = ""
# Volume
vol = song_stats['volume']
return({'state':state, 'artist':artist, 'title':title, 'eltime':eltime, 'volume':int(vol), 'audio_info':audio_info})
def main():
# Initialize Library
disp.begin()
# Get display width and height.
width = disp.width
height = disp.height
# Clear display
disp.clear()
disp.display()
# Create image buffer.
# Make sure to create image with mode '1' for 1-bit color.
image = Image.new('1', (width, height))
# Load default font.
font_artist = ImageFont.truetype('/home/pi/MoodeAudio-OLED/Arial-Unicode-Bold.ttf', 14)
font_title = ImageFont.truetype('/home/pi/MoodeAudio-OLED/Arial-Unicode-Regular.ttf', 13)
font_info = ImageFont.truetype('/home/pi/MoodeAudio-OLED/Verdana-Italic.ttf', 10)
font_time = ImageFont.truetype('/home/pi/MoodeAudio-OLED/Verdana.ttf', 13)
# Create drawing object.
draw = ImageDraw.Draw(image)
# First define some constants to allow easy resizing of shapes.
padding = 2
shape_width = 20
top = padding
bottom = height-padding
artoffset = 2
titoffset = 2
animate = 15
# MPD Connect
client = MPDConnect()
client.connect()
# Draw data to display
while True:
# Clear image buffer by drawing a black filled box.
draw.rectangle((0,0,width,height), outline=0, fill=0)
# Fetch data
info = client.fetch()
state = info['state']
eltime = info['eltime']
vol = info['volume']
artist = info['artist']
title = info['title']
audio = info['audio_info']
# Position text of Artist
artwd,artz = draw.textsize(unicode(artist), font=font_artist)
# Artist animate
if artwd < width:
artx = (width - artwd) / 2
artoffset = padding
else:
artx = artoffset
#artoffset -= animate
#if (artwd - (width + abs(artx))) < -120:
# artoffset = 100
# Position text of Title
titwd,titz = draw.textsize(unicode(title), font=font_title)
# Title animate
if titwd < width:
titx = (width - titwd) / 2
titoffset = padding
else:
titx = titoffset
titoffset -= animate
if (titwd - (width + abs(titx))) < -120:
titoffset = 100
# Position text of audio infomation
audiox,audioy = draw.textsize(audio, font=font_info)
if audiox < 126:
audiox,audioy = divmod((126-audiox),2)
else:
audiox = 2
if state == 'stop':
# Draw text
draw.text((30,15), "Music Stop", font=font_title, fill=255)
draw.text((padding,50), eltime, font=font_time, fill=255)
draw.text((75,50), "vol: " + str(vol) , font=font_time, fill=255)
else:
# Draw text.
draw.text((artx,top), unicode(artist), font=font_artist, fill=255)
draw.text((titx,18), unicode(title), font=font_title, fill=255)
draw.text((audiox,35), audio, font=font_info, fill=255)
draw.text((padding,50), eltime, font=font_time, fill=255)
draw.text((75,50), "vol: " + str(vol) , font=font_time, fill=255)
# Draw the image buffer.
disp.image(image)
disp.display()
# Pause briefly before drawing next frame.
time.sleep(1)
if __name__ == "__main__":
main()