]> vault307.fbx.one Git - micorpython_ir.git/blob - ir_tx/nec.py
micropython_ir projects
[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-2022 Released under the MIT license
6
7 # With thanks to J.E. Tannenbaum for information re Samsung protocol
8 from micropython import const
9 from ir_tx import IR, STOP
10
11 _TBURST = const(563)
12 _T_ONE = const(1687)
13
14 class NEC(IR):
15 valid = (0xffff, 0xff, 0) # Max addr, data, toggle
16 samsung = False
17
18 def __init__(self, pin, freq=38000, verbose=False): # NEC specifies 38KHz also Samsung
19 super().__init__(pin, freq, 68, 33, verbose) # Measured duty ratio 33%
20
21 def _bit(self, b):
22 self.append(_TBURST, _T_ONE if b else _TBURST)
23
24 def tx(self, addr, data, _): # Ignore toggle
25 if self.samsung:
26 self.append(4500, 4500)
27 else:
28 self.append(9000, 4500)
29 if addr < 256: # Short address: append complement
30 if self.samsung:
31 addr |= addr << 8
32 else:
33 addr |= ((addr ^ 0xff) << 8)
34 for _ in range(16):
35 self._bit(addr & 1)
36 addr >>= 1
37 data |= ((data ^ 0xff) << 8)
38 for _ in range(16):
39 self._bit(data & 1)
40 data >>= 1
41 self.append(_TBURST)
42
43 def repeat(self):
44 self.aptr = 0
45 self.append(9000, 2250, _TBURST)
46 self.trigger() # Initiate physical transmission.