Facebook
From qrfwqef, 1 Year ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 180
  1. import RPi.GPIO as GPIO
  2. import time
  3.  
  4. IR_PIN = 17  # GPIO pin connected to the IR receiver
  5.  
  6. def capture_ir_signals():
  7.     print("Capturing IR signals. Press Ctrl+C to stop...")
  8.     try:
  9.         while True:
  10.             pulse_lengths = []
  11.             # Wait for the start of a pulse
  12.             while GPIO.input(IR_PIN) == GPIO.HIGH:
  13.                 pass
  14.             # Record the start time of the pulse
  15.             start_time = time.time()
  16.             # Wait for the end of the pulse
  17.             while GPIO.input(IR_PIN) == GPIO.LOW:
  18.                 pass
  19.             # Record the duration of the pulse
  20.             pulse_duration = time.time() - start_time
  21.             pulse_lengths.append(pulse_duration)
  22.             # Continue to record pulse lengths until the signal ends
  23.             while GPIO.input(IR_PIN) == GPIO.HIGH:
  24.                 start_time = time.time()
  25.                 while GPIO.input(IR_PIN) == GPIO.LOW:
  26.                     pass
  27.                 pulse_duration = time.time() - start_time
  28.                 pulse_lengths.append(pulse_duration)
  29.             # Decode the pulse lengths
  30.             decode_signal(pulse_lengths)
  31.     except KeyboardInterrupt:
  32.         print("Stopping IR signal capture.")
  33.     finally:
  34.         GPIO.cleanup()
  35.  
  36. def decode_signal(pulse_lengths):
  37.     # Example decoding logic (replace with your own)
  38.     # This is a very basic example, actual decoding may be more complex
  39.     print("Received signal:", pulse_lengths)
  40.  
  41. if __name__ == "__main__":
  42.     GPIO.setmode(GPIO.BCM)
  43.     GPIO.setup(IR_PIN, GPIO.IN)
  44.     capture_ir_signals()
  45.