]>
vault307.fbx.one Git - micorpython_ir.git/blob - ir_tx/rp2_rmt.py
1 # rp2_rmt.py A RMT-like class for the RP2.
3 # Released under the MIT License (MIT). See LICENSE.
5 # Copyright (c) 2021 Peter Hinch
7 from machine
import Pin
, PWM
10 @rp2.asm_pio(set_init
=rp2
.PIO
.OUT_LOW
, autopull
=True, pull_thresh
=32)
13 out(x
, 32) # No of 1MHz ticks. Block if FIFO MT at end.
15 set(pins
, 1) # Set pin high
19 set(pins
, 0) # Set pin low
20 out(y
, 32) # Low time.
25 @rp2.asm_pio(autopull
=True, pull_thresh
=32)
28 out(x
, 32) # No of 1MHz ticks. Block if FIFO MT at end.
35 def duty_u16(self
, _
):
40 def __init__(self
, pin_pulse
=None, carrier
=None, sm_no
=0, sm_freq
=1_000_000):
45 pin_car
, freq
, duty
= carrier
46 self
.pwm
= PWM(pin_car
) # Set up PWM with carrier off.
49 self
.duty
= (int(0xffff * duty
// 100), 0)
51 self
.sm
= rp2
.StateMachine(sm_no
, irqtrain
, freq
=sm_freq
)
53 self
.sm
= rp2
.StateMachine(sm_no
, pulsetrain
, freq
=sm_freq
, set_base
=pin_pulse
)
54 self
.apt
= 0 # Array index
55 self
.arr
= None # Array
56 self
.ict
= None # Current IRQ count
57 self
.icm
= 0 # End IRQ count
58 self
.reps
= 0 # 0 == forever n == no. of reps
59 rp2
.PIO(0).irq(self
._cb
)
61 # IRQ callback. Because of FIFO IRQ's keep arriving after STOP.
63 self
.pwm
.duty_u16(self
.duty
[self
.ict
& 1])
65 if d
:= self
.arr
[self
.apt
]: # If data available feed FIFO
69 if r
:= self
.reps
!= 1: # All done if reps == 1
70 if r
: # 0 == run forever
72 self
.sm
.put(self
.arr
[0])
73 self
.apt
= 1 # Set pointer and count to state
74 self
.ict
= 1 # after 1st IRQ
76 # Arg is an array of times in μs terminated by 0.
77 def send(self
, ar
, reps
=1, check
=True):
80 ar
[-1] = 0 # Ensure at least one STOP
81 for x
, d
in enumerate(ar
): # Find 1st STOP
85 # Discard any trailing mark which would leave carrier on.
89 self
.icm
= x
# index of 1st STOP
91 n
= min(x
, 4) # Fill FIFO if there are enough data points.
92 self
.sm
.put(mv
[0 : n
])
93 self
.arr
= ar
# Initial conditions for ISR
94 self
.apt
= n
# Point to next data value
95 self
.ict
= 0 # IRQ count
100 return False # Just instantiated
101 return self
.ict
< self
.icm