-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
97 lines (83 loc) · 2.05 KB
/
main.py
File metadata and controls
97 lines (83 loc) · 2.05 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
#%%
from time import sleep
print(f"starting vlc")
import vlc
Instance = vlc.Instance("--no-video")
#create our playlist
import glob, random
playlist = glob.glob("music/*.mp4") + glob.glob("music/*.webm") + glob.glob("music/*.mkv")
random.shuffle(playlist)
idx=0
def getSong(num):
return Instance.media_new(playlist[idx % len(playlist)])
p = Instance.media_player_new()
p.set_media(getSong(idx))
p.pause()
#%%
print(f"starting alsa mixer interface")
import alsaaudio
m = alsaaudio.Mixer('PCM')
#%%
def playPause():
print(f"play/pause button pressed")
if(p.is_playing()):
p.pause()
print("pausing")
else:
p.play()
print("playing")
def volUp():
print(f"vol up button pressed")
vol=m.getvolume()[0]
vol+=5
if(vol>100):
vol=100
print(f"set vol to {vol}")
m.setvolume(vol)
def volDown():
print(f"vol down button pressed")
vol=m.getvolume()[0]
vol-=5
if(vol<0):
vol=0
print(f"set vol to {vol}")
m.setvolume(vol)
def nextSong():
global idx
print(f"next song button pressed")
idx=idx+1
song = getSong(idx)
print(f"playing {song.get_mrl()}")
p.stop()
p.set_media(song)
p.play()
def prevSong():
global idx
print(f"prev song button pressed")
idx=idx-1
song = getSong(idx)
print(f"playing {song.get_mrl()}")
p.stop()
p.set_media(song)
p.play()
#%%
print(f"hooking up our buttons to our functions")
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
def onVoltageIncrease(pin, fun):
print(f"added listener to BCN pin {pin}")
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.remove_event_detect(pin)
GPIO.add_event_detect(pin, GPIO.RISING, callback=lambda a:fun(), bouncetime=200)
onVoltageIncrease(5, playPause)
onVoltageIncrease(6, volUp)
onVoltageIncrease(13, volDown)
onVoltageIncrease(19, nextSong)
onVoltageIncrease(26, prevSong)
#%%
print("starting main loop")
while(True):
sleep(.100)
if(p.get_state()==vlc.State.Ended):
print("song ended going to next song")
nextSong()