This repository has been archived by the owner on Jan 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapture.py
131 lines (110 loc) · 3.84 KB
/
capture.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
import sys, time, logging, tempfile, json, base64
from twisted.python import usage, log
from twisted.internet import protocol
class IrToy(protocol.Protocol):
def __init__(self, options):
self.sleep_time = 0.005
self.handshake = None
self.byte_count = 0
self.complete = None
self.state = 'set_mode'
self.rbuf = bytearray()
self.wbuf = bytearray()
self.wbuf_cursor = 0
self.output_dir = options['dir']
self.options = options
self.send_remaining = 0
def _sleep(self):
time.sleep(self.sleep_time)
def connectionMade(self):
""" Reset the device to make sure we get it out of any mode it's in
and then set it to sampling mode """
self._sleep()
self.transport.writeSomeData("\0\0\0\0\0")
self._sleep()
self.transport.writeSomeData("S")
self._sleep()
def ready(self):
pass
def _state_set_mode(self, byte):
if len(self.rbuf) == 3:
self.rbuf = bytearray()
self.ready()
return 'recv'
else:
return 'set_mode'
def _state_recv(self, byte):
if self.rbuf.endswith(b'\xff\xff'):
with tempfile.NamedTemporaryFile(prefix='ir-',
dir=self.output_dir,
suffix='.bin',
delete=False) as f:
f.write(self.rbuf)
log.msg("Wrote IR file %s" % f.name)
self.rbuf = bytearray()
return 'recv'
return 'recv'
def _state_exit(self, *args):
self._sleep()
self.transport.writeSomeData("\0")
self._sleep()
reactor.stop()
raise SystemExit
def dataReceived(self, data):
log.msg("Data: %s" % ["%#x" % ord(byte) for byte in data])
for c in data:
byte = ord(c)
self.rbuf.append(byte)
self.state = getattr(self, '_state_'+self.state)(byte)
class TransmitIrToy(IrToy):
def _state_recv(self, byte):
if byte == 0x3E and self.wbuf_cursor < len(self.wbuf):
self.send_remaining = 0x3E
return self._state_start_xmit(byte)
elif byte == ord('C'):
return self._state_exit(byte)
else:
return 'recv'
def _state_start_xmit(self, byte):
self._sleep()
buf_slice = self.wbuf[
self.wbuf_cursor:self.wbuf_cursor+self.send_remaining]
self.transport.write("".join(map(chr, buf_slice)))
self.wbuf_cursor += len(buf_slice)
return 'recv'
def ready(self):
self.setTransmitMode()
def setTransmitMode(self):
self._sleep()
with open(self.options['file'], 'r') as infile:
button_data = json.load(infile)
self.wbuf = bytearray(
base64.b64decode(button_data['irdata']))
# Enable transmit handshake, return transmit count and notify on
# complete
self.transport.writeSomeData("\x26\x25\x24")
# Enter transmit mode
self._sleep()
self.transport.writeSomeData("\x03")
class Options(usage.Options):
optParameters = [
['port', 'p', '/dev/ttyACM0', 'ir toy'],
['dir', 'd', None, 'directory'],
['mode', 'm', 'recv', 'mode'],
['file', 'f', None, 'xmit file'],
]
if __name__ == '__main__':
from twisted.internet import reactor
from twisted.internet.serialport import SerialPort
o = Options()
try:
o.parseOptions()
except usage.UsageError, errortext:
raise SystemExit
logFile = sys.stdout
log.startLogging(logFile)
if o.opts['mode'] == 'xmit':
SerialPort(TransmitIrToy(o.opts), o.opts['port'], reactor, baudrate=115200)
else:
SerialPort(IrToy(o.opts), o.opts['port'], reactor, baudrate=115200)
reactor.run()