@@ -289,7 +289,7 @@ def __set__(self, obj, val):
289289 payload_ready = _RegisterBits (_REG_IRQ_FLAGS2 , offset = 2 )
290290
291291 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 ):
293293 self ._tx_power = 13
294294 self .high_power = high_power
295295 # 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',
340340 # Set the preamble length.
341341 self .preamble_length = preamble_length
342342 # Set frequency.
343- self .frequency = frequency
343+ self .frequency_mhz = frequency
344344 # Set encryption key.
345345 self .encryption_key = encryption_key
346346 # Set transmit power to 13 dBm, a safe value any module supports.
@@ -673,10 +673,12 @@ def frequency_deviation(self, val):
673673 self ._write_u8 (_REG_FDEV_MSB , fdev >> 8 )
674674 self ._write_u8 (_REG_FDEV_LSB , fdev & 0xFF )
675675
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)
680682 """
681683 # Disable pylint warning to not use length as a check for zero.
682684 # This is a puzzling warning as the below code is clearly the most
@@ -705,12 +707,17 @@ def send(self, data):
705707 self .transmit ()
706708 # Wait for packet sent interrupt with explicit polling (not ideal but
707709 # 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
710715 # Go back to idle mode after transmit.
711716 self .idle ()
717+ if timed_out :
718+ raise RuntimeError ('Timeout during packet send' )
712719
713- def receive (self , timeout_s = 0.5 , keep_listening = True ):
720+ def receive (self , timeout = 0.5 , keep_listening = True ):
714721 """Wait to receive a packet from the receiver. Will wait for up to timeout_s amount of
715722 seconds for a packet to be received and decoded. If a packet is found the payload bytes
716723 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):
726733 # enough, however it's the best that can be done from Python without
727734 # interrupt supports.
728735 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
732740 # Payload ready is set, a packet is in the FIFO.
733741 packet = None
734742 # Enter idle mode to stop receiving other packets.
735743 self .idle ()
744+ if timed_out :
745+ return None
736746 # Read the data from the FIFO.
737747 with self ._device as device :
738748 self ._BUFFER [0 ] = _REG_FIFO & 0x7F # Strip out top bit to set 0
0 commit comments