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_mcp4728.py b/adafruit_mcp4728.py index c0a00b3..14104dd 100644 --- a/adafruit_mcp4728.py +++ b/adafruit_mcp4728.py @@ -53,6 +53,7 @@ _MCP4728_CH_A_MULTI_EEPROM = 0x50 + class CV: """struct helper""" @@ -73,14 +74,15 @@ def is_valid(cls, value): "Returns true if the given value is a member of the CV" return value in cls.string + class Vref(CV): """Options for ``vref``""" - pass #pylint: disable=unnecessary-pass -Vref.add_values(( - ('VDD', 0, "VDD", None), - ('INTERNAL', 1, "Internal 2.048V", None), -)) + pass # pylint: disable=unnecessary-pass + + +Vref.add_values((("VDD", 0, "VDD", None), ("INTERNAL", 1, "Internal 2.048V", None),)) + class MCP4728: """Helper library for the Microchip MCP4728 I2C 12-bit Quad DAC. @@ -103,9 +105,9 @@ def __init__(self, i2c_bus, address=_MCP4728_DEFAULT_ADDRESS): @staticmethod def _get_flags(high_byte): - vref = (high_byte & 1<<7) > 0 - gain = (high_byte & 1<<4) > 0 - power_state = (high_byte & 0b011<<5)>>5 + vref = (high_byte & 1 << 7) > 0 + gain = (high_byte & 1 << 4) > 0 + power_state = (high_byte & 0b011 << 5) >> 5 return (vref, gain, power_state) @staticmethod @@ -122,7 +124,9 @@ def _read_registers(self): # and 3 for the eeprom. Here we only care about the output regoster so we throw out # the eeprom values as 'n/a' current_values = [] - for header, high_byte, low_byte, na_1, na_2, na_3 in self._chunk(buf, 6):#pylint:disable=unused-variable + # pylint:disable=unused-variable + for header, high_byte, low_byte, na_1, na_2, na_3 in self._chunk(buf, 6): + # pylint:enable=unused-variable value = (high_byte & 0b00001111) << 8 | low_byte vref, gain, power_state = self._get_flags(high_byte) current_values.append((value, vref, gain, power_state)) @@ -149,15 +153,15 @@ def _write_multi_eeprom(self, byte_list): with self.i2c_device as i2c: i2c.write(buf) - sleep(0.015) # the better to write you with + sleep(0.015) # the better to write you with def sync_vrefs(self): """Syncs the driver's vref state with the DAC""" gain_setter_command = 0b10000000 - gain_setter_command |= (self.channel_a.vref<<3) - gain_setter_command |= (self.channel_b.vref<<2) - gain_setter_command |= (self.channel_c.vref<<1) - gain_setter_command |= (self.channel_d.vref) + gain_setter_command |= self.channel_a.vref << 3 + gain_setter_command |= self.channel_b.vref << 2 + gain_setter_command |= self.channel_c.vref << 1 + gain_setter_command |= self.channel_d.vref buf = bytearray(1) pack_into(">B", buf, 0, gain_setter_command) @@ -168,10 +172,10 @@ def sync_gains(self): """Syncs the driver's gain state with the DAC""" sync_setter_command = 0b11000000 - sync_setter_command |= (self.channel_a.gain<<3) - sync_setter_command |= (self.channel_b.gain<<2) - sync_setter_command |= (self.channel_c.gain<<1) - sync_setter_command |= (self.channel_d.gain) + sync_setter_command |= self.channel_a.gain << 3 + sync_setter_command |= self.channel_b.gain << 2 + sync_setter_command |= self.channel_c.gain << 1 + sync_setter_command |= self.channel_d.gain buf = bytearray(1) pack_into(">B", buf, 0, sync_setter_command) @@ -183,8 +187,8 @@ def _set_value(self, channel): channel_bytes = self._generate_bytes_with_flags(channel) - write_command_byte = 0b01000000 # 0 1 0 0 0 DAC1 DAC0 UDAC - write_command_byte |= (channel.channel_index<<1) + write_command_byte = 0b01000000 # 0 1 0 0 0 DAC1 DAC0 UDAC + write_command_byte |= channel.channel_index << 1 output_buffer = bytearray([write_command_byte]) output_buffer.extend(channel_bytes) @@ -206,23 +210,25 @@ def _generate_bytes_with_flags(channel): def _chunk(big_list, chunk_size): """Divides a given list into `chunk_size` sized chunks""" for i in range(0, len(big_list), chunk_size): - yield big_list[i:i+chunk_size] + yield big_list[i : i + chunk_size] + class Channel: """An instance of a single channel for a multi-channel DAC. **All available channels are created automatically and should not be created by the user**""" + def __init__(self, dac_instance, cache_page, index): - self._vref = cache_page['vref'] - self._gain = cache_page['gain'] - self._raw_value = cache_page['value'] + self._vref = cache_page["vref"] + self._gain = cache_page["gain"] + self._raw_value = cache_page["value"] self._dac = dac_instance self.channel_index = index @property def normalized_value(self): """The DAC value as a floating point number in the range 0.0 to 1.0.""" - return self.raw_value / (2**12-1) + return self.raw_value / (2 ** 12 - 1) @normalized_value.setter def normalized_value(self, value): @@ -235,12 +241,14 @@ def normalized_value(self, value): def value(self): """The 16-bit scaled current value for the channel. Note that the MCP4728 is a 12-bit piece so quantization errors will occour""" - return self.normalized_value * (2**16-1) + return self.normalized_value * (2 ** 16 - 1) @value.setter def value(self, value): - if value < 0 or value > (2**16-1): - raise AttributeError("`value` must be a 16-bit integer between 0 and %s"%(2**16-1)) + if value < 0 or value > (2 ** 16 - 1): + raise AttributeError( + "`value` must be a 16-bit integer between 0 and %s" % (2 ** 16 - 1) + ) # Scale from 16-bit to 12-bit value (quantization errors will occur!). self.raw_value = value >> 4 @@ -252,12 +260,14 @@ def raw_value(self): @raw_value.setter def raw_value(self, value): - if value < 0 or value > (2**12-1): - raise AttributeError("`raw_value` must be a 12-bit integer between 0 and %s"%(2**12-1)) + if value < 0 or value > (2 ** 12 - 1): + raise AttributeError( + "`raw_value` must be a 12-bit integer between 0 and %s" % (2 ** 12 - 1) + ) self._raw_value = value # disabling the protected access warning here because making it public would be # more confusing - self._dac._set_value(self) #pylint:disable=protected-access + self._dac._set_value(self) # pylint:disable=protected-access @property def gain(self): @@ -272,7 +282,7 @@ def gain(self): def gain(self, value): if not value in (1, 2): raise AttributeError("`gain` must be 1 or 2") - self._gain = value-1 + self._gain = value - 1 self._dac.sync_gains() @property diff --git a/docs/conf.py b/docs/conf.py index a5a9d4f..4425f97 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,10 +11,10 @@ # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom # ones. extensions = [ - 'sphinx.ext.autodoc', - 'sphinx.ext.intersphinx', - 'sphinx.ext.napoleon', - 'sphinx.ext.todo', + "sphinx.ext.autodoc", + "sphinx.ext.intersphinx", + "sphinx.ext.napoleon", + "sphinx.ext.todo", ] # TODO: Please Read! @@ -23,30 +24,40 @@ autodoc_mock_imports = ["adafruit_bus_device"] - -intersphinx_mapping = {'python': ('https://docs.python.org/3.4', None),'BusDevice': ('https://circuitpython.readthedocs.io/projects/busdevice/en/latest/', None),'Register': ('https://circuitpython.readthedocs.io/projects/register/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, + ), + "Register": ( + "https://circuitpython.readthedocs.io/projects/register/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 MCP4728 Library' -copyright = u'2019 Bryan Siepert' -author = u'Bryan Siepert' +project = u"Adafruit MCP4728 Library" +copyright = u"2019 Bryan Siepert" +author = u"Bryan Siepert" # 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. @@ -58,7 +69,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. @@ -70,7 +81,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 @@ -85,59 +96,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 = 'AdafruitMcp4728Librarydoc' +htmlhelp_basename = "AdafruitMcp4728Librarydoc" # -- 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, 'AdafruitMCP4728Library.tex', u'AdafruitMCP4728 Library Documentation', - author, 'manual'), + ( + master_doc, + "AdafruitMCP4728Library.tex", + u"AdafruitMCP4728 Library Documentation", + author, + "manual", + ), ] # -- Options for manual page output --------------------------------------- @@ -145,8 +159,13 @@ # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). man_pages = [ - (master_doc, 'AdafruitMCP4728library', u'Adafruit MCP4728 Library Documentation', - [author], 1) + ( + master_doc, + "AdafruitMCP4728library", + u"Adafruit MCP4728 Library Documentation", + [author], + 1, + ) ] # -- Options for Texinfo output ------------------------------------------- @@ -155,7 +174,13 @@ # (source start file, target name, title, author, # dir menu entry, description, category) texinfo_documents = [ - (master_doc, 'AdafruitMCP4728Library', u'Adafruit MCP4728 Library Documentation', - author, 'AdafruitMCP4728Library', 'One line description of project.', - 'Miscellaneous'), + ( + master_doc, + "AdafruitMCP4728Library", + u"Adafruit MCP4728 Library Documentation", + author, + "AdafruitMCP4728Library", + "One line description of project.", + "Miscellaneous", + ), ] diff --git a/examples/mcp4728_simpletest.py b/examples/mcp4728_simpletest.py index 392841f..b50eb30 100644 --- a/examples/mcp4728_simpletest.py +++ b/examples/mcp4728_simpletest.py @@ -3,9 +3,9 @@ import adafruit_mcp4728 i2c = busio.I2C(board.SCL, board.SDA) -mcp4728 = adafruit_mcp4728.MCP4728(i2c) +mcp4728 = adafruit_mcp4728.MCP4728(i2c) -mcp4728.channel_a.value = 65535 # Voltage = VDD -mcp4728.channel_b.value = int(65535/2) # VDD/2 -mcp4728.channel_c.value = int(65535/4) # VDD/4 -mcp4728.channel_d.value = 0 # 0V +mcp4728.channel_a.value = 65535 # Voltage = VDD +mcp4728.channel_b.value = int(65535 / 2) # VDD/2 +mcp4728.channel_c.value = int(65535 / 4) # VDD/4 +mcp4728.channel_d.value = 0 # 0V diff --git a/examples/mcp4728_vref_example.py b/examples/mcp4728_vref_example.py index a4bfb51..8c5772c 100644 --- a/examples/mcp4728_vref_example.py +++ b/examples/mcp4728_vref_example.py @@ -4,19 +4,21 @@ import adafruit_mcp4728 i2c = busio.I2C(board.SCL, board.SDA) -mcp4728 = adafruit_mcp4728.MCP4728(i2c) +mcp4728 = adafruit_mcp4728.MCP4728(i2c) -FULL_VREF_RAW_VALUE = 4095 +FULL_VREF_RAW_VALUE = 4095 -#pylint: disable=no-member -mcp4728.channel_a.raw_value = int(FULL_VREF_RAW_VALUE/2) # VDD/2 -mcp4728.channel_a.vref = adafruit_mcp4728.Vref.VDD # sets the channel to scale between 0v and VDD +# pylint: disable=no-member +mcp4728.channel_a.raw_value = int(FULL_VREF_RAW_VALUE / 2) # VDD/2 +mcp4728.channel_a.vref = ( + adafruit_mcp4728.Vref.VDD +) # sets the channel to scale between 0v and VDD -mcp4728.channel_b.raw_value = int(FULL_VREF_RAW_VALUE/2) # VDD/2 +mcp4728.channel_b.raw_value = int(FULL_VREF_RAW_VALUE / 2) # VDD/2 mcp4728.channel_b.vref = adafruit_mcp4728.Vref.INTERNAL mcp4728.channel_b.gain = 1 -mcp4728.channel_c.raw_value = int(FULL_VREF_RAW_VALUE/2) # VDD/2 +mcp4728.channel_c.raw_value = int(FULL_VREF_RAW_VALUE / 2) # VDD/2 mcp4728.channel_c.vref = adafruit_mcp4728.Vref.INTERNAL mcp4728.channel_c.gain = 2 diff --git a/setup.py b/setup.py index 1e311ed..986390a 100644 --- a/setup.py +++ b/setup.py @@ -6,6 +6,7 @@ """ from setuptools import setup, find_packages + # To use a consistent encoding from codecs import open from os import path @@ -13,52 +14,40 @@ 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-mcp4728', - + name="adafruit-circuitpython-mcp4728", use_scm_version=True, - setup_requires=['setuptools_scm'], - - description='Helper library for the MCP4728 I2C 12-bit Quad DAC', + setup_requires=["setuptools_scm"], + description="Helper library for the MCP4728 I2C 12-bit Quad DAC", 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_MCP4728', - + url="https://github.com/adafruit/Adafruit_CircuitPython_MCP4728", # 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 blinka circuitpython micropython mcp4728 dac 12-bit quad i2c', - + keywords="adafruit blinka circuitpython micropython mcp4728 dac 12-bit quad i2c", # You can just specify the packages manually here if your project is # simple. Or you can use find_packages(). # TODO: IF LIBRARY FILES ARE A PACKAGE FOLDER, # CHANGE `py_modules=['...']` TO `packages=['...']` - py_modules=['adafruit_mcp4728'], + py_modules=["adafruit_mcp4728"], )