]>
vault307.fbx.one Git - micorpython_ir.git/blob - ir_rx/philips.py
cbd96f8e4de7eb1a2e58a1ba601d0e4e7b50a039
1 # philips.py Decoder for IR remote control using synchronous code
2 # Supports Philips RC-5 RC-6 mode 0 protocols.
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
11 def __init__(self
, pin
, callback
, *args
):
12 # Block lasts <= 30ms and has <= 28 edges
13 super().__init
__(pin
, 28, 30, callback
, *args
)
17 nedges
= self
.edge
# No. of edges detected
18 if not 14 <= nedges
<= 28:
19 raise RuntimeError(self
.OVERRUN
if nedges
> 28 else self
.BADSTART
)
20 # Regenerate bitstream
23 for x
in range(1, nedges
):
24 width
= ticks_diff(self
._times
[x
], self
._times
[x
- 1])
25 if not 500 < width
< 2000:
26 raise RuntimeError(self
.BADBLOCK
)
27 for _
in range(1 if width
< 1334 else 2):
31 self
.verbose
and print(bin(bits
)) # Matches inverted scope waveform
32 # Decode Manchester code
36 m0
= 1 << x
# Mask MS two bits (always 01)
38 v
= 0 # 14 bit bitstream
44 raise RuntimeError(self
.BADBLOCK
)
48 # Split into fields (val, addr, ctrl)
49 val
= (v
& 0x3f) |
(0x40 if ((v
>> 12) & 1) else 0)
50 addr
= (v
>> 6) & 0x1f
53 except RuntimeError as e
:
54 val
, addr
, ctrl
= e
.args
[0], 0, 0
55 # Set up for new data burst and run user callback
56 self
.do_callback(val
, addr
, ctrl
)
59 # Even on Pyboard D the 444μs nominal pulses can be recorded as up to 705μs
60 # Scope shows 360-520 μs (-84μs +76μs relative to nominal)
61 # Header nominal 2666, 889, 444, 889, 444, 444, 444, 444 carrier ON at end
62 hdr
= ((1800, 4000), (593, 1333), (222, 750), (593, 1333), (222, 750), (222, 750), (222, 750), (222, 750))
63 def __init__(self
, pin
, callback
, *args
):
64 # Block lasts 23ms nominal and has <=44 edges
65 super().__init
__(pin
, 44, 30, callback
, *args
)
69 nedges
= self
.edge
# No. of edges detected
70 if not 22 <= nedges
<= 44:
71 raise RuntimeError(self
.OVERRUN
if nedges
> 28 else self
.BADSTART
)
72 for x
, lims
in enumerate(self
.hdr
):
73 width
= ticks_diff(self
._times
[x
+ 1], self
._times
[x
])
74 if not (lims
[0] < width
< lims
[1]):
75 self
.verbose
and print('Bad start', x
, width
, lims
)
76 raise RuntimeError(self
.BADSTART
)
78 width
= ticks_diff(self
._times
[x
+ 1], self
._times
[x
])
79 # 2nd bit of last 0 is 444μs (0) or 1333μs (1)
80 if not 222 < width
< 1555:
81 self
.verbose
and print('Bad block 1 Width', width
, 'x', x
)
82 raise RuntimeError(self
.BADBLOCK
)
86 bits
= 1 # Bits decoded
88 width
= ticks_diff(self
._times
[x
+ 1], self
._times
[x
])
89 if not 222 < width
< 1555:
90 self
.verbose
and print('Bad block 2 Width', width
, 'x', x
)
91 raise RuntimeError(self
.BADBLOCK
)
95 x
+= 1 + int(short
) # If it's short, we know width of next
97 v |
= bit
# MSB of result
101 # -1 convert count to index, -1 because we look ahead
103 raise RuntimeError(self
.BADBLOCK
)
104 # width is 444/889 nominal
105 width
= ticks_diff(self
._times
[x
+ 1], self
._times
[x
])
106 if not 222 < width
< 1111:
107 self
.verbose
and print('Bad block 3 Width', width
, 'x', x
)
108 raise RuntimeError(self
.BADBLOCK
)
118 ss
= '20-bit format {:020b} x={} nedges={} bits={}'
119 print(ss
.format(v
, x
, nedges
, bits
))
122 addr
= (v
>> 8) & 0xff
124 except RuntimeError as e
:
125 val
, addr
, ctrl
= e
.args
[0], 0, 0
126 # Set up for new data burst and run user callback
127 self
.do_callback(val
, addr
, ctrl
)