From: Melissa LeBlanc-Williams Date: Tue, 11 Feb 2025 21:31:55 +0000 (-0800) Subject: Run pre-commit and fix issues X-Git-Tag: 8.54.0^2~1 X-Git-Url: https://git.ayoreis.com/Adafruit_Blinka-hackapet.git/commitdiff_plain/84a62542704ff3636ddafbc8f65a99cc19a7a57c?ds=inline;hp=--cc Run pre-commit and fix issues --- 84a62542704ff3636ddafbc8f65a99cc19a7a57c diff --git a/LICENSES/BSD-3-Clause.txt b/LICENSES/BSD-3-Clause.txt new file mode 100644 index 0000000..c8e1a24 --- /dev/null +++ b/LICENSES/BSD-3-Clause.txt @@ -0,0 +1,25 @@ +Copyright (c) 2023, Raspberry Pi Ltd. +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of the copyright holder nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + diff --git a/src/adafruit_blinka/microcontroller/bcm283x/rotaryio.py b/src/adafruit_blinka/microcontroller/bcm283x/rotaryio.py index c4c54c3..d7017ff 100644 --- a/src/adafruit_blinka/microcontroller/bcm283x/rotaryio.py +++ b/src/adafruit_blinka/microcontroller/bcm283x/rotaryio.py @@ -1,6 +1,7 @@ # SPDX-FileCopyrightText: 2025 Melissa LeBlanc-Williams for Adafruit Industries # # SPDX-License-Identifier: MIT +# SPDX-License-Identifier: BSD-3-Clause """ `rotaryio` - Support for reading rotation sensors =========================================================== @@ -12,16 +13,20 @@ Raspberry Pi PIO implementation """ from __future__ import annotations -import microcontroller import array +import microcontroller + try: import adafruit_pioasm from adafruit_rp1pio import StateMachine -except ImportError: - raise("adafruit_pioasm and adafruit_rp1pio are required for this module") +except ImportError as exc: + raise ImportError( + "adafruit_pioasm and adafruit_rp1pio are required for this module" + ) from exc _n_read = 17 -_program = adafruit_pioasm.Program(""" +_program = adafruit_pioasm.Program( + """ ; ; Copyright (c) 2023 Raspberry Pi (Trading) Ltd. ; @@ -108,10 +113,12 @@ increment: increment_cont: mov y, ~y .wrap ; the .wrap here avoids one jump instruction and saves a cycle too -""") +""" +) _zero_y = adafruit_pioasm.assemble("set y 0") + class IncrementalEncoder: """ IncrementalEncoder determines the relative rotational position based on two series of @@ -131,8 +138,8 @@ class IncrementalEncoder: Always operates in "x4" mode (one count per quadrature edge) Assumes but does not check that pin_b is one above pin_a.""" - #if pin_b is not None and pin_b.id != pin_a.id + 1: - # raise ValueError("pin_b must be None or one higher than pin_a") + if pin_b is not None and pin_b.id != pin_a.id + 1: + raise ValueError("pin_b must be None or one higher than pin_a") try: self._sm = StateMachine( @@ -145,16 +152,17 @@ class IncrementalEncoder: auto_push=True, push_threshold=32, in_shift_right=False, - **_program.pio_kwargs + **_program.pio_kwargs, ) except RuntimeError as e: if "(error -13)" in e.args[0]: raise RuntimeError( "This feature requires a rules file to allow access to PIO. See " - "https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/using-neopixels-on-the-pi-5#updating-permissions-3189429" + "https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/" + "using-neopixels-on-the-pi-5#updating-permissions-3189429" ) from e raise - self._buffer = array.array('i',[0] * _n_read) + self._buffer = array.array("i", [0] * _n_read) self.divisor = divisor self._position = 0 @@ -175,24 +183,10 @@ class IncrementalEncoder: @property def position(self): - self._sm.readinto(self._buffer) # read N stale values + 1 fresh value + """The current position in terms of pulses. The number of pulses per rotation is defined + by the specific hardware and by the divisor.""" + self._sm.readinto(self._buffer) # read N stale values + 1 fresh value raw_position = self._buffer[-1] delta = int((raw_position - self._position * self.divisor) / self.divisor) self._position += delta return self._position - -''' -if __name__ == '__main__': - import board - # D17/D18 on header pins 11/12 - # GND on header pin 6/9 - # +5V on header pins 2/4 - q = IncrementalEncoder(board.D17) - old_position = q.position - while True: - position = q.position - if position != old_position: - delta = position - old_position - print(f"{position:8d} {delta=}") - old_position = position -''' \ No newline at end of file diff --git a/src/rotaryio.py b/src/rotaryio.py index d81ae5c..b8bf4cb 100644 --- a/src/rotaryio.py +++ b/src/rotaryio.py @@ -11,12 +11,16 @@ See `CircuitPython:rotaryio` in CircuitPython for more details. from adafruit_blinka.agnostic import detector +# pylint: disable=unused-import + # Import any board specific modules here if detector.board.any_raspberry_pi_5_board: from adafruit_blinka.microcontroller.bcm283x.rotaryio import IncrementalEncoder elif detector.board.any_embedded_linux: - #fall back to the generic linux implementation - from adafruit_blinka.microcontroller.generic_linux.rotaryio import IncrementalEncoder + # fall back to the generic linux implementation + from adafruit_blinka.microcontroller.generic_linux.rotaryio import ( + IncrementalEncoder, + ) else: # For non-Linux Boards, threading likely will work in the same way raise NotImplementedError("Board not supported")