This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathremote_control.py
256 lines (225 loc) · 6.55 KB
/
remote_control.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!/usr/bin/env python
#
# Raspberry Pi remote control daemon (Non-Piface variants)
# $Id: remote_control.py,v 1.21 2017/10/24 13:08:46 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 ConfigParser
import pifacecad.ir
import sys
import os
import time
import signal
import socket
import errno
# Radio project imports
from config_class import Configuration
from rc_daemon import Daemon
from log_class import Log
log = Log()
IR_LED=11 # GPIO 11 pin 23
remote_led = IR_LED
muted = False
udphost = 'localhost' # IR Listener UDP host default localhost
udpport = 5100 # IR Listener UDP port number default 5100
config = Configuration()
pidfile = '/var/run/pifacercd.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 RemoteDaemon(Daemon):
def run(self):
global remote_led
global udpport
global udphost
log.init('radio')
progcall = str(sys.argv)
log.message(progcall, log.DEBUG)
log.message('Remote control running pid ' + str(os.getpid()), log.INFO)
signal.signal(signal.SIGHUP,signalHandler)
exec_cmd('sudo service lirc start')
remote_led = config.getRemoteLed()
if remote_led > 0:
GPIO.setwarnings(False) # Disable warnings
GPIO.setmode(GPIO.BCM) # Use BCM GPIO numbers
GPIO.setup(remote_led, GPIO.OUT) # Output LED
flash_led(remote_led)
else:
log.message("Remote control LED disabled", log.DEBUG)
udpport = config.getRemoteUdpPort()
udphost = config.getRemoteUdpHost()
log.message("UDP connect host " + udphost + " port " + str(udpport), log.DEBUG)
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
# Test udp send
def send(self,msg):
udpSend(msg)
return
# Test the LED
def flash(self):
log.init('radio')
remote_led = config.getRemoteLed()
if remote_led > 0:
GPIO.setwarnings(False) # Disable warnings
GPIO.setmode(GPIO.BCM) # Use BCM GPIO numbers
GPIO.setup(remote_led, GPIO.OUT) # Output LED
flash_led(remote_led)
return
# End of class overrides
# Handle events
def handleIRevent(event):
global muted
global remote_led
message = "Remote control sent " + event.ir_code
log.message(message, log.DEBUG)
if remote_led > 0:
GPIO.output(remote_led, True)
button = event.ir_code
print button
udpSend(button)
if remote_led > 0:
GPIO.output(remote_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 IR listener", log.DEBUG)
listener = pifacecad.ir.IREventListener(prog="piradio")
listener.register('KEY_VOLUMEUP',handleIRevent)
listener.register('KEY_VOLUMEDOWN',handleIRevent)
listener.register('KEY_CHANNELUP',handleIRevent)
listener.register('KEY_CHANNELDOWN',handleIRevent)
listener.register('KEY_MENU',handleIRevent)
listener.register('KEY_MUTE',handleIRevent)
listener.register('KEY_LEFT',handleIRevent)
listener.register('KEY_RIGHT',handleIRevent)
listener.register('KEY_UP',handleIRevent)
listener.register('KEY_DOWN',handleIRevent)
listener.register('KEY_OK',handleIRevent)
listener.register('KEY_LANGUAGE',handleIRevent)
listener.register('KEY_INFO',handleIRevent)
log.message("Activating IR Remote Control listener", log.DEBUG)
try:
listener.activate()
except Exception as e:
log.message(str(e), log.ERROR)
print e
mesg = "Possible configuration error, check /etc/lircd.conf"
log.message(mesg, log.ERROR)
print mesg
mesg = "Activation IR Remote Control failed - Exiting"
log.message(mesg, log.ERROR)
print mesg
sys.exit(1)
# Send button data to radio program
def udpSend(button):
global udpport
global udphost
data = ''
log.message("Remote control daemon udpSend " + button, log.DEBUG)
try:
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
clientsocket.settimeout(3)
clientsocket.sendto(button, (udphost, udpport))
data = clientsocket.recv(100).strip()
except socket.error, e:
err = e.args[0]
if err == errno.EAGAIN or err == errno.EWOULDBLOCK:
log.message("IR remote udpSend no data" + e, log.ERROR)
else:
# Errors such as timeout
log.message("IR remote udpSend " + str(e), log.ERROR)
if len(data) > 0:
log.message("IR daemon server sent: " + data, log.DEBUG)
return data
# Flash the IR activity LED
def flash_led(led):
count = 6
delay = 0.3
log.message("Flash LED on GPIO " + str(led), log.DEBUG)
while count > 0:
GPIO.output(led, True)
time.sleep(delay)
GPIO.output(led, False)
time.sleep(delay)
count -= 1
return
# Execute system command
def execCommand(cmd):
p = os.popen(cmd)
return p.readline().rstrip('\n')
# Print usage
def usage():
print "usage: %s start|stop|status|version|flash|send|config" % sys.argv[0]
sys.exit(2)
### Main routine ###
if __name__ == "__main__":
daemon = RemoteDaemon('/var/run/remote.pid')
if len(sys.argv) == 2:
if 'start' == sys.argv[1]:
daemon.start()
elif 'nodaemon' == sys.argv[1]:
daemon.nodaemon()
elif 'stop' == sys.argv[1]:
daemon.stop()
elif 'flash' == sys.argv[1]:
daemon.flash()
elif 'status' == sys.argv[1]:
daemon.status()
elif 'version' == sys.argv[1]:
print "Version 0.1"
elif 'send' == sys.argv[1]:
daemon.send('IR_REMOTE')
elif 'config' == sys.argv[1]:
config = Configuration()
print "LED = GPIO", config.getRemoteLed()
print "HOST =", config.getRemoteUdpHost()
print "PORT =", config.getRemoteUdpPort()
print "LISTEN =", config.getRemoteListenHost()
else:
print "Unknown command: " + sys.argv[1]
usage()
sys.exit(0)
else:
usage()
# End of script