/
player.py
executable file
·105 lines (90 loc) · 3.79 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
#!/usr/bin/env python
import time
import RPi.GPIO as GPIO
import os
import signal # to handle Ctrl+C
import sys
class Player():
def __init__(self,
EXT_SWITCH_CALLBACK,
TILT_SWITCH_CALLBACK,
PIN_LED_GREEN=18,
PIN_LED_RED=16,
PIN_TILT_SWITCH=11,
PIN_EXT_SWITCH=13):
"""
Sets the Player with default pinout if not provided
"""
self.PIN_LED_GREEN=PIN_LED_GREEN
self.PIN_LED_RED=PIN_LED_RED
self.PIN_TILT_SWITCH=PIN_TILT_SWITCH
self.PIN_EXT_SWITCH=PIN_EXT_SWITCH
self.TILT_SWITCH_CALLBACK=TILT_SWITCH_CALLBACK
self.EXT_SWITCH_CALLBACK=EXT_SWITCH_CALLBACK
self.pwd=os.path.dirname(__file__)
def setup(self):
# configure default audio output
os.system("amixer -q cset numid=3 1")
# hijack the Ctrl+C event and run teardown()
signal.signal(signal.SIGINT, self.teardown)
# setup the board type and clean the board config
GPIO.setmode(GPIO.BOARD)
GPIO.cleanup()
# configure inputs and outputs
GPIO.setup(self.PIN_LED_GREEN, GPIO.OUT) # green led
GPIO.setup(self.PIN_LED_RED, GPIO.OUT) # red led
GPIO.setup(self.PIN_TILT_SWITCH, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(self.PIN_EXT_SWITCH, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# add an event listener and callback function for the ext switch
GPIO.add_event_detect(self.PIN_EXT_SWITCH, GPIO.RISING, bouncetime=500)
GPIO.add_event_callback(self.PIN_EXT_SWITCH, self.EXT_SWITCH_CALLBACK, bouncetime=500)
#GPIO.add_event_callback(self.PIN_EXT_SWITCH, self.extSwitchCallBack, bouncetime=200)
# add an event listener and callback function for the tilt switch
GPIO.add_event_detect(self.PIN_TILT_SWITCH, GPIO.RISING, bouncetime=500)
GPIO.add_event_callback(self.PIN_TILT_SWITCH, self.TILT_SWITCH_CALLBACK, bouncetime=500)
#GPIO.add_event_callback(self.PIN_TILT_SWITCH, self.tiltSwitchCallBack, bouncetime=500)
# turn on the LEDs for 1 seconds to indicated that player booted up
self.toggleGreenLed()
self.toggleRedLed()
time.sleep(1)
self.toggleGreenLed()
self.toggleRedLed()
# wait until Ctrl+C is pressed
signal.pause()
def teardown(self, signal, frame):
"""
Will close the app in a nice way.
"""
# turn off the LEDs
GPIO.output(self.PIN_LED_GREEN, False) # turn the LED off
GPIO.output(self.PIN_LED_RED, False)
# cleant the board config
GPIO.cleanup()
# terminate the app
sys.exit(0)
def toggleGreenLed(self):
GPIO.output(self.PIN_LED_GREEN, not GPIO.input(self.PIN_LED_GREEN))
def toggleRedLed(self):
GPIO.output(self.PIN_LED_RED, not GPIO.input(self.PIN_LED_RED))
def input(self, channel):
return GPIO.input(channel)
def playMp3(self, filename):
try:
with open('%s/audio/%s' % (self.pwd, filename)):
os.system("mpg321 -q -g 40 %s/audio/%s" % (self.pwd,filename))
except IOError:
print "Couldn't find audio file: ./audio/%s" % filename
@staticmethod
def tiltSwitchCallBack(channel):
if GPIO.input(channel):
#GPIO.output(self.PIN_LED_GREEN, not GPIO.input(self.PIN_LED_GREEN))
print "Tilt switch triggered", time.time()
@staticmethod
def extSwitchCallBack(channel):
if GPIO.input(channel):
#GPIO.output(self.PIN_LED_RED, not GPIO.input(self.PIN_LED_RED))
print "Ext Switch Pressed", time.time()
if __name__ == "__main__":
p=Player(TILT_SWITCH_CALLBACK=Player.tiltSwitchCallBack,
EXT_SWITCH_CALLBACK=Player.extSwitchCallBack)
p.setup()