-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontrols.py
296 lines (260 loc) · 9.29 KB
/
controls.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
import os
import RPi.GPIO as GPIO
import pygame
import eyed3
import csv
import random
currentSong = []
menu = {
"Main": ["Music", "Applications", "Settings"],
"Music": ["Artists", "Albums", "Tracks"],
"Applications": [],
"Artists": [],
"Albums": [],
"Tracks": [],
"list": [],
"Queue": [],
"Settings": ["Update library", "Sleep", "Shutdown"],
"current": "musicController",
"history": [],
}
# Define colors
backgroundColor = (0,0,0) # global
primaryColor = (255, 255, 255) # global
secondaryColor = (100, 100, 255) # global
class interface:
selectedItem = 0
sleep = 0
metadata = []
tracks = {}
artists = []
albums = []
playing = 0
queueIndex = 0
volume = 15
def __init__(self):
os.putenv('SDL_FBDEV', '/dev/fb1') # Route the output to framebuffer 1 (TFT display)
pygame.init()
pygame.font.init()
self.lcd = pygame.display.set_mode((160, 128))
pygame.mouse.set_visible(False)
pygame.key.set_repeat(500, 100)
self.font = pygame.font.Font("TerminusTTF-4.46.0.ttf", 12)
pygame.mixer.init()
pygame.mixer.music.set_volume(0.15)
# Load music
self.load()
# Set backlight pin as output and turn it on
GPIO.setwarnings(False) # disable warning because it is known that the pin is already set as output
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.OUT)
GPIO.output(23, GPIO.HIGH)
def clear(self):
self.lcd.fill(backgroundColor)
def toggleSleep(self):
if self.sleep == 0:
GPIO.output(23, GPIO.LOW)
self.sleep = 1
else:
GPIO.output(23, GPIO.HIGH)
self.sleep = 0
def shutdown(self):
self.lcd.fill(backgroundColor)
text = self.font.render("Shutting down", True, primaryColor)
self.lcd.blit(text, ((160 - text.get_width()) / 2, (128 - text.get_height()) / 2))
pygame.display.flip()
os.system("halt")
def listView(self, menu):
self.clear()
index = 0
marginTop = 122/2-(15*self.selectedItem) # text height 12/2=6 128-6 = 122
marginLeft = 10
marginTop += 15*(self.selectedItem-4 if self.selectedItem > 4 else 0)
index += (self.selectedItem-4 if self.selectedItem > 4 else 0)
for item in menu[self.selectedItem-4 if self.selectedItem > 4 else 0:self.selectedItem+4]: # I'm sorry, if selected item is more then 4 start slicing the list
if index == self.selectedItem:
text = self.font.render(item, True, secondaryColor)
else:
text = self.font.render(item, True, primaryColor)
self.lcd.blit(text, (marginLeft, marginTop))
marginTop += 15
index += 1
def musicController(self):
self.clear()
if currentSong:
artist = self.font.render(currentSong[1], True, primaryColor)
album = self.font.render(currentSong[2], True, primaryColor)
title = self.font.render(currentSong[3], True, primaryColor)
self.lcd.blit(artist, (10, 10))
self.lcd.blit(album, (10, 25))
self.lcd.blit(title, (10, 40))
volumeText = self.font.render(str(int(100*pygame.mixer.music.get_volume())), True, primaryColor)
volumeUpText = self.font.render("+", True, secondaryColor if self.selectedItem == 0 else primaryColor)
volumeDownText = self.font.render("-", True, secondaryColor if self.selectedItem == 1 else primaryColor)
prevText = self.font.render("Prev", True, secondaryColor if self.selectedItem == 2 else primaryColor)
playPauseText = self.font.render("Play/Pause", True, secondaryColor if self.selectedItem == 3 else primaryColor)
nextText = self.font.render("Next", True, secondaryColor if self.selectedItem == 4 else primaryColor)
shuffleText = self.font.render("Shuffle", True, secondaryColor if self.selectedItem == 5 else primaryColor)
clearText = self.font.render("Clear", True, secondaryColor if self.selectedItem == 6 else primaryColor)
self.lcd.blit(volumeText, (160-10-volumeText.get_width(), 128-45))
self.lcd.blit(volumeUpText, (10, 128-45))
self.lcd.blit(volumeDownText, (15+volumeDownText.get_width(), 128-45))
self.lcd.blit(playPauseText, ((160-playPauseText.get_width())/2, 128-30))
self.lcd.blit(prevText, (10, 128-30))
self.lcd.blit(playPauseText, ((160-playPauseText.get_width())/2, 128-30))
self.lcd.blit(nextText, (160-nextText.get_width()-10, 128-30))
self.lcd.blit(shuffleText, (10, 128-15))
self.lcd.blit(clearText, (160-clearText.get_width()-10, 128-15))
def menuAction(self, action):
if action == "up":
if self.selectedItem > 0:
self.selectedItem -= 1
elif menu["current"] == "Queue":
menu["history"].pop()
self.selectedItem = 6
menu["current"] = "musicController"
elif action == "down":
if menu["current"] == "musicController":
if self.selectedItem < 6:
self.selectedItem += 1
else:
menu["history"].append(menu["current"]) # update history
menu["current"] = "Queue" # go to next menu
self.selectedItem = 0
else:
if self.selectedItem < len(menu[menu["current"]])-1:
self.selectedItem += 1
elif action == "select":
if menu["current"] == "Artists":
tempList = []
for item in menu["Tracks"]:
if item[1] == menu["Artists"][self.selectedItem]:
tempList.append(item)
menu["list"] = tempList
menu["current"] = "list"
self.selectedItem = 0
elif menu["current"] == "Albums":
tempList = []
for item in menu["Tracks"]:
if item[2] == menu["Albums"][self.selectedItem]:
tempList.append(item)
menu["list"] = tempList
menu["current"] = "list"
self.selectedItem = 0
elif menu["current"] == "Settings":
if menu["Settings"][self.selectedItem] == "Update library":
self.update()
elif menu["Settings"][self.selectedItem] == "Sleep":
self.toggleSleep()
elif menu["Settings"][self.selectedItem] == "Shutdown":
self.shutdown()
else:
if menu[menu["current"]]: # check if empty
menu["history"].append(menu["current"]) # update history
menu["current"] = menu[menu["current"]][self.selectedItem] # go to next menu
self.selectedItem = 0
elif action == "left":
# print(menu["history"])
self.selectedItem = 0
if menu["history"]: # check if history is empty
menu["current"] = menu["history"][-1::][0]
menu["history"].pop()
else:
menu["current"] = "musicController"
elif action == "right":
if menu["current"] == "musicController":
self.selectedItem = 0
menu["current"] = "Main"
def loop(self):
if self.playing == 1:
if not pygame.mixer.music.get_busy():
self.next()
def update(self):
self.playPause()
fileList = []
# print("Updating metadata")
for root, dirs, files in os.walk("Music/"):
for f in files:
filename = os.path.join(root, f)
if filename.endswith('.mp3'):
fileList.append(filename)
file = open("info.csv", "w", newline="")
writer = csv.writer(file, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)
# print("Saving metadate")
for i in fileList:
audiofile = eyed3.load(i)
tag = audiofile.tag
try:
writer.writerow((i, tag.artist, tag.album, tag.title))
except AttributeError:
print(i)
print("Done")
file.close()
self.load()
def load(self):
file = open("info.csv", "rt")
menu["Artists"] = []
menu["Albums"] = []
menu["Tracks"] = []
try:
reader = csv.reader(file)
for row in reader:
artistClear = row[1].lstrip().lower().title()
albumClear = row[2].lstrip().lower().title()
if artistClear is not "":
if artistClear not in menu["Artists"]:
menu["Artists"].append(artistClear)
if albumClear is not "":
if albumClear not in menu["Albums"]:
menu["Albums"].append(albumClear)
if row[3].lstrip() is not "":
self.metadata.append([row[0], artistClear, albumClear, row[3].lstrip()]) # [filename, artist, album, title]
finally:
file.close()
menu["Artists"].sort()
menu["Albums"].sort()
menu["Tracks"] = sorted(self.metadata, key=lambda meta:meta[3])
def play(self, info):
global currentSong
currentSong = list(info)
# print(currentSong)
pygame.mixer.music.load(info[0])
pygame.mixer.music.play()
self.playing = 1
def playPause(self):
if self.playing:
self.playing = 0
pygame.mixer.music.pause()
elif not self.playing:
self.playing = 1
pygame.mixer.music.unpause()
def shuffle(self):
# Before shuffling remove the already played songs to make sure these don't get played again
currentIndex = menu["Queue"].index(currentSong)+1
history = menu["Queue"][0:currentIndex]
menu["Queue"] = menu["Queue"][currentIndex::]
random.shuffle(menu["Queue"])
# Add the already played songs to the front again
menu["Queue"] = history + menu["Queue"]
def clearQueue(self):
del currentSong[:]
self.playing = 0
pygame.mixer.music.pause()
menu["Queue"] = []
def next(self):
if self.queueIndex < len(menu["Queue"])-1:
self.queueIndex += 1
self.play(menu["Queue"][self.queueIndex])
def prev(self):
if self.queueIndex > 0:
self.queueIndex -= 1
self.play(menu["Queue"][self.queueIndex])
def volumeUp(self):
if self.volume <= 95:
self.volume += 5
pygame.mixer.music.set_volume(self.volume/100)
print(self.volume)
def volumeDown(self):
if self.volume > 5:
self.volume -= 5
pygame.mixer.music.set_volume(self.volume/100)