80
80
81
81
# pylint: disable=bad-whitespace
82
82
# Internal constants:
83
- _REG_FIFO = const (0x00 )
84
- _REG_OP_MODE = const (0x01 )
85
- _REG_DATA_MOD = const (0x02 )
86
- _REG_BITRATE_MSB = const (0x03 )
87
- _REG_BITRATE_LSB = const (0x04 )
88
- _REG_FDEV_MSB = const (0x05 )
89
- _REG_FDEV_LSB = const (0x06 )
90
- _REG_FRF_MSB = const (0x07 )
91
- _REG_FRF_MID = const (0x08 )
92
- _REG_FRF_LSB = const (0x09 )
93
- _REG_VERSION = const (0x10 )
94
- _REG_PA_LEVEL = const (0x11 )
95
- _REG_RX_BW = const (0x19 )
96
- _REG_AFC_BW = const (0x1A )
97
- _REG_RSSI_VALUE = const (0x24 )
98
- _REG_DIO_MAPPING1 = const (0x25 )
99
- _REG_IRQ_FLAGS1 = const (0x27 )
100
- _REG_IRQ_FLAGS2 = const (0x28 )
101
- _REG_PREAMBLE_MSB = const (0x2C )
102
- _REG_PREAMBLE_LSB = const (0x2D )
103
- _REG_SYNC_CONFIG = const (0x2E )
104
- _REG_SYNC_VALUE1 = const (0x2F )
105
- _REG_PACKET_CONFIG1 = const (0x37 )
106
- _REG_FIFO_THRESH = const (0x3C )
107
- _REG_PACKET_CONFIG2 = const (0x3D )
108
- _REG_AES_KEY1 = const (0x3E )
109
- _REG_TEMP1 = const (0x4E )
110
- _REG_TEMP2 = const (0x4F )
111
- _REG_TEST_PA1 = const (0x5A )
112
- _REG_TEST_PA2 = const (0x5C )
113
- _REG_TEST_DAGC = const (0x6F )
114
-
115
- _TEST_PA1_NORMAL = const (0x55 )
116
- _TEST_PA1_BOOST = const (0x5D )
117
- _TEST_PA2_NORMAL = const (0x70 )
118
- _TEST_PA2_BOOST = const (0x7C )
83
+ _REG_FIFO = const (0x00 )
84
+ _REG_OP_MODE = const (0x01 )
85
+ _REG_DATA_MOD = const (0x02 )
86
+ _REG_BITRATE_MSB = const (0x03 )
87
+ _REG_BITRATE_LSB = const (0x04 )
88
+ _REG_FDEV_MSB = const (0x05 )
89
+ _REG_FDEV_LSB = const (0x06 )
90
+ _REG_FRF_MSB = const (0x07 )
91
+ _REG_FRF_MID = const (0x08 )
92
+ _REG_FRF_LSB = const (0x09 )
93
+ _REG_VERSION = const (0x10 )
94
+ _REG_PA_LEVEL = const (0x11 )
95
+ _REG_RX_BW = const (0x19 )
96
+ _REG_AFC_BW = const (0x1A )
97
+ _REG_RSSI_VALUE = const (0x24 )
98
+ _REG_DIO_MAPPING1 = const (0x25 )
99
+ _REG_IRQ_FLAGS1 = const (0x27 )
100
+ _REG_IRQ_FLAGS2 = const (0x28 )
101
+ _REG_PREAMBLE_MSB = const (0x2C )
102
+ _REG_PREAMBLE_LSB = const (0x2D )
103
+ _REG_SYNC_CONFIG = const (0x2E )
104
+ _REG_SYNC_VALUE1 = const (0x2F )
105
+ _REG_PACKET_CONFIG1 = const (0x37 )
106
+ _REG_FIFO_THRESH = const (0x3C )
107
+ _REG_PACKET_CONFIG2 = const (0x3D )
108
+ _REG_AES_KEY1 = const (0x3E )
109
+ _REG_TEMP1 = const (0x4E )
110
+ _REG_TEMP2 = const (0x4F )
111
+ _REG_TEST_PA1 = const (0x5A )
112
+ _REG_TEST_PA2 = const (0x5C )
113
+ _REG_TEST_DAGC = const (0x6F )
114
+
115
+ _TEST_PA1_NORMAL = const (0x55 )
116
+ _TEST_PA1_BOOST = const (0x5D )
117
+ _TEST_PA2_NORMAL = const (0x70 )
118
+ _TEST_PA2_BOOST = const (0x7C )
119
119
120
120
# The crystal oscillator frequency and frequency synthesizer step size.
121
121
# See the datasheet for details of this calculation.
122
- _FXOSC = 32000000.0
123
- _FSTEP = _FXOSC / 524288
122
+ _FXOSC = 32000000.0
123
+ _FSTEP = _FXOSC / 524288
124
124
125
125
# RadioHead specific compatibility constants.
126
126
_RH_BROADCAST_ADDRESS = const (0xFF )
127
127
128
128
# User facing constants:
129
- SLEEP_MODE = 0b000
129
+ SLEEP_MODE = 0b000
130
130
STANDBY_MODE = 0b001
131
- FS_MODE = 0b010
132
- TX_MODE = 0b011
133
- RX_MODE = 0b100
131
+ FS_MODE = 0b010
132
+ TX_MODE = 0b011
133
+ RX_MODE = 0b100
134
134
# pylint: enable=bad-whitespace
135
135
136
136
# Disable the silly too many instance members warning. Pylint has no knowledge
139
139
# the warning to work around the error.
140
140
# pylint: disable=too-many-instance-attributes
141
141
142
+
142
143
class RFM69 :
143
144
"""Interface to a RFM69 series packet radio. Allows simple sending and
144
145
receiving of wireless data at supported frequencies of the radio
@@ -288,13 +289,23 @@ def __set__(self, obj, val):
288
289
289
290
payload_ready = _RegisterBits (_REG_IRQ_FLAGS2 , offset = 2 )
290
291
291
- def __init__ (self , spi , cs , reset , frequency , * , sync_word = b'\x2D \xD4 ' ,
292
- preamble_length = 4 , encryption_key = None , high_power = True , baudrate = 5000000 ):
292
+ def __init__ (
293
+ self ,
294
+ spi ,
295
+ cs ,
296
+ reset ,
297
+ frequency ,
298
+ * ,
299
+ sync_word = b"\x2D \xD4 " ,
300
+ preamble_length = 4 ,
301
+ encryption_key = None ,
302
+ high_power = True ,
303
+ baudrate = 5000000
304
+ ):
293
305
self ._tx_power = 13
294
306
self .high_power = high_power
295
307
# Device support SPI mode 0 (polarity & phase = 0) up to a max of 10mhz.
296
- self ._device = spidev .SPIDevice (spi , cs , baudrate = baudrate ,
297
- polarity = 0 , phase = 0 )
308
+ self ._device = spidev .SPIDevice (spi , cs , baudrate = baudrate , polarity = 0 , phase = 0 )
298
309
# Setup reset as a digital output that's low.
299
310
self ._reset = reset
300
311
self ._reset .switch_to_output (value = False )
@@ -303,7 +314,9 @@ def __init__(self, spi, cs, reset, frequency, *, sync_word=b'\x2D\xD4',
303
314
# Check the version of the chip.
304
315
version = self ._read_u8 (_REG_VERSION )
305
316
if version != 0x24 :
306
- raise RuntimeError ('Failed to find RFM69 with expected version, check wiring!' )
317
+ raise RuntimeError (
318
+ "Failed to find RFM69 with expected version, check wiring!"
319
+ )
307
320
# Enter idle state.
308
321
self .idle ()
309
322
# Setup the chip in a similar way to the RadioHead RFM69 library.
@@ -321,22 +334,22 @@ def __init__(self, spi, cs, reset, frequency, *, sync_word=b'\x2D\xD4',
321
334
# by default. Users with advanced knowledge can manually reconfigure
322
335
# for any other mode (consulting the datasheet is absolutely
323
336
# necessary!).
324
- self .data_mode = 0b00 # Packet mode
325
- self .modulation_type = 0b00 # FSK modulation
326
- self .modulation_shaping = 0b01 # Gaussian filter, BT=1.0
327
- self .bitrate = 250000 # 250kbs
337
+ self .data_mode = 0b00 # Packet mode
338
+ self .modulation_type = 0b00 # FSK modulation
339
+ self .modulation_shaping = 0b01 # Gaussian filter, BT=1.0
340
+ self .bitrate = 250000 # 250kbs
328
341
self .frequency_deviation = 250000 # 250khz
329
- self .rx_bw_dcc_freq = 0b111 # RxBw register = 0xE0
342
+ self .rx_bw_dcc_freq = 0b111 # RxBw register = 0xE0
330
343
self .rx_bw_mantissa = 0b00
331
344
self .rx_bw_exponent = 0b000
332
- self .afc_bw_dcc_freq = 0b111 # AfcBw register = 0xE0
345
+ self .afc_bw_dcc_freq = 0b111 # AfcBw register = 0xE0
333
346
self .afc_bw_mantissa = 0b00
334
347
self .afc_bw_exponent = 0b000
335
- self .packet_format = 1 # Variable length.
336
- self .dc_free = 0b10 # Whitening
337
- self .crc_on = 1 # CRC enabled
338
- self .crc_auto_clear = 0 # Clear FIFO on CRC fail
339
- self .address_filtering = 0b00 # No address filtering
348
+ self .packet_format = 1 # Variable length.
349
+ self .dc_free = 0b10 # Whitening
350
+ self .crc_on = 1 # CRC enabled
351
+ self .crc_auto_clear = 0 # Clear FIFO on CRC fail
352
+ self .address_filtering = 0b00 # No address filtering
340
353
# Set the preamble length.
341
354
self .preamble_length = preamble_length
342
355
# Set frequency.
@@ -346,7 +359,7 @@ def __init__(self, spi, cs, reset, frequency, *, sync_word=b'\x2D\xD4',
346
359
# Set transmit power to 13 dBm, a safe value any module supports.
347
360
self .tx_power = 13
348
361
# last RSSI reading
349
- self .last_rssi = 0.
362
+ self .last_rssi = 0.0
350
363
351
364
# pylint: disable=no-member
352
365
# Reconsider this disable when it can be tested.
@@ -358,7 +371,7 @@ def _read_into(self, address, buf, length=None):
358
371
length = len (buf )
359
372
with self ._device as device :
360
373
self ._BUFFER [0 ] = address & 0x7F # Strip out top bit to set 0
361
- # value (read).
374
+ # value (read).
362
375
device .write (self ._BUFFER , end = 1 )
363
376
device .readinto (buf , end = length )
364
377
@@ -375,7 +388,7 @@ def _write_from(self, address, buf, length=None):
375
388
length = len (buf )
376
389
with self ._device as device :
377
390
self ._BUFFER [0 ] = (address | 0x80 ) & 0xFF # Set top bit to 1 to
378
- # indicate a write.
391
+ # indicate a write.
379
392
device .write (self ._BUFFER , end = 1 )
380
393
device .write (buf , end = length )
381
394
@@ -384,7 +397,7 @@ def _write_u8(self, address, val):
384
397
# 8-bit value to write to that address.
385
398
with self ._device as device :
386
399
self ._BUFFER [0 ] = (address | 0x80 ) & 0xFF # Set top bit to 1 to
387
- # indicate a write.
400
+ # indicate a write.
388
401
self ._BUFFER [1 ] = val & 0xFF
389
402
device .write (self ._BUFFER , end = 2 )
390
403
@@ -394,7 +407,7 @@ def reset(self):
394
407
self ._reset .value = True
395
408
time .sleep (0.0001 ) # 100 us
396
409
self ._reset .value = False
397
- time .sleep (0.005 ) # 5 ms
410
+ time .sleep (0.005 ) # 5 ms
398
411
399
412
def idle (self ):
400
413
"""Enter idle standby mode (switching off high power amplifiers if necessary)."""
@@ -467,7 +480,7 @@ def operation_mode(self, val):
467
480
# Set the mode bits inside the operation mode register.
468
481
op_mode = self ._read_u8 (_REG_OP_MODE )
469
482
op_mode &= 0b11100011
470
- op_mode |= ( val << 2 )
483
+ op_mode |= val << 2
471
484
self ._write_u8 (_REG_OP_MODE , op_mode )
472
485
# Wait for mode to change by polling interrupt bit.
473
486
while not self .mode_ready :
@@ -486,7 +499,7 @@ def sync_word(self):
486
499
return None
487
500
# Sync word is not disabled so read the current value.
488
501
sync_word_length = self .sync_size + 1 # Sync word size is offset by 1
489
- # according to datasheet.
502
+ # according to datasheet.
490
503
sync_word = bytearray (sync_word_length )
491
504
self ._read_into (_REG_SYNC_VALUE1 , sync_word )
492
505
return sync_word
@@ -502,7 +515,7 @@ def sync_word(self, val):
502
515
# Update the value, size and turn on the sync word.
503
516
self ._write_from (_REG_SYNC_VALUE1 , val )
504
517
self .sync_size = len (val ) - 1 # Again sync word size is offset by
505
- # 1 according to datasheet.
518
+ # 1 according to datasheet.
506
519
self .sync_on = 1
507
520
508
521
@property
@@ -598,7 +611,7 @@ def tx_power(self):
598
611
if not pa0 and pa1 and pa2 and self .high_power :
599
612
# 5 to 20 dBm range
600
613
return - 11 + self .output_power
601
- raise RuntimeError (' Power amplifiers in unknown state!' )
614
+ raise RuntimeError (" Power amplifiers in unknown state!" )
602
615
603
616
@tx_power .setter
604
617
def tx_power (self , val ):
@@ -641,7 +654,7 @@ def tx_power(self, val):
641
654
def rssi (self ):
642
655
"""The received strength indicator (in dBm) of the last received message."""
643
656
# Read RSSI register and convert to value using formula in datasheet.
644
- return - self ._read_u8 (_REG_RSSI_VALUE )/ 2.0
657
+ return - self ._read_u8 (_REG_RSSI_VALUE ) / 2.0
645
658
646
659
@property
647
660
def bitrate (self ):
@@ -655,7 +668,7 @@ def bitrate(self):
655
668
656
669
@bitrate .setter
657
670
def bitrate (self , val ):
658
- assert (_FXOSC / 65535 ) <= val <= 32000000.0
671
+ assert (_FXOSC / 65535 ) <= val <= 32000000.0
659
672
# Round up to the next closest bit-rate value with addition of 0.5.
660
673
bitrate = int ((_FXOSC / val ) + 0.5 ) & 0xFFFF
661
674
self ._write_u8 (_REG_BITRATE_MSB , bitrate >> 8 )
@@ -670,14 +683,19 @@ def frequency_deviation(self):
670
683
671
684
@frequency_deviation .setter
672
685
def frequency_deviation (self , val ):
673
- assert 0 <= val <= (_FSTEP * 16383 ) # fdev is a 14-bit unsigned value
686
+ assert 0 <= val <= (_FSTEP * 16383 ) # fdev is a 14-bit unsigned value
674
687
# Round up to the next closest integer value with addition of 0.5.
675
688
fdev = int ((val / _FSTEP ) + 0.5 ) & 0x3FFF
676
689
self ._write_u8 (_REG_FDEV_MSB , fdev >> 8 )
677
690
self ._write_u8 (_REG_FDEV_LSB , fdev & 0xFF )
678
691
679
- def send (self , data , timeout = 2. , keep_listening = False ,
680
- tx_header = (_RH_BROADCAST_ADDRESS , _RH_BROADCAST_ADDRESS , 0 , 0 )):
692
+ def send (
693
+ self ,
694
+ data ,
695
+ timeout = 2.0 ,
696
+ keep_listening = False ,
697
+ tx_header = (_RH_BROADCAST_ADDRESS , _RH_BROADCAST_ADDRESS , 0 , 0 ),
698
+ ):
681
699
"""Send a string of data using the transmitter.
682
700
You can only send 60 bytes at a time
683
701
(limited by chip's FIFO size and appended headers).
@@ -701,16 +719,16 @@ def send(self, data, timeout=2., keep_listening=False,
701
719
self .idle () # Stop receiving to clear FIFO and keep it clear.
702
720
# Fill the FIFO with a packet to send.
703
721
with self ._device as device :
704
- self ._BUFFER [0 ] = ( _REG_FIFO | 0x80 ) # Set top bit to 1 to
705
- # indicate a write.
722
+ self ._BUFFER [0 ] = _REG_FIFO | 0x80 # Set top bit to 1 to
723
+ # indicate a write.
706
724
self ._BUFFER [1 ] = (len (data ) + 4 ) & 0xFF
707
725
# Add 4 bytes of headers to match RadioHead library.
708
726
# Just use the defaults for global broadcast to all receivers
709
727
# for now.
710
- self ._BUFFER [2 ] = tx_header [0 ] # Header: To
711
- self ._BUFFER [3 ] = tx_header [1 ] # Header: From
712
- self ._BUFFER [4 ] = tx_header [2 ] # Header: Id
713
- self ._BUFFER [5 ] = tx_header [3 ] # Header: Flags
728
+ self ._BUFFER [2 ] = tx_header [0 ] # Header: To
729
+ self ._BUFFER [3 ] = tx_header [1 ] # Header: From
730
+ self ._BUFFER [4 ] = tx_header [2 ] # Header: Id
731
+ self ._BUFFER [5 ] = tx_header [3 ] # Header: Flags
714
732
device .write (self ._BUFFER , end = 6 )
715
733
# Now send the payload.
716
734
device .write (data )
@@ -727,13 +745,18 @@ def send(self, data, timeout=2., keep_listening=False,
727
745
if keep_listening :
728
746
self .listen ()
729
747
else :
730
- # Enter idle mode to stop receiving other packets.
748
+ # Enter idle mode to stop receiving other packets.
731
749
self .idle ()
732
750
733
751
return not timed_out
734
752
735
- def receive (self , timeout = 0.5 , keep_listening = True , with_header = False ,
736
- rx_filter = _RH_BROADCAST_ADDRESS ):
753
+ def receive (
754
+ self ,
755
+ timeout = 0.5 ,
756
+ keep_listening = True ,
757
+ with_header = False ,
758
+ rx_filter = _RH_BROADCAST_ADDRESS ,
759
+ ):
737
760
"""Wait to receive a packet from the receiver. Will wait for up to timeout_s amount of
738
761
seconds for a packet to be received and decoded. If a packet is found the payload bytes
739
762
are returned, otherwise None is returned (which indicates the timeout elapsed with no
@@ -778,7 +801,7 @@ def receive(self, timeout=0.5, keep_listening=True, with_header=False,
778
801
# Read the data from the FIFO.
779
802
with self ._device as device :
780
803
self ._BUFFER [0 ] = _REG_FIFO & 0x7F # Strip out top bit to set 0
781
- # value (read).
804
+ # value (read).
782
805
device .write (self ._BUFFER , end = 1 )
783
806
# Read the length of the FIFO.
784
807
device .readinto (self ._BUFFER , end = 1 )
@@ -793,8 +816,11 @@ def receive(self, timeout=0.5, keep_listening=True, with_header=False,
793
816
else :
794
817
packet = bytearray (fifo_length )
795
818
device .readinto (packet )
796
- if (rx_filter != _RH_BROADCAST_ADDRESS and packet [0 ] != _RH_BROADCAST_ADDRESS
797
- and packet [0 ] != rx_filter ):
819
+ if (
820
+ rx_filter != _RH_BROADCAST_ADDRESS
821
+ and packet [0 ] != _RH_BROADCAST_ADDRESS
822
+ and packet [0 ] != rx_filter
823
+ ):
798
824
packet = None
799
825
elif not with_header : # skip the header if not wanted
800
826
packet = packet [4 :]
0 commit comments