@@ -532,7 +532,7 @@ def rssi(self):
532
532
# Remember in LoRa mode the payload register changes function to RSSI!
533
533
return self ._read_u8 (_RH_RF95_REG_1A_PKT_RSSI_VALUE ) - 137
534
534
535
- def send (self , data ):
535
+ def send (self , data , timeout_s = 1. ):
536
536
"""Send a string of data using the transmitter. You can only send 252
537
537
bytes at a time (limited by chip's FIFO size and appended headers). Note
538
538
this appends a 4 byte header to be compatible with the RadioHead library.
@@ -560,12 +560,15 @@ def send(self, data):
560
560
self .transmit ()
561
561
# Wait for tx done interrupt with explicit polling (not ideal but
562
562
# best that can be done right now without interrupts).
563
- while not self .tx_done :
564
- pass
565
- # Clear interrupts.
566
- self ._write_u8 (_RH_RF95_REG_12_IRQ_FLAGS , 0xFF )
563
+ start = time .monotonic ()
564
+ timed_out = False
565
+ while not timed_out and not self .tx_done :
566
+ if (time .monotonic () - start ) >= timeout_s :
567
+ timed_out = True
567
568
# Go back to idle mode after transmit.
568
569
self .idle ()
570
+ # Clear interrupts.
571
+ self ._write_u8 (_RH_RF95_REG_12_IRQ_FLAGS , 0xFF )
569
572
570
573
def receive (self , timeout_s = 0.5 , keep_listening = True ):
571
574
"""Wait to receive a packet from the receiver. Will wait for up to
@@ -585,32 +588,35 @@ def receive(self, timeout_s=0.5, keep_listening=True):
585
588
# enough, however it's the best that can be done from Python without
586
589
# interrupt supports.
587
590
start = time .monotonic ()
588
- while not self .rx_done :
591
+ timed_out = False
592
+ while not timed_out and not self .rx_done :
589
593
if (time .monotonic () - start ) >= timeout_s :
590
- return None # Exceeded timeout.
591
- # Clear interrupt.
592
- self ._write_u8 (_RH_RF95_REG_12_IRQ_FLAGS , 0xFF )
594
+ timed_out = True
593
595
# Payload ready is set, a packet is in the FIFO.
594
596
packet = None
595
- # Enter idle mode to stop receiving other packets.
596
- self .idle ()
597
- # Grab the length of the received packet and check it has at least 5
598
- # bytes to indicate the 4 byte header and at least 1 byte of user data.
599
- length = self ._read_u8 (_RH_RF95_REG_13_RX_NB_BYTES )
600
- if length < 5 :
601
- packet = None
602
- else :
603
- # Have a good packet, grab it from the FIFO.
604
- # Reset the fifo read ptr to the beginning of the packet.
605
- current_addr = self ._read_u8 (_RH_RF95_REG_10_FIFO_RX_CURRENT_ADDR )
606
- self ._write_u8 (_RH_RF95_REG_0D_FIFO_ADDR_PTR , current_addr )
607
- # Read the first 4 bytes to grab the header.
608
- self ._read_into (_RH_RF95_REG_00_FIFO , self ._BUFFER , length = 4 )
609
- length -= 4
610
- # Next read the remaining data into a result packet buffer.
611
- packet = bytearray (length )
612
- self ._read_into (_RH_RF95_REG_00_FIFO , packet )
613
- # Listen again if necessary and return the result packet.
597
+ if not timed_out :
598
+ # Grab the length of the received packet and check it has at least 5
599
+ # bytes to indicate the 4 byte header and at least 1 byte of user data.
600
+ length = self ._read_u8 (_RH_RF95_REG_13_RX_NB_BYTES )
601
+ if length < 5 :
602
+ packet = None
603
+ else :
604
+ # Have a good packet, grab it from the FIFO.
605
+ # Reset the fifo read ptr to the beginning of the packet.
606
+ current_addr = self ._read_u8 (_RH_RF95_REG_10_FIFO_RX_CURRENT_ADDR )
607
+ self ._write_u8 (_RH_RF95_REG_0D_FIFO_ADDR_PTR , current_addr )
608
+ # Read the first 4 bytes to grab the header.
609
+ self ._read_into (_RH_RF95_REG_00_FIFO , self ._BUFFER , length = 4 )
610
+ length -= 4
611
+ # Next read the remaining data into a result packet buffer.
612
+ packet = bytearray (length )
613
+ self ._read_into (_RH_RF95_REG_00_FIFO , packet )
614
+ # Listen again if necessary and return the result packet.
614
615
if keep_listening :
615
616
self .listen ()
617
+ else :
618
+ # Enter idle mode to stop receiving other packets.
619
+ self .idle ()
620
+ # Clear interrupt.
621
+ self ._write_u8 (_RH_RF95_REG_12_IRQ_FLAGS , 0xFF )
616
622
return packet
0 commit comments