"""IR remote on a Pico: learn codes from a remote, and send them. Runs under CircuitPython (make install copies it to CIRCUITPY). Talks over the USB serial console (make console): - Point a remote at the receiver and press a button: it prints the code, e.g. ``got 20 DF 10 EF``. - Type ``send 20 DF 10 EF`` and Enter: the emitter sends that code (NEC timing). Samsung codes carry their name: ``got samsung E0 E0 40 BF``, ``send samsung E0 E0 40 BF``. - Any other remote (Sony's, ...) comes back raw, mark and space lengths in us: ``got raw 2400 600 1200 ...``; ``send raw 2400 600 1200 ...`` plays it back, three frames 45 ms apart, as a short press of the button does. The carrier is 38 kHz for all; Sony's 40 kHz receivers take it, a bit less keenly. Wiring, IR board (STEMMA JST PH) to Pico, as wiring.md lays it out: VIN -> 3V3(OUT) (pin 36) GND -> GND (pin 38) IR In (send) -> GP15 (pin 20) IR Out (receive) -> GP16 (pin 21) """ import array import time import board import pulseio import supervisor import adafruit_irremote from ir_commands import RAW_FRAMES, RAW_PERIOD_S, code_text, parse_send, protocol_of, raw_pulses SEND_PIN = board.GP15 RECEIVE_PIN = board.GP16 # NEC: 9 ms header mark, 4.5 ms space; 560 us marks; 1 = 1690 us space. NEC = adafruit_irremote.GenericTransmit( header=[9000, 4500], one=[560, 1690], zero=[560, 560], trail=560 ) # Samsung32: NEC's bits behind a 4.5 ms mark, 4.5 ms space header. SAMSUNG = adafruit_irremote.GenericTransmit( header=[4500, 4500], one=[560, 1690], zero=[560, 560], trail=560 ) SENDERS = {"nec": NEC, "samsung": SAMSUNG} def heard(message): """What to print for a burst: its code, raw pulses, or None (noise, NEC repeats).""" if isinstance(message, adafruit_irremote.NECRepeatIRMessage): return None if isinstance(message, adafruit_irremote.IRMessage): protocol = protocol_of(message.pulses) if protocol != "raw": return "got " + code_text(protocol, message.code) elif len(message.pulses) < 10: return "unparsed: " + message.reason + " " + str(len(message.pulses)) + " pulses" return "got " + code_text("raw", raw_pulses(message.pulses)) def send_raw(pulseout, pulses): durations = array.array("H", pulses) gap = max(RAW_PERIOD_S - sum(pulses) / 1e6, 0.01) for i in range(RAW_FRAMES): if i: time.sleep(gap) pulseout.send(durations) def main(): pulsein = pulseio.PulseIn(RECEIVE_PIN, maxlen=200, idle_state=True) pulseout = pulseio.PulseOut(SEND_PIN, frequency=38000, duty_cycle=2**15) decoder = adafruit_irremote.NonblockingGenericDecode(pulsein) print("ir-pico ready: press a remote button, or type: send 20 DF 10 EF") while True: for message in decoder.read(): line = heard(message) if line: print(line) if supervisor.runtime.serial_bytes_available: line = input() try: data = parse_send(line) except ValueError as err: print("bad code:", err) continue if data is None: print("say: send [samsung] XX XX ..., or send raw MARK SPACE MARK ...") continue protocol, data = data pulsein.pause() # don't hear our own echo if protocol == "raw": send_raw(pulseout, data) else: SENDERS[protocol].transmit(pulseout, data) pulsein.clear() pulsein.resume() print("sent", code_text(protocol, data)) main()