]> vault307.fbx.one Git - micorpython_ir.git/blob - ir_rx/sony.py
Release 0.1 Various bugfixes and improvements.
[micorpython_ir.git] / ir_rx / sony.py
1 # sony.py Decoder for IR remote control using synchronous code
2 # Sony SIRC protocol.
3
4 # Author: Peter Hinch
5 # Copyright Peter Hinch 2020 Released under the MIT license
6
7 from utime import ticks_us, ticks_diff
8 from ir_rx import IR_RX
9
10 class SONY_ABC(IR_RX): # Abstract base class
11 def __init__(self, pin, bits, callback, *args):
12 # 20 bit block has 42 edges and lasts <= 39ms nominal. Add 4ms to time
13 # for tolerances except in 20 bit case where timing is tight with a
14 # repeat period of 45ms.
15 t = int(3 + bits * 1.8) + (1 if bits == 20 else 4)
16 super().__init__(pin, 2 + bits * 2, t, callback, *args)
17 self._addr = 0
18 self._bits = 20
19
20 def decode(self, _):
21 try:
22 nedges = self.edge # No. of edges detected
23 self.verbose and print('nedges', nedges)
24 if nedges > 42:
25 raise RuntimeError(self.OVERRUN)
26 bits = (nedges - 2) // 2
27 if nedges not in (26, 32, 42) or bits > self._bits:
28 raise RuntimeError(self.BADBLOCK)
29 self.verbose and print('SIRC {}bit'.format(bits))
30 width = ticks_diff(self._times[1], self._times[0])
31 if not 1800 < width < 3000: # 2.4ms leading mark for all valid data
32 raise RuntimeError(self.BADSTART)
33 width = ticks_diff(self._times[2], self._times[1])
34 if not 350 < width < 1000: # 600μs space
35 raise RuntimeError(self.BADSTART)
36
37 val = 0 # Data received, LSB 1st
38 x = 2
39 bit = 1
40 while x <= nedges - 2:
41 if ticks_diff(self._times[x + 1], self._times[x]) > 900:
42 val |= bit
43 bit <<= 1
44 x += 2
45 cmd = val & 0x7f # 7 bit command
46 val >>= 7
47 if nedges < 42:
48 addr = val & 0xff # 5 or 8 bit addr
49 val = 0
50 else:
51 addr = val & 0x1f # 5 bit addr
52 val >>= 5 # 8 bit extended
53 except RuntimeError as e:
54 cmd = e.args[0]
55 addr = 0
56 val = 0
57 self.do_callback(cmd, addr, val)
58
59 class SONY_12(SONY_ABC):
60 def __init__(self, pin, callback, *args):
61 super().__init__(pin, 12, callback, *args)
62
63 class SONY_15(SONY_ABC):
64 def __init__(self, pin, callback, *args):
65 super().__init__(pin, 15, callback, *args)
66
67 class SONY_20(SONY_ABC):
68 def __init__(self, pin, callback, *args):
69 super().__init__(pin, 20, callback, *args)
70