Skip to content

Ran black, updated to pylint 2.x #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
61 changes: 41 additions & 20 deletions adafruit_irremote.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,18 @@
__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_IRRemote.git"


class IRDecodeException(Exception):
"""Generic decode exception"""
pass


class IRNECRepeatException(Exception):
"""Exception when a NEC repeat is decoded"""
pass


class GenericDecode:
"""Generic decoding of infrared signals"""

def bin_data(self, pulses):
"""Compute bins of pulse lengths where pulses are +-25% of the average.

Expand All @@ -97,26 +98,30 @@ def bin_data(self, pulses):

for _, pulse in enumerate(pulses):
matchedbin = False
#print(pulse, end=": ")
# print(pulse, end=": ")
for b, pulse_bin in enumerate(bins):
if pulse_bin[0]*0.75 <= pulse <= pulse_bin[0]*1.25:
#print("matches bin")
if pulse_bin[0] * 0.75 <= pulse <= pulse_bin[0] * 1.25:
# print("matches bin")
bins[b][0] = (pulse_bin[0] + pulse) // 2 # avg em
bins[b][1] += 1 # track it
bins[b][1] += 1 # track it
matchedbin = True
break
if not matchedbin:
bins.append([pulse, 1])
#print(bins)
# print(bins)
return bins

def decode_bits(self, pulses):
"""Decode the pulses into bits."""
# pylint: disable=too-many-branches,too-many-statements

# special exception for NEC repeat code!
if ((len(pulses) == 3) and (8000 <= pulses[0] <= 10000) and
(2000 <= pulses[1] <= 3000) and (450 <= pulses[2] <= 700)):
if (
(len(pulses) == 3)
and (8000 <= pulses[0] <= 10000)
and (2000 <= pulses[1] <= 3000)
and (450 <= pulses[2] <= 700)
):
raise IRNECRepeatException()

if len(pulses) < 10:
Expand Down Expand Up @@ -151,34 +156,39 @@ def decode_bits(self, pulses):

if len(pulse_bins) == 1:
raise IRDecodeException("Pulses do not differ")
elif len(pulse_bins) > 2:
if len(pulse_bins) > 2:
raise IRDecodeException("Only mark & space handled")

mark = min(pulse_bins[0][0], pulse_bins[1][0])
space = max(pulse_bins[0][0], pulse_bins[1][0])

if outliers:
# skip outliers
pulses = [p for p in pulses if not
(outliers[0]*0.75) <= p <= (outliers[0]*1.25)]
pulses = [
p
for p in pulses
if not (outliers[0] * 0.75) <= p <= (outliers[0] * 1.25)
]
# convert marks/spaces to 0 and 1
for i, pulse_length in enumerate(pulses):
if (space*0.75) <= pulse_length <= (space*1.25):
if (space * 0.75) <= pulse_length <= (space * 1.25):
pulses[i] = False
elif (mark*0.75) <= pulse_length <= (mark*1.25):
elif (mark * 0.75) <= pulse_length <= (mark * 1.25):
pulses[i] = True
else:
raise IRDecodeException("Pulses outside mark/space")

# convert bits to bytes!
output = [0] * ((len(pulses)+7)//8)
output = [0] * ((len(pulses) + 7) // 8)
for i, pulse_length in enumerate(pulses):
output[i // 8] = output[i // 8] << 1
if pulse_length:
output[i // 8] |= 1
return output

def _read_pulses_non_blocking(self, input_pulses, max_pulse=10000, pulse_window=0.10):
def _read_pulses_non_blocking(
self, input_pulses, max_pulse=10000, pulse_window=0.10
):
"""Read out a burst of pulses without blocking until pulses stop for a specified
period (pulse_window), pruning pulses after a pulse longer than ``max_pulse``.

