@@ -253,12 +253,9 @@ def __set__(self, obj, val):
253
253
crc_auto_clear_off = _RegisterBits (_REG_PACKET_CONFIG1 , offset = 3 , bits = 1 )
254
254
address_filter = _RegisterBits (_REG_PACKET_CONFIG1 , offset = 1 , bits = 2 )
255
255
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 )
258
256
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 )
261
257
258
+ # pylint: disable=too-many-statements
262
259
def __init__ (
263
260
self ,
264
261
spi ,
@@ -270,7 +267,7 @@ def __init__(
270
267
preamble_length = 4 ,
271
268
encryption_key = None ,
272
269
high_power = True ,
273
- baudrate = 5000000
270
+ baudrate = 2000000
274
271
):
275
272
self ._tx_power = 13
276
273
self .high_power = high_power
@@ -300,8 +297,24 @@ def __init__(
300
297
self .preamble_length = preamble_length # Set the preamble length.
301
298
self .frequency_mhz = frequency # Set frequency.
302
299
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
+
305
318
# initialize last RSSI reading
306
319
self .last_rssi = 0.0
307
320
"""The RSSI of the last received packet. Stored when the packet was received.
@@ -354,29 +367,7 @@ def __init__(
354
367
Fourth byte of the RadioHead header.
355
368
"""
356
369
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
380
371
381
372
# pylint: disable=no-member
382
373
# Reconsider this disable when it can be tested.
@@ -409,19 +400,6 @@ def _write_from(self, address, buf, length=None):
409
400
device .write (self ._BUFFER , end = 1 )
410
401
device .write (buf , end = length ) # send data
411
402
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
-
425
403
def _write_u8 (self , address , val ):
426
404
# Write a byte register to the chip. Specify the 7-bit address and the
427
405
# 8-bit value to write to that address.
@@ -722,6 +700,14 @@ def frequency_deviation(self, val):
722
700
self ._write_u8 (_REG_FDEV_MSB , fdev >> 8 )
723
701
self ._write_u8 (_REG_FDEV_LSB , fdev & 0xFF )
724
702
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
+
725
711
def send (
726
712
self ,
727
713
data ,
@@ -755,33 +741,34 @@ def send(
755
741
self .idle () # Stop receiving to clear FIFO and keep it clear.
756
742
# Fill the FIFO with a packet to send.
757
743
# Combine header and data to form payload
758
- payload = bytearray (4 )
744
+ payload = bytearray (5 )
745
+ payload [0 ] = 4 + len (data )
759
746
if destination is None : # use attribute
760
- payload [0 ] = self .destination
747
+ payload [1 ] = self .destination
761
748
else : # use kwarg
762
- payload [0 ] = destination
749
+ payload [1 ] = destination
763
750
if node is None : # use attribute
764
- payload [1 ] = self .node
751
+ payload [2 ] = self .node
765
752
else : # use kwarg
766
- payload [1 ] = node
753
+ payload [2 ] = node
767
754
if identifier is None : # use attribute
768
- payload [2 ] = self .identifier
755
+ payload [3 ] = self .identifier
769
756
else : # use kwarg
770
- payload [2 ] = identifier
757
+ payload [3 ] = identifier
771
758
if flags is None : # use attribute
772
- payload [3 ] = self .flags
759
+ payload [4 ] = self .flags
773
760
else : # use kwarg
774
- payload [3 ] = flags
761
+ payload [4 ] = flags
775
762
payload = payload + data
776
763
# Write payload to transmit fifo
777
- self ._write_fifo_from ( payload )
764
+ self ._write_from ( _REG_FIFO , payload )
778
765
# Turn on transmit mode to send out the packet.
779
766
self .transmit ()
780
767
# Wait for packet sent interrupt with explicit polling (not ideal but
781
768
# best that can be done right now without interrupts).
782
769
start = time .monotonic ()
783
770
timed_out = False
784
- while not timed_out and not self .packet_sent :
771
+ while not timed_out and not self .packet_sent () :
785
772
if (time .monotonic () - start ) >= self .xmit_timeout :
786
773
timed_out = True
787
774
# Listen again if requested.
@@ -858,7 +845,7 @@ def receive(
858
845
self .listen ()
859
846
start = time .monotonic ()
860
847
timed_out = False
861
- while not timed_out and not self .payload_ready :
848
+ while not timed_out and not self .payload_ready () :
862
849
if (time .monotonic () - start ) >= timeout :
863
850
timed_out = True
864
851
# Payload ready is set, a packet is in the FIFO.
@@ -874,7 +861,8 @@ def receive(
874
861
# RadioHead header and at least one byte of data --reject this packet and ignore it.
875
862
if fifo_length > 0 : # read and clear the FIFO if anything in it
876
863
packet = bytearray (fifo_length )
877
- self ._read_into (_REG_FIFO , packet )
864
+ self ._read_into (_REG_FIFO , packet , fifo_length )
865
+
878
866
if fifo_length < 5 :
879
867
packet = None
880
868
else :
@@ -893,10 +881,9 @@ def receive(
893
881
# delay before sending Ack to give receiver a chance to get ready
894
882
if self .ack_delay is not None :
895
883
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'!')
898
885
self .send (
899
- data ,
886
+ b"!" ,
900
887
destination = packet [1 ],
901
888
node = packet [0 ],
902
889
identifier = packet [2 ],
0 commit comments