-
Notifications
You must be signed in to change notification settings - Fork 0
/
python_lirc.py
executable file
·72 lines (60 loc) · 2.2 KB
/
python_lirc.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
#!/usr/bin/python
'''Python support for transmitting back a mode2 recording'''
import struct
import re
from argparse import ArgumentParser
RE_PULSE = re.compile(r"pulse\s+(\d+)")
RE_SPACE = re.compile(r"space\s+(\d+)")
class RawData(object):
'''Raw data to be sent to a remote'''
def __init__(self, device):
self.device = device
def _prepare(self, data):
'''Prepare everything for transmission'''
buff = bytes()
for value in data:
buff = buff + struct.pack("I", value)
# workaround the "parasite" pulse at the end of the sequence
# produced by the GPIO lirc tx driver - add a long space and a
# dummy pulse
buff = buff + struct.pack("I", 10000)
buff = buff + struct.pack("I", 0)
return buff
def xmit(self, data):
'''Transmit Data'''
l_fd = open(self.device, "w+")
l_fd.write(self._prepare(data))
l_fd.close()
def xmit_from_mode2_file(self, filename):
'''Take Mode2 Data and xmit it'''
started = False
data = []
with open(filename) as file_p:
prev = 0
for line in file_p.readlines():
if RE_PULSE.match(line) is not None:
if prev == 1:
data.append(0)
data.append(int(RE_PULSE.match(line).group(1)))
started = True
prev = 1
if RE_SPACE.match(line) is not None and started:
prev = 0
data.append(int(RE_SPACE.match(line).group(1)))
total = 0
for elem in data:
total = total + elem
self.xmit(data)
def main():
'''Run the LIRC Replay'''
aparser = ArgumentParser(description=main.__doc__)
aparser.add_argument('--device', help='output device', type=str)
aparser.add_argument('--file', help='input file', type=str)
args = vars(aparser.parse_args())
if ((args.get("file") is None) or (args.get("device") is None)):
print "Please supply a --file and --device argument"
exit(1)
r_d = RawData(args.get("device"))
r_d.xmit_from_mode2_file(args.get("file"))
if __name__ == '__main__':
main()