@@ -646,7 +646,7 @@ def enable_crc(self, val):
646
646
self ._read_u8 (_RH_RF95_REG_1E_MODEM_CONFIG2 ) & 0xfb
647
647
)
648
648
649
- def send (self , data , timeout = 2. ,
649
+ def send (self , data , timeout = 2. , keep_listening = False ,
650
650
tx_header = (_RH_BROADCAST_ADDRESS , _RH_BROADCAST_ADDRESS , 0 , 0 )):
651
651
"""Send a string of data using the transmitter.
652
652
You can only send 252 bytes at a time
@@ -655,11 +655,13 @@ def send(self, data, timeout=2.,
655
655
The tx_header defaults to using the Broadcast addresses. It may be overidden
656
656
by specifying a 4-tuple of bytes containing (To,From,ID,Flags)
657
657
The timeout is just to prevent a hang (arbitrarily set to 2 seconds)
658
+ The keep_listening argument should be set to True if you want to start listening
659
+ automatically after the packet is sent. The default setting is False.
658
660
"""
659
661
# Disable pylint warning to not use length as a check for zero.
660
662
# This is a puzzling warning as the below code is clearly the most
661
663
# efficient and proper way to ensure a precondition that the provided
662
- # buffer be within an expected range of bounds. Disable this check.
664
+ # buffer be within an expected range of bounds. Disable this check.
663
665
# pylint: disable=len-as-condition
664
666
assert 0 < len (data ) <= 252
665
667
assert len (tx_header ) == 4 , "tx header must be 4-tuple (To,From,ID,Flags)"
@@ -685,9 +687,13 @@ def send(self, data, timeout=2.,
685
687
while not timed_out and not self .tx_done :
686
688
if (time .monotonic () - start ) >= timeout :
687
689
timed_out = True
688
- # Go back to idle mode after transmit.
689
- self .idle ()
690
- # Clear interrupts.
690
+ # Listen again if necessary and return the result packet.
691
+ if keep_listening :
692
+ self .listen ()
693
+ else :
694
+ # Enter idle mode to stop receiving other packets.
695
+ self .idle ()
696
+ # Clear interrupt.
691
697
self ._write_u8 (_RH_RF95_REG_12_IRQ_FLAGS , 0xFF )
692
698
if timed_out :
693
699
raise RuntimeError ('Timeout during packet send' )
@@ -699,7 +705,7 @@ def receive(self, timeout=0.5, keep_listening=True, with_header=False,
699
705
"""Wait to receive a packet from the receiver. Will wait for up to timeout_s amount of
700
706
seconds for a packet to be received and decoded. If a packet is found the payload bytes
701
707
are returned, otherwise None is returned (which indicates the timeout elapsed with no
702
- reception).
708
+ reception). If timeout is None it is not used ( for use with interrupts)
703
709
If keep_listening is True (the default) the chip will immediately enter listening mode
704
710
after reception of a packet, otherwise it will fall back to idle mode and ignore any
705
711
future reception.
@@ -716,17 +722,18 @@ def receive(self, timeout=0.5, keep_listening=True, with_header=False,
716
722
If rx_filter is not 0xff and packet[0] does not match rx_filter then
717
723
the packet is ignored and None is returned.
718
724
"""
719
- # Make sure we are listening for packets.
720
- self .listen ()
721
- # Wait for the rx done interrupt. This is not ideal and will
722
- # surely miss or overflow the FIFO when packets aren't read fast
723
- # enough, however it's the best that can be done from Python without
724
- # interrupt supports.
725
- start = time .monotonic ()
726
725
timed_out = False
727
- while not timed_out and not self .rx_done :
728
- if (time .monotonic () - start ) >= timeout :
729
- timed_out = True
726
+ if timeout is not None :
727
+ # Make sure we are listening for packets.
728
+ self .listen ()
729
+ # Wait for the rx done interrupt. This is not ideal and will
730
+ # surely miss or overflow the FIFO when packets aren't read fast
731
+ # enough, however it's the best that can be done from Python without
732
+ # interrupt supports.
733
+ start = time .monotonic ()
734
+ while not timed_out and not self .rx_done :
735
+ if (time .monotonic () - start ) >= timeout :
736
+ timed_out = True
730
737
# Payload ready is set, a packet is in the FIFO.
731
738
packet = None
732
739
if not timed_out :
0 commit comments