- name: Translate Repo Name For Build Tools filename_prefix
id: repo-name
run: echo "repo-name=Adafruit-Blinka" >> $GITHUB_OUTPUT
- - name: Set up Python 3.7
+ - name: Set up Python 3.8
uses: actions/setup-python@v4
with:
- python-version: 3.7
+ python-version: 3.8
- name: Versions
run: |
python3 --version
--- /dev/null
+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.
--- /dev/null
+# SPDX-FileCopyrightText: 2025 Justin Myers
+#
+# SPDX-License-Identifier: MIT
+[pytest]
+pythonpath = src
if os.path.exists("/proc/device-tree/compatible"):
with open("/proc/device-tree/compatible", "rb") as f:
compat = f.read()
+ # Jetson Nano, TX2, Xavier, etc
if b"nvidia,tegra" in compat:
board_reqs = ["Jetson.GPIO"]
+ # Pi 5
+ elif b"brcm,bcm2712" in compat:
+ board_reqs = [
+ "rpi_ws281x>=4.0.0",
+ "rpi-lgpio",
+ "Adafruit-Blinka-Raspberry-Pi5-Neopixel",
+ ]
# Pi 4 and Earlier
- if (
+ elif (
b"brcm,bcm2835" in compat
or b"brcm,bcm2836" in compat
or b"brcm,bcm2837" in compat
or b"brcm,bcm2711" in compat
):
board_reqs = ["RPi.GPIO", "rpi_ws281x>=4.0.0"]
- # Pi 5
- if b"brcm,bcm2712" in compat:
- board_reqs = ["rpi_ws281x>=4.0.0", "rpi-lgpio"]
- if (
- b"ti,am335x" in compat
- ): # BeagleBone Black, Green, PocketBeagle, BeagleBone AI, etc.
+ # BeagleBone Black, Green, PocketBeagle, BeagleBone AI, etc.
+ elif b"ti,am335x" in compat:
board_reqs = ["Adafruit_BBIO"]
setup(
"pyftdi>=0.40.0",
"adafruit-circuitpython-typing",
"sysv_ipc>=1.1.0;sys_platform=='linux' and platform_machine!='mips'",
+ "toml>=0.10.2;python_version<'3.11'",
]
+ board_reqs,
license="MIT",
* Author(s): cefn
"""
+import os
+
+try:
+ import tomllib
+except ImportError:
+ import toml as tomllib
+
class Enum:
"""
self._locked = False
+def load_settings_toml():
+ """Load values from settings.toml into os.environ, so that os.getenv returns them."""
+ if not os.path.isfile("settings.toml"):
+ raise FileNotFoundError("settings.toml not cound in current directory.")
+
+ print("settings.toml found. Updating environment variables:")
+ with open("settings.toml", "rb") as toml_file:
+ try:
+ settings = tomllib.load(toml_file)
+ except tomllib.TOMLDecodeError as e:
+ raise tomllib.TOMLDecodeError("Error with settings.toml file.") from e
+
+ invalid_types = set()
+ for key, value in settings.items():
+ if not isinstance(value, (bool, int, float, str)):
+ invalid_types.add(type(value).__name__)
+ if invalid_types:
+ invalid_types_string = ", ".join(invalid_types)
+ raise ValueError(
+ f"The types: '{invalid_types_string}' are not supported in settings.toml."
+ )
+
+ for key, value in settings.items():
+ key = str(key)
+ if key in os.environ:
+ print(f" - {key} already exists in environment")
+ continue
+ os.environ[key] = str(value)
+ print(f" - {key} added")
+
+ return settings
+
+
def patch_system():
"""Patch modules that may be different due to the platform."""
# pylint: disable=import-outside-toplevel
--- /dev/null
+# 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
+===========================================================
+See `CircuitPython:rotaryio` in CircuitPython for more details.
+
+Raspberry Pi PIO implementation
+
+* Author(s): Melissa LeBlanc-Williams
+"""
+
+from __future__ import annotations
+import array
+import microcontroller
+
+try:
+ import adafruit_pioasm
+ from adafruit_rp1pio import StateMachine
+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(
+ """
+;
+; Copyright (c) 2023 Raspberry Pi (Trading) Ltd.
+;
+; SPDX-License-Identifier: BSD-3-Clause
+;
+.pio_version 0 // only requires PIO version 0
+
+.program quadrature_encoder
+
+; the code must be loaded at address 0, because it uses computed jumps
+.origin 0
+
+
+; the code works by running a loop that continuously shifts the 2 phase pins into
+; ISR and looks at the lower 4 bits to do a computed jump to an instruction that
+; does the proper "do nothing" | "increment" | "decrement" action for that pin
+; state change (or no change)
+
+; ISR holds the last state of the 2 pins during most of the code. The Y register
+; keeps the current encoder count and is incremented / decremented according to
+; the steps sampled
+
+; the program keeps trying to write the current count to the RX FIFO without
+; blocking. To read the current count, the user code must drain the FIFO first
+; and wait for a fresh sample (takes ~4 SM cycles on average). The worst case
+; sampling loop takes 10 cycles, so this program is able to read step rates up
+; to sysclk / 10 (e.g., sysclk 125MHz, max step rate = 12.5 Msteps/sec)
+
+; 00 state
+ jmp update ; read 00
+ jmp decrement ; read 01
+ jmp increment ; read 10
+ jmp update ; read 11
+
+; 01 state
+ jmp increment ; read 00
+ jmp update ; read 01
+ jmp update ; read 10
+ jmp decrement ; read 11
+
+; 10 state
+ jmp decrement ; read 00
+ jmp update ; read 01
+ jmp update ; read 10
+ jmp increment ; read 11
+
+; to reduce code size, the last 2 states are implemented in place and become the
+; target for the other jumps
+
+; 11 state
+ jmp update ; read 00
+ jmp increment ; read 01
+decrement:
+ ; note: the target of this instruction must be the next address, so that
+ ; the effect of the instruction does not depend on the value of Y. The
+ ; same is true for the "jmp y--" below. Basically "jmp y--, <next addr>"
+ ; is just a pure "decrement y" instruction, with no other side effects
+ jmp y--, update ; read 10
+
+ ; this is where the main loop starts
+.wrap_target
+update:
+ mov isr, y ; read 11
+ push noblock
+
+sample_pins:
+ ; we shift into ISR the last state of the 2 input pins (now in OSR) and
+ ; the new state of the 2 pins, thus producing the 4 bit target for the
+ ; computed jump into the correct action for this state. Both the PUSH
+ ; above and the OUT below zero out the other bits in ISR
+ out isr, 2
+ in pins, 2
+
+ ; save the state in the OSR, so that we can use ISR for other purposes
+ mov osr, isr
+ ; jump to the correct state machine action
+ mov pc, isr
+
+ ; the PIO does not have a increment instruction, so to do that we do a
+ ; negate, decrement, negate sequence
+increment:
+ mov y, ~y
+ jmp y--, increment_cont
+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
+ pulses. It assumes that the encoder’s common pin(s) are connected to ground,and enables
+ pull-ups on pin_a and pin_b.
+
+ Create an IncrementalEncoder object associated with the given pins. It tracks the
+ positional state of an incremental rotary encoder (also known as a quadrature encoder.)
+ Position is relative to the position when the object is constructed.
+ """
+
+ def __init__(
+ self, pin_a: microcontroller.Pin, pin_b: microcontroller.Pin, divisor: int = 4
+ ):
+ """Create an incremental encoder on pin_a and the next higher pin
+
+ 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")
+
+ try:
+ self._sm = StateMachine(
+ _program.assembled,
+ frequency=0,
+ init=_zero_y,
+ first_in_pin=pin_a,
+ in_pin_count=2,
+ pull_in_pin_up=0x3,
+ auto_push=True,
+ push_threshold=32,
+ in_shift_right=False,
+ **_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"
+ ) from e
+ raise
+ self._buffer = array.array("i", [0] * _n_read)
+ self.divisor = divisor
+ self._position = 0
+
+ def deinit(self):
+ """Deinitializes the IncrementalEncoder and releases any hardware resources for reuse."""
+ self._sm.deinit()
+
+ def __enter__(self) -> IncrementalEncoder:
+ """No-op used by Context Managers."""
+ return self
+
+ def __exit__(self, _type, _value, _traceback):
+ """
+ Automatically deinitializes when exiting a context. See
+ :ref:`lifetime-and-contextmanagers` for more info.
+ """
+ self.deinit()
+
+ @property
+ def position(self):
+ """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
--- /dev/null
+# SPDX-FileCopyrightText: 2025 Melissa LeBlanc-Williams for Adafruit Industries
+#
+# SPDX-License-Identifier: MIT
+"""
+`rotaryio` - Support for reading rotation sensors
+===========================================================
+See `CircuitPython:rotaryio` in CircuitPython for more details.
+
+Generic Threading/DigitalIO implementation for Linux
+
+* Author(s): Melissa LeBlanc-Williams
+"""
+
+from __future__ import annotations
+import threading
+import microcontroller
+import digitalio
+
+# Define the state transition table for the quadrature encoder
+transitions = [
+ 0, # 00 -> 00 no movement
+ -1, # 00 -> 01 3/4 ccw (11 detent) or 1/4 ccw (00 at detent)
+ +1, # 00 -> 10 3/4 cw or 1/4 cw
+ 0, # 00 -> 11 non-Gray-code transition
+ +1, # 01 -> 00 2/4 or 4/4 cw
+ 0, # 01 -> 01 no movement
+ 0, # 01 -> 10 non-Gray-code transition
+ -1, # 01 -> 11 4/4 or 2/4 ccw
+ -1, # 10 -> 00 2/4 or 4/4 ccw
+ 0, # 10 -> 01 non-Gray-code transition
+ 0, # 10 -> 10 no movement
+ +1, # 10 -> 11 4/4 or 2/4 cw
+ 0, # 11 -> 00 non-Gray-code transition
+ +1, # 11 -> 01 1/4 or 3/4 cw
+ -1, # 11 -> 10 1/4 or 3/4 ccw
+ 0, # 11 -> 11 no movement
+]
+
+
+class IncrementalEncoder:
+ """
+ IncrementalEncoder determines the relative rotational position based on two series of
+ pulses. It assumes that the encoder’s common pin(s) are connected to ground,and enables
+ pull-ups on pin_a and pin_b.
+
+ Create an IncrementalEncoder object associated with the given pins. It tracks the
+ positional state of an incremental rotary encoder (also known as a quadrature encoder.)
+ Position is relative to the position when the object is constructed.
+ """
+
+ def __init__(
+ self, pin_a: microcontroller.Pin, pin_b: microcontroller.Pin, divisor: int = 4
+ ):
+ """
+ Create an IncrementalEncoder object associated with the given pins. It tracks the
+ positional state of an incremental rotary encoder (also known as a quadrature encoder.)
+ Position is relative to the position when the object is constructed.
+
+ :param microcontroller.Pin pin_a: The first pin connected to the encoder.
+ :param microcontroller.Pin pin_b: The second pin connected to the encoder.
+ :param int divisor: The number of pulses per encoder step. Default is 4.
+ """
+ self._pin_a = digitalio.DigitalInOut(pin_a)
+ self._pin_a.switch_to_input(pull=digitalio.Pull.UP)
+ self._pin_b = digitalio.DigitalInOut(pin_b)
+ self._pin_b.switch_to_input(pull=digitalio.Pull.UP)
+ self._position = 0
+ self._last_state = 0
+ self._divisor = divisor
+ self._sub_count = 0
+ self._poll_thread = threading.Thread(target=self._polling_loop, daemon=True)
+ self._poll_thread.start()
+
+ def deinit(self):
+ """Deinitializes the IncrementalEncoder and releases any hardware resources for reuse."""
+ self._pin_a.deinit()
+ self._pin_b.deinit()
+ if self._poll_thread.is_alive():
+ self._poll_thread.join()
+
+ def __enter__(self) -> IncrementalEncoder:
+ """No-op used by Context Managers."""
+ return self
+
+ def __exit__(self, _type, _value, _traceback):
+ """
+ Automatically deinitializes when exiting a context. See
+ :ref:`lifetime-and-contextmanagers` for more info.
+ """
+ self.deinit()
+
+ @property
+ def divisor(self) -> int:
+ """The divisor of the quadrature signal. Use 1 for encoders without detents, or encoders
+ with 4 detents per cycle. Use 2 for encoders with 2 detents per cycle. Use 4 for encoders
+ with 1 detent per cycle."""
+ return self._divisor
+
+ @divisor.setter
+ def divisor(self, value: int):
+ self._divisor = value
+
+ @property
+ def position(self) -> int:
+ """The current position in terms of pulses. The number of pulses per rotation is defined
+ by the specific hardware and by the divisor."""
+ return self._position
+
+ @position.setter
+ def position(self, value: int):
+ self._position = value
+
+ def _get_pin_state(self) -> int:
+ """Returns the current state of the pins."""
+ return self._pin_a.value << 1 | self._pin_b.value
+
+ def _polling_loop(self):
+ while True:
+ self._poll_encoder()
+
+ def _poll_encoder(self):
+ # Check the state of the pins
+ # if either pin has changed, update the state
+ new_state = self._get_pin_state()
+ if new_state != self._last_state:
+ self._state_update(new_state)
+ self._last_state = new_state
+
+ def _state_update(self, new_state: int):
+ new_state &= 3
+ index = self._last_state << 2 | new_state
+ sub_increment = transitions[index]
+ self._sub_count += sub_increment
+ if self._sub_count >= self._divisor:
+ self._position += 1
+ self._sub_count = 0
+ elif self._sub_count <= -self._divisor:
+ self._position -= 1
+ self._sub_count = 0
elif board_id == ap_board.RASPBERRY_PI_PICO:
from adafruit_blinka.board.raspberrypi.pico import *
-elif (
- detector.board.RASPBERRY_PI_4B
- or detector.board.RASPBERRY_PI_CM4
- or detector.board.RASPBERRY_PI_CM4S
- or detector.board.RASPBERRY_PI_400
- or detector.board.RASPBERRY_PI_5
-):
+elif detector.board.any_raspberry_pi_4_board or detector.board.any_raspberry_pi_5_board:
from adafruit_blinka.board.raspberrypi.raspi_4b import *
elif detector.board.any_raspberry_pi_40_pin:
* Author(s): ladyada
"""
-# pylint: disable=too-many-boolean-expressions
+# pylint: disable=too-many-boolean-expressions, ungrouped-imports
import sys
from adafruit_blinka.agnostic import detector
if detector.board.any_raspberry_pi:
- from adafruit_blinka.microcontroller.bcm283x import neopixel as _neopixel
+ if detector.board.any_raspberry_pi_5_board:
+ import adafruit_raspberry_pi5_neopixel_write as _neopixel
+ else:
+ from adafruit_blinka.microcontroller.bcm283x import neopixel as _neopixel
elif detector.board.pico_u2if:
from adafruit_blinka.microcontroller.rp2040_u2if import neopixel as _neopixel
elif detector.board.OS_AGNOSTIC_BOARD:
--- /dev/null
+# SPDX-FileCopyrightText: 2025 Melissa LeBlanc-Williams for Adafruit Industries
+#
+# SPDX-License-Identifier: MIT
+"""
+`rotaryio` - Support for reading rotation sensors
+===========================================================
+See `CircuitPython:rotaryio` in CircuitPython for more details.
+
+* Author(s): Melissa LeBlanc-Williams
+"""
+
+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,
+ )
+else:
+ # For non-Linux Boards, threading likely will work in the same way
+ raise NotImplementedError("Board not supported")
--- /dev/null
+# SPDX-FileCopyrightText: 2025 Justin Myers
+#
+# SPDX-License-Identifier: MIT
+import os
+from unittest import mock
+import pytest
+from adafruit_blinka import load_settings_toml
+
+try:
+ import tomllib
+except ImportError:
+ import toml as tomllib
+
+# pylint: disable=no-self-use,unused-argument
+
+CONVERTED_TOML = {
+ "123": 123,
+ "test": "test",
+ "test-hyphen": "test-hyphen",
+ "test_bool": True,
+ "test_number": 123,
+ "test_space": "test space",
+ "test_underscore": "test_underscore",
+ "true": False,
+}
+
+
+INVALID_TOML = b"""
+# strings
+test=test
+"""
+
+
+VALID_TOML = b"""
+# strings
+test="test"
+test_space="test space"
+test_underscore="test_underscore"
+test-hyphen="test-hyphen"
+# number
+test_number=123
+# bool
+test_bool=true
+# other
+123=123
+true=false
+"""
+
+VALID_TOML_WITH_UNSUPPORTED_DATA_DICT = b"""
+# dict
+data = { key_1 = "value", key_2 = "value" }
+"""
+
+VALID_TOML_WITH_UNSUPPORTED_DATA_LIST = b"""
+# list
+numbers = [ 1, 2, 3 ]
+"""
+
+VALID_TOML_WITH_UNSUPPORTED_DATA_MANY = b"""
+# dict
+data = { key_1 = "value", key_2 = "value" }
+
+# list
+numbers = [ 1, 2, 3 ]
+
+[nested]
+test="test"
+"""
+
+VALID_TOML_WITH_UNSUPPORTED_DATA_NESTED = b"""
+[nested]
+test="test"
+"""
+
+
+class TestLoadSettingsToml:
+ @mock.patch("adafruit_blinka.os.path.isfile", mock.Mock(return_value=False))
+ def test_raises_with_no_file(self):
+ with pytest.raises(
+ FileNotFoundError, match="settings.toml not cound in current directory."
+ ):
+ load_settings_toml()
+
+ @mock.patch("adafruit_blinka.os.path.isfile", mock.Mock(return_value=True))
+ @mock.patch("builtins.open", mock.mock_open(read_data=INVALID_TOML))
+ def test_raises_with_invalid_file(self):
+ with pytest.raises(
+ tomllib.TOMLDecodeError, match="Error with settings.toml file."
+ ):
+ load_settings_toml()
+
+ @mock.patch("adafruit_blinka.os.path.isfile", mock.Mock(return_value=True))
+ @mock.patch(
+ "builtins.open", mock.mock_open(read_data=VALID_TOML_WITH_UNSUPPORTED_DATA_DICT)
+ )
+ def test_raises_with_invalid_file_dict(self):
+ with pytest.raises(
+ ValueError, match="The types: 'dict' are not supported in settings.toml."
+ ):
+ load_settings_toml()
+
+ @mock.patch("adafruit_blinka.os.path.isfile", mock.Mock(return_value=True))
+ @mock.patch(
+ "builtins.open", mock.mock_open(read_data=VALID_TOML_WITH_UNSUPPORTED_DATA_LIST)
+ )
+ def test_raises_with_invalid_file_list(self):
+ with pytest.raises(
+ ValueError, match="The types: 'list' are not supported in settings.toml."
+ ):
+ load_settings_toml()
+
+ @mock.patch("adafruit_blinka.os.path.isfile", mock.Mock(return_value=True))
+ @mock.patch(
+ "builtins.open", mock.mock_open(read_data=VALID_TOML_WITH_UNSUPPORTED_DATA_MANY)
+ )
+ def test_raises_with_invalid_file_many(self):
+ with pytest.raises(
+ ValueError,
+ match="The types: 'dict, list' are not supported in settings.toml.",
+ ):
+ load_settings_toml()
+
+ @mock.patch("adafruit_blinka.os.path.isfile", mock.Mock(return_value=True))
+ @mock.patch(
+ "builtins.open",
+ mock.mock_open(read_data=VALID_TOML_WITH_UNSUPPORTED_DATA_NESTED),
+ )
+ def test_raises_with_invalid_file_nested(self):
+ with pytest.raises(
+ ValueError, match="The types: 'dict' are not supported in settings.toml."
+ ):
+ load_settings_toml()
+
+ @mock.patch("adafruit_blinka.os.path.isfile", mock.Mock(return_value=True))
+ @mock.patch("builtins.open", mock.mock_open(read_data=VALID_TOML))
+ @mock.patch.dict(os.environ, {}, clear=True)
+ def test_returns_data(self):
+ for key in CONVERTED_TOML:
+ assert os.getenv(key) is None
+
+ assert load_settings_toml() == CONVERTED_TOML
+
+ for key, value in CONVERTED_TOML.items():
+ assert os.getenv(key) == str(value)