]>
vault307.fbx.one Git - micorpython_ir.git/blob - ir_tx.py
1 # ir_tx.py Nonblocking IR blaster
2 # Runs on Pyboard D or Pyboard 1.x only (not Pyboard Lite)
4 # Released under the MIT License (MIT). See LICENSE.
6 # Copyright (c) 2020 Peter Hinch
8 from pyb
import Pin
, Timer
9 from time
import sleep_us
, sleep
10 from micropython
import const
11 from array
import array
14 # micropython.alloc_emergency_exception_buf(100)
17 _SPACE
= const(0) # Or 100. Depends on wiring: 0 assumes logic 0 turns IR off.
18 _STOP
= const(0) # End of data
23 _T_RC5
= const(889) # Time for pulse of carrier
28 # IR abstract base class. Array holds periods in μs between toggling 36/38KHz
29 # carrier on or off. Physical transmission occurs in an ISR context controlled
30 # by timer 2 and timer 5.
31 # Operation is in two phases: .transmit populates .arr with times in μs (via
32 # subclass), then initiates physical transmission.
35 def __init__(self
, pin
, freq
, asize
, duty
, verbose
):
36 tim
= Timer(2, freq
=freq
) # Timer 2/pin produces 36/38KHz carrier
37 self
._ch
= tim
.channel(1, Timer
.PWM
, pin
=pin
)
38 self
._ch
.pulse_width_percent(_SPACE
) # Turn off IR LED
40 self
._tim
= Timer(5) # Timer 5 controls carrier on/off times
42 self
.verbose
= verbose
43 self
.arr
= array('H', 0 for _
in range(asize
)) # on/off times (μs)
44 self
.carrier
= False # Notional carrier state while encoding biphase
45 self
.aptr
= 0 # Index into array
47 # Before populating array, zero pointer, set notional carrier state (off).
48 def transmit(self
, addr
, data
, toggle
=0): # NEC: toggle is unused
49 self
.aptr
= 0 # Inital conditions for tx: index into array
51 self
.tx(addr
, data
, toggle
)
53 self
.aptr
= 0 # Reset pointer
54 self
._cb
(self
._tim
) # Initiate physical transmission.
56 def _cb(self
, t
): # T5 callback, generate a carrier mark or space
61 self
._ch
.pulse_width_percent(_SPACE
) # Turn off IR LED.
63 self
._ch
.pulse_width_percent(_SPACE
if p
& 1 else self
._duty
)
64 self
._tim
.init(prescaler
=84, period
=v
, callback
=self
._tcb
)
67 def append(self
, *times
): # Append one or more time peiods to .arr
69 self
.arr
[self
.aptr
] = t
71 self
.carrier
= not self
.carrier
# Keep track of carrier state
72 self
.verbose
and print('append', t
, 'carrier', self
.carrier
)
74 def add(self
, t
): # Increase last time value
75 self
.verbose
and print('add', t
)
76 self
.arr
[self
.aptr
- 1] += t
# Carrier unaffected
81 def __init__(self
, pin
, freq
=38000, verbose
=False): # NEC specifies 38KHz
82 super().__init
__(pin
, freq
, 68, 50, verbose
)
85 self
.append(_TBURST
, _T_ONE
if b
else _TBURST
)
87 def tx(self
, addr
, data
, _
): # Ignore toggle
88 self
.append(9000, 4500)
89 if addr
< 256: # Short address: append complement
90 addr |
= ((addr ^
0xff) << 8)
94 data |
= ((data ^
0xff) << 8)
102 self
.append(9000, 2250, _TBURST
)
105 # Philips RC5 protocol
108 def __init__(self
, pin
, freq
=36000, verbose
=False):
109 super().__init
__(pin
, freq
, 28, 30, verbose
)
111 def tx(self
, addr
, data
, toggle
):
112 d
= (data
& 0x3f) |
((addr
& 0x1f) << 6) |
((data
& 0x40) << 6) |
((toggle
& 1) << 11)
113 self
.verbose
and print(bin(d
))
120 if bit ^ self
.carrier
:
124 self
.append(_T_RC5
, _T_RC5
)
127 # Philips RC6 mode 0 protocol
130 def __init__(self
, pin
, freq
=36000, verbose
=False):
131 super().__init
__(pin
, freq
, 44, 30, verbose
)
133 def tx(self
, addr
, data
, toggle
):
135 self
.append(2666, _T2_RC6
, _T_RC6
, _T2_RC6
, _T_RC6
, _T_RC6
, _T_RC6
, _T_RC6
, _T_RC6
)
136 # Append a single bit of twice duration
141 self
.append(_T2_RC6
, _T2_RC6
)
142 d
= (data
& 0xff) |
((addr
& 0xff) << 8)
144 self
.verbose
and print('toggle', toggle
, self
.carrier
, bool(d
& mask
))
147 if bit ^ self
.carrier
:
148 self
.append(_T_RC6
, _T_RC6
)