]> vault307.fbx.one Git - micorpython_ir.git/blob - ir_tx/nec.py
Release 0.1 Various bugfixes and improvements.
[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 valid = (0xffff, 0xff, 0) # Max addr, data, toggle
15
16 def __init__(self, pin, freq=38000, verbose=False): # NEC specifies 38KHz
17 super().__init__(pin, freq, 68, 33, verbose) # Measured duty ratio 33%
18
19 def _bit(self, b):
20 self.append(_TBURST, _T_ONE if b else _TBURST)
21
22 def tx(self, addr, data, _): # Ignore toggle
23 self.append(9000, 4500)
24 if addr < 256: # Short address: append complement
25 addr |= ((addr ^ 0xff) << 8)
26 for _ in range(16):
27 self._bit(addr & 1)
28 addr >>= 1
29 data |= ((data ^ 0xff) << 8)
30 for _ in range(16):
31 self._bit(data & 1)
32 data >>= 1
33 self.append(_TBURST)
34
35 def repeat(self):
36 self.aptr = 0
37 self.append(9000, 2250, _TBURST)
38 self.trigger() # Initiate physical transmission.