"""Plain-Python parts of the IR remote: no hardware, so they test anywhere. A code carries its protocol, in the same form both ways (what ``got`` prints, ``send`` takes): NEC bare (``20 DF 10 EF``), the rest by name (``samsung E0 E0 40 BF``, ``raw 2400 600 1200 ...``). """ PROTOCOLS = ("nec", "samsung", "raw") MAX_RAW = 199 # PulseIn keeps 200 # Sony's devices want 3 frames per press, and other protocols take repeats as a held # button, so one raw send is 3 frames: one short press. RAW_FRAMES = 3 RAW_PERIOD_S = 0.045 # frame to frame, as Sony's remotes do def protocol_of(pulses): """Tell NEC from Samsung by the header mark and space, in microseconds; anything else is raw. The two share bit timing: NEC's header mark is 9 ms; Samsung's is 4.5 ms, then a 4.5 ms space. >>> protocol_of([9050, 4480, 560]) 'nec' >>> protocol_of([4520, 4470, 560]) 'samsung' Sony's 2.4 ms header used to pass for Samsung's: >>> protocol_of([2390, 610, 1200]) 'raw' """ if pulses[0] > 6750: return "nec" if pulses[0] > 3500 and pulses[1] > 3500: return "samsung" return "raw" def raw_pulses(pulses): """A burst as a raw code sends it: to the nearest 10 us, ending on a mark. A raw code is the burst's mark and space lengths in microseconds, mark first. Sent back, it needs no protocol. >>> raw_pulses([2394, 606, 1203, 597, 598, 12]) [2390, 610, 1200, 600, 600] """ out = [max(10, int(round(p / 10)) * 10) for p in pulses[:MAX_RAW]] return out if len(out) % 2 else out[:-1] def parse_send(line): """Parse a ``send`` command into its protocol and the bytes (or, raw, pulses) to transmit. >>> parse_send("send 20 DF 10 EF") ('nec', [32, 223, 16, 239]) >>> parse_send("send samsung E0 E0 40 BF") ('samsung', [224, 224, 64, 191]) >>> parse_send("send raw 2400 600 1200") ('raw', [2400, 600, 1200]) >>> parse_send("send samsung") is None True >>> parse_send("hello") is None True A raw burst starts and ends on a mark, and each length fits PulseOut's 16 bits: >>> parse_send("send raw 2400 600") Traceback (most recent call last): ... ValueError: raw: an odd number of pulses, 1 to 199 >>> parse_send("send raw 70000") Traceback (most recent call last): ... ValueError: raw: each 1 to 65535 us """ words = line.split() if len(words) < 2 or words[0] != "send": return None protocol = "nec" if words[1] in PROTOCOLS: protocol, words = words[1], words[1:] if len(words) < 2: return None if protocol != "raw": return protocol, [int(w, 16) for w in words[1:]] pulses = [int(w) for w in words[1:]] if len(pulses) % 2 == 0 or len(pulses) > MAX_RAW: raise ValueError("raw: an odd number of pulses, 1 to %d" % MAX_RAW) if not all(0 < p < 65536 for p in pulses): raise ValueError("raw: each 1 to 65535 us") return protocol, pulses def hex_bytes(data): """Format bytes as the console shows them. >>> hex_bytes((32, 223, 16, 239)) '20 DF 10 EF' """ return " ".join("%02X" % b for b in data) def code_text(protocol, data): """A code as listen reports it and send takes it: NEC bare, others named. >>> code_text("nec", (32, 223, 16, 239)) '20 DF 10 EF' >>> code_text("samsung", (224, 224, 103, 152)) 'samsung E0 E0 67 98' >>> code_text("raw", [2400, 600, 1200]) 'raw 2400 600 1200' """ if protocol == "raw": return "raw " + " ".join(str(p) for p in data) return hex_bytes(data) if protocol == "nec" else protocol + " " + hex_bytes(data)