Skip to content

Commit

Permalink
Run pre-commit and fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
makermelissa committed Feb 11, 2025
1 parent d21bd2e commit 84a6254
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 29 deletions.
25 changes: 25 additions & 0 deletions LICENSES/BSD-3-Clause.txt
Original file line number Diff line number Diff line change
@@ -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.

48 changes: 21 additions & 27 deletions src/adafruit_blinka/microcontroller/bcm283x/rotaryio.py
Original file line number Diff line number Diff line change
@@ -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
===========================================================
Expand All @@ -12,16 +13,20 @@
"""

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.
;
Expand Down Expand Up @@ -108,10 +113,12 @@
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
Expand All @@ -131,8 +138,8 @@ def __init__(
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(
Expand All @@ -145,16 +152,17 @@ def __init__(
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

Expand All @@ -175,24 +183,10 @@ def __exit__(self, _type, _value, _traceback):

@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
'''
8 changes: 6 additions & 2 deletions src/rotaryio.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@

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")

0 comments on commit 84a6254

Please sign in to comment.