-
Notifications
You must be signed in to change notification settings - Fork 0
/
moode_oled_3.12_spi_audiophonics2.py
492 lines (429 loc) · 17.7 KB
/
moode_oled_3.12_spi_audiophonics2.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# AUDIOPHONICS RASPDAC MINI OLED Script #
# 11 Septembre 2018
from __future__ import unicode_literals
import RPi.GPIO as GPIO
from luma.oled.device import ssd1322
from luma.core.render import canvas
from luma.core.interface.serial import spi
from PIL import ImageDraw
from PIL import ImageFont
from PIL import Image
from subprocess import Popen, PIPE
from importlib import reload
import subprocess
import re
import socket
import time
import os
import sys
reload(sys)
#sys.setdefaultencoding('utf-8')
#import smbus
#bus = smbus.SMBus(1)
#import json
#import urllib2
#serial = spi(port=0, device=0, gpio_DC=27, gpio_RST=24, cs_high=True)
serial = spi(port=0, device=0, gpio_DC=27, gpio_RST=24)
device = ssd1322(serial, rotate=0, mode="1")
mpd_music_dir = "/var/lib/mpd/music/"
title_height = 40
scroll_unit = 2
oled_width = 256
oled_height = 64
def make_font(name, size):
font_path = os.path.abspath(os.path.join(
os.path.dirname(__file__), 'fonts', name))
return ImageFont.truetype(font_path, size)
font_title = make_font('msyh.ttf', 26)
font_info = make_font('msyh.ttf', 20)
font_vol = make_font('msyh.ttf', 55)
font_ip = make_font('msyh.ttf', 15)
font_time = make_font('msyh.ttf', 18)
font_20 = make_font('msyh.ttf', 18)
font_date = make_font('arial.ttf', 25)
#font_logo = make_font('msyh.ttf', 24)
font_logo = make_font('arial.ttf', 42)
font_32 = make_font('arial.ttf', 50)
awesomefont = make_font("fontawesome-webfont.ttf", 16)
awesomefontbig = make_font("fontawesome-webfont.ttf", 42)
speaker = "\uf028"
wifi = "\uf1eb"
link = "\uf0e8"
clock = "\uf017"
mpd_host = 'localhost'
mpd_port = 6600
mpd_bufsize = 8192
def getWanIP():
# can be any routable address,
fakeDest = ("223.5.5.5", 53)
wanIP = ""
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(fakeDest)
wanIP = s.getsockname()[0]
s.close()
except Exception as e:
pass
return wanIP
def GetLANIP():
cmd = "ip addr show eth0 | grep inet | grep -v inet6 | awk '{print $2}' | cut -d '/' -f 1"
p = Popen(cmd, shell=True, stdout=PIPE)
output = p.communicate()[0]
return output[:-1]
def GetInput():
cmd = "amixer sget -c 0 'I2S/SPDIF Select' | grep Item0: | awk '{print $2}' "
p = Popen(cmd, shell=True, stdout=PIPE)
output = p.communicate()[0]
return output[:-1]
# OLED images
image = Image.new('1', (oled_width, oled_height))
draw = ImageDraw.Draw(image)
music_file = ""
shift = 0
title_image = Image.new('L', (oled_width, title_height))
title_offset = 0
current_page = 0
vol_val_store = 0
screen_sleep = 0
timer_vol = 0
input_counter = 0
screensave = 3
# Socket
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
soc.connect((mpd_host, mpd_port))
soc.recv(mpd_bufsize)
soc.send(b'commands\n')
rcv = soc.recv(mpd_bufsize)
shift = 1
music_file = ""
GPIO.cleanup
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP)
spdif = False
def InputSelect():
global spdif
if(spdif == False):
spdif = True
os.system("amixer sset -c 0 'I2S/SPDIF Select' SPDIF")
elif(spdif == True):
spdif = False
os.system("amixer sset -c 0 'I2S/SPDIF Select' I2S")
def optionButPress(value):
global startt, endt
if GPIO.input(22) == 1:
startt = time.time()
if GPIO.input(22) == 0:
endt = time.time()
InputSelect()
#GPIO.add_event_detect(22, GPIO.BOTH, callback=optionButPress, bouncetime=200)
#os.system("amixer sset -c 1 'I2S/SPDIF Select' I2S")
with canvas(device) as draw:
draw.text((3, 10), "Audiophonics", font=font_logo, fill="white")
time.sleep(2)
try:
while True:
soc.send(b'currentsong\n')
buff = soc.recv(mpd_bufsize)
song_list = buff.splitlines()
soc.send(b'status\n')
buff = soc.recv(mpd_bufsize)
state_list = buff.splitlines()
info_state = ""
info_audio = ""
info_elapsed = 0
info_duration = 0
info_title = ""
info_artist = ""
info_album = ""
info_name = ""
info_file = ""
bit_val = ""
samp_val = ""
time_val = time_min = time_sec = vol_val = audio_val = 0
#jsonapi = json.load(urllib2.urlopen('http://127.0.0.1:3000/api/v1/getstate'))
for line in range(0, len(state_list)):
if state_list[line].startswith("state: "):
info_state = state_list[line].replace("state: ", "")
# info_elapsed = float(state_list[line].replace("elapsed: ", ""))
if state_list[line].startswith("elapsed: "):
time_val = float(state_list[line].replace("elapsed: ", ""))
time_bar = time_val
time_min = time_val/60
time_sec = time_val % 60
time_min = "%2d" % time_min
time_sec = "%02d" % time_sec
time_val = str(time_min)+":"+str(time_sec)
if state_list[line].startswith("time: "):
info_duration = float(state_list[line].split(":")[2])
# Volume
if state_list[line].startswith("volume: "):
vol_val = state_list[line].replace("volume: ", "")
# Volume NULL
if vol_val == "":
vol_val = "0"
subprocess.Popen(['mpc', 'volume', '0'])
# Sampling rate / bit
if state_list[line].startswith(r"audio: "):
audio_val = state_list[line]
audio_val = audio_val.replace("audio: ", "")
audio_val = re.split(':', audio_val)
bit_val = audio_val[1]+'bit '
if audio_val[0] == '22050':
samp_val = '22.05k/'
elif audio_val[0] == '32000':
samp_val = '32k/'
elif audio_val[0] == '44100':
samp_val = '44.1k/'
elif audio_val[0] == '48000':
samp_val = '48k/'
elif audio_val[0] == '88200':
samp_val = '88.2k/'
elif audio_val[0] == '96000':
samp_val = '96k/'
elif audio_val[0] == '176400':
samp_val = '176.4k/'
elif audio_val[0] == 'dsd64':
samp_val = 'DSD64/'
bit_val = '1bit'
elif audio_val[0] == '192000':
samp_val = '192k/'
elif audio_val[0] == '352800':
samp_val = '352.8k/'
elif audio_val[0] == 'dsd128':
samp_val = 'DSD128/'
bit_val = '1bit'
elif audio_val[0] == '384000':
samp_val = '384k/'
elif audio_val[0] == '705600':
samp_val = '705.6k/'
elif audio_val[0] == 'dsd256':
samp_val = 'DSD256/'
bit_val = '1bit'
elif audio_val[0] == '768000':
samp_val = '768k/'
elif audio_val[0] == '1411200':
samp_val = 'DSD512/'
bit_val = '1bit'
elif audio_val[0] == '6144000':
samp_val = 'DSD1024/'
bit_val = '1bit'
else:
samp_val = state_list[line].replace("audio: ", "")
bit_val = ""
if audio_val[1] == 'f':
bit_val = '24bit'
for song_line in range(0, len(song_list)):
if song_list[song_line].startswith("file: "):
info_file = song_list[song_line].replace("file: ", "")
if song_list[song_line].startswith("Artist: "):
info_artist = song_list[song_line].replace("Artist: ", "")
if song_list[song_line].startswith("Album: "):
info_album = song_list[song_line].replace("Album: ", "")
if song_list[song_line].startswith("Title: "):
info_title = song_list[song_line].replace("Title: ", "")
if song_list[song_line].startswith("Name: "):
info_name = song_list[song_line].replace("Name: ", "")
# Counter because Getinput slow down the scrolling
if input_counter == 10:
dac_input = str(GetInput())
input_counter = 0
else:
input_counter = input_counter + 1
# Volume change screen
if vol_val != vol_val_store:
timer_vol = 20
if timer_vol > 0:
with canvas(device) as draw:
vol_width, char = font_vol.getsize(vol_val)
x_vol = ((oled_width - vol_width) / 2)
# Volume Display
draw.text((5, 5), text="\uf028",
font=awesomefontbig, fill="white")
draw.text((x_vol, -15), vol_val, font=font_vol, fill="white")
# Volume Bar
draw.rectangle((0, 53, 255, 60), outline=1, fill=0)
Volume_bar = ((int(float(vol_val)) * 2.52)+2)
draw.rectangle((2, 55, Volume_bar, 58), outline=0, fill=1)
vol_val_store = vol_val
timer_vol = timer_vol - 1
screen_sleep = 0
time.sleep(0.1)
# SPDIF screen
elif(dac_input == "'SPDIF'"):
if screen_sleep < 600:
with canvas(device) as draw:
draw.text((20, -4), "SPDIF", font=font_title, fill="white")
draw.text((45, 33), vol_val, font=font_title, fill="white")
draw.text((15, 41), text="\uf028",
font=awesomefont, fill="white")
# Volume Bar
draw.rectangle((120, 0, 127, 62), outline=1, fill=0)
Volume_bar = (58 - (int(float(vol_val)) / 1.785))
draw.rectangle((122, Volume_bar, 125, 60),
outline=0, fill=1)
time.sleep(0.5)
screen_sleep = screen_sleep + 1
else:
with canvas(device) as draw:
draw.text((0, 48), ".", font=font_time, fill="white")
time.sleep(1)
# Play screen
elif info_state != "stop":
if info_title == "":
name = info_file.split('/')
name.reverse()
info_title = name[0]
try:
info_album = name[1]
except:
info_album = ""
try:
info_artist = name[2]
except:
info_artist = ""
if info_name != "":
info_artist = info_name
if info_duration != 0:
time_bar = time_bar / info_duration * 256
if info_file != music_file or time_bar < 5:
# Called one time / file
music_file = info_file
# Generate title image
# if title_width < artist_width:
# title_width = artist_width
if info_duration != 0:
dura_min = info_duration/60
dura_sec = info_duration % 60
dura_min = "%2d" % dura_min
dura_sec = "%02d" % dura_sec
dura_val = "/ " + str(dura_min)+":"+str(dura_sec)
else:
dura_val = ""
bit_val = bytes(bit_val) # 2018.1.5
samp_val = bytes(samp_val) # 2018.1.7
artist_offset = 10
album_offset = 10
title_offset = 10
title_width, char = font_info.getsize(info_title)
artist_width, char = font_info.getsize(info_artist)
album_width, char = font_info.getsize(info_album)
bitrate_width, char = font_time.getsize(samp_val + bit_val)
current_page = 0
# OFFSETS*****************************************************
x_artist = 0
if oled_width < artist_width:
if artist_width < -(artist_offset + 20):
artist_offset = 0
if artist_offset < 0:
x_artist = artist_offset
artist_offset = artist_offset - scroll_unit
x_album = 0
if oled_width < album_width:
if album_width < -(album_offset + 20):
album_offset = 0
if album_offset < 0:
x_album = album_offset
album_offset = album_offset - scroll_unit
x_title = 0
if oled_width < title_width:
if title_width < -(title_offset + 20):
title_offset = 0
if title_offset < 0:
x_title = title_offset
title_offset = title_offset - scroll_unit
x_bitrate = (oled_width - bitrate_width) / 2
with canvas(device) as draw:
if current_page < 150:
draw.text((x_title, -7), info_title,
font=font_info, fill="white")
if title_width < -(title_offset - oled_width) and title_width > oled_width:
draw.text((x_title + title_width + 10, -7),
"- " + info_title, font=font_info, fill="white")
draw.text((x_bitrate, 20), (samp_val + bit_val),
font=font_time, fill="white")
if info_state == "pause":
draw.text((0, 43), text="\uf04c",
font=awesomefont, fill="white")
else:
draw.text((0, 41), time_val,
font=font_time, fill="white")
draw.text((55, 41), dura_val,
font=font_time, fill="white")
#draw.text((58, 48), text="\uf001", font=awesomefont, fill="white")
draw.rectangle((0, 40, time_bar, 44), outline=0, fill=1)
draw.text((200, 44), text="\uf028",
font=awesomefont, fill="white")
draw.text((220, 41), vol_val, font=font_time, fill="white")
current_page = current_page + 1
artist_offset = 10
album_offset = 10
elif current_page < 300 :
# artist name
draw.text((x_artist, -7), info_artist,
font=font_info, fill="white")
if artist_width < -(artist_offset - oled_width) and artist_width > oled_width:
draw.text((x_artist + artist_width + 10, -7),
"- " + info_artist, font=font_info, fill="white")
# album name
draw.text((x_album, 14), info_album,
font=font_info, fill="white")
if album_width < -(album_offset - oled_width) and album_width > oled_width:
draw.text((x_album + album_width + 10, 14),
"- " + info_album, font=font_info, fill="white")
# Bottom line
if info_state == "pause":
draw.text((0, 43), text="\uf04c",
font=awesomefont, fill="white")
else:
draw.text((0, 41), time_val,
font=font_time, fill="white")
draw.text((55, 41), dura_val,
font=font_time, fill="white")
draw.rectangle((0, 40, time_bar, 44), outline=0, fill=1)
draw.text((200, 44), text="\uf028",
font=awesomefont, fill="white")
draw.text((220, 41), vol_val, font=font_time, fill="white")
current_page = current_page + 1
if current_page == 300:
current_page = 0
title_offset = 10
time.sleep(0.05)
else:
# Time IP screen
music_file = ""
ip = getWanIP()
#ip = str(GetLANIP())
if screen_sleep < 20000:
with canvas(device) as draw:
#draw.text((1, -6),"Volumio", font=font_logo,fill="white")
if ip != "":
draw.text((140, 45), ip, font=font_ip, fill="white")
draw.text((120, 45), link,
font=awesomefont, fill="white")
else:
draw.text((140, 45), time.strftime(
"192.168.211.1"), font=font_ip, fill="white")
draw.text((120, 45), wifi,
font=awesomefont, fill="white")
draw.text((28, -10), time.strftime("%X"),
font=font_32, fill="white")
draw.text((20, 40), vol_val, font=font_time, fill="white")
draw.text((1, 43), text="\uf028",
font=awesomefont, fill="white")
screen_sleep = screen_sleep + 1
else:
with canvas(device) as draw:
screensave += 2
if screensave > 120:
screensave = 3
draw.text((screensave, 45), ".",
font=font_time, fill="white")
time.sleep(1)
time.sleep(0.1)
except Exception as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print(exc_type, fname, exc_tb.tb_lineno)