Skip to content

Commit cdf4c5e

Browse files
committed
Add accesss to RAdioHead header
1 parent f9822d0 commit cdf4c5e

File tree

1 file changed

+44
-23
lines changed

1 file changed

+44
-23
lines changed

adafruit_rfm9x.py

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
import digitalio
3434
from micropython import const
3535

36-
import adafruit_bus_device.spi_device as spi_device
36+
import adafruit_bus_device.spi_device as spidev
3737

3838

3939
__version__ = "0.0.0-auto.0"
@@ -338,8 +338,8 @@ def __init__(self, spi, cs, reset, frequency, *, preamble_length=8,
338338
self.high_power = high_power
339339
# Device support SPI mode 0 (polarity & phase = 0) up to a max of 10mhz.
340340
# Set Default Baudrate to 5MHz to avoid problems
341-
self._device = spi_device.SPIDevice(spi, cs, baudrate=baudrate,
342-
polarity=0, phase=0)
341+
self._device = spidev.SPIDevice(spi, cs, baudrate=baudrate,
342+
polarity=0, phase=0)
343343
# Setup reset as a digital input (default state for reset line according
344344
# to the datasheet). This line is pulled low as an output quickly to
345345
# trigger a reset. Note that reset MUST be done like this and set as
@@ -533,27 +533,32 @@ def rssi(self):
533533
# Remember in LoRa mode the payload register changes function to RSSI!
534534
return self._read_u8(_RH_RF95_REG_1A_PKT_RSSI_VALUE) - 137
535535

536-
def send(self, data, timeout=2.):
537-
"""Send a string of data using the transmitter. You can only send 252
538-
bytes at a time (limited by chip's FIFO size and appended headers). Note
539-
this appends a 4 byte header to be compatible with the RadioHead library.
540-
The timeout is just to prevent a hang (arbitrarily set to 2 Seconds).
536+
def send(self, data, timeout=2.,
537+
tx_header=(_RH_BROADCAST_ADDRESS, _RH_BROADCAST_ADDRESS, 0, 0)):
538+
"""Send a string of data using the transmitter.
539+
You can only send 252 bytes at a time
540+
(limited by chip's FIFO size and appended headers).
541+
This appends a 4 byte header to be compatible with the RadioHead library.
542+
The tx_header defaults to using the Broadcast addresses. It may be overidden
543+
by specifying a 4-tuple of bytes containing (To,From,ID,Flags)
544+
The timeout is just to prevent a hang (arbitrarily set to 2 seconds)
541545
"""
542546
# Disable pylint warning to not use length as a check for zero.
543547
# This is a puzzling warning as the below code is clearly the most
544548
# efficient and proper way to ensure a precondition that the provided
545549
# buffer be within an expected range of bounds. Disable this check.
546550
# pylint: disable=len-as-condition
547551
assert 0 < len(data) <= 252
552+
assert len(tx_header) == 4, "tx header must be 4-tuple (To,From,ID,Flags)"
548553
# pylint: enable=len-as-condition
549554
self.idle() # Stop receiving to clear FIFO and keep it clear.
550555
# Fill the FIFO with a packet to send.
551556
self._write_u8(_RH_RF95_REG_0D_FIFO_ADDR_PTR, 0x00) # FIFO starts at 0.
552557
# Write header bytes.
553-
self._write_u8(_RH_RF95_REG_00_FIFO, _RH_BROADCAST_ADDRESS) # txHeaderTo
554-
self._write_u8(_RH_RF95_REG_00_FIFO, _RH_BROADCAST_ADDRESS) # txHeaderFrom
555-
self._write_u8(_RH_RF95_REG_00_FIFO, 0x00) # txHeaderId
556-
self._write_u8(_RH_RF95_REG_00_FIFO, 0x00) # txHeaderFlags
558+
self._write_u8(_RH_RF95_REG_00_FIFO, tx_header[0]) # Header: To
559+
self._write_u8(_RH_RF95_REG_00_FIFO, tx_header[1]) # Header: From
560+
self._write_u8(_RH_RF95_REG_00_FIFO, tx_header[2]) # Header: Id
561+
self._write_u8(_RH_RF95_REG_00_FIFO, tx_header[3]) # Header: Flags
557562
# Write payload.
558563
self._write_from(_RH_RF95_REG_00_FIFO, data)
559564
# Write payload and header length.
@@ -574,16 +579,29 @@ def send(self, data, timeout=2.):
574579
if timed_out:
575580
raise RuntimeError('Timeout during packet send')
576581

577-
def receive(self, timeout=0.5, keep_listening=True):
578-
"""Wait to receive a packet from the receiver. Will wait for up to
579-
timeout amount of seconds for a packet to be received and decoded. If
580-
a packet is found the payload bytes are returned, otherwise None is
581-
returned (which indicates the timeout elapsed with no reception). Note
582-
this assumes a 4-byte header is prepended to the data for compatibilty
583-
with the RadioHead library (the header is not validated nor returned).
584-
If keep_listening is True (the default) the chip will immediately enter
585-
listening mode after reception of a packet, otherwise it will fall back
586-
to idle mode and ignore any future reception.
582+
583+
584+
def receive(self, timeout=0.5, keep_listening=True, with_header=False,
585+
rx_filter=_RH_BROADCAST_ADDRESS):
586+
"""Wait to receive a packet from the receiver. Will wait for up to timeout_s amount of
587+
seconds for a packet to be received and decoded. If a packet is found the payload bytes
588+
are returned, otherwise None is returned (which indicates the timeout elapsed with no
589+
reception).
590+
If keep_listening is True (the default) the chip will immediately enter listening mode
591+
after reception of a packet, otherwise it will fall back to idle mode and ignore any
592+
future reception.
593+
A 4-byte header must be prepended to the data for compatibilty with the
594+
RadioHead library.
595+
The header consists of a 4 bytes (To,From,ID,Flags). The default setting will accept
596+
any incomming packet and strip the header before returning the packet to the caller.
597+
If with_header is True then the 4 byte header will be returned with the packet.
598+
The payload then begins at packet[4].
599+
rx_fliter may be set to reject any "non-broadcast" packets that do not contain the
600+
specfied "To" value in the header.
601+
if rx_filter is set to 0xff (_RH_BROADCAST_ADDRESS) or if the "To" field (packet[[0])
602+
is equal to 0xff then the packet will be accepted and returned to the caller.
603+
If rx_filter is not 0xff and packet[0] does not match rx_filter then
604+
the packet is ignored and None is returned.
587605
"""
588606
# Make sure we are listening for packets.
589607
self.listen()
@@ -612,7 +630,10 @@ def receive(self, timeout=0.5, keep_listening=True):
612630
packet = bytearray(length)
613631
# Read the packet.
614632
self._read_into(_RH_RF95_REG_00_FIFO, packet)
615-
# strip off the header
633+
if (rx_filter != _RH_BROADCAST_ADDRESS and packet[0] != _RH_BROADCAST_ADDRESS
634+
and packet[0] != rx_filter):
635+
packet = None
636+
if not with_header: # skip the header if not wanted
616637
packet = packet[4:]
617638
# Listen again if necessary and return the result packet.
618639
if keep_listening:

0 commit comments

Comments
 (0)