/
pyirw.py
executable file
·41 lines (31 loc) · 1.04 KB
/
pyirw.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
#!/usr/bin/env python
# Read lirc output, in order to sense key presses on an IR remote.
# There are various Python packages that claim to do this but
# they tend to require elaborate setup and I couldn't get any to work.
# This approach requires a lircd.conf but does not require a lircrc.
# If irw works, then in theory, this should too.
# Based on irw.c, https://github.com/aldebaran/lirc/blob/master/tools/irw.c
import socket
SOCKPATH = "/var/run/lirc/lircd"
sock = None
def init_irw():
global sock
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
print ('starting up on %s' % SOCKPATH)
sock.connect(SOCKPATH)
def next_key():
'''Get the next key pressed. Return keyname, updown.
'''
while True:
data = sock.recv(128)
# print("Data: " + data)
data = data.strip()
if data:
break
words = data.split()
return words[2], words[1]
if __name__ == '__main__':
init_irw()
while True:
keyname, updown = next_key()
print('%s (%s)' % (keyname, updown))