forked from bobrathbone/piradio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
piface_remote.py
168 lines (143 loc) · 3.96 KB
/
piface_remote.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python
#
# Raspberry Pi PiFace remote control daemon
# $Id: piface_remote.py,v 1.6 2015/03/14 13:21:18 bob Exp $
#
# Author : Bob Rathbone
# Site : http://www.bobrathbone.com
#
# This program uses the piface CAD libraries
# See http://www.piface.org.uk/products/piface_control_and_display/
#
# License: GNU V3, See https://www.gnu.org/copyleft/gpl.html
#
# Disclaimer: Software is provided as is and absolutly no warranties are implied or given.
# The authors shall not be liable for any loss or damage however caused.
#
# The important configuration files are
# /etc/lirc/lircrc Program to event registration file
# /etc/lircd.conf User generated remote control configuration file
#
import RPi.GPIO as GPIO
import pifacecad.ir
import sys
import os
import time
import signal
from signal import SIGUSR1
# Radio project imports
from rc_daemon import Daemon
from log_class import Log
log = Log()
IR_LED=11 # GPIO 11 pin 23
muted = False
pidfile = '/var/run/radiod.pid'
# Signal SIGTERM handler
def signalHandler(signal,frame):
global log
pid = os.getpid()
log.message("Remote control stopped, PID " + str(pid), log.INFO)
sys.exit(0)
# Daemon class
class MyDaemon(Daemon):
def run(self):
log.init('radio')
signal.signal(signal.SIGHUP,signalHandler)
progcall = str(sys.argv)
log.message('Remote control running pid ' + str(os.getpid()), log.INFO)
exec_cmd('sudo service lirc start')
GPIO.setwarnings(False) # Disable warnings
GPIO.setmode(GPIO.BCM) # Use BCM GPIO numbers
GPIO.setup(IR_LED, GPIO.OUT) # Output LED
listener()
def status(self):
# Get the pid from the pidfile
try:
pf = file(self.pidfile,'r')
pid = int(pf.read().strip())
pf.close()
except IOError:
pid = None
if not pid:
message = "Remote control status: not running"
log.message(message, log.INFO)
print message
else:
message = "Remote control running pid " + str(pid)
log.message(message, log.INFO)
print message
return
# End of class overrides
# Handle events
def print_ir_code(event):
global muted
message = "Remote:", event.ir_code
log.message(message, log.DEBUG)
GPIO.output(IR_LED, True)
key = event.ir_code
if key == 'KEY_VOLUMEUP':
exec_cmd('mpc play')
exec_cmd('mpc volume +5')
elif key == 'KEY_VOLUMEDOWN':
exec_cmd('mpc play')
exec_cmd('mpc volume -5')
elif key == 'KEY_CHANNELUP':
exec_cmd('mpc next')
elif key == 'KEY_CHANNELDOWN':
exec_cmd('mpc prev')
elif key == 'KEY_MUTE':
if not muted:
exec_cmd('mpc pause')
muted = True
else:
exec_cmd('mpc play')
exec_cmd('mpc volume +1')
muted = False
elif key == 'KEY_MENU':
if os.path.exists(pidfile):
pf = file(pidfile,'r')
pid = int(pf.read().strip())
pf.close()
os.kill(pid, SIGUSR1)
GPIO.output(IR_LED, False)
return
# Execute system command
def exec_cmd(cmd):
log.message(cmd, log.DEBUG)
p = os.popen(cmd)
result = p.readline().rstrip('\n')
return result
# The main Remote control listen routine
def listener():
log.message("Remote: setup listener", log.DEBUG)
listener = pifacecad.ir.IREventListener(prog="piradio")
listener.register('KEY_VOLUMEUP',print_ir_code)
listener.register('KEY_VOLUMEDOWN',print_ir_code)
listener.register('KEY_CHANNELUP',print_ir_code)
listener.register('KEY_CHANNELDOWN',print_ir_code)
listener.register('KEY_MENU',print_ir_code)
listener.register('KEY_MUTE',print_ir_code)
print "Activating"
listener.activate()
### Main routine ###
if __name__ == "__main__":
daemon = MyDaemon('/var/run/remote.pid')
if len(sys.argv) == 2:
if 'start' == sys.argv[1]:
daemon.start()
elif 'stop' == sys.argv[1]:
daemon.stop()
elif 'restart' == sys.argv[1]:
daemon.restart()
elif 'status' == sys.argv[1]:
daemon.status()
elif 'version' == sys.argv[1]:
print "Version 0.1"
else:
print "Unknown command: " + sys.argv[1]
sys.exit(2)
sys.exit(0)
else:
print "usage: %s start|stop|restart|status|version" % sys.argv[0]
sys.exit(2)
# End of script