@@ -289,7 +289,7 @@ def __set__(self, obj, val):
289
289
payload_ready = _RegisterBits (_REG_IRQ_FLAGS2 , offset = 2 )
290
290
291
291
def __init__ (self , spi , cs , reset , frequency , * , sync_word = b'\x2D \xD4 ' ,
292
- preamble_length = 4 , encryption_key = None , high_power = True , baudrate = 10000000 ):
292
+ preamble_length = 4 , encryption_key = None , high_power = True , baudrate = 5000000 ):
293
293
self ._tx_power = 13
294
294
self .high_power = high_power
295
295
# Device support SPI mode 0 (polarity & phase = 0) up to a max of 10mhz.
@@ -340,7 +340,7 @@ def __init__(self, spi, cs, reset, frequency, *, sync_word=b'\x2D\xD4',
340
340
# Set the preamble length.
341
341
self .preamble_length = preamble_length
342
342
# Set frequency.
343
- self .frequency = frequency
343
+ self .frequency_mhz = frequency
344
344
# Set encryption key.
345
345
self .encryption_key = encryption_key
346
346
# Set transmit power to 13 dBm, a safe value any module supports.
@@ -673,10 +673,12 @@ def frequency_deviation(self, val):
673
673
self ._write_u8 (_REG_FDEV_MSB , fdev >> 8 )
674
674
self ._write_u8 (_REG_FDEV_LSB , fdev & 0xFF )
675
675
676
- def send (self , data ):
677
- """Send a string of data using the transmitter. You can only send 60 bytes at a time
678
- (limited by chip's FIFO size and appended headers). Note this appends a 4 byte header to
679
- be compatible with the RadioHead library.
676
+ def send (self , data , timeout = 2. ):
677
+ """Send a string of data using the transmitter.
678
+ You can only send 60 bytes at a time
679
+ (limited by chip's FIFO size and appended headers).
680
+ Note this appends a 4 byte header to be compatible with the RadioHead library.
681
+ The timeout is just to prevent a hang (arbitrarily set to 2 seconds)
680
682
"""
681
683
# Disable pylint warning to not use length as a check for zero.
682
684
# This is a puzzling warning as the below code is clearly the most
@@ -705,12 +707,17 @@ def send(self, data):
705
707
self .transmit ()
706
708
# Wait for packet sent interrupt with explicit polling (not ideal but
707
709
# best that can be done right now without interrupts).
708
- while not self .packet_sent :
709
- pass
710
+ start = time .monotonic ()
711
+ timed_out = False
712
+ while not timed_out and not self .packet_sent :
713
+ if (time .monotonic () - start ) >= timeout :
714
+ timed_out = True
710
715
# Go back to idle mode after transmit.
711
716
self .idle ()
717
+ if timed_out :
718
+ raise RuntimeError ('Timeout during packet send' )
712
719
713
- def receive (self , timeout_s = 0.5 , keep_listening = True ):
720
+ def receive (self , timeout = 0.5 , keep_listening = True ):
714
721
"""Wait to receive a packet from the receiver. Will wait for up to timeout_s amount of
715
722
seconds for a packet to be received and decoded. If a packet is found the payload bytes
716
723
are returned, otherwise None is returned (which indicates the timeout elapsed with no
@@ -726,13 +733,16 @@ def receive(self, timeout_s=0.5, keep_listening=True):
726
733
# enough, however it's the best that can be done from Python without
727
734
# interrupt supports.
728
735
start = time .monotonic ()
729
- while not self .payload_ready :
730
- if (time .monotonic () - start ) >= timeout_s :
731
- return None # Exceeded timeout.
736
+ timed_out = False
737
+ while not timed_out and not self .payload_ready :
738
+ if (time .monotonic () - start ) >= timeout :
739
+ timed_out = True
732
740
# Payload ready is set, a packet is in the FIFO.
733
741
packet = None
734
742
# Enter idle mode to stop receiving other packets.
735
743
self .idle ()
744
+ if timed_out :
745
+ return None
736
746
# Read the data from the FIFO.
737
747
with self ._device as device :
738
748
self ._BUFFER [0 ] = _REG_FIFO & 0x7F # Strip out top bit to set 0
0 commit comments