-
Notifications
You must be signed in to change notification settings - Fork 0
/
musicbox.py
39 lines (32 loc) · 1.29 KB
/
musicbox.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
import os
from gpiozero import Button
from time import sleep
from signal import pause
import subprocess
class Switch:
def __init__(self, pinNumber, path):
self.buttonCounter = 0
self.pushButton = Button(pinNumber)
self.dirPath = path
self.funSongs = os.listdir(self.dirPath)
self.buttonCounter = 0
self.funSongsPlaylist=[]
for self.songNames in self.funSongs:
if self.songNames.endswith(".wav") and not self.songNames.startswith("."):
self.funSongsPlaylist.append(self.songNames)
def playPushButton(self, buttonCounter):
print(self.buttonCounter)
self.playThisSong = self.funSongsPlaylist[self.buttonCounter]
subprocess.call(['killall', 'aplay'])
subprocess.Popen(['aplay', self.dirPath + self.playThisSong])
def buttonPressCounter(self):
self.songCount = len(self.funSongsPlaylist)
if self.buttonCounter == self.songCount:
self.buttonCounter = 0
self.playPushButton(self.buttonCounter)
else:
self.playPushButton(self.buttonCounter)
self.buttonCounter = self.buttonCounter + 1
pressSwitch = Switch(13, "/home/pi/switchmusicbox/songs/")
pressSwitch.pushButton.when_pressed = pressSwitch.buttonPressCounter
pause()