-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
135 lines (120 loc) · 3.91 KB
/
player.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
import RPi.GPIO as GPIO
import playback
import threading
from time import sleep
class Player(threading.Thread):
def __init__(self, config):
self.config = config
# Get GPIO Pins
self.v_dn = config['controls']['v_dn']
self.v_up = config['controls']['v_up']
self.prv = config['controls']['prev']
self.pp = config['controls']['pp']
self.nxt = config['controls']['next']
# Initialize volume
self.default_volume = config['alarm']['default_volume']
self.max_vol = config['alarm']['max_vol']
self.min_vol = config['alarm']['min_vol']
self.keep_running = True
playback.volume(self.default_volume)
self.volume_now = self.default_volume
# Setup GPIO
GPIO.setwarnings(True)
# Use BCM mode
GPIO.setmode(GPIO.BCM)
# define the Encoder switch inputs
GPIO.setup(self.v_up, GPIO.IN)
GPIO.setup(self.v_dn, GPIO.IN)
# define the Button switch inputs
# GPIO.setup(snz, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(self.prv, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(self.pp, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(self.nxt, GPIO.IN, pull_up_down=GPIO.PUD_UP)
### Volume knob events
# setup callback thread for the A and B encoder
# use interrupts for all inputs
self.host = config['player']['host']
self.uri = config['player']['uri']
playback.load_playlist()
GPIO.add_event_detect(
self.v_dn,
GPIO.FALLING,
callback=self.volume_dn,
bouncetime=4
)
GPIO.add_event_detect(
self.v_up,
GPIO.FALLING,
callback=self.volume_up,
bouncetime=4
)
# Button events
GPIO.add_event_detect(
self.prv,
GPIO.FALLING,
callback=self.pr,
bouncetime=500
)
GPIO.add_event_detect(
self.pp,
GPIO.FALLING,
callback=self.pyps,
bouncetime=500
)
GPIO.add_event_detect(
self.nxt,
GPIO.FALLING,
callback=self.nx,
bouncetime=500
)
threading.Thread.__init__(self)
self.daemon = False
self.start()
return
def pr(self, b):
playback.prev()
return
def pyps(self, b):
print "pp pressed"
playback.pp()
return
def nx(self, b):
playback.next()
return
def volume_up(self, k):
# read both of the switches
up = GPIO.input(self.v_up)
down = GPIO.input(self.v_dn)
print "up: " + str(up)
print "down: " + str(down)
if (up == 1) and (down == 0) : # up then down ->
self.volume_now += 1
print "direction -> ", self.volume_now
while (up != 1 and down !=1):
up = GPIO.input(self.v_up)
down = GPIO.input(self.v_dn)
playback.volume(self.volume_now)
return
else: # discard all other combinations
return
def volume_dn(self, k):
# read both of the switches
up = GPIO.input(self.v_up)
down = GPIO.input(self.v_dn)
if (up == 0) and (down == 1):
self.volume_now -= 1
print "direction <- ", self.volume_now
# A is already high, wait for A to drop to end the click cycle
while (up != 1 and down !=1):
up = GPIO.input(self.v_up)
down = GPIO.input(self.v_dn)
playback.volume(self.volume_now)
return
else: # discard all other combinations
return
def run(self):
print "Player running!"
while self.keep_running:
if playback.tracks() == 0:
playback.load_playlist()
sleep(10)