Skip to content

Commit

Permalink
Merge pull request #11 from adafruit/pylint-update
Browse files Browse the repository at this point in the history
Ran black, updated to pylint 2.x
  • Loading branch information
kattni committed Mar 19, 2020
2 parents 049b3b2 + 88e6c82 commit 7c39b8e
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 154 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
118 changes: 61 additions & 57 deletions adafruit_mma8451.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,43 +42,43 @@
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MMA8451.git"


#pylint: disable=bad-whitespace
# pylint: disable=bad-whitespace
# Internal constants:
_MMA8451_DEFAULT_ADDRESS = const(0x1D)
_MMA8451_REG_OUT_X_MSB = const(0x01)
_MMA8451_REG_SYSMOD = const(0x0B)
_MMA8451_REG_WHOAMI = const(0x0D)
_MMA8451_REG_XYZ_DATA_CFG = const(0x0E)
_MMA8451_REG_PL_STATUS = const(0x10)
_MMA8451_REG_PL_CFG = const(0x11)
_MMA8451_REG_CTRL_REG1 = const(0x2A)
_MMA8451_REG_CTRL_REG2 = const(0x2B)
_MMA8451_REG_CTRL_REG4 = const(0x2D)
_MMA8451_REG_CTRL_REG5 = const(0x2E)
_MMA8451_DATARATE_MASK = const(0b111)
_SENSORS_GRAVITY_EARTH = 9.80665
_MMA8451_DEFAULT_ADDRESS = const(0x1D)
_MMA8451_REG_OUT_X_MSB = const(0x01)
_MMA8451_REG_SYSMOD = const(0x0B)
_MMA8451_REG_WHOAMI = const(0x0D)
_MMA8451_REG_XYZ_DATA_CFG = const(0x0E)
_MMA8451_REG_PL_STATUS = const(0x10)
_MMA8451_REG_PL_CFG = const(0x11)
_MMA8451_REG_CTRL_REG1 = const(0x2A)
_MMA8451_REG_CTRL_REG2 = const(0x2B)
_MMA8451_REG_CTRL_REG4 = const(0x2D)
_MMA8451_REG_CTRL_REG5 = const(0x2E)
_MMA8451_DATARATE_MASK = const(0b111)
_SENSORS_GRAVITY_EARTH = 9.80665

# External user-facing constants:
PL_PUF = 0 # Portrait, up, front
PL_PUB = 1 # Portrait, up, back
PL_PDF = 2 # Portrait, down, front
PL_PDB = 3 # Portrait, down, back
PL_LRF = 4 # Landscape, right, front
PL_LRB = 5 # Landscape, right, back
PL_LLF = 6 # Landscape, left, front
PL_LLB = 7 # Landscape, left, back
RANGE_8G = 0b10 # +/- 8g
RANGE_4G = 0b01 # +/- 4g (default value)
RANGE_2G = 0b00 # +/- 2g
DATARATE_800HZ = 0b000 # 800Hz
DATARATE_400HZ = 0b001 # 400Hz
DATARATE_200HZ = 0b010 # 200Hz
DATARATE_100HZ = 0b011 # 100Hz
DATARATE_50HZ = 0b100 # 50Hz
DATARATE_12_5HZ = 0b101 # 12.5Hz
DATARATE_6_25HZ = 0b110 # 6.25Hz
DATARATE_1_56HZ = 0b111 # 1.56Hz
#pylint: enable=bad-whitespace
PL_PUF = 0 # Portrait, up, front
PL_PUB = 1 # Portrait, up, back
PL_PDF = 2 # Portrait, down, front
PL_PDB = 3 # Portrait, down, back
PL_LRF = 4 # Landscape, right, front
PL_LRB = 5 # Landscape, right, back
PL_LLF = 6 # Landscape, left, front
PL_LLB = 7 # Landscape, left, back
RANGE_8G = 0b10 # +/- 8g
RANGE_4G = 0b01 # +/- 4g (default value)
RANGE_2G = 0b00 # +/- 2g
DATARATE_800HZ = 0b000 # 800Hz
DATARATE_400HZ = 0b001 # 400Hz
DATARATE_200HZ = 0b010 # 200Hz
DATARATE_100HZ = 0b011 # 100Hz
DATARATE_50HZ = 0b100 # 50Hz
DATARATE_12_5HZ = 0b101 # 12.5Hz
DATARATE_6_25HZ = 0b110 # 6.25Hz
DATARATE_1_56HZ = 0b111 # 1.56Hz
# pylint: enable=bad-whitespace


