@@ -674,7 +674,7 @@ def frequency_deviation(self, val):
674674 self ._write_u8 (_REG_FDEV_MSB , fdev >> 8 )
675675 self ._write_u8 (_REG_FDEV_LSB , fdev & 0xFF )
676676
677- def send (self , data , timeout = 2. ,
677+ def send (self , data , timeout = 2. , keep_listening = False ,
678678 tx_header = (_RH_BROADCAST_ADDRESS , _RH_BROADCAST_ADDRESS , 0 , 0 )):
679679 """Send a string of data using the transmitter.
680680 You can only send 60 bytes at a time
@@ -683,6 +683,8 @@ def send(self, data, timeout=2.,
683683 The tx_header defaults to using the Broadcast addresses. It may be overidden
684684 by specifying a 4-tuple of bytes containing (To,From,ID,Flags)
685685 The timeout is just to prevent a hang (arbitrarily set to 2 seconds)
686+ The keep_listening argument should be set to True if you want to start listening
687+ automatically after the packet is sent. The default setting is False.
686688 """
687689 # Disable pylint warning to not use length as a check for zero.
688690 # This is a puzzling warning as the below code is clearly the most
@@ -717,8 +719,12 @@ def send(self, data, timeout=2.,
717719 while not timed_out and not self .packet_sent :
718720 if (time .monotonic () - start ) >= timeout :
719721 timed_out = True
720- # Go back to idle mode after transmit.
721- self .idle ()
722+ # Listen again if necessary and return the result packet.
723+ if keep_listening :
724+ self .listen ()
725+ else :
726+ # Enter idle mode to stop receiving other packets.
727+ self .idle ()
722728 if timed_out :
723729 raise RuntimeError ('Timeout during packet send' )
724730
@@ -727,7 +733,7 @@ def receive(self, timeout=0.5, keep_listening=True, with_header=False,
727733 """Wait to receive a packet from the receiver. Will wait for up to timeout_s amount of
728734 seconds for a packet to be received and decoded. If a packet is found the payload bytes
729735 are returned, otherwise None is returned (which indicates the timeout elapsed with no
730- reception).
736+ reception). If timeout is None then it is not used ( for use with interrupts)
731737 If keep_listening is True (the default) the chip will immediately enter listening mode
732738 after reception of a packet, otherwise it will fall back to idle mode and ignore any
733739 future reception.
@@ -744,17 +750,19 @@ def receive(self, timeout=0.5, keep_listening=True, with_header=False,
744750 If rx_filter is not 0xff and packet[0] does not match rx_filter then
745751 the packet is ignored and None is returned.
746752 """
747- # Make sure we are listening for packets.
748- self .listen ()
749- # Wait for the payload_ready interrupt. This is not ideal and will
750- # surely miss or overflow the FIFO when packets aren't read fast
751- # enough, however it's the best that can be done from Python without
752- # interrupt supports.
753- start = time .monotonic ()
754753 timed_out = False
755- while not timed_out and not self .payload_ready :
756- if (time .monotonic () - start ) >= timeout :
757- timed_out = True
754+ if timeout is not None :
755+ # Make sure we are listening for packets.
756+ self .listen ()
757+ # Wait for the payload_ready interrupt. This is not ideal and will
758+ # surely miss or overflow the FIFO when packets aren't read fast
759+ # enough, however it's the best that can be done from Python without
760+ # interrupt supports.
761+ start = time .monotonic ()
762+ timed_out = False
763+ while not timed_out and not self .payload_ready :
764+ if (time .monotonic () - start ) >= timeout :
765+ timed_out = True
758766 # Payload ready is set, a packet is in the FIFO.
759767 packet = None
760768 # Enter idle mode to stop receiving other packets.
0 commit comments