Expand Down Expand Up @@ -207,8 +217,15 @@ def _read_pulses_non_blocking(self, input_pulses, max_pulse=10000, pulse_window=
recent_count = 0
time.sleep(pulse_window)

def read_pulses(self, input_pulses, *, max_pulse=10000, blocking=True,
pulse_window=0.10, blocking_delay=0.10):
def read_pulses(
self,
input_pulses,
*,
max_pulse=10000,
blocking=True,
pulse_window=0.10,
blocking_delay=0.10
):
"""Read out a burst of pulses until pulses stop for a specified
period (pulse_window), pruning pulses after a pulse longer than ``max_pulse``.

Expand All @@ -221,14 +238,18 @@ def read_pulses(self, input_pulses, *, max_pulse=10000, blocking=True,
:param float blocking_delay: delay between pulse checks when blocking
"""
while True:
pulses = self._read_pulses_non_blocking(input_pulses, max_pulse, pulse_window)
pulses = self._read_pulses_non_blocking(
input_pulses, max_pulse, pulse_window
)
if blocking and pulses is None:
time.sleep(blocking_delay)
continue
return pulses


class GenericTransmit:
"""Generic infrared transmit class that handles encoding."""

def __init__(self, header, one, zero, trail):
self.header = header
self.one = one
Expand All @@ -241,7 +262,7 @@ def transmit(self, pulseout, data):
:param pulseio.PulseOut pulseout: PulseOut to transmit on
:param bytearray data: Data to transmit
"""
durations = array.array('H', [0] * (2 + len(data) * 8 * 2 + 1))
durations = array.array("H", [0] * (2 + len(data) * 8 * 2 + 1))
durations[0] = self.header[0]
durations[1] = self.header[1]
durations[-1] = self.trail
Expand Down
110 changes: 64 additions & 46 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,46 @@

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.viewcode',
"sphinx.ext.autodoc",
"sphinx.ext.intersphinx",
"sphinx.ext.viewcode",
]

intersphinx_mapping = {'python': ('https://docs.python.org/3.4', None),'CircuitPython': ('https://circuitpython.readthedocs.io/en/latest/', None)}
intersphinx_mapping = {
"python": ("https://docs.python.org/3.4", 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 IRREMOTE Library'
copyright = u'2017 Scott Shawcroft'
author = u'Scott Shawcroft'
project = u"Adafruit IRREMOTE Library"
copyright = u"2017 Scott Shawcroft"
author = u"Scott Shawcroft"

# 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 @@ -49,7 +53,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 @@ -61,7 +65,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 @@ -75,68 +79,76 @@
# 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 = 'AdafruitIRREMOTELibrarydoc'
htmlhelp_basename = "AdafruitIRREMOTELibrarydoc"

# -- 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, 'AdafruitIRREMOTELibrary.tex', u'Adafruit IRREMOTE Library Documentation',
author, 'manual'),
(
master_doc,
"AdafruitIRREMOTELibrary.tex",
u"Adafruit IRREMOTE 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, 'adafruitIRREMOTElibrary', u'Adafruit IRREMOTE Library Documentation',
[author], 1)
(
master_doc,
"adafruitIRREMOTElibrary",
u"Adafruit IRREMOTE Library Documentation",
[author],
1,
)
]

# -- Options for Texinfo output -------------------------------------------
Expand All @@ -145,7 +157,13 @@
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
(master_doc, 'AdafruitIRREMOTELibrary', u'Adafruit IRREMOTE Library Documentation',
author, 'AdafruitIRREMOTELibrary', 'One line description of project.',
'Miscellaneous'),
(
master_doc,
"AdafruitIRREMOTELibrary",
u"Adafruit IRREMOTE Library Documentation",
author,
"AdafruitIRREMOTELibrary",
"One line description of project.",
"Miscellaneous",
),
]
2 changes: 1 addition & 1 deletion examples/irremote_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
print("Decoded:", code)
except adafruit_irremote.IRNECRepeatException: # unusual short code!
print("NEC repeat!")
except adafruit_irremote.IRDecodeException as e: # failed to decode
except adafruit_irremote.IRDecodeException as e: # failed to decode
print("Failed to decode: ", e.args)

print("----------------------------")
7 changes: 4 additions & 3 deletions examples/irremote_transmit.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""IR transmit example using Circuit Playground Express"""
#pylint: disable-msg=no-member
# pylint: disable-msg=no-member
import time
import pulseio
import board
Expand All @@ -15,8 +15,9 @@
pwm = pulseio.PWMOut(board.IR_TX, frequency=38000, duty_cycle=2 ** 15)
pulseout = pulseio.PulseOut(pwm)
# Create an encoder that will take numbers and turn them into NEC IR pulses
encoder = adafruit_irremote.GenericTransmit(header=[9500, 4500], one=[550, 550],
zero=[550, 1700], trail=0)
encoder = adafruit_irremote.GenericTransmit(
header=[9500, 4500], one=[550, 550], zero=[550, 1700], trail=0
)

while True:
if button.value:
Expand Down
Loading