class MMA8451:
Expand All @@ -97,7 +97,7 @@ def __init__(self, i2c, *, address=_MMA8451_DEFAULT_ADDRESS):
self._device = i2c_device.I2CDevice(i2c, address)
# Verify device ID.
if self._read_u8(_MMA8451_REG_WHOAMI) != 0x1A:
raise RuntimeError('Failed to find MMA8451, check wiring!')
raise RuntimeError("Failed to find MMA8451, check wiring!")
# Reset and wait for chip to be ready.
self._write_u8(_MMA8451_REG_CTRL_REG2, 0x40)
while self._read_u8(_MMA8451_REG_CTRL_REG2) & 0x40 > 0:
Expand All @@ -122,12 +122,11 @@ def _read_into(self, address, buf, count=None):
# has at least 1 value. I don't trust the implicit true/false
# recommendation as it was not designed for bytearrays which may not
# follow that semantic. Ignore pylint's superfulous complaint.
assert len(buf) > 0 #pylint: disable=len-as-condition
assert len(buf) > 0 # pylint: disable=len-as-condition
if count is None:
count = len(buf)
with self._device as i2c:
i2c.write_then_readinto(bytes([address & 0xFF]), buf,
in_end=count)
i2c.write_then_readinto(bytes([address & 0xFF]), buf, in_end=count)

def _read_u8(self, address):
# Read an 8-bit unsigned value from the specified 8-bit address.
Expand All @@ -154,9 +153,9 @@ def range(self):
def range(self, val):
assert 0 <= val <= 2
reg1 = self._read_u8(_MMA8451_REG_CTRL_REG1)
self._write_u8(_MMA8451_REG_CTRL_REG1, 0x00) # deactivate
self._write_u8(_MMA8451_REG_CTRL_REG1, 0x00) # deactivate
self._write_u8(_MMA8451_REG_XYZ_DATA_CFG, val)
self._write_u8(_MMA8451_REG_CTRL_REG1, reg1 | 0x01) # activate
self._write_u8(_MMA8451_REG_CTRL_REG1, reg1 | 0x01) # activate

