]>
vault307.fbx.one Git - micorpython_ir.git/blob - ir_tx.py
97578407f391b9469929c168336c81db647033ca
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
30 # Subclass is responsible for populating .arr and initiating transmission.
31 # Operation is in two phases: .transmit populates .arr with times in μs, then
32 # calls .start to initiate physical transmission.
35 def __init__(self
, pin
, freq
, asize
, duty
):
36 tim
= Timer(2, freq
=freq
)
37 self
._ch
= tim
.channel(1, Timer
.PWM
, pin
=pin
)
38 self
._ch
.pulse_width_percent(_SPACE
)
40 self
.arr
= array('H', 0 for _
in range(asize
))
45 # Before populating array, zero pointer, set notional carrier state (off).
47 self
.aptr
= 0 # Index into array
51 self
.aptr
= 0 # Reset pointer and initiate TX.
59 self
._ch
.pulse_width_percent(_SPACE
) # Turn off IR LED.
61 self
._ch
.pulse_width_percent(_SPACE
if p
& 1 else self
.duty
)
62 self
._tim
.init(prescaler
=84, period
=v
, callback
=self
._tcb
)
65 def append(self
, *times
): # Append one or more time peiods to .arr
67 self
.arr
[self
.aptr
] = t
69 self
.carrier
= not self
.carrier
# Keep track of carrier state
70 print('append', t
, 'carrier', self
.carrier
)
72 def add(self
, t
): # Increase last time value
74 self
.arr
[self
.aptr
- 1] += t
# Carrier unaffected
79 def __init__(self
, pin
, freq
=38000): # NEC specifies 38KHz
80 super().__init
__(pin
, freq
, 68, 50)
83 self
.append(_TBURST
, _T_ONE
if b
else _TBURST
)
85 def transmit(self
, addr
, data
, _
=0): # Ignore toggle if passed
86 self
.pretrans() # Set initial conditions
87 self
.append(9000, 4500)
88 if addr
< 256: # Short address: append complement
89 addr |
= ((addr ^
0xff) << 8)
93 data |
= ((data ^
0xff) << 8)
97 self
.append(_TBURST
, _STOP
)
102 self
.append(9000, 2250, _TBURST
, _STOP
)
105 # Philips RC5 protocol
108 def __init__(self
, pin
, freq
=36000):
109 super().__init
__(pin
, freq
, 28, 30)
111 def transmit(self
, addr
, data
, toggle
):
112 self
.pretrans() # Set initial conditions
113 d
= (data
& 0x3f) |
((addr
& 0x1f) << 6) |
((data
& 0x40) << 6) |
((toggle
& 1) << 11)
121 if bit ^ self
.carrier
:
125 self
.append(_T_RC5
, _T_RC5
)
130 # Philips RC6 mode 0 protocol
133 def __init__(self
, pin
, freq
=36000):
134 super().__init
__(pin
, freq
, 44, 30)
136 def transmit(self
, addr
, data
, toggle
):
137 self
.pretrans() # Set initial conditions
139 self
.append(2666, _T2_RC6
, _T_RC6
, _T2_RC6
, _T_RC6
, _T_RC6
, _T_RC6
, _T_RC6
, _T_RC6
)
140 # Append a single bit of twice duration
145 self
.append(_T2_RC6
, _T2_RC6
)
146 d
= (data
& 0xff) |
((addr
& 0xff) << 8)
148 print('toggle', toggle
, self
.carrier
, bool(d
& mask
))
151 if bit ^ self
.carrier
:
152 self
.append(_T_RC6
, _T_RC6
)