]>
vault307.fbx.one Git - micorpython_ir.git/blob - ir_rx/nec.py
1 # nec.py Decoder for IR remote control using synchronous code
2 # Supports NEC and Samsung protocols.
3 # With thanks to J.E. Tannenbaum for information re Samsung protocol
5 # For a remote using NEC see https://www.adafruit.com/products/389
8 # Copyright Peter Hinch 2020-2022 Released under the MIT license
10 from utime
import ticks_us
, ticks_diff
11 from ir_rx
import IR_RX
14 def __init__(self
, pin
, extended
, samsung
, callback
, *args
):
15 # Block lasts <= 80ms (extended mode) and has 68 edges
16 super().__init
__(pin
, 68, 80, callback
, *args
)
17 self
._extended
= extended
19 self
._leader
= 2500 if samsung
else 4000 # 4.5ms for Samsung else 9ms
24 raise RuntimeError(self
.OVERRUN
)
25 width
= ticks_diff(self
._times
[1], self
._times
[0])
26 if width
< self
._leader
: # 9ms leading mark for all valid data
27 raise RuntimeError(self
.BADSTART
)
28 width
= ticks_diff(self
._times
[2], self
._times
[1])
29 if width
> 3000: # 4.5ms space for normal data
30 if self
.edge
< 68: # Haven't received the correct number of edges
31 raise RuntimeError(self
.BADBLOCK
)
32 # Time spaces only (marks are always 562.5µs)
33 # Space is 1.6875ms (1) or 562.5µs (0)
34 # Skip last bit which is always 1
36 for edge
in range(3, 68 - 2, 2):
38 if ticks_diff(self
._times
[edge
+ 1], self
._times
[edge
]) > 1120:
40 elif width
> 1700: # 2.5ms space for a repeat code. Should have exactly 4 edges.
41 raise RuntimeError(self
.REPEAT
if self
.edge
== 4 else self
.BADREP
) # Treat REPEAT as error.
43 raise RuntimeError(self
.BADSTART
)
44 addr
= val
& 0xff # 8 bit addr
45 cmd
= (val
>> 16) & 0xff
46 if cmd
!= (val
>> 24) ^
0xff:
47 raise RuntimeError(self
.BADDATA
)
48 if addr
!= ((val
>> 8) ^
0xff) & 0xff: # 8 bit addr doesn't match check
49 if not self
._extended
:
50 raise RuntimeError(self
.BADADDR
)
51 addr |
= val
& 0xff00 # pass assumed 16 bit address to callback
53 except RuntimeError as e
:
55 addr
= self
._addr
if cmd
== self
.REPEAT
else 0 # REPEAT uses last address
56 # Set up for new data burst and run user callback
57 self
.do_callback(cmd
, addr
, 0, self
.REPEAT
)
60 def __init__(self
, pin
, callback
, *args
):
61 super().__init
__(pin
, False, False, callback
, *args
)
63 class NEC_16(NEC_ABC
):
64 def __init__(self
, pin
, callback
, *args
):
65 super().__init
__(pin
, True, False, callback
, *args
)
67 class SAMSUNG(NEC_ABC
):
68 def __init__(self
, pin
, callback
, *args
):
69 super().__init
__(pin
, True, True, callback
, *args
)