From d6dc78f252b10508b36cad98b1ef344ce108cd74 Mon Sep 17 00:00:00 2001 From: dherrada Date: Mon, 16 Mar 2020 16:34:20 -0400 Subject: [PATCH 1/2] Ran black, updated to pylint 2.x --- .github/workflows/build.yml | 2 +- adafruit_rfm69.py | 198 ++++++++++++++++++-------------- docs/conf.py | 114 ++++++++++-------- examples/rfm69_rpi_interrupt.py | 44 ++++--- examples/rfm69_simpletest.py | 34 +++--- examples/rfm69_transmit.py | 38 +++--- setup.py | 50 ++++---- 7 files changed, 265 insertions(+), 215 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index fff3aa9..1dad804 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -40,7 +40,7 @@ jobs: source actions-ci/install.sh - name: Pip install pylint, black, & Sphinx run: | - pip install --force-reinstall pylint==1.9.2 black==19.10b0 Sphinx sphinx-rtd-theme + pip install --force-reinstall pylint black==19.10b0 Sphinx sphinx-rtd-theme - name: Library version run: git describe --dirty --always --tags - name: PyLint diff --git a/adafruit_rfm69.py b/adafruit_rfm69.py index 17de383..1d66842 100644 --- a/adafruit_rfm69.py +++ b/adafruit_rfm69.py @@ -80,57 +80,57 @@ # pylint: disable=bad-whitespace # Internal constants: -_REG_FIFO = const(0x00) -_REG_OP_MODE = const(0x01) -_REG_DATA_MOD = const(0x02) -_REG_BITRATE_MSB = const(0x03) -_REG_BITRATE_LSB = const(0x04) -_REG_FDEV_MSB = const(0x05) -_REG_FDEV_LSB = const(0x06) -_REG_FRF_MSB = const(0x07) -_REG_FRF_MID = const(0x08) -_REG_FRF_LSB = const(0x09) -_REG_VERSION = const(0x10) -_REG_PA_LEVEL = const(0x11) -_REG_RX_BW = const(0x19) -_REG_AFC_BW = const(0x1A) -_REG_RSSI_VALUE = const(0x24) -_REG_DIO_MAPPING1 = const(0x25) -_REG_IRQ_FLAGS1 = const(0x27) -_REG_IRQ_FLAGS2 = const(0x28) -_REG_PREAMBLE_MSB = const(0x2C) -_REG_PREAMBLE_LSB = const(0x2D) -_REG_SYNC_CONFIG = const(0x2E) -_REG_SYNC_VALUE1 = const(0x2F) -_REG_PACKET_CONFIG1 = const(0x37) -_REG_FIFO_THRESH = const(0x3C) -_REG_PACKET_CONFIG2 = const(0x3D) -_REG_AES_KEY1 = const(0x3E) -_REG_TEMP1 = const(0x4E) -_REG_TEMP2 = const(0x4F) -_REG_TEST_PA1 = const(0x5A) -_REG_TEST_PA2 = const(0x5C) -_REG_TEST_DAGC = const(0x6F) - -_TEST_PA1_NORMAL = const(0x55) -_TEST_PA1_BOOST = const(0x5D) -_TEST_PA2_NORMAL = const(0x70) -_TEST_PA2_BOOST = const(0x7C) +_REG_FIFO = const(0x00) +_REG_OP_MODE = const(0x01) +_REG_DATA_MOD = const(0x02) +_REG_BITRATE_MSB = const(0x03) +_REG_BITRATE_LSB = const(0x04) +_REG_FDEV_MSB = const(0x05) +_REG_FDEV_LSB = const(0x06) +_REG_FRF_MSB = const(0x07) +_REG_FRF_MID = const(0x08) +_REG_FRF_LSB = const(0x09) +_REG_VERSION = const(0x10) +_REG_PA_LEVEL = const(0x11) +_REG_RX_BW = const(0x19) +_REG_AFC_BW = const(0x1A) +_REG_RSSI_VALUE = const(0x24) +_REG_DIO_MAPPING1 = const(0x25) +_REG_IRQ_FLAGS1 = const(0x27) +_REG_IRQ_FLAGS2 = const(0x28) +_REG_PREAMBLE_MSB = const(0x2C) +_REG_PREAMBLE_LSB = const(0x2D) +_REG_SYNC_CONFIG = const(0x2E) +_REG_SYNC_VALUE1 = const(0x2F) +_REG_PACKET_CONFIG1 = const(0x37) +_REG_FIFO_THRESH = const(0x3C) +_REG_PACKET_CONFIG2 = const(0x3D) +_REG_AES_KEY1 = const(0x3E) +_REG_TEMP1 = const(0x4E) +_REG_TEMP2 = const(0x4F) +_REG_TEST_PA1 = const(0x5A) +_REG_TEST_PA2 = const(0x5C) +_REG_TEST_DAGC = const(0x6F) + +_TEST_PA1_NORMAL = const(0x55) +_TEST_PA1_BOOST = const(0x5D) +_TEST_PA2_NORMAL = const(0x70) +_TEST_PA2_BOOST = const(0x7C) # The crystal oscillator frequency and frequency synthesizer step size. # See the datasheet for details of this calculation. -_FXOSC = 32000000.0 -_FSTEP = _FXOSC / 524288 +_FXOSC = 32000000.0 +_FSTEP = _FXOSC / 524288 # RadioHead specific compatibility constants. _RH_BROADCAST_ADDRESS = const(0xFF) # User facing constants: -SLEEP_MODE = 0b000 +SLEEP_MODE = 0b000 STANDBY_MODE = 0b001 -FS_MODE = 0b010 -TX_MODE = 0b011 -RX_MODE = 0b100 +FS_MODE = 0b010 +TX_MODE = 0b011 +RX_MODE = 0b100 # pylint: enable=bad-whitespace # Disable the silly too many instance members warning. Pylint has no knowledge @@ -139,6 +139,7 @@ # the warning to work around the error. # pylint: disable=too-many-instance-attributes + class RFM69: """Interface to a RFM69 series packet radio. Allows simple sending and receiving of wireless data at supported frequencies of the radio @@ -288,13 +289,23 @@ def __set__(self, obj, val): payload_ready = _RegisterBits(_REG_IRQ_FLAGS2, offset=2) - def __init__(self, spi, cs, reset, frequency, *, sync_word=b'\x2D\xD4', - preamble_length=4, encryption_key=None, high_power=True, baudrate=5000000): + def __init__( + self, + spi, + cs, + reset, + frequency, + *, + sync_word=b"\x2D\xD4", + preamble_length=4, + encryption_key=None, + high_power=True, + baudrate=5000000 + ): self._tx_power = 13 self.high_power = high_power # Device support SPI mode 0 (polarity & phase = 0) up to a max of 10mhz. - self._device = spidev.SPIDevice(spi, cs, baudrate=baudrate, - polarity=0, phase=0) + self._device = spidev.SPIDevice(spi, cs, baudrate=baudrate, polarity=0, phase=0) # Setup reset as a digital output that's low. self._reset = reset self._reset.switch_to_output(value=False) @@ -303,7 +314,9 @@ def __init__(self, spi, cs, reset, frequency, *, sync_word=b'\x2D\xD4', # Check the version of the chip. version = self._read_u8(_REG_VERSION) if version != 0x24: - raise RuntimeError('Failed to find RFM69 with expected version, check wiring!') + raise RuntimeError( + "Failed to find RFM69 with expected version, check wiring!" + ) # Enter idle state. self.idle() # 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', # by default. Users with advanced knowledge can manually reconfigure # for any other mode (consulting the datasheet is absolutely # necessary!). - self.data_mode = 0b00 # Packet mode - self.modulation_type = 0b00 # FSK modulation - self.modulation_shaping = 0b01 # Gaussian filter, BT=1.0 - self.bitrate = 250000 # 250kbs + self.data_mode = 0b00 # Packet mode + self.modulation_type = 0b00 # FSK modulation + self.modulation_shaping = 0b01 # Gaussian filter, BT=1.0 + self.bitrate = 250000 # 250kbs self.frequency_deviation = 250000 # 250khz - self.rx_bw_dcc_freq = 0b111 # RxBw register = 0xE0 + self.rx_bw_dcc_freq = 0b111 # RxBw register = 0xE0 self.rx_bw_mantissa = 0b00 self.rx_bw_exponent = 0b000 - self.afc_bw_dcc_freq = 0b111 # AfcBw register = 0xE0 + self.afc_bw_dcc_freq = 0b111 # AfcBw register = 0xE0 self.afc_bw_mantissa = 0b00 self.afc_bw_exponent = 0b000 - self.packet_format = 1 # Variable length. - self.dc_free = 0b10 # Whitening - self.crc_on = 1 # CRC enabled - self.crc_auto_clear = 0 # Clear FIFO on CRC fail - self.address_filtering = 0b00 # No address filtering + self.packet_format = 1 # Variable length. + self.dc_free = 0b10 # Whitening + self.crc_on = 1 # CRC enabled + self.crc_auto_clear = 0 # Clear FIFO on CRC fail + self.address_filtering = 0b00 # No address filtering # Set the preamble length. self.preamble_length = preamble_length # Set frequency. @@ -346,7 +359,7 @@ def __init__(self, spi, cs, reset, frequency, *, sync_word=b'\x2D\xD4', # Set transmit power to 13 dBm, a safe value any module supports. self.tx_power = 13 # last RSSI reading - self.last_rssi = 0. + self.last_rssi = 0.0 # pylint: disable=no-member # Reconsider this disable when it can be tested. @@ -358,7 +371,7 @@ def _read_into(self, address, buf, length=None): length = len(buf) with self._device as device: self._BUFFER[0] = address & 0x7F # Strip out top bit to set 0 - # value (read). + # value (read). device.write(self._BUFFER, end=1) device.readinto(buf, end=length) @@ -375,7 +388,7 @@ def _write_from(self, address, buf, length=None): length = len(buf) with self._device as device: self._BUFFER[0] = (address | 0x80) & 0xFF # Set top bit to 1 to - # indicate a write. + # indicate a write. device.write(self._BUFFER, end=1) device.write(buf, end=length) @@ -384,7 +397,7 @@ def _write_u8(self, address, val): # 8-bit value to write to that address. with self._device as device: self._BUFFER[0] = (address | 0x80) & 0xFF # Set top bit to 1 to - # indicate a write. + # indicate a write. self._BUFFER[1] = val & 0xFF device.write(self._BUFFER, end=2) @@ -394,7 +407,7 @@ def reset(self): self._reset.value = True time.sleep(0.0001) # 100 us self._reset.value = False - time.sleep(0.005) # 5 ms + time.sleep(0.005) # 5 ms def idle(self): """Enter idle standby mode (switching off high power amplifiers if necessary).""" @@ -467,7 +480,7 @@ def operation_mode(self, val): # Set the mode bits inside the operation mode register. op_mode = self._read_u8(_REG_OP_MODE) op_mode &= 0b11100011 - op_mode |= (val << 2) + op_mode |= val << 2 self._write_u8(_REG_OP_MODE, op_mode) # Wait for mode to change by polling interrupt bit. while not self.mode_ready: @@ -486,7 +499,7 @@ def sync_word(self): return None # Sync word is not disabled so read the current value. sync_word_length = self.sync_size + 1 # Sync word size is offset by 1 - # according to datasheet. + # according to datasheet. sync_word = bytearray(sync_word_length) self._read_into(_REG_SYNC_VALUE1, sync_word) return sync_word @@ -502,7 +515,7 @@ def sync_word(self, val): # Update the value, size and turn on the sync word. self._write_from(_REG_SYNC_VALUE1, val) self.sync_size = len(val) - 1 # Again sync word size is offset by - # 1 according to datasheet. + # 1 according to datasheet. self.sync_on = 1 @property @@ -598,7 +611,7 @@ def tx_power(self): if not pa0 and pa1 and pa2 and self.high_power: # 5 to 20 dBm range return -11 + self.output_power - raise RuntimeError('Power amplifiers in unknown state!') + raise RuntimeError("Power amplifiers in unknown state!") @tx_power.setter def tx_power(self, val): @@ -641,7 +654,7 @@ def tx_power(self, val): def rssi(self): """The received strength indicator (in dBm) of the last received message.""" # Read RSSI register and convert to value using formula in datasheet. - return -self._read_u8(_REG_RSSI_VALUE)/2.0 + return -self._read_u8(_REG_RSSI_VALUE) / 2.0 @property def bitrate(self): @@ -655,7 +668,7 @@ def bitrate(self): @bitrate.setter def bitrate(self, val): - assert (_FXOSC/65535) <= val <= 32000000.0 + assert (_FXOSC / 65535) <= val <= 32000000.0 # Round up to the next closest bit-rate value with addition of 0.5. bitrate = int((_FXOSC / val) + 0.5) & 0xFFFF self._write_u8(_REG_BITRATE_MSB, bitrate >> 8) @@ -670,14 +683,19 @@ def frequency_deviation(self): @frequency_deviation.setter def frequency_deviation(self, val): - assert 0 <= val <= (_FSTEP*16383) # fdev is a 14-bit unsigned value + assert 0 <= val <= (_FSTEP * 16383) # fdev is a 14-bit unsigned value # Round up to the next closest integer value with addition of 0.5. fdev = int((val / _FSTEP) + 0.5) & 0x3FFF self._write_u8(_REG_FDEV_MSB, fdev >> 8) self._write_u8(_REG_FDEV_LSB, fdev & 0xFF) - def send(self, data, timeout=2., keep_listening=False, - tx_header=(_RH_BROADCAST_ADDRESS, _RH_BROADCAST_ADDRESS, 0, 0)): + def send( + self, + data, + timeout=2.0, + keep_listening=False, + tx_header=(_RH_BROADCAST_ADDRESS, _RH_BROADCAST_ADDRESS, 0, 0), + ): """Send a string of data using the transmitter. You can only send 60 bytes at a time (limited by chip's FIFO size and appended headers). @@ -699,16 +717,16 @@ def send(self, data, timeout=2., keep_listening=False, self.idle() # Stop receiving to clear FIFO and keep it clear. # Fill the FIFO with a packet to send. with self._device as device: - self._BUFFER[0] = (_REG_FIFO | 0x80) # Set top bit to 1 to - # indicate a write. + self._BUFFER[0] = _REG_FIFO | 0x80 # Set top bit to 1 to + # indicate a write. self._BUFFER[1] = (len(data) + 4) & 0xFF # Add 4 bytes of headers to match RadioHead library. # Just use the defaults for global broadcast to all receivers # for now. - self._BUFFER[2] = tx_header[0] # Header: To - self._BUFFER[3] = tx_header[1] # Header: From - self._BUFFER[4] = tx_header[2] # Header: Id - self._BUFFER[5] = tx_header[3] # Header: Flags + self._BUFFER[2] = tx_header[0] # Header: To + self._BUFFER[3] = tx_header[1] # Header: From + self._BUFFER[4] = tx_header[2] # Header: Id + self._BUFFER[5] = tx_header[3] # Header: Flags device.write(self._BUFFER, end=6) # Now send the payload. device.write(data) @@ -725,13 +743,18 @@ def send(self, data, timeout=2., keep_listening=False, if keep_listening: self.listen() else: - # Enter idle mode to stop receiving other packets. + # Enter idle mode to stop receiving other packets. self.idle() if timed_out: - raise RuntimeError('Timeout during packet send') - - def receive(self, timeout=0.5, keep_listening=True, with_header=False, - rx_filter=_RH_BROADCAST_ADDRESS): + raise RuntimeError("Timeout during packet send") + + def receive( + self, + timeout=0.5, + keep_listening=True, + with_header=False, + rx_filter=_RH_BROADCAST_ADDRESS, + ): """Wait to receive a packet from the receiver. Will wait for up to timeout_s amount of seconds for a packet to be received and decoded. If a packet is found the payload bytes are returned, otherwise None is returned (which indicates the timeout elapsed with no @@ -776,7 +799,7 @@ def receive(self, timeout=0.5, keep_listening=True, with_header=False, # Read the data from the FIFO. with self._device as device: self._BUFFER[0] = _REG_FIFO & 0x7F # Strip out top bit to set 0 - # value (read). + # value (read). device.write(self._BUFFER, end=1) # Read the length of the FIFO. device.readinto(self._BUFFER, end=1) @@ -791,8 +814,11 @@ def receive(self, timeout=0.5, keep_listening=True, with_header=False, else: packet = bytearray(fifo_length) device.readinto(packet) - if (rx_filter != _RH_BROADCAST_ADDRESS and packet[0] != _RH_BROADCAST_ADDRESS - and packet[0] != rx_filter): + if ( + rx_filter != _RH_BROADCAST_ADDRESS + and packet[0] != _RH_BROADCAST_ADDRESS + and packet[0] != rx_filter + ): packet = None elif not with_header: # skip the header if not wanted packet = packet[4:] diff --git a/docs/conf.py b/docs/conf.py index 1236cfd..eb90f85 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -2,7 +2,8 @@ import os import sys -sys.path.insert(0, os.path.abspath('..')) + +sys.path.insert(0, os.path.abspath("..")) # -- General configuration ------------------------------------------------ @@ -10,9 +11,9 @@ # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom # ones. extensions = [ - 'sphinx.ext.autodoc', - 'sphinx.ext.intersphinx', - 'sphinx.ext.viewcode', + "sphinx.ext.autodoc", + "sphinx.ext.intersphinx", + "sphinx.ext.viewcode", ] # Uncomment the below if you use native CircuitPython modules such as @@ -20,29 +21,36 @@ # autodoc module docs will fail to generate with a warning. # autodoc_mock_imports = ["adafruit_bus_device", "micropython"] -intersphinx_mapping = {'python': ('https://docs.python.org/3.4', None),'BusDevice': ('https://circuitpython.readthedocs.io/projects/busdevice/en/latest/', None),'CircuitPython': ('https://circuitpython.readthedocs.io/en/latest/', None)} +intersphinx_mapping = { + "python": ("https://docs.python.org/3.4", None), + "BusDevice": ( + "https://circuitpython.readthedocs.io/projects/busdevice/en/latest/", + None, + ), + "CircuitPython": ("https://circuitpython.readthedocs.io/en/latest/", None), +} # Add any paths that contain templates here, relative to this directory. -templates_path = ['_templates'] +templates_path = ["_templates"] -source_suffix = '.rst' +source_suffix = ".rst" # The master toctree document. -master_doc = 'index' +master_doc = "index" # General information about the project. -project = u'Adafruit RFM69 Library' -copyright = u'2017 Tony DiCola' -author = u'Tony DiCola' +project = u"Adafruit RFM69 Library" +copyright = u"2017 Tony DiCola" +author = u"Tony DiCola" # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the # built documents. # # The short X.Y version. -version = u'1.0' +version = u"1.0" # The full version, including alpha/beta/rc tags. -release = u'1.0' +release = u"1.0" # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. @@ -54,7 +62,7 @@ # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. # This patterns also effect to html_static_path and html_extra_path -exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store', '.env', 'CODE_OF_CONDUCT.md'] +exclude_patterns = ["_build", "Thumbs.db", ".DS_Store", ".env", "CODE_OF_CONDUCT.md"] # The reST default role (used for this markup: `text`) to use for all # documents. @@ -66,7 +74,7 @@ add_function_parentheses = True # The name of the Pygments (syntax highlighting) style to use. -pygments_style = 'sphinx' +pygments_style = "sphinx" # If true, `todo` and `todoList` produce output, else they produce nothing. todo_include_todos = False @@ -80,59 +88,62 @@ # The theme to use for HTML and HTML Help pages. See the documentation for # a list of builtin themes. # -on_rtd = os.environ.get('READTHEDOCS', None) == 'True' +on_rtd = os.environ.get("READTHEDOCS", None) == "True" if not on_rtd: # only import and set the theme if we're building docs locally try: import sphinx_rtd_theme - html_theme = 'sphinx_rtd_theme' - html_theme_path = [sphinx_rtd_theme.get_html_theme_path(), '.'] + + html_theme = "sphinx_rtd_theme" + html_theme_path = [sphinx_rtd_theme.get_html_theme_path(), "."] except: - html_theme = 'default' - html_theme_path = ['.'] + html_theme = "default" + html_theme_path = ["."] else: - html_theme_path = ['.'] + html_theme_path = ["."] # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". -html_static_path = ['_static'] +html_static_path = ["_static"] # The name of an image file (relative to this directory) to use as a favicon of # the docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 # pixels large. # -html_favicon = '_static/favicon.ico' +html_favicon = "_static/favicon.ico" # Output file base name for HTML help builder. -htmlhelp_basename = 'AdafruitRfm69Librarydoc' +htmlhelp_basename = "AdafruitRfm69Librarydoc" # -- Options for LaTeX output --------------------------------------------- latex_elements = { - # The paper size ('letterpaper' or 'a4paper'). - # - # 'papersize': 'letterpaper', - - # The font size ('10pt', '11pt' or '12pt'). - # - # 'pointsize': '10pt', - - # Additional stuff for the LaTeX preamble. - # - # 'preamble': '', - - # Latex figure (float) alignment - # - # 'figure_align': 'htbp', + # The paper size ('letterpaper' or 'a4paper'). + # + # 'papersize': 'letterpaper', + # The font size ('10pt', '11pt' or '12pt'). + # + # 'pointsize': '10pt', + # Additional stuff for the LaTeX preamble. + # + # 'preamble': '', + # Latex figure (float) alignment + # + # 'figure_align': 'htbp', } # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, # author, documentclass [howto, manual, or own class]). latex_documents = [ - (master_doc, 'AdafruitRFM69Library.tex', u'AdafruitRFM69 Library Documentation', - author, 'manual'), + ( + master_doc, + "AdafruitRFM69Library.tex", + u"AdafruitRFM69 Library Documentation", + author, + "manual", + ), ] # -- Options for manual page output --------------------------------------- @@ -140,8 +151,13 @@ # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). man_pages = [ - (master_doc, 'AdafruitRFM69library', u'Adafruit RFM69 Library Documentation', - [author], 1) + ( + master_doc, + "AdafruitRFM69library", + u"Adafruit RFM69 Library Documentation", + [author], + 1, + ) ] # -- Options for Texinfo output ------------------------------------------- @@ -150,7 +166,13 @@ # (source start file, target name, title, author, # dir menu entry, description, category) texinfo_documents = [ - (master_doc, 'AdafruitRFM69Library', u'Adafruit RFM69 Library Documentation', - author, 'AdafruitRFM69Library', 'One line description of project.', - 'Miscellaneous'), + ( + master_doc, + "AdafruitRFM69Library", + u"Adafruit RFM69 Library Documentation", + author, + "AdafruitRFM69Library", + "One line description of project.", + "Miscellaneous", + ), ] diff --git a/examples/rfm69_rpi_interrupt.py b/examples/rfm69_rpi_interrupt.py index 3e1fec8..2dc02aa 100644 --- a/examples/rfm69_rpi_interrupt.py +++ b/examples/rfm69_rpi_interrupt.py @@ -12,23 +12,27 @@ # setup interrupt callback function def rfm69_callback(rfm69_irq): - global packet_received #pylint: disable=global-statement - print("IRQ detected on pin {0} payload_ready {1} ".format(rfm69_irq, rfm69.payload_ready)) + global packet_received # pylint: disable=global-statement + print( + "IRQ detected on pin {0} payload_ready {1} ".format( + rfm69_irq, rfm69.payload_ready + ) + ) # see if this was a payload_ready interrupt ignore if not if rfm69.payload_ready: - packet = rfm69.receive(timeout = None) + packet = rfm69.receive(timeout=None) if packet is not None: # Received a packet! packet_received = True # Print out the raw bytes of the packet: - print('Received (raw bytes): {0}'.format(packet)) + print("Received (raw bytes): {0}".format(packet)) print([hex(x) for x in packet]) - print('RSSI: {0}'.format(rfm69.last_rssi)) + print("RSSI: {0}".format(rfm69.last_rssi)) # Define radio parameters. RADIO_FREQ_MHZ = 915.0 # Frequency of the radio in Mhz. Must match your - # module! Can be a value like 915.0, 433.0, etc. +# module! Can be a value like 915.0, 433.0, etc. # Define pins connected to the chip, use these if wiring up the breakout according to the guide: CS = digitalio.DigitalInOut(board.CE1) @@ -42,28 +46,30 @@ def rfm69_callback(rfm69_irq): # Optionally set an encryption key (16 byte AES key). MUST match both # on the transmitter and receiver (or be set to None to disable/the default). -rfm69.encryption_key = b'\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08' +rfm69.encryption_key = ( + b"\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08" +) # Print out some chip state: -print('Temperature: {0}C'.format(rfm69.temperature)) -print('Frequency: {0}mhz'.format(rfm69.frequency_mhz)) -print('Bit rate: {0}kbit/s'.format(rfm69.bitrate/1000)) -print('Frequency deviation: {0}hz'.format(rfm69.frequency_deviation)) +print("Temperature: {0}C".format(rfm69.temperature)) +print("Frequency: {0}mhz".format(rfm69.frequency_mhz)) +print("Bit rate: {0}kbit/s".format(rfm69.bitrate / 1000)) +print("Frequency deviation: {0}hz".format(rfm69.frequency_deviation)) # configure the interrupt pin and event handling. RFM69_G0 = 22 io.setmode(io.BCM) -io.setup(RFM69_G0, io.IN,pull_up_down=io.PUD_DOWN) # activate input -io.add_event_detect(RFM69_G0,io.RISING) -io.add_event_callback(RFM69_G0,rfm69_callback) +io.setup(RFM69_G0, io.IN, pull_up_down=io.PUD_DOWN) # activate input +io.add_event_detect(RFM69_G0, io.RISING) +io.add_event_callback(RFM69_G0, rfm69_callback) packet_received = False # Send a packet. Note you can only send a packet up to 60 bytes in length. # This is a limitation of the radio packet size, so if you need to send larger # amounts of data you will need to break it into smaller send calls. Each send # call will wait for the previous one to finish before continuing. -rfm69.send(bytes('Hello world!\r\n',"utf-8"), keep_listening = True) -print('Sent hello world message!') +rfm69.send(bytes("Hello world!\r\n", "utf-8"), keep_listening=True) +print("Sent hello world message!") # If you don't wawnt to send a message to start you can just start lintening # rmf69.listen() @@ -71,14 +77,14 @@ def rfm69_callback(rfm69_irq): # rate, in fact it can only receive and process one 60 byte packet at a time. # This means you should only use this for low bandwidth scenarios, like sending # and receiving a single message at a time. -print('Waiting for packets...') +print("Waiting for packets...") # the loop is where you can do any desire processing # the global variable packet_received can be used to determine if a packet was received. while True: # the sleep time is arbitrary since any incomming packe will trigger an interrupt # and be received. - time.sleep(.1) + time.sleep(0.1) if packet_received: - print('received message!') + print("received message!") packet_received = False diff --git a/examples/rfm69_simpletest.py b/examples/rfm69_simpletest.py index 2b7b849..bf93cef 100644 --- a/examples/rfm69_simpletest.py +++ b/examples/rfm69_simpletest.py @@ -11,15 +11,15 @@ # Define radio parameters. RADIO_FREQ_MHZ = 915.0 # Frequency of the radio in Mhz. Must match your - # module! Can be a value like 915.0, 433.0, etc. +# module! Can be a value like 915.0, 433.0, etc. # Define pins connected to the chip, use these if wiring up the breakout according to the guide: CS = digitalio.DigitalInOut(board.D5) RESET = digitalio.DigitalInOut(board.D6) # Or uncomment and instead use these if using a Feather M0 RFM69 board # and the appropriate CircuitPython build: -#CS = digitalio.DigitalInOut(board.RFM69_CS) -#RESET = digitalio.DigitalInOut(board.RFM69_RST) +# CS = digitalio.DigitalInOut(board.RFM69_CS) +# RESET = digitalio.DigitalInOut(board.RFM69_RST) # Define the onboard LED LED = digitalio.DigitalInOut(board.D13) @@ -33,43 +33,45 @@ # Optionally set an encryption key (16 byte AES key). MUST match both # on the transmitter and receiver (or be set to None to disable/the default). -rfm69.encryption_key = b'\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08' +rfm69.encryption_key = ( + b"\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08" +) # Print out some chip state: -print('Temperature: {0}C'.format(rfm69.temperature)) -print('Frequency: {0}mhz'.format(rfm69.frequency_mhz)) -print('Bit rate: {0}kbit/s'.format(rfm69.bitrate/1000)) -print('Frequency deviation: {0}hz'.format(rfm69.frequency_deviation)) +print("Temperature: {0}C".format(rfm69.temperature)) +print("Frequency: {0}mhz".format(rfm69.frequency_mhz)) +print("Bit rate: {0}kbit/s".format(rfm69.bitrate / 1000)) +print("Frequency deviation: {0}hz".format(rfm69.frequency_deviation)) # Send a packet. Note you can only send a packet up to 60 bytes in length. # This is a limitation of the radio packet size, so if you need to send larger # amounts of data you will need to break it into smaller send calls. Each send # call will wait for the previous one to finish before continuing. -rfm69.send(bytes('Hello world!\r\n',"utf-8")) -print('Sent hello world message!') +rfm69.send(bytes("Hello world!\r\n", "utf-8")) +print("Sent hello world message!") # Wait to receive packets. Note that this library can't receive data at a fast # rate, in fact it can only receive and process one 60 byte packet at a time. # This means you should only use this for low bandwidth scenarios, like sending # and receiving a single message at a time. -print('Waiting for packets...') +print("Waiting for packets...") while True: packet = rfm69.receive() # Optionally change the receive timeout from its default of 0.5 seconds: - #packet = rfm69.receive(timeout=5.0) + # packet = rfm69.receive(timeout=5.0) # If no packet was received during the timeout then None is returned. if packet is None: # Packet has not been received LED.value = False - print('Received nothing! Listening again...') + print("Received nothing! Listening again...") else: # Received a packet! LED.value = True # Print out the raw bytes of the packet: - print('Received (raw bytes): {0}'.format(packet)) + print("Received (raw bytes): {0}".format(packet)) # And decode to ASCII text and print it too. Note that you always # receive raw bytes and need to convert to a text format like ASCII # if you intend to do string processing on your data. Make sure the # sending side is sending ASCII data before you try to decode! - packet_text = str(packet, 'ascii') - print('Received (ASCII): {0}'.format(packet_text)) + packet_text = str(packet, "ascii") + print("Received (ASCII): {0}".format(packet_text)) diff --git a/examples/rfm69_transmit.py b/examples/rfm69_transmit.py index edb2f84..39c9832 100644 --- a/examples/rfm69_transmit.py +++ b/examples/rfm69_transmit.py @@ -8,14 +8,14 @@ import adafruit_rfm69 # set the time interval (seconds) for sending packets -transmit_interval=10 +transmit_interval = 10 # Define radio parameters. -RADIO_FREQ_MHZ = 915.0 # Frequency of the radio in Mhz. Must match your - # module! Can be a value like 915.0, 433.0, etc. +RADIO_FREQ_MHZ = 915.0 # Frequency of the radio in Mhz. Must match your +# module! Can be a value like 915.0, 433.0, etc. # Define pins connected to the chip. -CS = digitalio.DigitalInOut(board.CE1) +CS = digitalio.DigitalInOut(board.CE1) RESET = digitalio.DigitalInOut(board.D25) # Initialize SPI bus. @@ -26,18 +26,20 @@ # Optionally set an encryption key (16 byte AES key). MUST match both # on the transmitter and receiver (or be set to None to disable/the default). -rfm69.encryption_key = b'\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08' +rfm69.encryption_key = ( + b"\x01\x02\x03\x04\x05\x06\x07\x08\x01\x02\x03\x04\x05\x06\x07\x08" +) # initialize counter counter = 0 -#send a broadcast mesage -rfm69.send(bytes("message number {}".format(counter),"UTF-8")) +# send a broadcast mesage +rfm69.send(bytes("message number {}".format(counter), "UTF-8")) # Wait to receive packets. -print('Waiting for packets...') -#initialize flag and timer -send_reading=False -time_now=time.monotonic() +print("Waiting for packets...") +# initialize flag and timer +send_reading = False +time_now = time.monotonic() while True: # Look for a new packet - wait up to 5 seconds: packet = rfm69.receive(timeout=5.0) @@ -45,12 +47,12 @@ if packet is not None: # Received a packet! # Print out the raw bytes of the packet: - print('Received (raw bytes): {0}'.format(packet)) + print("Received (raw bytes): {0}".format(packet)) # send reading after any packet received - if time.monotonic()-time_now>transmit_interval: - #reset timeer - time_now=time.monotonic() - #clear flag to send data - send_reading=False + if time.monotonic() - time_now > transmit_interval: + # reset timeer + time_now = time.monotonic() + # clear flag to send data + send_reading = False counter = counter + 1 - rfm69.send(bytes("message number {}".format(counter),"UTF-8")) + rfm69.send(bytes("message number {}".format(counter), "UTF-8")) diff --git a/setup.py b/setup.py index d4eefa8..7eadc3f 100644 --- a/setup.py +++ b/setup.py @@ -7,6 +7,7 @@ # Always prefer setuptools over distutils from setuptools import setup, find_packages + # To use a consistent encoding from codecs import open from os import path @@ -14,47 +15,38 @@ here = path.abspath(path.dirname(__file__)) # Get the long description from the README file -with open(path.join(here, 'README.rst'), encoding='utf-8') as f: +with open(path.join(here, "README.rst"), encoding="utf-8") as f: long_description = f.read() setup( - name='adafruit-circuitpython-rfm69', - + name="adafruit-circuitpython-rfm69", use_scm_version=True, - setup_requires=['setuptools_scm'], - - description='CircuitPython library for RFM69 packet radio.', + setup_requires=["setuptools_scm"], + description="CircuitPython library for RFM69 packet radio.", long_description=long_description, - long_description_content_type='text/x-rst', - + long_description_content_type="text/x-rst", # The project's main homepage. - url='https://github.com/adafruit/Adafruit_CircuitPython_RFM69', - + url="https://github.com/adafruit/Adafruit_CircuitPython_RFM69", # Author details - author='Adafruit Industries', - author_email='circuitpython@adafruit.com', - - install_requires=['Adafruit-Blinka', 'adafruit-circuitpython-busdevice'], - + author="Adafruit Industries", + author_email="circuitpython@adafruit.com", + install_requires=["Adafruit-Blinka", "adafruit-circuitpython-busdevice"], # Choose your license - license='MIT', - + license="MIT", # See https://pypi.python.org/pypi?%3Aaction=list_classifiers classifiers=[ - 'Development Status :: 3 - Alpha', - 'Intended Audience :: Developers', - 'Topic :: Software Development :: Libraries', - 'Topic :: System :: Hardware', - 'License :: OSI Approved :: MIT License', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.4', - 'Programming Language :: Python :: 3.5', + "Development Status :: 3 - Alpha", + "Intended Audience :: Developers", + "Topic :: Software Development :: Libraries", + "Topic :: System :: Hardware", + "License :: OSI Approved :: MIT License", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.4", + "Programming Language :: Python :: 3.5", ], - # What does your project relate to? - keywords='adafruit rfm69 packet radio hardware micropython circuitpython', - + keywords="adafruit rfm69 packet radio hardware micropython circuitpython", # You can just specify the packages manually here if your project is # simple. Or you can use find_packages(). - py_modules=['adafruit_rfm69'], + py_modules=["adafruit_rfm69"], ) From d02046377c7981b3953118e45c9e979930ea2dbe Mon Sep 17 00:00:00 2001 From: dherrada Date: Tue, 17 Mar 2020 16:20:54 -0400 Subject: [PATCH 2/2] Re-ran black --- adafruit_rfm69.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/adafruit_rfm69.py b/adafruit_rfm69.py index 4c2adc3..06627a4 100644 --- a/adafruit_rfm69.py +++ b/adafruit_rfm69.py @@ -750,8 +750,13 @@ def send( return not timed_out - def receive(self, timeout=0.5, keep_listening=True, with_header=False, - rx_filter=_RH_BROADCAST_ADDRESS): + def receive( + self, + timeout=0.5, + keep_listening=True, + with_header=False, + rx_filter=_RH_BROADCAST_ADDRESS, + ): """Wait to receive a packet from the receiver. Will wait for up to timeout_s amount of seconds for a packet to be received and decoded. If a packet is found the payload bytes are returned, otherwise None is returned (which indicates the timeout elapsed with no