@@ -674,11 +674,14 @@ def frequency_deviation(self, val):
674674 self ._write_u8 (_REG_FDEV_MSB , fdev >> 8 )
675675 self ._write_u8 (_REG_FDEV_LSB , fdev & 0xFF )
676676
677- def send (self , data , timeout = 2. ):
677+ def send (self , data , timeout = 2. ,
678+ tx_header = (_RH_BROADCAST_ADDRESS , _RH_BROADCAST_ADDRESS , 0 , 0 )):
678679 """Send a string of data using the transmitter.
679680 You can only send 60 bytes at a time
680681 (limited by chip's FIFO size and appended headers).
681- Note this appends a 4 byte header to be compatible with the RadioHead library.
682+ This appends a 4 byte header to be compatible with the RadioHead library.
683+ The tx_header defaults to using the Broadcast addresses. It may be overidden
684+ by specifying a 4-tuple of bytes containing (To,From,ID,Flags)
682685 The timeout is just to prevent a hang (arbitrarily set to 2 seconds)
683686 """
684687 # Disable pylint warning to not use length as a check for zero.
@@ -687,6 +690,7 @@ def send(self, data, timeout=2.):
687690 # buffer be within an expected range of bounds. Disable this check.
688691 # pylint: disable=len-as-condition
689692 assert 0 < len (data ) <= 60
693+ assert len (tx_header ) == 4 , "tx header must be 4-tuple (To,From,ID,Flags)"
690694 # pylint: enable=len-as-condition
691695 self .idle () # Stop receiving to clear FIFO and keep it clear.
692696 # Fill the FIFO with a packet to send.
@@ -697,10 +701,10 @@ def send(self, data, timeout=2.):
697701 # Add 4 bytes of headers to match RadioHead library.
698702 # Just use the defaults for global broadcast to all receivers
699703 # for now.
700- self ._BUFFER [2 ] = _RH_BROADCAST_ADDRESS # txHeaderTo
701- self ._BUFFER [3 ] = _RH_BROADCAST_ADDRESS # txHeaderFrom
702- self ._BUFFER [4 ] = 0 # txHeaderId
703- self ._BUFFER [5 ] = 0 # txHeaderFlags
704+ self ._BUFFER [2 ] = tx_header [ 0 ] # Header: To
705+ self ._BUFFER [3 ] = tx_header [ 1 ] # Header: From
706+ self ._BUFFER [4 ] = tx_header [ 2 ] # Header: Id
707+ self ._BUFFER [5 ] = tx_header [ 3 ] # Header: Flags
704708 device .write (self ._BUFFER , end = 6 )
705709 # Now send the payload.
706710 device .write (data )
@@ -718,14 +722,27 @@ def send(self, data, timeout=2.):
718722 if timed_out :
719723 raise RuntimeError ('Timeout during packet send' )
720724
721- def receive (self , timeout = 0.5 , keep_listening = True ):
725+ def receive (self , timeout = 0.5 , keep_listening = True , with_header = False ,
726+ rx_filter = _RH_BROADCAST_ADDRESS ):
722727 """Wait to receive a packet from the receiver. Will wait for up to timeout_s amount of
723728 seconds for a packet to be received and decoded. If a packet is found the payload bytes
724729 are returned, otherwise None is returned (which indicates the timeout elapsed with no
725- reception). Note this assumes a 4-byte header is prepended to the data for compatibilty
726- with the RadioHead library (the header is not validated nor returned). If keep_listening
727- is True (the default) the chip will immediately enter listening mode after reception of
728- a packet, otherwise it will fall back to idle mode and ignore any future reception.
730+ reception).
731+ If keep_listening is True (the default) the chip will immediately enter listening mode
732+ after reception of a packet, otherwise it will fall back to idle mode and ignore any
733+ future reception.
734+ A 4-byte header must be prepended to the data for compatibilty with the
735+ RadioHead library.
736+ The header consists of a 4 bytes (To,From,ID,Flags). The default setting will accept
737+ any incomming packet and strip the header before returning the packet to the caller.
738+ If with_header is True then the 4 byte header will be returned with the packet.
739+ The payload then begins at packet[4].
740+ rx_fliter may be set to reject any "non-broadcast" packets that do not contain the
741+ specfied "To" value in the header.
742+ if rx_filter is set to 0xff (_RH_BROADCAST_ADDRESS) or if the "To" field (packet[[0])
743+ is equal to 0xff then the packet will be accepted and returned to the caller.
744+ If rx_filter is not 0xff and packet[0] does not match rx_filter then
745+ the packet is ignored and None is returned.
729746 """
730747 # Make sure we are listening for packets.
731748 self .listen ()
@@ -760,13 +777,14 @@ def receive(self, timeout=0.5, keep_listening=True):
760777 device .readinto (self ._BUFFER , end = fifo_length )
761778 packet = None
762779 else :
763- # Read the 4 bytes of the RadioHead header.
764- device .readinto (self ._BUFFER , end = 4 )
765- # Ignore validating any of the header bytes.
766- # Now read the remaining packet payload as the result.
767- fifo_length -= 4
768780 packet = bytearray (fifo_length )
769781 device .readinto (packet )
782+ if (rx_filter != _RH_BROADCAST_ADDRESS and packet [0 ] != _RH_BROADCAST_ADDRESS
783+ and packet [0 ] != rx_filter ):
784+ packet = None
785+ if not with_header : # skip the header if not wanted
786+ packet = packet [4 :]
787+
770788 # Listen again if necessary and return the result packet.
771789 if keep_listening :
772790 self .listen ()
0 commit comments