Skip to content

Commit 9c74587

Browse files
committed
incorporate rfm69 updates
1 parent f9696de commit 9c74587

File tree

1 file changed

+82
-35
lines changed

1 file changed

+82
-35
lines changed

adafruit_rfm9x.py

Lines changed: 82 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -401,41 +401,75 @@ def __init__(
401401
self._write_u8(_RH_RF95_REG_0F_FIFO_RX_BASE_ADDR, 0x00)
402402
# Set mode idle
403403
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
414404
# Set frequency
415405
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+
"""
418416
# initialize timeouts and delays delays
419417
self.ack_wait = 0.5
418+
"""The delay time before attempting a retry after not receiving an ACK"""
420419
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+
"""
421423
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+
"""
422427
self.ack_retries = 5
428+
"""The number of ACK retries before reporting a failure."""
423429
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+
"""
424433
# initialize sequence number counter for reliabe datagram mode
425434
self.sequence_number = 0
426435
# create seen Ids list
427436
self.seen_ids = bytearray(256)
428437
# initialize packet header
429438
# node address - default is broadcast
430439
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+
"""
431444
# destination address - default is broadcast
432445
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+
"""
433450
# ID - contains seq count for reliable datagram mode
434451
self.identifier = 0
452+
"""Automatically set to the sequence number when send_with_ack() used.
453+
Third byte of the RadioHead header.
454+
"""
435455
# flags - identifies ack/reetry packet for reliable datagram mode
436456
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
439473

440474
# pylint: disable=no-member
441475
# Reconsider pylint: disable when this can be tested
@@ -683,41 +717,57 @@ def enable_crc(self, val):
683717
self._read_u8(_RH_RF95_REG_1E_MODEM_CONFIG2) & 0xFB,
684718
)
685719

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+
):
687730
"""Send a string of data using the transmitter.
688731
You can only send 252 bytes at a time
689732
(limited by chip's FIFO size and appended headers).
690733
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:
692735
(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.
694738
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.
696742
"""
697743
# Disable pylint warning to not use length as a check for zero.
698744
# This is a puzzling warning as the below code is clearly the most
699745
# efficient and proper way to ensure a precondition that the provided
700746
# buffer be within an expected range of bounds. Disable this check.
701747
# pylint: disable=len-as-condition
702748
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)"
705749
# pylint: enable=len-as-condition
706750
self.idle() # Stop receiving to clear FIFO and keep it clear.
707751
# Fill the FIFO with a packet to send.
708752
self._write_u8(_RH_RF95_REG_0D_FIFO_ADDR_PTR, 0x00) # FIFO starts at 0.
709753
# Combine header and data to form payload
710754
payload = bytearray(4)
711-
if tx_header is None: # use attributes
755+
if destination is None: # use attribute
712756
payload[0] = self.destination
757+
else: # use kwarg
758+
payload[0] = destination
759+
if node is None: # use attribute
713760
payload[1] = self.node
761+
else: # use kwarg
762+
payload[1] = node
763+
if identifier is None: # use attribute
714764
payload[2] = self.identifier
765+
else: # use kwarg
766+
payload[2] = identifier
767+
if flags is None: # use attribute
715768
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
721771
payload = payload + data
722772
# Write payload.
723773
self._write_from(_RH_RF95_REG_00_FIFO, payload)
@@ -740,8 +790,7 @@ def send(self, data, keep_listening=False, tx_header=None):
740790
self.idle()
741791
# Clear interrupt.
742792
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
745794

746795
def send_with_ack(self, data):
747796
"""Reliabe Datagram mode:
@@ -782,7 +831,7 @@ def send_with_ack(self, data):
782831

783832
# pylint: disable=too-many-branches
784833
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
786835
):
787836
"""Wait to receive a packet from the receiver. If a packet is found the payload bytes
788837
are returned, otherwise None is returned (which indicates the timeout elapsed with no
@@ -858,12 +907,10 @@ def receive(
858907
data = bytes("!", "UTF-8")
859908
self.send(
860909
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),
867914
)
868915
# reject Retries if we have seen this idetifier from this source before
869916
if (self.seen_ids[packet[1]] == packet[2]) and (

0 commit comments

Comments
 (0)