]> vault307.fbx.one Git - micorpython_ir.git/blob - ir_tx/mce.py
micropython_ir projects
[micorpython_ir.git] / ir_tx / mce.py
1 # mce.py Encoder for IR remote control using synchronous code
2 # Supports Microsoft MCE edition remote protocol.
3
4 # Author: Peter Hinch
5 # Copyright Peter Hinch 2020 Released under the MIT license
6
7 # WARNING: This is experimental and subject to change.
8
9 from micropython import const
10 from ir_tx import IR
11
12 _TBIT = const(500) # Time (μs) for pulse of carrier
13
14
15 class MCE(IR):
16 valid = (0xf, 0x3f, 3) # Max addr, data, toggle
17 init_cs = 4 # http://www.hifi-remote.com/johnsfine/DecodeIR.html#OrtekMCE says 3
18
19 def __init__(self, pin, freq=38000, verbose=False):
20 super().__init__(pin, freq, 34, 30, verbose)
21
22 def tx(self, addr, data, toggle):
23 def checksum(v):
24 cs = self.init_cs
25 for _ in range(12):
26 if v & 1:
27 cs += 1
28 v >>= 1
29 return cs
30
31 self.append(2000, 1000, _TBIT)
32 d = ((data & 0x3f) << 6) | (addr & 0xf) | ((toggle & 3) << 4)
33 d |= checksum(d) << 12
34 self.verbose and print(bin(d))
35
36 mask = 1
37 while mask < 0x10000:
38 bit = bool(d & mask)
39 if bit ^ self.carrier:
40 self.add(_TBIT)
41 self.append(_TBIT)
42 else:
43 self.append(_TBIT, _TBIT)
44 mask <<= 1