@property
def data_rate(self):
Expand All @@ -170,17 +169,16 @@ def data_rate(self):
- DATARATE_6_25HZ: 6.25Hz
- DATARATE_1_56HZ: 1.56Hz
"""
return (self._read_u8(_MMA8451_REG_CTRL_REG1) >> 3) & \
_MMA8451_DATARATE_MASK
return (self._read_u8(_MMA8451_REG_CTRL_REG1) >> 3) & _MMA8451_DATARATE_MASK

@data_rate.setter
def data_rate(self, val):
assert 0 <= val <= 7
ctl1 = self._read_u8(_MMA8451_REG_CTRL_REG1)
self._write_u8(_MMA8451_REG_CTRL_REG1, 0x00) # deactivate
ctl1 &= ~(_MMA8451_DATARATE_MASK << 3) # mask off bits
ctl1 |= (val << 3)
self._write_u8(_MMA8451_REG_CTRL_REG1, ctl1 | 0x01) # activate
self._write_u8(_MMA8451_REG_CTRL_REG1, 0x00) # deactivate
ctl1 &= ~(_MMA8451_DATARATE_MASK << 3) # mask off bits
ctl1 |= val << 3
self._write_u8(_MMA8451_REG_CTRL_REG1, ctl1 | 0x01) # activate

@property
def acceleration(self):
Expand All @@ -192,26 +190,32 @@ def acceleration(self):
# Read 6 bytes for 16-bit X, Y, Z values.
self._read_into(_MMA8451_REG_OUT_X_MSB, self._BUFFER, count=6)
# Reconstruct signed 16-bit integers.
x, y, z = struct.unpack('>hhh', self._BUFFER)
x, y, z = struct.unpack(">hhh", self._BUFFER)
x >>= 2
y >>= 2
z >>= 2
# Scale values based on current sensor range to get proper units.
_range = self.range
if _range == RANGE_8G:
return (x/1024.0*_SENSORS_GRAVITY_EARTH,
y/1024.0*_SENSORS_GRAVITY_EARTH,
z/1024.0*_SENSORS_GRAVITY_EARTH)
return (
x / 1024.0 * _SENSORS_GRAVITY_EARTH,
y / 1024.0 * _SENSORS_GRAVITY_EARTH,
z / 1024.0 * _SENSORS_GRAVITY_EARTH,
)
elif _range == RANGE_4G:
return (x/2048.0*_SENSORS_GRAVITY_EARTH,
y/2048.0*_SENSORS_GRAVITY_EARTH,
z/2048.0*_SENSORS_GRAVITY_EARTH)
return (
x / 2048.0 * _SENSORS_GRAVITY_EARTH,
y / 2048.0 * _SENSORS_GRAVITY_EARTH,
z / 2048.0 * _SENSORS_GRAVITY_EARTH,
)
elif _range == RANGE_2G:
return (x/4096.0*_SENSORS_GRAVITY_EARTH,
y/4096.0*_SENSORS_GRAVITY_EARTH,
z/4096.0*_SENSORS_GRAVITY_EARTH)
return (
x / 4096.0 * _SENSORS_GRAVITY_EARTH,
y / 4096.0 * _SENSORS_GRAVITY_EARTH,
z / 4096.0 * _SENSORS_GRAVITY_EARTH,
)
else:
raise RuntimeError('Unexpected range!')
raise RuntimeError("Unexpected range!")

@property
def orientation(self):
Expand Down
112 changes: 67 additions & 45 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,55 @@

import os
import sys
sys.path.insert(0, os.path.abspath('..'))

sys.path.insert(0, os.path.abspath(".."))

# -- General configuration ------------------------------------------------

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.intersphinx',
'sphinx.ext.todo',
"sphinx.ext.autodoc",
"sphinx.ext.intersphinx",
"sphinx.ext.todo",
]

# Uncomment the below if you use native CircuitPython modules such as
# digitalio, micropython and busio. List the modules you use. Without it, the
# autodoc module docs will fail to generate with a warning.
# autodoc_mock_imports = ["digitalio", "busio", "micropython", "adafruit_bus_device", "adafruit_bus_device.i2c_device"]

intersphinx_mapping = {'python': ('https://docs.python.org/3.4', None),'BusDevice': ('https://circuitpython.readthedocs.io/projects/bus_device/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 MMA8451 Library'
copyright = u'2017 Tony DiCola'
author = u'Tony DiCola'
project = u"Adafruit MMA8451 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.
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -80,62 +88,70 @@
# 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"]

# Output file base name for HTML help builder.
htmlhelp_basename = 'AdafruitMma8451Librarydoc'
htmlhelp_basename = "AdafruitMma8451Librarydoc"

# -- 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, 'AdafruitMMA8451Library.tex', u'AdafruitMMA8451 Library Documentation',
author, 'manual'),
(
master_doc,
"AdafruitMMA8451Library.tex",
u"AdafruitMMA8451 Library Documentation",
author,
"manual",
),
]

# -- Options for manual page output ---------------------------------------

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
(master_doc, 'AdafruitMMA8451library', u'Adafruit MMA8451 Library Documentation',
[author], 1)
(
master_doc,
"AdafruitMMA8451library",
u"Adafruit MMA8451 Library Documentation",
[author],
1,
)
]

# -- Options for Texinfo output -------------------------------------------
Expand All @@ -144,7 +160,13 @@
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
(master_doc, 'AdafruitMMA8451Library', u'Adafruit MMA8451 Library Documentation',
author, 'AdafruitMMA8451Library', 'One line description of project.',
'Miscellaneous'),
(
master_doc,
"AdafruitMMA8451Library",
u"Adafruit MMA8451 Library Documentation",
author,
"AdafruitMMA8451Library",
"One line description of project.",
"Miscellaneous",
),
]

0 comments on commit 7c39b8e

Please sign in to comment.