@@ -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.
@@ -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
@@ -707,10 +709,17 @@ def send(self, data):
707
709
# best that can be done right now without interrupts).
708
710
while not self .packet_sent :
709
711
pass
712
+ start = time .monotonic ()
713
+ timed_out = False
714
+ while not timed_out and not self .packet_sent :
715
+ if (time .monotonic () - start ) >= timeout :
716
+ timed_out = True
710
717
# Go back to idle mode after transmit.
711
718
self .idle ()
719
+ if timed_out :
720
+ raise RuntimeError ('Timeout during packet send' )
712
721
713
- def receive (self , timeout_s = 0.5 , keep_listening = True ):
722
+ def receive (self , timeout = 0.5 , keep_listening = True ):
714
723
"""Wait to receive a packet from the receiver. Will wait for up to timeout_s amount of
715
724
seconds for a packet to be received and decoded. If a packet is found the payload bytes
716
725
are returned, otherwise None is returned (which indicates the timeout elapsed with no
@@ -726,37 +735,39 @@ def receive(self, timeout_s=0.5, keep_listening=True):
726
735
# enough, however it's the best that can be done from Python without
727
736
# interrupt supports.
728
737
start = time .monotonic ()
729
- while not self .payload_ready :
730
- if (time .monotonic () - start ) >= timeout_s :
731
- return None # Exceeded timeout.
738
+ timed_out = False
739
+ while not timed_out and not self .payload_ready :
740
+ if (time .monotonic () - start ) >= timeout :
741
+ timed_out = True
732
742
# Payload ready is set, a packet is in the FIFO.
733
743
packet = None
734
744
# Enter idle mode to stop receiving other packets.
735
745
self .idle ()
736
- # Read the data from the FIFO.
737
- with self ._device as device :
738
- self ._BUFFER [0 ] = _REG_FIFO & 0x7F # Strip out top bit to set 0
739
- # value (read).
740
- device .write (self ._BUFFER , end = 1 )
741
- # Read the length of the FIFO.
742
- device .readinto (self ._BUFFER , end = 1 )
743
- fifo_length = self ._BUFFER [0 ]
744
- # Handle if the received packet is too small to include the 4 byte
745
- # RadioHead header--reject this packet and ignore it.
746
- if fifo_length < 4 :
747
- # Invalid packet, ignore it. However finish reading the FIFO
748
- # to clear the packet.
749
- device .readinto (self ._BUFFER , end = fifo_length )
750
- packet = None
751
- else :
752
- # Read the 4 bytes of the RadioHead header.
753
- device .readinto (self ._BUFFER , end = 4 )
754
- # Ignore validating any of the header bytes.
755
- # Now read the remaining packet payload as the result.
756
- fifo_length -= 4
757
- packet = bytearray (fifo_length )
758
- device .readinto (packet )
759
- # Listen again if necessary and return the result packet.
760
- if keep_listening :
761
- self .listen ()
746
+ if not timed_out :
747
+ # Read the data from the FIFO.
748
+ with self ._device as device :
749
+ self ._BUFFER [0 ] = _REG_FIFO & 0x7F # Strip out top bit to set 0
750
+ # value (read).
751
+ device .write (self ._BUFFER , end = 1 )
752
+ # Read the length of the FIFO.
753
+ device .readinto (self ._BUFFER , end = 1 )
754
+ fifo_length = self ._BUFFER [0 ]
755
+ # Handle if the received packet is too small to include the 4 byte
756
+ # RadioHead header--reject this packet and ignore it.
757
+ if fifo_length < 4 :
758
+ # Invalid packet, ignore it. However finish reading the FIFO
759
+ # to clear the packet.
760
+ device .readinto (self ._BUFFER , end = fifo_length )
761
+ packet = None
762
+ else :
763
+ # Read the 4 bytes of the RadioHead header.
764
+ device .readinto (self ._BUFFER , end = 4 )
765
+ # Ignore validating any of the header bytes.
766
+ # Now read the remaining packet payload as the result.
767
+ fifo_length -= 4
768
+ packet = bytearray (fifo_length )
769
+ device .readinto (packet )
770
+ # Listen again if necessary and return the result packet.
771
+ if keep_listening :
772
+ self .listen ()
762
773
return packet
0 commit comments