Skip to content

Commit d8260f2

Browse files
authored
Merge pull request #46 from kevinjwalters/tx-var-bits
Enhance transmit in GenericTransmit to optionally allowing sending bits and repeating commands
2 parents 67f4e81 + a416791 commit d8260f2

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

adafruit_irremote.py

+36-6
Original file line numberDiff line numberDiff line change
@@ -233,25 +233,46 @@ def read_pulses(
233233

234234

235235
class GenericTransmit:
236-
"""Generic infrared transmit class that handles encoding."""
236+
"""Generic infrared transmit class that handles encoding.
237237
238-
def __init__(self, header, one, zero, trail):
238+
:param int header: The length of header in microseconds
239+
:param int one: The length of a one in microseconds
240+
:param int zero: The length of a zero in microseconds
241+
:param int trail: The length of the trail in microseconds, set to None to disable
242+
:param bool debug: Enable debug output, default False
243+
"""
244+
245+
def __init__(self, header, one, zero, trail, *, debug=False):
239246
self.header = header
240247
self.one = one
241248
self.zero = zero
242249
self.trail = trail
250+
self.debug = debug
243251

244-
def transmit(self, pulseout, data):
252+
def transmit(self, pulseout, data, *, repeat=0, delay=0, nbits=None):
245253
"""Transmit the ``data`` using the ``pulseout``.
246254
247255
:param pulseio.PulseOut pulseout: PulseOut to transmit on
248256
:param bytearray data: Data to transmit
257+
:param int repeat: Number of additional retransmissions of the data, default 0
258+
:param float delay: Delay between any retransmissions, default 0
259+
:param int nbits: Optional number of bits to send,
260+
useful to send fewer bits than in the data bytes
249261
"""
250-
durations = array.array("H", [0] * (2 + len(data) * 8 * 2 + 1))
262+
bits_to_send = len(data) * 8
263+
if nbits is not None and nbits < bits_to_send:
264+
bits_to_send = nbits
265+
266+
durations = array.array(
267+
"H", [0] * (2 + bits_to_send * 2 + (0 if self.trail is None else 1))
268+
)
269+
251270
durations[0] = self.header[0]
252271
durations[1] = self.header[1]
253-
durations[-1] = self.trail
272+
if self.trail is not None:
273+
durations[-1] = self.trail
254274
out = 2
275+
bit_count = 0
255276
for byte_index, _ in enumerate(data):
256277
for i in range(7, -1, -1):
257278
if (data[byte_index] & 1 << i) > 0:
@@ -261,6 +282,15 @@ def transmit(self, pulseout, data):
261282
durations[out] = self.zero[0]
262283
durations[out + 1] = self.zero[1]
263284
out += 2
285+
bit_count += 1
286+
if bit_count >= bits_to_send:
287+
break
288+
289+
if self.debug:
290+
print(durations)
264291

265-
# print(durations)
266292
pulseout.send(durations)
293+
for _ in range(repeat):
294+
if delay:
295+
time.sleep(delay)
296+
pulseout.send(durations)

0 commit comments

Comments
 (0)