From 270d99eb783363d238482e44e0888724e8c36766 Mon Sep 17 00:00:00 2001 From: Kevin J Walters Date: Fri, 9 Apr 2021 18:24:07 +0100 Subject: [PATCH 1/2] Adding new functionality to transmit in GenericTransmit. transmit now takes some keyword args to control additional repeats, delays for those repeats and nbits to allow a number of bits less than those in data bytes (useful for Sony codes). debug option added to constructor to control previously commented out printing of durations. --- adafruit_irremote.py | 42 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/adafruit_irremote.py b/adafruit_irremote.py index 828df99..f4baa94 100644 --- a/adafruit_irremote.py +++ b/adafruit_irremote.py @@ -233,25 +233,46 @@ def read_pulses( class GenericTransmit: - """Generic infrared transmit class that handles encoding.""" + """Generic infrared transmit class that handles encoding. - def __init__(self, header, one, zero, trail): + :param int header: The length of header in microseconds + :param int one: The length of a one in microseconds + :param int zero: The length of a zero in microseconds + :param int trail: The length of the trail in microseconds, set to None to disable + :param bool debug: Enable debug output, default False + """ + + def __init__(self, header, one, zero, trail, *, debug=False): self.header = header self.one = one self.zero = zero self.trail = trail + self.debug = debug - def transmit(self, pulseout, data): + def transmit(self, pulseout, data, *, repeat=0, delay=0, nbits=None): """Transmit the ``data`` using the ``pulseout``. :param pulseio.PulseOut pulseout: PulseOut to transmit on :param bytearray data: Data to transmit + :param int repeat: Number of additional retransmissions of the data, default 0 + :param float delay: Delay between any retransmissions, default 0 + :param int nbits: Optional number of bits to send, + useful to send fewer bits than in the data bytes """ - durations = array.array("H", [0] * (2 + len(data) * 8 * 2 + 1)) + bits_to_send = len(data) * 8 + if nbits is not None and nbits < bits_to_send: + bits_to_send = nbits + + durations = array.array("H", [0] * (2 + + bits_to_send * 2 + + (0 if self.trail is None else 1))) + durations[0] = self.header[0] durations[1] = self.header[1] - durations[-1] = self.trail + if self.trail is not None: + durations[-1] = self.trail out = 2 + bit_count = 0 for byte_index, _ in enumerate(data): for i in range(7, -1, -1): if (data[byte_index] & 1 << i) > 0: @@ -261,6 +282,15 @@ def transmit(self, pulseout, data): durations[out] = self.zero[0] durations[out + 1] = self.zero[1] out += 2 + bit_count += 1 + if bit_count >= bits_to_send: + break + + if self.debug: + print(durations) - # print(durations) pulseout.send(durations) + for _ in range(repeat): + if delay: + time.sleep(delay) + pulseout.send(durations) From a4167918184974bcfc126e516ac96b910fc70cb4 Mon Sep 17 00:00:00 2001 From: Kevin J Walters Date: Fri, 9 Apr 2021 19:35:37 +0100 Subject: [PATCH 2/2] Formatting by black. --- adafruit_irremote.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/adafruit_irremote.py b/adafruit_irremote.py index f4baa94..7e58f6d 100644 --- a/adafruit_irremote.py +++ b/adafruit_irremote.py @@ -235,11 +235,11 @@ def read_pulses( class GenericTransmit: """Generic infrared transmit class that handles encoding. - :param int header: The length of header in microseconds - :param int one: The length of a one in microseconds - :param int zero: The length of a zero in microseconds - :param int trail: The length of the trail in microseconds, set to None to disable - :param bool debug: Enable debug output, default False + :param int header: The length of header in microseconds + :param int one: The length of a one in microseconds + :param int zero: The length of a zero in microseconds + :param int trail: The length of the trail in microseconds, set to None to disable + :param bool debug: Enable debug output, default False """ def __init__(self, header, one, zero, trail, *, debug=False): @@ -263,9 +263,9 @@ def transmit(self, pulseout, data, *, repeat=0, delay=0, nbits=None): if nbits is not None and nbits < bits_to_send: bits_to_send = nbits - durations = array.array("H", [0] * (2 + - bits_to_send * 2 + - (0 if self.trail is None else 1))) + durations = array.array( + "H", [0] * (2 + bits_to_send * 2 + (0 if self.trail is None else 1)) + ) durations[0] = self.header[0] durations[1] = self.header[1]