]> vault307.fbx.one Git - micorpython_ir.git/blob - ir_rx/acquire.py
7b492e015b4c2dca02d584e5ffe11caf48950d4e
[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 nedges = self.edge
25 if nedges < 4:
26 return # Noise
27 burst = []
28 duration = ticks_diff(self._times[0], self._times[nedges]) # 24892 for RC-5 22205 for RC-6
29 for x in range(nedges - 1):
30 dt = ticks_diff(self._times[x + 1], self._times[x])
31 if x > 0 and dt > 10000: # Reached gap between repeats
32 break
33 burst.append(dt)
34
35 if self.display:
36 detected = False
37 for x, e in enumerate(burst):
38 print('{:03d} {:5d}'.format(x, e))
39 print()
40 if burst[0] > 5000:
41 print('NEC')
42 detected = True
43 elif burst[0] > 2000: # Sony or Philips RC-6
44 if burst[1] > 750: # Probably Philips
45 if min(burst) < 600:
46 print('Philips RC-6 mode 0')
47 detected = True
48 else:
49 lb = len(burst)
50 try:
51 nbits = {25:12, 31:15, 41:20}[len(burst)]
52 except IndexError:
53 pass
54 else:
55 detected = True
56 if detected:
57 print('Sony {}bit'.format(nbits))
58
59 elif burst[0] < 1200:
60 print('Philips RC-5')
61 detected = True
62 if not detected:
63 print('Unknown protocol')
64
65 print()
66 self.data = burst
67 # Set up for new data burst. Run null callback
68 self.do_callback(0, 0, 0)
69
70 def acquire(self):
71 while self.data is None:
72 sleep_ms(5)
73 self.close()
74 return self.data
75
76 def test():
77 # Define pin according to platform
78 if platform == 'pyboard':
79 pin = Pin('X3', Pin.IN)
80 elif platform == 'esp8266':
81 freq(160000000)
82 pin = Pin(13, Pin.IN)
83 elif platform == 'esp32' or platform == 'esp32_LoBo':
84 pin = Pin(23, Pin.IN)
85 irg = IR_GET(pin)
86 print('Waiting for IR data...')
87 irg.acquire()
88
89 # RC Python Calculated
90 # NEC 66 66 66
91 # Sony 12: 24 24 26 (2 hdr + 2*(7 data + 5 addr) RC issues another: detect 26ms gap
92 # Sony 15: 75 30
93 # Sony 20 n/a 40
94
95 # Yamaha NEC
96 # Pi/Vista MCE RC6 mode 0 Din't receive
97 # Panasonic TV recorder RC6 mode 0 Didn't receive
98 # Virgin RC-5 Receive OK
99 # Samsung TV RC6 mode 0 Didn't receive