@@ -253,12 +253,9 @@ def __set__(self, obj, val):
253253 crc_auto_clear_off = _RegisterBits (_REG_PACKET_CONFIG1 , offset = 3 , bits = 1 )
254254 address_filter = _RegisterBits (_REG_PACKET_CONFIG1 , offset = 1 , bits = 2 )
255255 mode_ready = _RegisterBits (_REG_IRQ_FLAGS1 , offset = 7 )
256- rx_ready = _RegisterBits (_REG_IRQ_FLAGS1 , offset = 6 )
257- tx_ready = _RegisterBits (_REG_IRQ_FLAGS1 , offset = 5 )
258256 dio_0_mapping = _RegisterBits (_REG_DIO_MAPPING1 , offset = 6 , bits = 2 )
259- packet_sent = _RegisterBits (_REG_IRQ_FLAGS2 , offset = 3 )
260- payload_ready = _RegisterBits (_REG_IRQ_FLAGS2 , offset = 2 )
261257
258+ # pylint: disable=too-many-statements
262259 def __init__ (
263260 self ,
264261 spi ,
@@ -270,7 +267,7 @@ def __init__(
270267 preamble_length = 4 ,
271268 encryption_key = None ,
272269 high_power = True ,
273- baudrate = 5000000
270+ baudrate = 2000000
274271 ):
275272 self ._tx_power = 13
276273 self .high_power = high_power
@@ -300,8 +297,24 @@ def __init__(
300297 self .preamble_length = preamble_length # Set the preamble length.
301298 self .frequency_mhz = frequency # Set frequency.
302299 self .encryption_key = encryption_key # Set encryption key.
303- # set radio configuration parameters
304- self ._configure_radio ()
300+ # Configure modulation for RadioHead library GFSK_Rb250Fd250 mode
301+ # by default. Users with advanced knowledge can manually reconfigure
302+ # for any other mode (consulting the datasheet is absolutely
303+ # necessary!).
304+ self .modulation_shaping = 0b01 # Gaussian filter, BT=1.0
305+ self .bitrate = 250000 # 250kbs
306+ self .frequency_deviation = 250000 # 250khz
307+ self .rx_bw_dcc_freq = 0b111 # RxBw register = 0xE0
308+ self .rx_bw_mantissa = 0b00
309+ self .rx_bw_exponent = 0b000
310+ self .afc_bw_dcc_freq = 0b111 # AfcBw register = 0xE0
311+ self .afc_bw_mantissa = 0b00
312+ self .afc_bw_exponent = 0b000
313+ self .packet_format = 1 # Variable length.
314+ self .dc_free = 0b10 # Whitening
315+ # Set transmit power to 13 dBm, a safe value any module supports.
316+ self .tx_power = 13
317+
305318 # initialize last RSSI reading
306319 self .last_rssi = 0.0
307320 """The RSSI of the last received packet. Stored when the packet was received.
@@ -354,29 +367,7 @@ def __init__(
354367 Fourth byte of the RadioHead header.
355368 """
356369
357- def _configure_radio (self ):
358- # Configure modulation for RadioHead library GFSK_Rb250Fd250 mode
359- # by default. Users with advanced knowledge can manually reconfigure
360- # for any other mode (consulting the datasheet is absolutely
361- # necessary!).
362- self .data_mode = 0b00 # Packet mode
363- self .modulation_type = 0b00 # FSK modulation
364- self .modulation_shaping = 0b01 # Gaussian filter, BT=1.0
365- self .bitrate = 250000 # 250kbs
366- self .frequency_deviation = 250000 # 250khz
367- self .rx_bw_dcc_freq = 0b111 # RxBw register = 0xE0
368- self .rx_bw_mantissa = 0b00
369- self .rx_bw_exponent = 0b000
370- self .afc_bw_dcc_freq = 0b111 # AfcBw register = 0xE0
371- self .afc_bw_mantissa = 0b00
372- self .afc_bw_exponent = 0b000
373- self .packet_format = 1 # Variable length.
374- self .dc_free = 0b10 # Whitening
375- self .crc_on = 1 # CRC enabled
376- self .crc_auto_clear = 0 # Clear FIFO on CRC fail
377- self .address_filtering = 0b00 # No address filtering
378- # Set transmit power to 13 dBm, a safe value any module supports.
379- self .tx_power = 13
370+ # pylint: enable=too-many-statements
380371
381372 # pylint: disable=no-member
382373 # Reconsider this disable when it can be tested.
@@ -409,19 +400,6 @@ def _write_from(self, address, buf, length=None):
409400 device .write (self ._BUFFER , end = 1 )
410401 device .write (buf , end = length ) # send data
411402
412- def _write_fifo_from (self , buf , length = None ):
413- # Write a number of bytes to the transmit FIFO and taken from the
414- # provided buffer. If no length is specified (the default) the entire
415- # buffer is written.
416- if length is None :
417- length = len (buf )
418- with self ._device as device :
419- self ._BUFFER [0 ] = (_REG_FIFO | 0x80 ) & 0xFF # Set top bit to 1 to
420- # indicate a write.
421- self ._BUFFER [1 ] = length & 0xFF # Set packt length
422- device .write (self ._BUFFER , end = 2 ) # send address and lenght)
423- device .write (buf , end = length ) # send data
424-
425403 def _write_u8 (self , address , val ):
426404 # Write a byte register to the chip. Specify the 7-bit address and the
427405 # 8-bit value to write to that address.
@@ -722,6 +700,14 @@ def frequency_deviation(self, val):
722700 self ._write_u8 (_REG_FDEV_MSB , fdev >> 8 )
723701 self ._write_u8 (_REG_FDEV_LSB , fdev & 0xFF )
724702
703+ def packet_sent (self ):
704+ """Transmit status"""
705+ return (self ._read_u8 (_REG_IRQ_FLAGS2 ) & 0x8 ) >> 3
706+
707+ def payload_ready (self ):
708+ """Receive status"""
709+ return (self ._read_u8 (_REG_IRQ_FLAGS2 ) & 0x4 ) >> 2
710+
725711 def send (
726712 self ,
727713 data ,
@@ -755,33 +741,34 @@ def send(
755741 self .idle () # Stop receiving to clear FIFO and keep it clear.
756742 # Fill the FIFO with a packet to send.
757743 # Combine header and data to form payload
758- payload = bytearray (4 )
744+ payload = bytearray (5 )
745+ payload [0 ] = 4 + len (data )
759746 if destination is None : # use attribute
760- payload [0 ] = self .destination
747+ payload [1 ] = self .destination
761748 else : # use kwarg
762- payload [0 ] = destination
749+ payload [1 ] = destination
763750 if node is None : # use attribute
764- payload [1 ] = self .node
751+ payload [2 ] = self .node
765752 else : # use kwarg
766- payload [1 ] = node
753+ payload [2 ] = node
767754 if identifier is None : # use attribute
768- payload [2 ] = self .identifier
755+ payload [3 ] = self .identifier
769756 else : # use kwarg
770- payload [2 ] = identifier
757+ payload [3 ] = identifier
771758 if flags is None : # use attribute
772- payload [3 ] = self .flags
759+ payload [4 ] = self .flags
773760 else : # use kwarg
774- payload [3 ] = flags
761+ payload [4 ] = flags
775762 payload = payload + data
776763 # Write payload to transmit fifo
777- self ._write_fifo_from ( payload )
764+ self ._write_from ( _REG_FIFO , payload )
778765 # Turn on transmit mode to send out the packet.
779766 self .transmit ()
780767 # Wait for packet sent interrupt with explicit polling (not ideal but
781768 # best that can be done right now without interrupts).
782769 start = time .monotonic ()
783770 timed_out = False
784- while not timed_out and not self .packet_sent :
771+ while not timed_out and not self .packet_sent () :
785772 if (time .monotonic () - start ) >= self .xmit_timeout :
786773 timed_out = True
787774 # Listen again if requested.
@@ -858,7 +845,7 @@ def receive(
858845 self .listen ()
859846 start = time .monotonic ()
860847 timed_out = False
861- while not timed_out and not self .payload_ready :
848+ while not timed_out and not self .payload_ready () :
862849 if (time .monotonic () - start ) >= timeout :
863850 timed_out = True
864851 # Payload ready is set, a packet is in the FIFO.
@@ -874,7 +861,8 @@ def receive(
874861 # RadioHead header and at least one byte of data --reject this packet and ignore it.
875862 if fifo_length > 0 : # read and clear the FIFO if anything in it
876863 packet = bytearray (fifo_length )
877- self ._read_into (_REG_FIFO , packet )
864+ self ._read_into (_REG_FIFO , packet , fifo_length )
865+
878866 if fifo_length < 5 :
879867 packet = None
880868 else :
@@ -893,10 +881,9 @@ def receive(
893881 # delay before sending Ack to give receiver a chance to get ready
894882 if self .ack_delay is not None :
895883 time .sleep (self .ack_delay )
896- # send ACK packet to sender
897- data = bytes ("!" , "UTF-8" )
884+ # send ACK packet to sender (data is b'!')
898885 self .send (
899- data ,
886+ b"!" ,
900887 destination = packet [1 ],
901888 node = packet [0 ],
902889 identifier = packet [2 ],
0 commit comments