8080
8181# pylint: disable=bad-whitespace
8282# 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 )
119119
120120# The crystal oscillator frequency and frequency synthesizer step size.
121121# See the datasheet for details of this calculation.
122- _FXOSC = 32000000.0
123- _FSTEP = _FXOSC / 524288
122+ _FXOSC = 32000000.0
123+ _FSTEP = _FXOSC / 524288
124124
125125# RadioHead specific compatibility constants.
126126_RH_BROADCAST_ADDRESS = const (0xFF )
127127
128128# User facing constants:
129- SLEEP_MODE = 0b000
129+ SLEEP_MODE = 0b000
130130STANDBY_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
134134# pylint: enable=bad-whitespace
135135
136136# Disable the silly too many instance members warning. Pylint has no knowledge
139139# the warning to work around the error.
140140# pylint: disable=too-many-instance-attributes
141141
142+
142143class RFM69 :
143144 """Interface to a RFM69 series packet radio. Allows simple sending and
144145 receiving of wireless data at supported frequencies of the radio
@@ -288,13 +289,23 @@ def __set__(self, obj, val):
288289
289290 payload_ready = _RegisterBits (_REG_IRQ_FLAGS2 , offset = 2 )
290291
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+ ):
293305 self ._tx_power = 13
294306 self .high_power = high_power
295307 # 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 )
298309 # Setup reset as a digital output that's low.
299310 self ._reset = reset
300311 self ._reset .switch_to_output (value = False )
@@ -303,7 +314,9 @@ def __init__(self, spi, cs, reset, frequency, *, sync_word=b'\x2D\xD4',
303314 # Check the version of the chip.
304315 version = self ._read_u8 (_REG_VERSION )
305316 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+ )
307320 # Enter idle state.
308321 self .idle ()
309322 # 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',
321334 # by default. Users with advanced knowledge can manually reconfigure
322335 # for any other mode (consulting the datasheet is absolutely
323336 # 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
328341 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
330343 self .rx_bw_mantissa = 0b00
331344 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
333346 self .afc_bw_mantissa = 0b00
334347 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
340353 # Set the preamble length.
341354 self .preamble_length = preamble_length
342355 # Set frequency.
@@ -346,7 +359,7 @@ def __init__(self, spi, cs, reset, frequency, *, sync_word=b'\x2D\xD4',
346359 # Set transmit power to 13 dBm, a safe value any module supports.
347360 self .tx_power = 13
348361 # last RSSI reading
349- self .last_rssi = 0.
362+ self .last_rssi = 0.0
350363
351364 # pylint: disable=no-member
352365 # Reconsider this disable when it can be tested.
@@ -358,7 +371,7 @@ def _read_into(self, address, buf, length=None):
358371 length = len (buf )
359372 with self ._device as device :
360373 self ._BUFFER [0 ] = address & 0x7F # Strip out top bit to set 0
361- # value (read).
374+ # value (read).
362375 device .write (self ._BUFFER , end = 1 )
363376 device .readinto (buf , end = length )
364377
@@ -375,7 +388,7 @@ def _write_from(self, address, buf, length=None):
375388 length = len (buf )
376389 with self ._device as device :
377390 self ._BUFFER [0 ] = (address | 0x80 ) & 0xFF # Set top bit to 1 to
378- # indicate a write.
391+ # indicate a write.
379392 device .write (self ._BUFFER , end = 1 )
380393 device .write (buf , end = length )
381394
@@ -384,7 +397,7 @@ def _write_u8(self, address, val):
384397 # 8-bit value to write to that address.
385398 with self ._device as device :
386399 self ._BUFFER [0 ] = (address | 0x80 ) & 0xFF # Set top bit to 1 to
387- # indicate a write.
400+ # indicate a write.
388401 self ._BUFFER [1 ] = val & 0xFF
389402 device .write (self ._BUFFER , end = 2 )
390403
@@ -394,7 +407,7 @@ def reset(self):
394407 self ._reset .value = True
395408 time .sleep (0.0001 ) # 100 us
396409 self ._reset .value = False
397- time .sleep (0.005 ) # 5 ms
410+ time .sleep (0.005 ) # 5 ms
398411
399412 def idle (self ):
400413 """Enter idle standby mode (switching off high power amplifiers if necessary)."""
@@ -467,7 +480,7 @@ def operation_mode(self, val):
467480 # Set the mode bits inside the operation mode register.
468481 op_mode = self ._read_u8 (_REG_OP_MODE )
469482 op_mode &= 0b11100011
470- op_mode |= ( val << 2 )
483+ op_mode |= val << 2
471484 self ._write_u8 (_REG_OP_MODE , op_mode )
472485 # Wait for mode to change by polling interrupt bit.
473486 while not self .mode_ready :
@@ -486,7 +499,7 @@ def sync_word(self):
486499 return None
487500 # Sync word is not disabled so read the current value.
488501 sync_word_length = self .sync_size + 1 # Sync word size is offset by 1
489- # according to datasheet.
502+ # according to datasheet.
490503 sync_word = bytearray (sync_word_length )
491504 self ._read_into (_REG_SYNC_VALUE1 , sync_word )
492505 return sync_word
@@ -502,7 +515,7 @@ def sync_word(self, val):
502515 # Update the value, size and turn on the sync word.
503516 self ._write_from (_REG_SYNC_VALUE1 , val )
504517 self .sync_size = len (val ) - 1 # Again sync word size is offset by
505- # 1 according to datasheet.
518+ # 1 according to datasheet.
506519 self .sync_on = 1
507520
508521 @property
@@ -598,7 +611,7 @@ def tx_power(self):
598611 if not pa0 and pa1 and pa2 and self .high_power :
599612 # 5 to 20 dBm range
600613 return - 11 + self .output_power
601- raise RuntimeError (' Power amplifiers in unknown state!' )
614+ raise RuntimeError (" Power amplifiers in unknown state!" )
602615
603616 @tx_power .setter
604617 def tx_power (self , val ):
@@ -641,7 +654,7 @@ def tx_power(self, val):
641654 def rssi (self ):
642655 """The received strength indicator (in dBm) of the last received message."""
643656 # 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
645658
646659 @property
647660 def bitrate (self ):
@@ -655,7 +668,7 @@ def bitrate(self):
655668
656669 @bitrate .setter
657670 def bitrate (self , val ):
658- assert (_FXOSC / 65535 ) <= val <= 32000000.0
671+ assert (_FXOSC / 65535 ) <= val <= 32000000.0
659672 # Round up to the next closest bit-rate value with addition of 0.5.
660673 bitrate = int ((_FXOSC / val ) + 0.5 ) & 0xFFFF
661674 self ._write_u8 (_REG_BITRATE_MSB , bitrate >> 8 )
@@ -670,14 +683,19 @@ def frequency_deviation(self):
670683
671684 @frequency_deviation .setter
672685 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
674687 # Round up to the next closest integer value with addition of 0.5.
675688 fdev = int ((val / _FSTEP ) + 0.5 ) & 0x3FFF
676689 self ._write_u8 (_REG_FDEV_MSB , fdev >> 8 )
677690 self ._write_u8 (_REG_FDEV_LSB , fdev & 0xFF )
678691
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+ ):
681699 """Send a string of data using the transmitter.
682700 You can only send 60 bytes at a time
683701 (limited by chip's FIFO size and appended headers).
@@ -701,16 +719,16 @@ def send(self, data, timeout=2., keep_listening=False,
701719 self .idle () # Stop receiving to clear FIFO and keep it clear.
702720 # Fill the FIFO with a packet to send.
703721 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.
706724 self ._BUFFER [1 ] = (len (data ) + 4 ) & 0xFF
707725 # Add 4 bytes of headers to match RadioHead library.
708726 # Just use the defaults for global broadcast to all receivers
709727 # 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
714732 device .write (self ._BUFFER , end = 6 )
715733 # Now send the payload.
716734 device .write (data )
@@ -727,13 +745,18 @@ def send(self, data, timeout=2., keep_listening=False,
727745 if keep_listening :
728746 self .listen ()
729747 else :
730- # Enter idle mode to stop receiving other packets.
748+ # Enter idle mode to stop receiving other packets.
731749 self .idle ()
732750
733751 return not timed_out
734752
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+ ):
737760 """Wait to receive a packet from the receiver. Will wait for up to timeout_s amount of
738761 seconds for a packet to be received and decoded. If a packet is found the payload bytes
739762 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,
778801 # Read the data from the FIFO.
779802 with self ._device as device :
780803 self ._BUFFER [0 ] = _REG_FIFO & 0x7F # Strip out top bit to set 0
781- # value (read).
804+ # value (read).
782805 device .write (self ._BUFFER , end = 1 )
783806 # Read the length of the FIFO.
784807 device .readinto (self ._BUFFER , end = 1 )
@@ -793,8 +816,11 @@ def receive(self, timeout=0.5, keep_listening=True, with_header=False,
793816 else :
794817 packet = bytearray (fifo_length )
795818 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+ ):
798824 packet = None
799825 elif not with_header : # skip the header if not wanted
800826 packet = packet [4 :]
0 commit comments