diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..21c125c --- /dev/null +++ b/.gitattributes @@ -0,0 +1,11 @@ +# SPDX-FileCopyrightText: 2024 Justin Myers for Adafruit Industries +# +# SPDX-License-Identifier: Unlicense + +.py text eol=lf +.rst text eol=lf +.txt text eol=lf +.yaml text eol=lf +.toml text eol=lf +.license text eol=lf +.md text eol=lf diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 70ade69..f27b786 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,42 +1,22 @@ # SPDX-FileCopyrightText: 2020 Diego Elio Pettenò +# SPDX-FileCopyrightText: 2024 Justin Myers # # SPDX-License-Identifier: Unlicense repos: - - repo: https://github.com/python/black - rev: 23.3.0 - hooks: - - id: black - - repo: https://github.com/fsfe/reuse-tool - rev: v1.1.2 - hooks: - - id: reuse - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.4.0 + rev: v4.5.0 hooks: - id: check-yaml - id: end-of-file-fixer - id: trailing-whitespace - - repo: https://github.com/pycqa/pylint - rev: v2.17.4 + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.3.4 hooks: - - id: pylint - name: pylint (library code) - types: [python] - args: - - --disable=consider-using-f-string - exclude: "^(docs/|examples/|tests/|setup.py$)" - - id: pylint - name: pylint (example code) - description: Run pylint rules on "examples/*.py" files - types: [python] - files: "^examples/" - args: - - --disable=missing-docstring,invalid-name,consider-using-f-string,duplicate-code - - id: pylint - name: pylint (test code) - description: Run pylint rules on "tests/*.py" files - types: [python] - files: "^tests/" - args: - - --disable=missing-docstring,consider-using-f-string,duplicate-code + - id: ruff-format + - id: ruff + args: ["--fix"] + - repo: https://github.com/fsfe/reuse-tool + rev: v3.0.1 + hooks: + - id: reuse diff --git a/README.rst b/README.rst index 2794828..44c7360 100644 --- a/README.rst +++ b/README.rst @@ -13,9 +13,9 @@ Introduction :target: https://github.com/adafruit/Adafruit_CircuitPython_VS1053/actions/ :alt: Build Status -.. image:: https://img.shields.io/badge/code%20style-black-000000.svg - :target: https://github.com/psf/black - :alt: Code Style: Black +.. image:: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json + :target: https://github.com/astral-sh/ruff + :alt: Code Style: Ruff Driver for interacting and playing media files with the VS1053 audio codec over a SPI connection. diff --git a/adafruit_vs1053.py b/adafruit_vs1053.py index 3661f65..60aefd8 100644 --- a/adafruit_vs1053.py +++ b/adafruit_vs1053.py @@ -41,15 +41,17 @@ """ import time + import digitalio -from micropython import const from adafruit_bus_device.spi_device import SPIDevice +from micropython import const try: from typing import Optional + + from busio import SPI from circuitpython_typing import ReadableBuffer from microcontroller import Pin - from busio import SPI except ImportError: pass @@ -104,9 +106,7 @@ def __init__( # pylint: disable=invalid-name ) -> None: # Create SPI device for VS1053 self._cs = digitalio.DigitalInOut(cs) - self._vs1053_spi = SPIDevice( - spi, self._cs, baudrate=_COMMAND_BAUDRATE, polarity=0, phase=0 - ) + self._vs1053_spi = SPIDevice(spi, self._cs, baudrate=_COMMAND_BAUDRATE, polarity=0, phase=0) # Setup control lines. self._xdcs = digitalio.DigitalInOut(xdcs) self._xdcs.switch_to_output(value=True) @@ -147,9 +147,7 @@ def _sci_read(self, address: int) -> int: def soft_reset(self) -> None: """Perform a quick soft reset of the VS1053.""" - self._sci_write( - _VS1053_REG_MODE, _VS1053_MODE_SM_SDINEW | _VS1053_MODE_SM_RESET - ) + self._sci_write(_VS1053_REG_MODE, _VS1053_MODE_SM_SDINEW | _VS1053_MODE_SM_RESET) time.sleep(0.1) def reset(self) -> None: @@ -203,9 +201,7 @@ def start_playback(self) -> None: buffers of music data to the play_data function. """ # Reset playback. - self._sci_write( - _VS1053_REG_MODE, _VS1053_MODE_SM_LINE1 | _VS1053_MODE_SM_SDINEW - ) + self._sci_write(_VS1053_REG_MODE, _VS1053_MODE_SM_LINE1 | _VS1053_MODE_SM_SDINEW) # Resync. self._sci_write(_VS1053_REG_WRAMADDR, 0x1E29) self._sci_write(_VS1053_REG_WRAM, 0) @@ -219,9 +215,7 @@ def stop_playback(self) -> None: _VS1053_MODE_SM_LINE1 | _VS1053_MODE_SM_SDINEW | _VS1053_MODE_SM_CANCEL, ) - def play_data( - self, data_buffer: ReadableBuffer, start: int = 0, end: Optional[int] = None - ): + def play_data(self, data_buffer: ReadableBuffer, start: int = 0, end: Optional[int] = None): """Send a buffer of file data to the VS1053 for playback. Make sure the ready_for_data property is True before calling! """ diff --git a/docs/conf.py b/docs/conf.py index dfb7ba8..747395a 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -1,12 +1,10 @@ -# -*- coding: utf-8 -*- - # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # # SPDX-License-Identifier: MIT +import datetime import os import sys -import datetime sys.path.insert(0, os.path.abspath("..")) @@ -56,9 +54,7 @@ creation_year = "2018" current_year = str(datetime.datetime.now().year) year_duration = ( - current_year - if current_year == creation_year - else creation_year + " - " + current_year + current_year if current_year == creation_year else creation_year + " - " + current_year ) copyright = year_duration + " Tony DiCola" author = "Tony DiCola" diff --git a/examples/vs1053_MIDI.py b/examples/vs1053_MIDI.py index d26ef8a..5d4f7e5 100755 --- a/examples/vs1053_MIDI.py +++ b/examples/vs1053_MIDI.py @@ -2,6 +2,7 @@ # SPDX-License-Identifier: MIT import time + import board import busio diff --git a/examples/vs1053_simpletest.py b/examples/vs1053_simpletest.py index 4144b0b..d99d217 100644 --- a/examples/vs1053_simpletest.py +++ b/examples/vs1053_simpletest.py @@ -10,15 +10,14 @@ # We'll need to look into interrupt support perhaps to monitor DREQ like in the # arduino library. Basic sine wave playback does however work and monitoring # of attributes like status register and other VS1053 state is accessible. +import adafruit_sdcard import board import busio import digitalio import storage -import adafruit_sdcard import adafruit_vs1053 - # Define pins connected to VS1053: # For FeatherWing with Feather M0: @@ -71,7 +70,7 @@ # the VS1053 making static, stopping, and eventually requiring a hard reset. # We'll need to look into interrupt support perhaps to monitor DREQ like in the # arduino library. -print("Playing {}...".format(PLAYBACK_FILE)) +print(f"Playing {PLAYBACK_FILE}...") vs1053.start_playback() with open(PLAYBACK_FILE, "rb") as infile: music_data = infile.read(BUFFER_SIZE) diff --git a/ruff.toml b/ruff.toml new file mode 100644 index 0000000..db37c83 --- /dev/null +++ b/ruff.toml @@ -0,0 +1,99 @@ +# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries +# +# SPDX-License-Identifier: MIT + +target-version = "py38" +line-length = 100 + +[lint] +select = ["I", "PL", "UP"] + +extend-select = [ + "D419", # empty-docstring + "E501", # line-too-long + "W291", # trailing-whitespace + "PLC0414", # useless-import-alias + "PLC2401", # non-ascii-name + "PLC2801", # unnecessary-dunder-call + "PLC3002", # unnecessary-direct-lambda-call + "E999", # syntax-error + "PLE0101", # return-in-init + "F706", # return-outside-function + "F704", # yield-outside-function + "PLE0116", # continue-in-finally + "PLE0117", # nonlocal-without-binding + "PLE0241", # duplicate-bases + "PLE0302", # unexpected-special-method-signature + "PLE0604", # invalid-all-object + "PLE0605", # invalid-all-format + "PLE0643", # potential-index-error + "PLE0704", # misplaced-bare-raise + "PLE1141", # dict-iter-missing-items + "PLE1142", # await-outside-async + "PLE1205", # logging-too-many-args + "PLE1206", # logging-too-few-args + "PLE1307", # bad-string-format-type + "PLE1310", # bad-str-strip-call + "PLE1507", # invalid-envvar-value + "PLE2502", # bidirectional-unicode + "PLE2510", # invalid-character-backspace + "PLE2512", # invalid-character-sub + "PLE2513", # invalid-character-esc + "PLE2514", # invalid-character-nul + "PLE2515", # invalid-character-zero-width-space + "PLR0124", # comparison-with-itself + "PLR0202", # no-classmethod-decorator + "PLR0203", # no-staticmethod-decorator + "UP004", # useless-object-inheritance + "PLR0206", # property-with-parameters + "PLR0904", # too-many-public-methods + "PLR0911", # too-many-return-statements + "PLR0912", # too-many-branches + "PLR0913", # too-many-arguments + "PLR0914", # too-many-locals + "PLR0915", # too-many-statements + "PLR0916", # too-many-boolean-expressions + "PLR1702", # too-many-nested-blocks + "PLR1704", # redefined-argument-from-local + "PLR1711", # useless-return + "C416", # unnecessary-comprehension + "PLR1733", # unnecessary-dict-index-lookup + "PLR1736", # unnecessary-list-index-lookup + + # ruff reports this rule is unstable + #"PLR6301", # no-self-use + + "PLW0108", # unnecessary-lambda + "PLW0120", # useless-else-on-loop + "PLW0127", # self-assigning-variable + "PLW0129", # assert-on-string-literal + "B033", # duplicate-value + "PLW0131", # named-expr-without-context + "PLW0245", # super-without-brackets + "PLW0406", # import-self + "PLW0602", # global-variable-not-assigned + "PLW0603", # global-statement + "PLW0604", # global-at-module-level + + # fails on the try: import typing used by libraries + #"F401", # unused-import + + "F841", # unused-variable + "E722", # bare-except + "PLW0711", # binary-op-exception + "PLW1501", # bad-open-mode + "PLW1508", # invalid-envvar-default + "PLW1509", # subprocess-popen-preexec-fn + "PLW2101", # useless-with-lock + "PLW3301", # nested-min-max +] + +ignore = [ + "PLR2004", # magic-value-comparison + "UP030", # format literals + "PLW1514", # unspecified-encoding + +] + +[format] +line-ending = "lf"