-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.py
102 lines (75 loc) · 2.64 KB
/
backend.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
from importlib.resources import path
import RPi.GPIO as GPIO
from vlc import Instance
import time
import os
from subprocess import call
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP)
play_flag = 0
playing_album = 0
path_album_one = r"/home/pi/1"
path_album_tow = r"/home/pi/2"
path_album_three = r"/home/pi/3"
class VLC:
def __init__(self):
self.Player = Instance('--loop')
def addPlaylist(self, path):
self.mediaList = self.Player.media_list_new()
songs = os.listdir(path)
for s in songs:
self.mediaList.add_media(self.Player.media_new(os.path.join(path, s)))
self.listPlayer = self.Player.media_list_player_new()
self.listPlayer.set_media_list(self.mediaList)
def play(self):
self.listPlayer.play()
def next(self):
self.listPlayer.next()
def pause(self):
self.listPlayer.pause()
def previous(self):
self.listPlayer.previous()
def stop(self):
self.listPlayer.stop()
def gpio_control(channel, path):
global play_flag
global playing_album
if GPIO.input(channel) == GPIO.LOW and play_flag == 1 and channel != playing_album and path != "volup" and path != "voldown":
p.stop()
p.addPlaylist(path)
playing_album = channel
p.play()
time.sleep(0.3)
if GPIO.input(channel) == GPIO.LOW and play_flag == 0 and path != "volup" and path != "voldown":
p.addPlaylist(path)
play_flag = 1
playing_album = channel
p.play()
time.sleep(0.3)
if GPIO.input(channel) == GPIO.LOW and play_flag == 1 and playing_album == channel and path != "volup" and path != "voldown":
play_flag = 0
playing_album = 0
p.stop()
time.sleep(0.3)
if GPIO.input(channel) == GPIO.LOW and path == "volup":
call(["/usr/bin/amixer", "-q", "-M", "set", "Headphone", "3%+"])
if GPIO.input(channel) == GPIO.LOW and path == "voldown":
call(["/usr/bin/amixer", "-q", "-M", "set", "Headphone", "3%-"])
p = VLC()
while True:
if GPIO.input(17) == GPIO.LOW:
gpio_control(17, path_album_one)
if GPIO.input(22) == GPIO.LOW:
gpio_control(22, path_album_tow)
if GPIO.input(27) == GPIO.LOW:
gpio_control(27, path_album_three)
if GPIO.input(23) == GPIO.LOW:
gpio_control(23, "volup")
if GPIO.input(24) == GPIO.LOW:
gpio_control(24, "voldown")
else:
pass