]>
vault307.fbx.one Git - micorpython_ir.git/blob - ir_rx/__init__.py
525a859353b43488f062b275efcf96e156819c01
1 # ir_rx __init__.py Decoder for IR remote control using synchronous code
2 # IR_RX abstract base class for IR receivers.
5 # Copyright Peter Hinch 2020 Released under the MIT license
7 from machine
import Timer
, Pin
8 from array
import array
9 from utime
import ticks_us
12 # from micropython import alloc_emergency_exception_buf
13 # alloc_emergency_exception_buf(100)
16 # On 1st edge start a block timer. While the timer is running, record the time
17 # of each edge. When the timer times out decode the data. Duration must exceed
18 # the worst case block transmission time, but be less than the interval between
19 # a block start and a repeat code start (~108ms depending on protocol)
33 def __init__(self
, pin
, nedges
, tblock
, callback
, *args
): # Optional args for callback
37 self
.callback
= callback
39 self
._errf
= lambda _
: None
42 self
._times
= array('i', (0 for _
in range(nedges
+ 1))) # +1 for overrun
43 pin
.irq(handler
= self
._cb
_pin
, trigger
= (Pin
.IRQ_FALLING | Pin
.IRQ_RISING
))
45 self
.tim
= Timer(-1) # Sofware timer
48 # Pin interrupt. Save time of each edge for later decode.
49 def _cb_pin(self
, line
):
51 # On overrun ignore pulses until software timer times out
52 if self
.edge
<= self
._nedges
: # Allow 1 extra pulse to record overrun
53 if not self
.edge
: # First edge received
54 self
.tim
.init(period
=self
._tblock
, mode
=Timer
.ONE_SHOT
, callback
=self
.cb
)
55 self
._times
[self
.edge
] = t
58 def do_callback(self
, cmd
, addr
, ext
, thresh
=0):
61 self
.callback(cmd
, addr
, ext
, *self
.args
)
65 def error_function(self
, func
):
69 self
._pin
.irq(handler
= None)