]> vault307.fbx.one Git - micorpython_ir.git/blob - ir_rx/acquire.py
Release 0.1 Various bugfixes and improvements.
[micorpython_ir.git] / ir_rx / acquire.py
1 # acquire.py Acquire a pulse train from an IR remote
2 # Supports NEC protocol.
3 # For a remote using NEC see https://www.adafruit.com/products/389
4
5 # Author: Peter Hinch
6 # Copyright Peter Hinch 2020 Released under the MIT license
7
8 from machine import Pin, freq
9 from sys import platform
10
11 from utime import sleep_ms, ticks_us, ticks_diff
12 from ir_rx import IR_RX
13
14
15 class IR_GET(IR_RX):
16 def __init__(self, pin, nedges=100, twait=100, display=True):
17 self.display = display
18 super().__init__(pin, nedges, twait, lambda *_ : None)
19 self.data = None
20
21 def decode(self, _):
22 def near(v, target):
23 return target * 0.8 < v < target * 1.2
24 lb = self.edge - 1 # Possible length of burst
25 if lb < 3:
26 return # Noise
27 burst = []
28 for x in range(lb):
29 dt = ticks_diff(self._times[x + 1], self._times[x])
30 if x > 0 and dt > 10000: # Reached gap between repeats
31 break
32 burst.append(dt)
33 lb = len(burst) # Actual length
34 # Duration of pulse train 24892 for RC-5 22205 for RC-6
35 duration = ticks_diff(self._times[lb - 1], self._times[0])
36
37 if self.display:
38 for x, e in enumerate(burst):
39 print('{:03d} {:5d}'.format(x, e))
40 print()
41 # Attempt to determine protocol
42 ok = False # Protocol not yet found
43 if near(burst[0], 9000) and lb == 67:
44 print('NEC')
45 ok = True
46
47 if not ok and near(burst[0], 2400) and near(burst[1], 600): # Maybe Sony
48 try:
49 nbits = {25:12, 31:15, 41:20}[lb]
50 except KeyError:
51 pass
52 else:
53 ok = True
54 print('Sony {}bit'.format(nbits))
55
56 if not ok and near(burst[0], 889): # Maybe RC-5
57 if near(duration, 24892) and near(max(burst), 1778):
58 print('Philps RC-5')
59 ok = True
60
61 if not ok and near(burst[0], 2666) and near(burst[1], 889): # RC-6?
62 if near(duration, 22205) and near(burst[1], 889) and near(burst[2], 444):
63 print('Philips RC-6 mode 0')
64 ok = True
65
66 if not ok and near(burst[0], 2000) and near(burst[1], 1000):
67 if near(duration, 19000):
68 print('Microsoft MCE edition protocol.')
69 # Constant duration, variable burst length, presumably bi-phase
70 print('Protocol start {} {} Burst length {} duration {}'.format(burst[0], burst[1], lb, duration))
71 ok = True
72
73 if not ok and near(burst[0], 4500) and near(burst[1], 4500): # Samsung?
74 print('Unsupported protocol. Samsung?')
75 ok = True
76
77 if not ok and near(burst[0], 3500) and near(burst[1], 1680): # Panasonic?
78 print('Unsupported protocol. Panasonic?')
79 ok = True
80
81 if not ok:
82 print('Unknown protocol start {} {} Burst length {} duration {}'.format(burst[0], burst[1], lb, duration))
83
84 print()
85 self.data = burst
86 # Set up for new data burst. Run null callback
87 self.do_callback(0, 0, 0)
88
89 def acquire(self):
90 while self.data is None:
91 sleep_ms(5)
92 self.close()
93 return self.data
94
95 def test():
96 # Define pin according to platform
97 if platform == 'pyboard':
98 pin = Pin('X3', Pin.IN)
99 elif platform == 'esp8266':
100 freq(160000000)
101 pin = Pin(13, Pin.IN)
102 elif platform == 'esp32' or platform == 'esp32_LoBo':
103 pin = Pin(23, Pin.IN)
104 irg = IR_GET(pin)
105 print('Waiting for IR data...')
106 return irg.acquire()