]>
vault307.fbx.one Git - micorpython_ir.git/blob - ir_rx.py
69d2da983fedb4bfcf617f55af0b6232e75e2de4
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 ei
= ExtInt(pin
, ExtInt
.IRQ_RISING_FALLING
, Pin
.PULL_NONE
, self
._cb
_pin
)
54 ei
= pin
.irq(handler
= self
._cb
_pin
, trigger
= (Pin
.IRQ_FALLING | Pin
.IRQ_RISING
))
56 ei
= pin
.irq(handler
= self
._cb
_pin
, trigger
= (Pin
.IRQ_FALLING | Pin
.IRQ_RISING
), hard
= True)
57 self
._eint
= ei
# Keep reference??
59 self
.tim
= Timer(-1) # Sofware timer
63 # Pin interrupt. Save time of each edge for later decode.
64 def _cb_pin(self
, line
):
66 # On overrun ignore pulses until software timer times out
67 if self
.edge
<= self
._nedges
: # Allow 1 extra pulse to record overrun
68 if not self
.edge
: # First edge received
69 self
.tim
.init(period
=self
._tblock
, mode
=Timer
.ONE_SHOT
, callback
=self
.cb
)
70 self
._times
[self
.edge
] = t
74 def __init__(self
, pin
, callback
, extended
=True, *args
):
75 # Block lasts <= 80ms and has 68 edges
76 tblock
= 80 if extended
else 73 # Allow for some tx tolerance (?)
77 super().__init
__(pin
, 68, tblock
, callback
, *args
)
78 self
._extended
= extended
84 raise RuntimeError(OVERRUN
)
85 width
= ticks_diff(self
._times
[1], self
._times
[0])
86 if width
< 4000: # 9ms leading mark for all valid data
87 raise RuntimeError(BADSTART
)
88 width
= ticks_diff(self
._times
[2], self
._times
[1])
89 if width
> 3000: # 4.5ms space for normal data
90 if self
.edge
< 68: # Haven't received the correct number of edges
91 raise RuntimeError(BADBLOCK
)
92 # Time spaces only (marks are always 562.5µs)
93 # Space is 1.6875ms (1) or 562.5µs (0)
94 # Skip last bit which is always 1
96 for edge
in range(3, 68 - 2, 2):
98 if ticks_diff(self
._times
[edge
+ 1], self
._times
[edge
]) > 1120:
100 elif width
> 1700: # 2.5ms space for a repeat code. Should have exactly 4 edges.
101 raise RuntimeError(REPEAT
if self
.edge
== 4 else BADREP
) # Treat REPEAT as error.
103 raise RuntimeError(BADSTART
)
104 addr
= val
& 0xff # 8 bit addr
105 cmd
= (val
>> 16) & 0xff
106 if cmd
!= (val
>> 24) ^
0xff:
107 raise RuntimeError(BADDATA
)
108 if addr
!= ((val
>> 8) ^
0xff) & 0xff: # 8 bit addr doesn't match check
109 if not self
._extended
:
110 raise RuntimeError(BADADDR
)
111 addr |
= val
& 0xff00 # pass assumed 16 bit address to callback
113 except RuntimeError as e
:
115 addr
= self
._addr
if cmd
== REPEAT
else 0 # REPEAT uses last address
116 self
.edge
= 0 # Set up for new data burst and run user callback
117 self
.callback(cmd
, addr
, 0, *self
.args
)
120 class SONY_IR(IR_RX
):
121 def __init__(self
, pin
, callback
, bits
=20, *args
):
122 # 20 bit block has 42 edges and lasts <= 39ms nominal. Add 4ms to time
123 # for tolerances except in 20 bit case where timing is tight with a
124 # repeat period of 45ms.
125 t
= int(3 + bits
* 1.8) + (1 if bits
== 20 else 4)
126 super().__init
__(pin
, 2 + bits
* 2, t
, callback
, *args
)
132 nedges
= self
.edge
# No. of edges detected
133 self
.verbose
and print('nedges', nedges
)
135 raise RuntimeError(OVERRUN
)
136 bits
= (nedges
- 2) // 2
137 if nedges
not in (26, 32, 42) or bits
> self
._bits
:
138 raise RuntimeError(BADBLOCK
)
139 self
.verbose
and print('SIRC {}bit'.format(bits
))
140 width
= ticks_diff(self
._times
[1], self
._times
[0])
141 if not 1800 < width
< 3000: # 2.4ms leading mark for all valid data
142 raise RuntimeError(BADSTART
)
143 width
= ticks_diff(self
._times
[2], self
._times
[1])
144 if not 350 < width
< 1000: # 600μs space
145 raise RuntimeError(BADSTART
)
147 val
= 0 # Data received, LSB 1st
150 while x
< nedges
- 2:
151 if ticks_diff(self
._times
[x
+ 1], self
._times
[x
]) > 900:
156 cmd
= val
& 0x7f # 7 bit command
159 addr
= val
& 0xff # 5 or 8 bit addr
162 addr
= val
& 0x1f # 5 bit addr
163 val
>>= 5 # 8 bit extended
164 except RuntimeError as e
:
168 self
.edge
= 0 # Set up for new data burst and run user callback
169 self
.callback(cmd
, addr
, val
, *self
.args
)
172 def __init__(self
, pin
, callback
, *args
):
173 # Block lasts <= 30ms and has <= 28 edges
174 super().__init
__(pin
, 28, 30, callback
, *args
)
178 nedges
= self
.edge
# No. of edges detected
179 if not 14 <= nedges
<= 28:
180 raise RuntimeError(OVERRUN
if nedges
> 28 else BADSTART
)
181 # Regenerate bitstream
184 for x
in range(1, nedges
):
185 width
= ticks_diff(self
._times
[x
], self
._times
[x
- 1])
186 if not 500 < width
< 2000:
187 raise RuntimeError(BADBLOCK
)
188 for _
in range(1 if width
< 1334 else 2):
192 self
.verbose
and print(bin(bits
)) # Matches inverted scope waveform
193 # Decode Manchester code
197 m0
= 1 << x
# Mask MS two bits (always 01)
199 v
= 0 # 14 bit bitstream
205 raise RuntimeError(BADBLOCK
)
209 # Split into fields (val, addr, ctrl)
210 val
= (v
& 0x3f) |
(0x40 if ((v
>> 12) & 1) else 0)
211 addr
= (v
>> 6) & 0x1f
214 except RuntimeError as e
:
215 val
, addr
, ctrl
= e
.args
[0], 0, 0
216 self
.edge
= 0 # Set up for new data burst and run user callback
217 self
.callback(val
, addr
, ctrl
, *self
.args
)
220 # Even on Pyboard D the 444μs nominal pulses can be recorded as up to 705μs
221 # Scope shows 360-520 μs (-84μs +76μs relative to nominal)
222 # Header nominal 2666, 889, 444, 889, 444, 444, 444, 444 carrier ON at end
223 hdr
= ((1800, 4000), (593, 1333), (222, 750), (593, 1333), (222, 750), (222, 750), (222, 750), (222, 750))
224 def __init__(self
, pin
, callback
, *args
):
225 # Block lasts 23ms nominal and has <=44 edges
226 super().__init
__(pin
, 44, 30, callback
, *args
)
230 nedges
= self
.edge
# No. of edges detected
231 if not 22 <= nedges
<= 44:
232 raise RuntimeError(OVERRUN
if nedges
> 28 else BADSTART
)
233 for x
, lims
in enumerate(self
.hdr
):
234 width
= ticks_diff(self
._times
[x
+ 1], self
._times
[x
])
235 if not (lims
[0] < width
< lims
[1]):
236 self
.verbose
and print('Bad start', x
, width
, lims
)
237 raise RuntimeError(BADSTART
)
239 width
= ticks_diff(self
._times
[x
+ 1], self
._times
[x
])
240 # 2nd bit of last 0 is 444μs (0) or 1333μs (1)
241 if not 222 < width
< 1555:
242 self
.verbose
and print('Bad block 1 Width', width
, 'x', x
)
243 raise RuntimeError(BADBLOCK
)
247 bits
= 1 # Bits decoded
249 width
= ticks_diff(self
._times
[x
+ 1], self
._times
[x
])
250 if not 222 < width
< 1555:
251 self
.verbose
and print('Bad block 2 Width', width
, 'x', x
)
252 raise RuntimeError(BADBLOCK
)
256 x
+= 1 + int(short
) # If it's short, we know width of next
258 v |
= bit
# MSB of result
262 # -1 convert count to index, -1 because we look ahead
264 raise RuntimeError(BADBLOCK
)
265 # width is 444/889 nominal
266 width
= ticks_diff(self
._times
[x
+ 1], self
._times
[x
])
267 if not 222 < width
< 1111:
268 self
.verbose
and print('Bad block 3 Width', width
, 'x', x
)
269 raise RuntimeError(BADBLOCK
)
279 ss
= '20-bit format {:020b} x={} nedges={} bits={}'
280 print(ss
.format(v
, x
, nedges
, bits
))
283 addr
= (v
>> 8) & 0xff
285 except RuntimeError as e
:
286 val
, addr
, ctrl
= e
.args
[0], 0, 0
287 self
.edge
= 0 # Set up for new data burst and run user callback
288 self
.callback(val
, addr
, ctrl
, *self
.args
)