@@ -233,25 +233,46 @@ def read_pulses(
233
233
234
234
235
235
class GenericTransmit :
236
- """Generic infrared transmit class that handles encoding."""
236
+ """Generic infrared transmit class that handles encoding.
237
237
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 ):
239
246
self .header = header
240
247
self .one = one
241
248
self .zero = zero
242
249
self .trail = trail
250
+ self .debug = debug
243
251
244
- def transmit (self , pulseout , data ):
252
+ def transmit (self , pulseout , data , * , repeat = 0 , delay = 0 , nbits = None ):
245
253
"""Transmit the ``data`` using the ``pulseout``.
246
254
247
255
:param pulseio.PulseOut pulseout: PulseOut to transmit on
248
256
: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
249
261
"""
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
+
251
270
durations [0 ] = self .header [0 ]
252
271
durations [1 ] = self .header [1 ]
253
- durations [- 1 ] = self .trail
272
+ if self .trail is not None :
273
+ durations [- 1 ] = self .trail
254
274
out = 2
275
+ bit_count = 0
255
276
for byte_index , _ in enumerate (data ):
256
277
for i in range (7 , - 1 , - 1 ):
257
278
if (data [byte_index ] & 1 << i ) > 0 :
@@ -261,6 +282,15 @@ def transmit(self, pulseout, data):
261
282
durations [out ] = self .zero [0 ]
262
283
durations [out + 1 ] = self .zero [1 ]
263
284
out += 2
285
+ bit_count += 1
286
+ if bit_count >= bits_to_send :
287
+ break
288
+
289
+ if self .debug :
290
+ print (durations )
264
291
265
- # print(durations)
266
292
pulseout .send (durations )
293
+ for _ in range (repeat ):
294
+ if delay :
295
+ time .sleep (delay )
296
+ pulseout .send (durations )
0 commit comments