]>
vault307.fbx.one Git - micorpython_ir.git/blob - ir_rx/sony.py
1 # sony.py Decoder for IR remote control using synchronous code
5 # Copyright Peter Hinch 2020 Released under the MIT license
7 from utime
import ticks_us
, ticks_diff
8 from ir_rx
import IR_RX
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
)
22 nedges
= self
.edge
# No. of edges detected
23 self
.verbose
and print('nedges', nedges
)
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
)
37 val
= 0 # Data received, LSB 1st
40 while x
<= nedges
- 2:
41 if ticks_diff(self
._times
[x
+ 1], self
._times
[x
]) > 900:
45 cmd
= val
& 0x7f # 7 bit command
48 addr
= val
& 0xff # 5 or 8 bit addr
51 addr
= val
& 0x1f # 5 bit addr
52 val
>>= 5 # 8 bit extended
53 except RuntimeError as e
:
57 self
.do_callback(cmd
, addr
, val
)
59 class SONY_12(SONY_ABC
):
60 def __init__(self
, pin
, callback
, *args
):
61 super().__init
__(pin
, 12, callback
, *args
)
63 class SONY_15(SONY_ABC
):
64 def __init__(self
, pin
, callback
, *args
):
65 super().__init
__(pin
, 15, callback
, *args
)
67 class SONY_20(SONY_ABC
):
68 def __init__(self
, pin
, callback
, *args
):
69 super().__init
__(pin
, 20, callback
, *args
)