code.py
1 # SPDX-FileCopyrightText: 2019 Anne Barela for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 import board 6 import pulseio 7 import adafruit_irremote 8 9 IR_PIN = board.D2 # Pin connected to IR receiver. 10 11 # Expected pulse, pasted in from previous recording REPL session: 12 pulse = [9144, 4480, 602, 535, 600, 540, 595, 536, 599, 537, 600, 536, 13 596, 540, 595, 544, 591, 539, 596, 1668, 592, 1676, 593, 1667, 14 593, 1674, 596, 1670, 590, 1674, 595, 535, 590, 1673, 597, 541, 15 595, 536, 597, 538, 597, 538, 597, 1666, 594, 541, 594, 541, 594, 16 540, 595, 1668, 596, 1673, 592, 1668, 592, 1672, 601, 540, 592, 17 1669, 590, 1672, 598, 1667, 593] 18 19 print('IR listener') 20 # Fuzzy pulse comparison function: 21 def fuzzy_pulse_compare(pulse1, pulse2, fuzzyness=0.2): 22 if len(pulse1) != len(pulse2): 23 return False 24 for i in range(len(pulse1)): 25 threshold = int(pulse1[i] * fuzzyness) 26 if abs(pulse1[i] - pulse2[i]) > threshold: 27 return False 28 return True 29 30 # Create pulse input and IR decoder. 31 pulses = pulseio.PulseIn(IR_PIN, maxlen=200, idle_state=True) 32 decoder = adafruit_irremote.GenericDecode() 33 pulses.clear() 34 pulses.resume() 35 # Loop waiting to receive pulses. 36 while True: 37 # Wait for a pulse to be detected. 38 detected = decoder.read_pulses(pulses) 39 print('got a pulse...') 40 # Got a pulse, now compare. 41 if fuzzy_pulse_compare(pulse, detected): 42 print('Received correct remote control press!')