]> Repositories - Adafruit_Blinka-hackapet.git/commitdiff
Run pre-commit and fix issues
authorMelissa LeBlanc-Williams <melissa@adafruit.com>
Tue, 11 Feb 2025 21:31:55 +0000 (13:31 -0800)
committerMelissa LeBlanc-Williams <melissa@adafruit.com>
Tue, 11 Feb 2025 21:31:55 +0000 (13:31 -0800)
LICENSES/BSD-3-Clause.txt [new file with mode: 0644]
src/adafruit_blinka/microcontroller/bcm283x/rotaryio.py
src/rotaryio.py

diff --git a/LICENSES/BSD-3-Clause.txt b/LICENSES/BSD-3-Clause.txt
new file mode 100644 (file)
index 0000000..c8e1a24
--- /dev/null
@@ -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.
+
index c4c54c330c14784760e28b50338c433e8e516649..d7017ff81b6938a172e385c8c25ff9994ede6f22 100644 (file)
@@ -1,6 +1,7 @@
 # SPDX-FileCopyrightText: 2025 Melissa LeBlanc-Williams for Adafruit Industries
 #
 # SPDX-License-Identifier: MIT
 # 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
 ===========================================================
 """
 `rotaryio` - Support for reading rotation sensors
 ===========================================================
@@ -12,16 +13,20 @@ Raspberry Pi PIO implementation
 """
 
 from __future__ import annotations
 """
 
 from __future__ import annotations
-import microcontroller
 import array
 import array
+import microcontroller
+
 try:
     import adafruit_pioasm
     from adafruit_rp1pio import StateMachine
 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
 
 _n_read = 17
-_program = adafruit_pioasm.Program("""
+_program = adafruit_pioasm.Program(
+    """
 ;
 ; Copyright (c) 2023 Raspberry Pi (Trading) Ltd.
 ;
 ;
 ; 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
 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")
 
 
 _zero_y = adafruit_pioasm.assemble("set y 0")
 
+
 class IncrementalEncoder:
     """
     IncrementalEncoder determines the relative rotational position based on two series of
 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."""
         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(
 
         try:
             self._sm = StateMachine(
@@ -145,16 +152,17 @@ class IncrementalEncoder:
                 auto_push=True,
                 push_threshold=32,
                 in_shift_right=False,
                 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 "
             )
         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
                 ) 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
 
         self.divisor = divisor
         self._position = 0
 
@@ -175,24 +183,10 @@ class IncrementalEncoder:
 
     @property
     def position(self):
 
     @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
         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
index d81ae5c511cbdd362e4eb879f9a53d6e5d2012d4..b8bf4cb6451c2c160ece506a63c5426a3f3cc85a 100644 (file)
@@ -11,12 +11,16 @@ See `CircuitPython:rotaryio` in CircuitPython for more details.
 
 from adafruit_blinka.agnostic import detector
 
 
 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:
 # 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")
 else:
     # For non-Linux Boards, threading likely will work in the same way
     raise NotImplementedError("Board not supported")