-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboot.py
More file actions
521 lines (486 loc) · 18.6 KB
/
boot.py
File metadata and controls
521 lines (486 loc) · 18.6 KB
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# A runatboot file
import vlc #play music
import time #control time
import discid #read discid
import musicbrainzngs #fetch data
import sys
import cdio, pycdio #cdtxt & trackcount
from multiprocessing import Process, Pipe
import threading #scheduling
from math import floor#round down
import signal
import board
from digitalio import DigitalInOut
from adafruit_character_lcd.character_lcd import Character_LCD_Mono
import os
import pylast
from pydbus import SystemBus
import timeout_decorator
import configparser
from unidecode import unidecode#decoding characters to their english ASCII version
from gpiozero import Button
from subprocess import check_call#for shutdown only
#from fonts.py import *
#------------PARSING CONFIG-------------
config = configparser.ConfigParser()
dir_path = os.path.dirname(os.path.realpath(__file__))
config.read(dir_path + '/config.ini')
is_lastfm = config.getboolean('Last_Fm', 'Enable')
API_SECRET = config.get('Last_Fm', 'Api_secret')
os.environ['LAST_FM_API_SECRET'] = API_SECRET
API_KEY = config.get('Last_Fm', 'Api_key')
username = config.get('Last_Fm', 'Username')
password_hash = pylast.md5(config.get('Last_Fm', 'Password'))
lcd_columns = config.getint('Lcd', 'Columns')
lcd_rows = config.getint('Lcd', 'Rows')
lcd_rs = DigitalInOut(board.D26)
lcd_en = DigitalInOut(board.D25)
lcd_d4 = DigitalInOut(board.D10)
lcd_d5 = DigitalInOut(board.D9)
lcd_d6 = DigitalInOut(board.D11)
lcd_d7 = DigitalInOut(board.D0)
# Initialise the LCD class
lcd = Character_LCD_Mono(
lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows)
lcd.clear()
lcd.cursor = False
lcd.blink = False
Button_up = Button(config.get('Buttons', 'Up'))
Button_right = Button(config.get('Buttons', 'Right'),bounce_time=0.05)
Button_left = Button(config.get('Buttons', 'Left'))
Button_middle = Button(config.get('Buttons', 'Middle'))
Button_down = Button(config.get('Buttons', 'Down'))
command = 0 # 0-nothing 1-play 2-pause 3-stop 4-next 5-prev
textb = 15
page = 0
MAX_PAGE = 2
start = 0
playchar = [
0b01000,
0b01100,
0b01110,
0b01111,
0b01110,
0b01100,
0b01000,
0b00000]
pausechar = [
0b00000,
0b10010,
0b10010,
0b10010,
0b10010,
0b10010,
0b10010,
0b00000]
stopchar = [
0b00000,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b00000,
0b00000]
lcd.create_char(0, playchar)
lcd.create_char(1, pausechar)
lcd.create_char(2, stopchar)
def MsToTime(start, end):
now_secs= '0' + str(floor((start/1000)%60)) if floor((start/1000)%60) < 10 else str(floor((start/1000)%60))
now_min= '0' + str(floor((start/1000)/60)) if floor((start/1000)/60) < 10 else str(floor((start/1000)/60))
end_secs= '0' + str(floor((end/1000)%60)) if floor((end/1000)%60) < 10 else str(floor((end/1000)%60))
end_min= '0' + str(floor((end/1000)/60)) if floor((end/1000)/60) < 10 else str(floor((end/1000)/60))
Timer= now_min + ":" + now_secs + " - " + end_min + ":" + end_secs
return Timer
def SearchExactTracklist(data): #function to find exalctly wchich cd was inserted
for x in range(0,len(data["disc"]["release-list"])):
for y in range(0,len(data["disc"]["release-list"][x]["medium-list"])):
for z in range(0,len(data["disc"]["release-list"][x]["medium-list"][y]["disc-list"])):
if data["disc"]["release-list"][x]["medium-list"][y]["disc-list"][z]["id"] == data["disc"]["id"]:
return x, y
@timeout_decorator.timeout(5)#handling crash of Musicbrainzg
def FetchDataMB():
musicbrainzngs.set_useragent("Small_diy_cd_player", "0.1", config.get('MusicBrainz', 'Mail'))
disc = discid.read()#id read
try:
result = musicbrainzngs.get_releases_by_discid(disc.id,includes=["artists", "recordings"]) #get data from Musicbrainz
return result
except musicbrainzngs.ResponseError: #if not available search for cdtext
raise ValueError("Error:can't find on musicbrainzngs")
def FetchData():
try:
d = cdio.Device(driver_id=pycdio.DRIVER_UNKNOWN)
drive_name = d.get_device()
i_tracks = d.get_num_tracks()#ilosc trackow
artists = ["Unknown"] * i_tracks
track_list = ["Unknown"] * i_tracks
album = "Unknown"
except IOError:
print("Problem finding a CD-ROM")
sys.exit(1) #if no drive exit
try:
result = FetchDataMB()
except (ValueError, timeout_decorator.timeout_decorator.TimeoutError): #if not available search for cdtext
print("disc not found or bad response, using cdtxt instead")
cdt = d.get_cdtext()
i_first_track = pycdio.get_first_track_num(d.cd)
for t in range(i_first_track, i_tracks + i_first_track):
for i in range(pycdio.MIN_CDTEXT_FIELD, pycdio.MAX_CDTEXT_FIELDS):
#print(pycdio.cdtext_field2str(i)) ##0-TITLE 1-PERFORMER 2-SONGWRITER 3-COMPOSER 4-MESSAGE 5-ARRANGER 6-ISRC 7-UPC_EAN 8-GENERE 9-DISC_ID
value = cdt.get(i, t)
if value is not None:
if i == 0:
track_list[t-1] = value
pass
elif i == 1:
artists[t-1] = value
pass
pass
pass
if(track_list[t-1] == "Unknown"):
track_list[t-1] = "Untitled-" + str(t)
artists[t-1] = "Unknown"
else: #if available pull from Musicbrainz
a, b=SearchExactTracklist(result)
if result.get("disc"):
artists = [result["disc"]["release-list"][a]["artist-credit-phrase"]] * i_tracks
album = result["disc"]["release-list"][a]["title"]
elif result.get("cdstub"):
artists = [result["cdstub"]["artist"]] * i_tracks
album = result["cdstub"]["title"]
if(len(result["disc"]["release-list"][a]["medium-list"][b]["track-list"]) < i_tracks): #i have some albums that idk why have 1 more track according to pcdio than it should
for t in range(0,len(result["disc"]["release-list"][a]["medium-list"][b]["track-list"])):
track_list[t]=(result["disc"]["release-list"][a]["medium-list"][b]["track-list"][t]["recording"]["title"]) #uzupelnij tracklist
for t in range(len(result["disc"]["release-list"][a]["medium-list"][b]["track-list"]), i_tracks):
track_list[t]="Untitled-" + str(t+1)
else:
for t in range(0,i_tracks):
#print(t)
track_list[t]=(result["disc"]["release-list"][a]["medium-list"][b]["track-list"][t]["recording"]["title"]) #uzupelnij tracklist
#print(track_list)
if(artists[0] =="Various Artists"): #if musicbrainz returns Variouus artists check if cdtxt has better info
cdt = d.get_cdtext()
i_first_track = pycdio.get_first_track_num(d.cd)
for t in range(i_first_track, i_tracks + i_first_track):
value = cdt.get(1, t)
if value != "Unknown" and value is not None:
artists[t-1] = value
pass
print(artists, '\n', track_list, '\n', album, '\n', i_tracks)
return artists, track_list, album, i_tracks
def PlayCd(conn):
try:
artists, track_list, album, i_tracks = FetchData()
except cdio.TrackError:
print("NO CD DUMBASS")
else:
instance = vlc.Instance() #uruchom vlc
player = instance.media_player_new()
medialist = instance.media_list_new()
listplayer = instance.media_list_player_new()
listplayer.set_media_player(player)
for i in (range(1,i_tracks+1)): #second option
track = instance.media_new("cdda:///dev/cdrom", (":cdda-track=" + str(i)))
medialist.add_media(track)
listplayer.set_media_list(medialist)
listplayer.play()
time.sleep(5)#wait to spin
last_scrobble=""
network = pylast.LastFMNetwork(api_key=API_KEY, api_secret=API_SECRET, username=username, password_hash=password_hash)
while True:
dump = conn.recv()
index = medialist.index_of_item(listplayer.get_media_player().get_media())
match dump:
case 0:
conn.send([index+1, i_tracks, track_list[index], artists[index], album, MsToTime(player.get_time(), player.get_length()), player.get_state()])
#case 1:
#listplayer.play()
case 2:
if(player.get_state() == vlc.State.Stopped):
listplayer.play()
else:
listplayer.pause()
case 3:
listplayer.stop()
#last_scrobble=""
case 4:
listplayer.next()
#last_scrobble=""
case 5:
if(player.get_position()>0.4):
player.set_position(0)
else:
listplayer.previous()
if((player.get_position()*100 > 50) and last_scrobble != track_list[index]):
last_scrobble= track_list[index]
try:
if album != "Unknown":
network.scrobble(artist=artists[index], title=track_list[index], timestamp=int(time.time()), album=album)
elif track_list[index] != "Unknown" and artists[index] != "Unknown":
network.scrobble(artist=artists[index], title=track_list[index], timestamp=int(time.time()))
print("Scrobbling success: " + artists[index] + track_list[index] + album + str(time.time()))
except Exception as e:
print("Scrobbling failed: " + artists[index] + track_list[index] + album + str(time.time()))
print("Reason? ->")
print(e)
def loop_media(event):
print("Station ended. Moving to next station...")
listplayer.next()
def PlayRadio(conn):
instance = vlc.Instance()
player = instance.media_player_new()
listplayer = instance.media_list_player_new()
listplayer.set_media_player(player)
# Create a playlist
playlist = instance.media_list_new()
radio_stations = {
"RP Main mix": "http://stream.radioparadise.com/flacm",
"RP Mellow mix": "http://stream.radioparadise.com/mellow-flacm",
"RP Rock mix": "http://stream.radioparadise.com/rock-flacm",
"RP Global mix": "http://stream.radioparadise.com/global-flacm",
"Radio 357": "https://stream.radio357.pl",
"Tok Fm": "http://radiostream.pl/tuba10-1.mp3",
"Rock Radio": "http://radiostream.pl/tuba8-1.mp3"
}
i_tracks = len(radio_stations)
# Add streams to the playlist
for station_name, stream_url in radio_stations.items():
media = instance.media_new(stream_url)
media.set_meta(vlc.Meta.Title, station_name)
playlist.add_media(media)
# Set the media list to the player
listplayer.set_media_list(playlist)
# Register the callback
event_manager = player.event_manager()
event_manager.event_attach(vlc.EventType.MediaPlayerEndReached, loop_media)
# Start playing the first item in the playlist
print("Playing Radio Paradise streams...")
listplayer.play()
while True:
dump = conn.recv()
index = playlist.index_of_item(listplayer.get_media_player().get_media())
current_media = player.get_media()
match dump:
case 0:
conn.send([index+1, i_tracks, list(radio_stations.keys())[index], current_media.get_meta(vlc.Meta.Artist),current_media.get_meta(vlc.Meta.Album), MsToTime(player.get_time(), player.get_length()), player.get_state()])
#case 1:
#listplayer.play()
case 2:
if(player.get_state() == vlc.State.Stopped):
listplayer.play()
else:
listplayer.pause()
case 3:
listplayer.stop()
#last_scrobble=""
case 4:
listplayer.next()
#last_scrobble=""
case 5:
listplayer.previous()
def ShowCd(dump):
global textb
lcd.clear()
if(len(dump[2])>16):
if textb >= len(dump[2])+1:
textb = 16
TrimMessage=dump[2][textb-16:textb]
textb+=1
else:
TrimMessage=dump[2]
lcd.message=unidecode(TrimMessage) #title
lcd.cursor_position(0,1)
lcd.message=unidecode(dump[5]) # time
lcd.cursor_position(15,1)
match dump[6]:
case vlc.State.Playing:
lcd.message='\x00'
case vlc.State.Paused:
lcd.message='\x01'
case vlc.State.Stopped:
lcd.message='\x02'
case "playing":
lcd.message='\x00'
case "paused":
lcd.message='\x01'
class BtPlayer(object):
def __new__(self):
bus = SystemBus()
manager = bus.get('org.bluez', '/')
for obj in manager.GetManagedObjects():
if obj.endswith('/player0') or obj.endswith('/player2') or obj.endswith('/player1'):
return bus.get('org.bluez', obj)
raise BtPlayer.DeviceNotFoundError
class DeviceNotFoundError(Exception):
def __init__(self):
super().__init__('No music bluetooth device was found')
def next():
global run, command
match page:
case 0:
if(not run):
command = 4
else:
parent_conn.send(4)
case 2:
if(not run):
command = 4
else:
parent_conn.send(4)
case 1:
command = 4
def back():
global run, command
match page:
case 0:
if(not run):
command = 5
else:
parent_conn.send(5)
case 2:
if(not run):
command = 5
else:
parent_conn.send(5)
case 1:
command = 5
def stop():
global run, command
match page:
case 0:
parent_conn.send(3)
case 2:
parent_conn.send(3)
case 1:
command = 3
def pause():
global run, command
match page:
case 0:
parent_conn.send(2)
case 2:
parent_conn.send(2)
case 1:
command = 2
def middle():
global start
start = time.time()
def shutdown():
global start, command
end = time.time()
if(end-start>2):
lcd.clear()
lcd.message="Goodbye"
check_call(['sudo', 'poweroff'])
else:
command = -1
Button_up.when_pressed = pause
Button_left.when_pressed = back
Button_right.when_pressed = next
Button_down.when_pressed = stop
Button_middle.when_pressed = middle
Button_middle.when_released = shutdown
parent_conn, child_conn = Pipe()
lcd.clear()
while True:
run = False
match command:
case 4:
page+=1
case 5:
page-=1
case -1:
run = True
command = 0
if page == -1:
page = MAX_PAGE
elif page > MAX_PAGE:
page = 0
match page:
case 0:
lcd.clear()
lcd.message = "CD Player"
if(run):
lcd.clear()
lcd.message = "Loading..."
cdplayer = Process(target=PlayCd, args=(child_conn,))
cdplayer.start()
time.sleep(5)
while(run):
if command == -1 and cdplayer.is_alive():
cdplayer.kill()
cdplayer.join()
run = False
elif cdplayer.is_alive():
parent_conn.send(0)#you must send to recive
time.sleep(0.1)
dump=parent_conn.recv()
if(dump[6] == vlc.State.Ended):
cdplayer.kill()
cdplayer.join()
lcd.clear()
run = False
else:
ShowCd(dump)
else:
run = False
command=0
time.sleep(1)
case 1:
lcd.clear()
lcd.message = "Bluetooth"
while(run):
try:
handle = BtPlayer()
ShowCd([1,1,handle.Track['Title'], handle.Track['Artist'], 'Untitled', MsToTime(handle.Position, handle.Track['Duration']), handle.Status])
match command:
case -1:
run=False
case 1:
handle.Play()
case 2:
handle.Pause()
case 4:
handle.Next()
case 5:
handle.Previous()
case 3:
handle.Stop()
except:
#print("Something crashed, try again") #dbus-monitor --address "unix:path=/run/dbus/system_bus_socket" "type='signal',sender='org.bluez'"
lcd.clear()
lcd.message = "Something\nwent wrong"
if(command == -1):
run=False
command = 0
time.sleep(1)
case 2:
lcd.clear()
lcd.message = "Internet\nRadio"
if(run):
lcd.clear()
lcd.message = "Loading..."
cdplayer = Process(target=PlayRadio, args=(child_conn,))
cdplayer.start()
time.sleep(2)
while(run):
if command == -1 and cdplayer.is_alive():
cdplayer.kill()
cdplayer.join()
run = False
elif cdplayer.is_alive():
parent_conn.send(0)#you must send to recive
time.sleep(0.1)
dump=parent_conn.recv()
if(dump[6] == vlc.State.Ended):
cdplayer.kill()
cdplayer.join()
lcd.clear()
run = False
else:
ShowCd(dump)
else:
run = False
command=0
time.sleep(1)
time.sleep(0.5)