]>
vault307.fbx.one Git - micorpython_ir.git/blob - ir_rx.py
1 # ir_rx.py Decoder for IR remote control using synchronous code
2 # Supports RC-5 RC-6 mode 0 and NEC protocols.
3 # For a remote using NEC see https://www.adafruit.com/products/389
6 # Copyright Peter Hinch 2020 Released under the MIT license
8 from sys
import platform
9 from micropython
import const
10 from machine
import Timer
11 from array
import array
12 from utime
import ticks_us
, ticks_diff
14 if platform
== 'pyboard':
15 from pyb
import Pin
, ExtInt
17 from machine
import Pin
19 ESP32
= platform
== 'esp32' or platform
== 'esp32_LoBo'
22 # from micropython import alloc_emergency_exception_buf
23 # alloc_emergency_exception_buf(100)
25 # Result codes (accessible to application)
37 # On 1st edge start a block timer. While the timer is running, record the time
38 # of each edge. When the timer times out decode the data. Duration must exceed
39 # the worst case block transmission time, but be less than the interval between
40 # a block start and a repeat code start (~108ms depending on protocol)
44 def __init__(self
, pin
, nedges
, tblock
, callback
, *args
): # Optional args for callback
47 self
.callback
= callback
50 self
._times
= array('i', (0 for _
in range(nedges
+ 1))) # +1 for overrun
51 if platform
== 'pyboard':
52 ExtInt(pin
, ExtInt
.IRQ_RISING_FALLING
, Pin
.PULL_NONE
, self
._cb
_pin
)
54 pin
.irq(handler
= self
._cb
_pin
, trigger
= (Pin
.IRQ_FALLING | Pin
.IRQ_RISING
))
56 pin
.irq(handler
= self
._cb
_pin
, trigger
= (Pin
.IRQ_FALLING | Pin
.IRQ_RISING
), hard
= True)
58 self
.tim
= Timer(-1) # Sofware timer
62 # Pin interrupt. Save time of each edge for later decode.
63 def _cb_pin(self
, line
):
65 # On overrun ignore pulses until software timer times out
66 if self
.edge
<= self
._nedges
: # Allow 1 extra pulse to record overrun
67 if not self
.edge
: # First edge received
68 self
.tim
.init(period
=self
._tblock
, mode
=Timer
.ONE_SHOT
, callback
=self
.cb
)
69 self
._times
[self
.edge
] = t
73 def __init__(self
, pin
, callback
, extended
=True, *args
):
74 # Block lasts <= 80ms and has 68 edges
75 tblock
= 80 if extended
else 73 # Allow for some tx tolerance (?)
76 super().__init
__(pin
, 68, tblock
, callback
, *args
)
77 self
._extended
= extended
83 raise RuntimeError(OVERRUN
)
84 width
= ticks_diff(self
._times
[1], self
._times
[0])
85 if width
< 4000: # 9ms leading mark for all valid data
86 raise RuntimeError(BADSTART
)
87 width
= ticks_diff(self
._times
[2], self
._times
[1])
88 if width
> 3000: # 4.5ms space for normal data
89 if self
.edge
< 68: # Haven't received the correct number of edges
90 raise RuntimeError(BADBLOCK
)
91 # Time spaces only (marks are always 562.5µs)
92 # Space is 1.6875ms (1) or 562.5µs (0)
93 # Skip last bit which is always 1
95 for edge
in range(3, 68 - 2, 2):
97 if ticks_diff(self
._times
[edge
+ 1], self
._times
[edge
]) > 1120:
99 elif width
> 1700: # 2.5ms space for a repeat code. Should have exactly 4 edges.
100 raise RuntimeError(REPEAT
if self
.edge
== 4 else BADREP
) # Treat REPEAT as error.
102 raise RuntimeError(BADSTART
)
103 addr
= val
& 0xff # 8 bit addr
104 cmd
= (val
>> 16) & 0xff
105 if cmd
!= (val
>> 24) ^
0xff:
106 raise RuntimeError(BADDATA
)
107 if addr
!= ((val
>> 8) ^
0xff) & 0xff: # 8 bit addr doesn't match check
108 if not self
._extended
:
109 raise RuntimeError(BADADDR
)
110 addr |
= val
& 0xff00 # pass assumed 16 bit address to callback
112 except RuntimeError as e
:
114 addr
= self
._addr
if cmd
== REPEAT
else 0 # REPEAT uses last address
115 self
.edge
= 0 # Set up for new data burst and run user callback
116 self
.callback(cmd
, addr
, 0, *self
.args
)
119 class SONY_IR(IR_RX
):
120 def __init__(self
, pin
, callback
, bits
=20, *args
):
121 # 20 bit block has 42 edges and lasts <= 39ms nominal. Add 4ms to time
122 # for tolerances except in 20 bit case where timing is tight with a
123 # repeat period of 45ms.
124 t
= int(3 + bits
* 1.8) + (1 if bits
== 20 else 4)
125 super().__init
__(pin
, 2 + bits
* 2, t
, callback
, *args
)
131 nedges
= self
.edge
# No. of edges detected
134 raise RuntimeError(OVERRUN
)
135 bits
= (nedges
- 2) // 2
136 if nedges
not in (26, 32, 42) or bits
> self
._bits
:
137 raise RuntimeError(BADBLOCK
)
138 self
.verbose
and print('SIRC {}bit'.format(bits
))
139 width
= ticks_diff(self
._times
[1], self
._times
[0])
140 if not 1800 < width
< 3000: # 2.4ms leading mark for all valid data
141 raise RuntimeError(BADSTART
)
142 width
= ticks_diff(self
._times
[2], self
._times
[1])
143 if not 350 < width
< 1000: # 600μs space
144 raise RuntimeError(BADSTART
)
146 val
= 0 # Data received, LSB 1st
149 while x
< nedges
- 2:
150 if ticks_diff(self
._times
[x
+ 1], self
._times
[x
]) > 900:
155 cmd
= val
& 0x7f # 7 bit command
158 addr
= val
& 0xff # 5 or 8 bit addr
161 addr
= val
& 0x1f # 5 bit addr
162 val
>>= 5 # 8 bit extended
163 except RuntimeError as e
:
167 self
.edge
= 0 # Set up for new data burst and run user callback
168 self
.callback(cmd
, addr
, val
, *self
.args
)
171 def __init__(self
, pin
, callback
, *args
):
172 # Block lasts <= 30ms and has <= 28 edges
173 super().__init
__(pin
, 28, 30, callback
, *args
)
177 nedges
= self
.edge
# No. of edges detected
178 if not 14 <= nedges
<= 28:
179 raise RuntimeError(OVERRUN
if nedges
> 28 else BADSTART
)
180 # Regenerate bitstream
183 for x
in range(1, nedges
):
184 width
= ticks_diff(self
._times
[x
], self
._times
[x
- 1])
185 if not 500 < width
< 2000:
186 raise RuntimeError(BADBLOCK
)
187 for _
in range(1 if width
< 1334 else 2):
191 self
.verbose
and print(bin(bits
)) # Matches inverted scope waveform
192 # Decode Manchester code
196 m0
= 1 << x
# Mask MS two bits (always 01)
198 v
= 0 # 14 bit bitstream
204 raise RuntimeError(BADBLOCK
)
208 # Split into fields (val, addr, ctrl)
209 val
= (v
& 0x3f) |
(0x40 if ((v
>> 12) & 1) else 0)
210 addr
= (v
>> 6) & 0x1f
213 except RuntimeError as e
:
214 val
, addr
, ctrl
= e
.args
[0], 0, 0
215 self
.edge
= 0 # Set up for new data burst and run user callback
216 self
.callback(val
, addr
, ctrl
, *self
.args
)
219 # Even on Pyboard D the 444μs nominal pulses can be recorded as up to 705μs
220 # Scope shows 360-520 μs (-84μs +76μs relative to nominal)
221 # Header nominal 2666, 889, 444, 889, 444, 444, 444, 444 carrier ON at end
222 hdr
= ((1800, 4000), (593, 1333), (222, 750), (593, 1333), (222, 750), (222, 750), (222, 750), (222, 750))
223 def __init__(self
, pin
, callback
, *args
):
224 # Block lasts 23ms nominal and has <=44 edges
225 super().__init
__(pin
, 44, 30, callback
, *args
)
229 nedges
= self
.edge
# No. of edges detected
230 if not 22 <= nedges
<= 44:
231 raise RuntimeError(OVERRUN
if nedges
> 28 else BADSTART
)
232 for x
, lims
in enumerate(self
.hdr
):
233 width
= ticks_diff(self
._times
[x
+ 1], self
._times
[x
])
234 if not (lims
[0] < width
< lims
[1]):
235 self
.verbose
and print('Bad start', x
, width
, lims
)
236 raise RuntimeError(BADSTART
)
238 width
= ticks_diff(self
._times
[x
+ 1], self
._times
[x
])
239 # 2nd bit of last 0 is 444μs (0) or 1333μs (1)
240 if not 222 < width
< 1555:
241 self
.verbose
and print('Bad block 1 Width', width
, 'x', x
)
242 raise RuntimeError(BADBLOCK
)
246 bits
= 1 # Bits decoded
248 width
= ticks_diff(self
._times
[x
+ 1], self
._times
[x
])
249 if not 222 < width
< 1555:
250 self
.verbose
and print('Bad block 2 Width', width
, 'x', x
)
251 raise RuntimeError(BADBLOCK
)
255 x
+= 1 + int(short
) # If it's short, we know width of next
257 v |
= bit
# MSB of result
261 # -1 convert count to index, -1 because we look ahead
263 raise RuntimeError(BADBLOCK
)
264 # width is 444/889 nominal
265 width
= ticks_diff(self
._times
[x
+ 1], self
._times
[x
])
266 if not 222 < width
< 1111:
267 self
.verbose
and print('Bad block 3 Width', width
, 'x', x
)
268 raise RuntimeError(BADBLOCK
)
278 ss
= '20-bit format {:020b} x={} nedges={} bits={}'
279 print(ss
.format(v
, x
, nedges
, bits
))
282 addr
= (v
>> 8) & 0xff
284 except RuntimeError as e
:
285 val
, addr
, ctrl
= e
.args
[0], 0, 0
286 self
.edge
= 0 # Set up for new data burst and run user callback
287 self
.callback(val
, addr
, ctrl
, *self
.args
)