]> vault307.fbx.one Git - micorpython_ir.git/blob - ir_tx/nec.py
10cc67bfe164b72bcb5822dbbc02af3aefa3caf9
[micorpython_ir.git] / ir_tx / nec.py
1 # nec.py Encoder for IR remote control using synchronous code
2 # NEC protocol.
3
4 # Author: Peter Hinch
5 # Copyright Peter Hinch 2020 Released under the MIT license
6
7 from micropython import const
8 from ir_tx import IR, STOP
9
10 _TBURST = const(563)
11 _T_ONE = const(1687)
12
13 class NEC(IR):
14
15 def __init__(self, pin, freq=38000, verbose=False): # NEC specifies 38KHz
16 super().__init__(pin, freq, 68, 33, verbose) # Measured duty ratio 33%
17
18 def _bit(self, b):
19 self.append(_TBURST, _T_ONE if b else _TBURST)
20
21 def tx(self, addr, data, _): # Ignore toggle
22 self.append(9000, 4500)
23 if addr < 256: # Short address: append complement
24 addr |= ((addr ^ 0xff) << 8)
25 for _ in range(16):
26 self._bit(addr & 1)
27 addr >>= 1
28 data |= ((data ^ 0xff) << 8)
29 for _ in range(16):
30 self._bit(data & 1)
31 data >>= 1
32 self.append(_TBURST)
33
34 def repeat(self):
35 self.aptr = 0
36 self.append(9000, 2250, _TBURST)
37 self.trigger() # Initiate physical transmission.