@@ -401,41 +401,75 @@ def __init__(
401
401
self ._write_u8 (_RH_RF95_REG_0F_FIFO_RX_BASE_ADDR , 0x00 )
402
402
# Set mode idle
403
403
self .idle ()
404
- # Defaults set modem config to RadioHead compatible Bw125Cr45Sf128 mode.
405
- self .signal_bandwidth = 125000
406
- self .coding_rate = 5
407
- self .spreading_factor = 7
408
- # Default to disable CRC checking on incoming packets.
409
- self .enable_crc = False
410
- # Note no sync word is set for LoRa mode either!
411
- self ._write_u8 (_RH_RF95_REG_26_MODEM_CONFIG3 , 0x00 ) # Preamble lsb?
412
- # Set preamble length (default 8 bytes to match radiohead).
413
- self .preamble_length = preamble_length
414
404
# Set frequency
415
405
self .frequency_mhz = frequency
416
- # Set TX power to low defaut, 13 dB.
417
- self .tx_power = 13
406
+ # Set preamble length (default 8 bytes to match radiohead).
407
+ self .preamble_length = preamble_length
408
+ # set radio configuration parameters
409
+ self ._configure_radio ()
410
+ # initialize last RSSI reading
411
+ self .last_rssi = 0.0
412
+ """The RSSI of the last received packet. Stored when the packet was received.
413
+ This instataneous RSSI value may not be accurate once the
414
+ operating mode has been changed.
415
+ """
418
416
# initialize timeouts and delays delays
419
417
self .ack_wait = 0.5
418
+ """The delay time before attempting a retry after not receiving an ACK"""
420
419
self .receive_timeout = 0.5
420
+ """The amount of time to poll for a received packet.
421
+ If no packet is received, the returned packet will be None
422
+ """
421
423
self .xmit_timeout = 2.0
424
+ """The amount of time to wait for the HW to transmit the packet.
425
+ This is mainly used to prevent a hang due to a HW issue
426
+ """
422
427
self .ack_retries = 5
428
+ """The number of ACK retries before reporting a failure."""
423
429
self .ack_delay = None
430
+ """The delay time before attemting to send an ACK.
431
+ If ACKs are being missed try setting this to .1 or .2.
432
+ """
424
433
# initialize sequence number counter for reliabe datagram mode
425
434
self .sequence_number = 0
426
435
# create seen Ids list
427
436
self .seen_ids = bytearray (256 )
428
437
# initialize packet header
429
438
# node address - default is broadcast
430
439
self .node = _RH_BROADCAST_ADDRESS
440
+ """The default address of this Node. (0-255).
441
+ If not 255 (0xff) then only packets address to this node will be accepted.
442
+ First byte of the RadioHead header.
443
+ """
431
444
# destination address - default is broadcast
432
445
self .destination = _RH_BROADCAST_ADDRESS
446
+ """The default destination address for packet transmissions. (0-255).
447
+ If 255 (0xff) then any receiving node should accept the packet.
448
+ Second byte of the RadioHead header.
449
+ """
433
450
# ID - contains seq count for reliable datagram mode
434
451
self .identifier = 0
452
+ """Automatically set to the sequence number when send_with_ack() used.
453
+ Third byte of the RadioHead header.
454
+ """
435
455
# flags - identifies ack/reetry packet for reliable datagram mode
436
456
self .flags = 0
437
- # initialize last RSSI reading
438
- self .last_rssi = 0.0
457
+ """Upper 4 bits reserved for use by Reliable Datagram Mode.
458
+ Lower 4 bits may be used to pass information.
459
+ Fourth byte of the RadioHead header.
460
+ """
461
+
462
+ def _configure_radio (self ):
463
+ # Defaults set modem config to RadioHead compatible Bw125Cr45Sf128 mode.
464
+ self .signal_bandwidth = 125000
465
+ self .coding_rate = 5
466
+ self .spreading_factor = 7
467
+ # Default to disable CRC checking on incoming packets.
468
+ self .enable_crc = False
469
+ # Note no sync word is set for LoRa mode either!
470
+ self ._write_u8 (_RH_RF95_REG_26_MODEM_CONFIG3 , 0x00 ) # Preamble lsb?
471
+ # Set transmit power to 13 dBm, a safe value any module supports.
472
+ self .tx_power = 13
439
473
440
474
# pylint: disable=no-member
441
475
# Reconsider pylint: disable when this can be tested
@@ -683,41 +717,57 @@ def enable_crc(self, val):
683
717
self ._read_u8 (_RH_RF95_REG_1E_MODEM_CONFIG2 ) & 0xFB ,
684
718
)
685
719
686
- def send (self , data , keep_listening = False , tx_header = None ):
720
+ def send (
721
+ self ,
722
+ data ,
723
+ * ,
724
+ keep_listening = False ,
725
+ destination = None ,
726
+ node = None ,
727
+ identifier = None ,
728
+ flags = None
729
+ ):
687
730
"""Send a string of data using the transmitter.
688
731
You can only send 252 bytes at a time
689
732
(limited by chip's FIFO size and appended headers).
690
733
This appends a 4 byte header to be compatible with the RadioHead library.
691
- The tx_header defaults to using the initialized attributes:
734
+ The header defaults to using the initialized attributes:
692
735
(destination,node,identifier,flags)
693
- It may be overidden by specifying a 4-tuple of bytes containing (To,From,ID,Flags)
736
+ It may be temporarily overidden via the kwargs - destination,node,identifier,flags.
737
+ Values passed via kwargs do not alter the attribute settings.
694
738
The keep_listening argument should be set to True if you want to start listening
695
- automatically after the packet is sent. The default setting is False
739
+ automatically after the packet is sent. The default setting is False.
740
+
741
+ Returns: True if success or False if the send timed out.
696
742
"""
697
743
# Disable pylint warning to not use length as a check for zero.
698
744
# This is a puzzling warning as the below code is clearly the most
699
745
# efficient and proper way to ensure a precondition that the provided
700
746
# buffer be within an expected range of bounds. Disable this check.
701
747
# pylint: disable=len-as-condition
702
748
assert 0 < len (data ) <= 252
703
- if tx_header is not None :
704
- assert len (tx_header ) == 4 , "tx header must be 4-tuple (To,From,ID,Flags)"
705
749
# pylint: enable=len-as-condition
706
750
self .idle () # Stop receiving to clear FIFO and keep it clear.
707
751
# Fill the FIFO with a packet to send.
708
752
self ._write_u8 (_RH_RF95_REG_0D_FIFO_ADDR_PTR , 0x00 ) # FIFO starts at 0.
709
753
# Combine header and data to form payload
710
754
payload = bytearray (4 )
711
- if tx_header is None : # use attributes
755
+ if destination is None : # use attribute
712
756
payload [0 ] = self .destination
757
+ else : # use kwarg
758
+ payload [0 ] = destination
759
+ if node is None : # use attribute
713
760
payload [1 ] = self .node
761
+ else : # use kwarg
762
+ payload [1 ] = node
763
+ if identifier is None : # use attribute
714
764
payload [2 ] = self .identifier
765
+ else : # use kwarg
766
+ payload [2 ] = identifier
767
+ if flags is None : # use attribute
715
768
payload [3 ] = self .flags
716
- else : # use header passed as argument
717
- payload [0 ] = tx_header [0 ]
718
- payload [1 ] = tx_header [1 ]
719
- payload [2 ] = tx_header [2 ]
720
- payload [3 ] = tx_header [3 ]
769
+ else : # use kwarg
770
+ payload [3 ] = flags
721
771
payload = payload + data
722
772
# Write payload.
723
773
self ._write_from (_RH_RF95_REG_00_FIFO , payload )
@@ -740,8 +790,7 @@ def send(self, data, keep_listening=False, tx_header=None):
740
790
self .idle ()
741
791
# Clear interrupt.
742
792
self ._write_u8 (_RH_RF95_REG_12_IRQ_FLAGS , 0xFF )
743
- if timed_out :
744
- raise RuntimeError ("Timeout during packet send" )
793
+ return not timed_out
745
794
746
795
def send_with_ack (self , data ):
747
796
"""Reliabe Datagram mode:
@@ -782,7 +831,7 @@ def send_with_ack(self, data):
782
831
783
832
# pylint: disable=too-many-branches
784
833
def receive (
785
- self , keep_listening = True , with_header = False , with_ack = False , timeout = None
834
+ self , * , keep_listening = True , with_header = False , with_ack = False , timeout = None
786
835
):
787
836
"""Wait to receive a packet from the receiver. If a packet is found the payload bytes
788
837
are returned, otherwise None is returned (which indicates the timeout elapsed with no
@@ -858,12 +907,10 @@ def receive(
858
907
data = bytes ("!" , "UTF-8" )
859
908
self .send (
860
909
data ,
861
- tx_header = (
862
- packet [1 ],
863
- packet [0 ],
864
- packet [2 ],
865
- packet [3 ] | _RH_FLAGS_ACK ,
866
- ),
910
+ destination = packet [1 ],
911
+ node = packet [0 ],
912
+ identifier = packet [2 ],
913
+ flags = (packet [3 ] | _RH_FLAGS_ACK ),
867
914
)
868
915
# reject Retries if we have seen this idetifier from this source before
869
916
if (self .seen_ids [packet [1 ]] == packet [2 ]) and (
0 commit comments