]> vault307.fbx.one Git - micorpython_ir.git/blob - ir_rx/mce.py
Add MCE. Fix bug with RC5X.
[micorpython_ir.git] / ir_rx / mce.py
1 # mce.py Decoder 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 utime import ticks_us, ticks_diff
10 from ir_rx import IR_RX
11
12 class MCE(IR_RX):
13 init_cs = 4 # http://www.hifi-remote.com/johnsfine/DecodeIR.html#OrtekMCE says 3
14 def __init__(self, pin, callback, *args):
15 # Block lasts ~19ms and has <= 34 edges
16 super().__init__(pin, 34, 25, callback, *args)
17
18 def decode(self, _):
19 def check(v):
20 if self.init_cs == -1:
21 return True
22 csum = v >> 12
23 cs = self.init_cs
24 for _ in range(12):
25 if v & 1:
26 cs += 1
27 v >>= 1
28 return cs == csum
29
30 try:
31 t0 = ticks_diff(self._times[1], self._times[0]) # 2000μs mark
32 t1 = ticks_diff(self._times[2], self._times[1]) # 1000μs space
33 if not ((1800 < t0 < 2200) and (800 < t1 < 1200)):
34 raise RuntimeError(self.BADSTART)
35 nedges = self.edge # No. of edges detected
36 if not 14 <= nedges <= 34:
37 raise RuntimeError(self.OVERRUN if nedges > 28 else self.BADSTART)
38 # Manchester decode
39 mask = 1
40 bit = 1
41 v = 0
42 x = 2
43 for _ in range(16):
44 # -1 convert count to index, -1 because we look ahead
45 if x > nedges - 2:
46 raise RuntimeError(self.BADBLOCK)
47 # width is 500/1000 nominal
48 width = ticks_diff(self._times[x + 1], self._times[x])
49 if not 250 < width < 1350:
50 self.verbose and print('Bad block 3 Width', width, 'x', x)
51 raise RuntimeError(self.BADBLOCK)
52 short = int(width < 750)
53 bit ^= short ^ 1
54 v |= mask if bit else 0
55 mask <<= 1
56 x += 1 + short
57
58 self.verbose and print(bin(v))
59 print(bin(v)) # TEST
60 #if not check(v):
61 #raise RuntimeError(self.BADDATA)
62 val = (v >> 6) & 0x3f
63 addr = v & 0xf # Constant for all buttons on my remote
64 ctrl = (v >> 4) & 3
65
66 except RuntimeError as e:
67 val, addr, ctrl = e.args[0], 0, 0
68 # Set up for new data burst and run user callback/error function
69 self.do_callback(val, addr, ctrl)