- 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",
+ "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",
- "Adafruit-Blinka-Raspberry-Pi5-Neopixel",
- ]
- 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: 2021 Melissa LeBlanc-Williams for Adafruit Industries
+#
+# SPDX-License-Identifier: MIT
+"""Pin definitions for the Orange Pi 3 LTS."""
+
+from adafruit_blinka.microcontroller.allwinner.h6 import pin
+
+PD26 = pin.PD26
+SDA = pin.PD26
+PD25 = pin.PD25
+SCL = pin.PD25
+PD22 = pin.PD22
+PL2 = pin.PL2
+PL3 = pin.PL3
+PD24 = pin.PD24
+UART3_RX = pin.PD24
+PD18 = pin.PD18
+PD23 = pin.PD23
+UART3_TX = pin.PD23
+PL10 = pin.PL10
+PD15 = pin.PD15
+PD16 = pin.PD16
+PH5 = pin.PH5
+PH6 = pin.PH6
+PD21 = pin.PD21
+PH4 = pin.PH4
+SPI1_CLK = pin.PH4
+PH3 = pin.PH3
+SPI1_CS = pin.PH3
+PL8 = pin.PL8
+
+SCLK = pin.PH4
+MOSI = pin.PH5
+MISO = pin.PH6
--- /dev/null
+# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
+#
+# SPDX-License-Identifier: MIT
+"""Pin definitions for Raspberry Pi 5 models using the BCM2712."""
+
+from adafruit_blinka.microcontroller.bcm2712 import pin
+
+D0 = pin.D0
+D1 = pin.D1
+
+D2 = pin.D2
+SDA = pin.SDA
+D3 = pin.D3
+SCL = pin.SCL
+
+D4 = pin.D4
+D5 = pin.D5
+D6 = pin.D6
+
+D7 = pin.D7
+CE1 = pin.D7
+D8 = pin.D8
+CE0 = pin.D8
+D9 = pin.D9
+MISO = pin.D9
+D10 = pin.D10
+MOSI = pin.D10
+D11 = pin.D11
+SCLK = pin.D11
+SCK = pin.D11
+
+D12 = pin.D12
+D13 = pin.D13
+
+D14 = pin.D14
+TXD = pin.D14
+D15 = pin.D15
+RXD = pin.D15
+# create alias for most of the examples
+TX = pin.D14
+RX = pin.D15
+
+D16 = pin.D16
+D17 = pin.D17
+D18 = pin.D18
+D19 = pin.D19
+MISO_1 = pin.D19
+D20 = pin.D20
+MOSI_1 = pin.D20
+D21 = pin.D21
+SCLK_1 = pin.D21
+SCK_1 = pin.D21
+D22 = pin.D22
+D23 = pin.D23
+D24 = pin.D24
+D25 = pin.D25
+D26 = pin.D26
+D27 = pin.D27
--- /dev/null
+# SPDX-FileCopyrightText: 2025 djkabutar
+# See https://docs.vicharak.in/vicharak_sbcs/vaaman/vaaman-gpio-description for pinout
+#
+# SPDX-License-Identifier: MIT
+"""Pin definitions for the Vicharak Vaaman."""
+
+from adafruit_blinka.microcontroller.rockchip.rk3588 import pin
+
+D2 = pin.GPIO0_B6
+D4 = pin.GPIO0_B5
+D9 = pin.GPIO2_C1
+D10 = pin.GPIO2_B6
+D11 = pin.GPIO2_C0
+D12 = pin.GPIO2_B7
+D13 = pin.GPIO0_C0
+D17 = pin.GPIO1_D0
+D18 = pin.GPIO1_D1
+D19 = pin.GPIO1_D3
+D20 = pin.GPIO1_D2
+D23 = pin.GPIO1_B3
+D29 = pin.ADC_IN1
+D30 = pin.ADC_IN2
+D28 = pin.ADC_IN3
+D27 = pin.ADC_IN4
+
+# UART
+# UART2_M0
+UART2_RX = D2
+UART2_TX = D4
+# UART1_M0
+UART1_RX = D10
+UART1_TX = D12
+UART1_CSTN = D9
+UART1_RSTN = D11
+# UART6_M2
+UART6_RX = D17
+UART6_TX = D18
+# UART4_M0
+UART4_RX = D19
+UART4_TX = D20
+
+# Default UART -> UART2_M0
+UART_RX = UART2_RX
+UART_TX = UART2_TX
+
+# I2C
+# I2C2_M1
+I2C2_SCL = D9
+I2C2_SDA = D11
+# I2C5_M4
+I2C5_SCL = D10
+I2C5_SDA = D12
+# I2C7_M0
+I2C7_SCL = D17
+I2C7_SDA = D18
+# I2C1_M4
+I2C1_SCL = D20
+I2C1_SDA = D19
+
+# Default I2C -> I2C2_M1
+SCL = I2C2_SCL
+SDA = I2C2_SDA
+
+# SPI
+# SPI1_M2
+SPI_MOSI = D18
+SPI_MISO = D17
+SPI_CLK = D20
+SPI_SCLK = SPI_CLK
+SPI_CS0 = D19
+SPI_CS = SPI_CS0
+
+MOSI = D18
+MISO = D17
+SCLK = SPI_CLK
+CS = D19
+
+# PWM
+# PWM0_M1
+PWM0 = D20
+PWM1_M0 = D13
+PWM1_M1 = D19
+
+# ADC
+ADC_IN1 = D29
+ADC_IN2 = D30
+ADC_IN3 = D28
+ADC_IN4 = D27
--- /dev/null
+# SPDX-FileCopyrightText: 2025 djkabutar
+# See https://docs.vicharak.in/vicharak_sbcs/vaaman/vaaman-gpio-description for pinout
+#
+# SPDX-License-Identifier: MIT
+"""Pin definitions for the Vicharak Vaaman."""
+
+from adafruit_blinka.microcontroller.rockchip.rk3399 import pin
+
+D7 = pin.GPIO2_B3
+D8 = pin.GPIO4_C4
+D10 = pin.GPIO4_C3
+D11 = pin.GPIO4_C2
+D12 = pin.GPIO4_A3
+D13 = pin.GPIO4_C6
+D15 = pin.GPIO4_C5
+D16 = pin.GPIO4_D2
+D18 = pin.GPIO4_D4
+D22 = pin.GPIO4_D5
+D27 = pin.GPIO2_A0
+D28 = pin.GPIO2_A1
+D29 = pin.GPIO2_B2
+D31 = pin.GPIO2_B1
+D32 = pin.GPIO3_C0
+D33 = pin.GPIO2_B4
+D35 = pin.GPIO4_A5
+D36 = pin.GPIO4_A4
+D37 = pin.GPIO4_D6
+D38 = pin.GPIO4_A6
+D40 = pin.GPIO4_A7
+
+SDA2 = D27
+SCL2 = D28
+
+SDA6 = D31
+SCL6 = D29
+
+SDA7 = pin.I2C7_SDA
+SCL7 = pin.I2C7_SCL
+
+SDA = SDA2
+SCL = SCL2
+
+UART2_TX = D8
+UART2_RX = D10
+
+UART_TX = UART2_TX
+UART_RX = UART2_RX
+
+PWM0 = pin.PWM0
+PWM1 = pin.PWM1
+
+ADC_IN0 = pin.ADC_IN0
#
# SPDX-License-Identifier: MIT
"""Broadcom BCM2711 pin names"""
-from RPi import GPIO
-from adafruit_blinka.microcontroller.bcm283x.pin import Pin
-GPIO.setmode(GPIO.BCM) # Use BCM pins D4 = GPIO #4
-GPIO.setwarnings(False) # shh!
+# Use RPi.GPIO pins for Raspberry Pi 4
+from adafruit_blinka.microcontroller.generic_linux.rpi_gpio_pin import Pin
D0 = Pin(0)
D1 = Pin(1)
--- /dev/null
+# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
+#
+# SPDX-License-Identifier: MIT
+"""Broadcom BCM2712 pin names"""
+
+# Use lgpio pins for Raspberry Pi 5
+from adafruit_blinka.microcontroller.generic_linux.lgpio_pin import Pin
+
+D0 = Pin(0)
+D1 = Pin(1)
+
+D2 = Pin(2)
+SDA = Pin(2)
+D3 = Pin(3)
+SCL = Pin(3)
+
+D4 = Pin(4)
+D5 = Pin(5)
+D6 = Pin(6)
+
+D7 = Pin(7)
+CE1 = Pin(7)
+D8 = Pin(8)
+CE0 = Pin(8)
+D9 = Pin(9)
+MISO = Pin(9)
+D10 = Pin(10)
+MOSI = Pin(10)
+D11 = Pin(11)
+SCLK = Pin(11) # Raspberry Pi naming
+SCK = Pin(11) # CircuitPython naming
+
+D12 = Pin(12)
+D13 = Pin(13)
+
+D14 = Pin(14)
+TXD = Pin(14)
+D15 = Pin(15)
+RXD = Pin(15)
+
+D16 = Pin(16)
+D17 = Pin(17)
+D18 = Pin(18)
+D19 = Pin(19)
+MISO_1 = Pin(19)
+D20 = Pin(20)
+MOSI_1 = Pin(20)
+D21 = Pin(21)
+SCLK_1 = Pin(21)
+SCK_1 = Pin(21)
+D22 = Pin(22)
+D23 = Pin(23)
+D24 = Pin(24)
+D25 = Pin(25)
+D26 = Pin(26)
+D27 = Pin(27)
+D28 = Pin(28)
+D29 = Pin(29)
+D30 = Pin(30)
+D31 = Pin(31)
+D32 = Pin(32)
+D33 = Pin(33)
+D34 = Pin(34)
+D35 = Pin(35)
+D36 = Pin(36)
+D37 = Pin(37)
+D38 = Pin(38)
+D39 = Pin(39)
+D40 = Pin(40)
+MISO_2 = Pin(40)
+D41 = Pin(41)
+MOSI_2 = Pin(41)
+D42 = Pin(42)
+SCLK_2 = Pin(42)
+SCK_2 = Pin(43)
+D43 = Pin(43)
+D44 = Pin(44)
+D45 = Pin(45)
+
+# ordered as spiId, sckId, mosiId, misoId
+spiPorts = (
+ (0, SCLK, MOSI, MISO),
+ (1, SCLK_1, MOSI_1, MISO_1),
+ (2, SCLK_2, MOSI_2, MISO_2),
+ (3, D3, D2, D1),
+ (4, D7, D6, D5),
+ (5, D15, D14, D13),
+)
+
+# ordered as uartId, txId, rxId
+uartPorts = ((1, TXD, RXD),)
+
+# These are the known hardware I2C ports / pins.
+# For software I2C ports created with the i2c-gpio overlay, see:
+# https://github.com/adafruit/Adafruit_Python_Extended_Bus
+i2cPorts = (
+ (1, SCL, SDA),
+ (0, D1, D0), # both pi 1 and pi 2 i2c ports!
+ (10, D45, D44), # internal i2c bus for the CM4
+)
#
# SPDX-License-Identifier: MIT
"""Broadcom BCM283x pin names"""
-from RPi import GPIO
-GPIO.setmode(GPIO.BCM) # Use BCM pins D4 = GPIO #4
-GPIO.setwarnings(False) # shh!
+# Use RPi.GPIO pins for Raspberry Pi 1-3B+
+from adafruit_blinka.microcontroller.generic_linux.rpi_gpio_pin import Pin
-
-class Pin:
- """Pins dont exist in CPython so...lets make our own!"""
-
- IN = 0
- OUT = 1
- LOW = 0
- HIGH = 1
- PULL_NONE = 0
- PULL_UP = 1
- PULL_DOWN = 2
-
- id = None
- _value = LOW
- _mode = IN
-
- def __init__(self, bcm_number):
- self.id = bcm_number
-
- def __repr__(self):
- return str(self.id)
-
- def __eq__(self, other):
- return self.id == other
-
- def init(self, mode=IN, pull=None):
- """Initialize the Pin"""
- if mode is not None:
- if mode == self.IN:
- self._mode = self.IN
- GPIO.setup(self.id, GPIO.IN)
- elif mode == self.OUT:
- self._mode = self.OUT
- GPIO.setup(self.id, GPIO.OUT)
- else:
- raise RuntimeError("Invalid mode for pin: %s" % self.id)
- if pull is not None:
- if self._mode != self.IN:
- raise RuntimeError("Cannot set pull resistor on output")
- if pull == self.PULL_UP:
- GPIO.setup(self.id, GPIO.IN, pull_up_down=GPIO.PUD_UP)
- elif pull == self.PULL_DOWN:
- GPIO.setup(self.id, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
- else:
- raise RuntimeError("Invalid pull for pin: %s" % self.id)
-
- def value(self, val=None):
- """Set or return the Pin Value"""
- if val is not None:
- if val == self.LOW:
- self._value = val
- GPIO.output(self.id, val)
- elif val == self.HIGH:
- self._value = val
- GPIO.output(self.id, val)
- else:
- raise RuntimeError("Invalid value for pin")
- return None
- return GPIO.input(self.id)
-
-
-# Pi 1B rev1 only?
D0 = Pin(0)
D1 = Pin(1)
--- /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
+"""A Pin class for use with lgpio."""
+
+from pathlib import Path
+import lgpio
+
+
+def _get_gpiochip():
+ """
+ Determines the handle of the GPIO chip device to access.
+
+ iterate through sysfs to find a GPIO chip device with a driver known to be
+ used for userspace GPIO access.
+ """
+ for dev in Path("/sys/bus/gpio/devices").glob("gpiochip*"):
+ drivers = set((dev / "of_node/compatible").read_text().split("\0"))
+ # check if driver names are intended for userspace control
+ if drivers & {
+ "raspberrypi,rp1-gpio",
+ "raspberrypi,bcm2835-gpio",
+ "raspberrypi,bcm2711-gpio",
+ }:
+ return lgpio.gpiochip_open(int(dev.name[-1]))
+ # return chip0 as a fallback
+ return lgpio.gpiochip_open(0)
+
+
+CHIP = _get_gpiochip()
+
+
+class Pin:
+ """Pins dont exist in CPython so...lets make our own!"""
+
+ LOW = 0
+ HIGH = 1
+ OFF = LOW
+ ON = HIGH
+
+ # values of lg mode constants
+ PULL_NONE = 0x80
+ PULL_UP = 0x20
+ PULL_DOWN = 0x40
+ ACTIVE_LOW = 0x02
+
+ # drive mode lg constants
+ OPEN_DRAIN = 0x04
+ IN = 0x0100
+ OUT = 0x0200
+
+ # LG mode constants
+ _LG_ALERT = 0x400
+ _LG_GROUP = 0x800
+ _LG_MODES = IN | OUT | _LG_ALERT | _LG_GROUP
+ _LG_PULLS = PULL_NONE | PULL_UP | PULL_NONE | ACTIVE_LOW
+ _LG_DRIVES = OPEN_DRAIN
+
+ id = None
+ _value = LOW
+ _mode = IN
+
+ # we want exceptions
+ lgpio.exceptions = True
+
+ def __init__(self, bcm_number):
+ self.id = bcm_number
+
+ def __repr__(self):
+ return str(self.id)
+
+ def __eq__(self, other):
+ return self.id == other
+
+ def init(self, mode=IN, pull=None):
+ """Initialize the Pin"""
+ if mode is not None:
+ if mode == Pin.IN:
+ self._mode = Pin.IN
+ self._set_gpio_mode_in()
+ elif mode == self.OUT:
+ self._mode = Pin.OUT
+ Pin._check_result(lgpio.gpio_claim_output(CHIP, self.id, Pin.LOW))
+ else:
+ raise RuntimeError(f"Invalid mode for pin: {self.id}")
+ if pull is not None:
+ if self._mode != Pin.IN:
+ raise RuntimeError("Can only set pull resistor on input")
+ if pull in {Pin.PULL_UP, Pin.PULL_DOWN, Pin.PULL_NONE}:
+ self._set_gpio_mode_in(lflags=pull)
+ else:
+ raise RuntimeError(f"Invalid pull for pin: {self.id}")
+
+ def value(self, val=None):
+ """Set or return the Pin Value"""
+ if val is not None:
+ if val == Pin.LOW:
+ self._value = val
+ Pin._check_result(lgpio.gpio_write(CHIP, self.id, val))
+ elif val == Pin.HIGH:
+ self._value = val
+ Pin._check_result(lgpio.gpio_write(CHIP, self.id, val))
+ else:
+ raise RuntimeError("Invalid value for pin")
+ return None
+ return Pin._check_result(lgpio.gpio_read(CHIP, self.id))
+
+ @staticmethod
+ def _check_result(result):
+ """
+ convert any result other than zero to a text message and pass it back
+ as a runtime exception. Typical usage: use the lgpio call as the
+ argument.
+ """
+ if result < 0:
+ raise RuntimeError(lgpio.error_text(result))
+ return result
+
+ def _set_gpio_mode_in(self, lflags=0):
+ """
+ claim a gpio as input, or modify the flags (PULL_UP, PULL_DOWN, ... )
+ """
+ # This gpio_free may seem redundant, but is required when
+ # changing the line-flags of an already acquired input line
+ try:
+ lgpio.gpio_free(CHIP, self.id)
+ except lgpio.error:
+ pass
+ Pin._check_result(lgpio.gpio_claim_input(CHIP, self.id, lFlags=lflags))
--- /dev/null
+# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries
+#
+# SPDX-License-Identifier: MIT
+
+""" PWMOut Class for lgpio lg library tx_pwm library """
+
+import lgpio
+from adafruit_blinka.microcontroller.generic_linux.lgpio_pin import CHIP
+
+
+class PWMError(IOError):
+ """Base class for PWM errors."""
+
+
+class PWMOut:
+ """Pulse Width Modulation Output Class"""
+
+ def __init__(self, pin, *, frequency=500, duty_cycle=0, variable_frequency=False):
+ if variable_frequency:
+ print("Variable Frequency is not supported, ignoring...")
+ self._pin = pin
+ result = lgpio.gpio_claim_output(CHIP, self._pin.id, lFlags=lgpio.SET_PULL_NONE)
+ if result < 0:
+ raise RuntimeError(lgpio.error_text(result))
+ self._enabled = False
+ self._deinited = False
+ self._period = 0
+ # set frequency
+ self._frequency = frequency
+ # set duty
+ self.duty_cycle = duty_cycle
+ self.enabled = True
+
+ def __del__(self):
+ self.deinit()
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, _exc_type, _exc_val, _exc_tb):
+ self.deinit()
+
+ def deinit(self):
+ """Deinit the PWM."""
+ if not self._deinited:
+ if self.enabled:
+ self._enabled = False # turn off the pwm
+ self._deinited = True
+
+ def _is_deinited(self):
+ """raise Value error if the object has been de-inited"""
+ if self._deinited:
+ raise ValueError(
+ "Object has been deinitialize and can no longer "
+ "be used. Create a new object."
+ )
+
+ @property
+ def period(self):
+ """Get or set the PWM's output period in seconds.
+
+ Raises:
+ PWMError: if an I/O or OS error occurs.
+ TypeError: if value type is not int or float.
+
+ :type: int, float
+ """
+ return 1.0 / self.frequency
+
+ @period.setter
+ def period(self, period):
+ if not isinstance(period, (int, float)):
+ raise TypeError("Invalid period type, should be int or float.")
+
+ self.frequency = 1.0 / period
+
+ @property
+ def duty_cycle(self):
+ """Get or set the PWM's output duty cycle which is the fraction of
+ each pulse which is high. 16-bit
+
+ Raises:
+ PWMError: if an I/O or OS error occurs.
+ TypeError: if value type is not int or float.
+ ValueError: if value is out of bounds of 0.0 to 1.0.
+
+ :type: int, float
+ """
+ return int(self._duty_cycle * 65535)
+
+ @duty_cycle.setter
+ def duty_cycle(self, duty_cycle):
+ if not isinstance(duty_cycle, (int, float)):
+ raise TypeError("Invalid duty cycle type, should be int or float.")
+
+ if not 0 <= duty_cycle <= 65535:
+ raise ValueError("Invalid duty cycle value, should be between 0 and 65535")
+
+ # convert from 16-bit
+ duty_cycle /= 65535.0
+
+ self._duty_cycle = duty_cycle
+ if self._enabled:
+ self.enabled = True # turn on with new values
+
+ @property
+ def frequency(self):
+ """Get or set the PWM's output frequency in Hertz.
+
+ Raises:
+ PWMError: if an I/O or OS error occurs.
+ TypeError: if value type is not int or float.
+
+ :type: int, float
+ """
+
+ return self._frequency
+
+ @frequency.setter
+ def frequency(self, frequency):
+ if not isinstance(frequency, (int, float)):
+ raise TypeError("Invalid frequency type, should be int or float.")
+
+ self._frequency = frequency
+ if self.enabled:
+ self.enabled = True # turn on with new values
+
+ @property
+ def enabled(self):
+ """Get or set the PWM's output enabled state.
+
+ Raises:
+ PWMError: if an I/O or OS error occurs.
+ TypeError: if value type is not bool.
+
+ :type: bool
+ """
+ return self._enabled
+
+ @enabled.setter
+ def enabled(self, value):
+ if not isinstance(value, bool):
+ raise TypeError("Invalid enabled type, should be bool.")
+
+ frequency = self._frequency if value else 0
+ duty_cycle = round(self._duty_cycle * 100)
+ self._enabled = value
+ result = lgpio.tx_pwm(CHIP, self._pin.id, frequency, duty_cycle)
+ if result < 0:
+ raise RuntimeError(lgpio.error_text(result))
+ return result
+
+ # String representation
+ def __str__(self):
+ return (
+ f"pin {self._pin} (freq={self.frequency:f} Hz, duty_cycle="
+ f"{self.duty_cycle}({round(self.duty_cycle / 655.35)}%)"
+ )
--- /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
--- /dev/null
+# SPDX-FileCopyrightText: 2025 Melissa LeBlanc-Williams for Adafruit Industries
+#
+# SPDX-License-Identifier: MIT
+"""A Pin class for use with Rpi.GPIO."""
+
+from RPi import GPIO
+
+GPIO.setmode(GPIO.BCM) # Use BCM pins D4 = GPIO #4
+GPIO.setwarnings(False) # shh!
+
+
+class Pin:
+ """Pins dont exist in CPython so...lets make our own!"""
+
+ IN = 0
+ OUT = 1
+ LOW = 0
+ HIGH = 1
+ PULL_NONE = 0
+ PULL_UP = 1
+ PULL_DOWN = 2
+
+ id = None
+ _value = LOW
+ _mode = IN
+
+ def __init__(self, bcm_number):
+ self.id = bcm_number
+
+ def __repr__(self):
+ return str(self.id)
+
+ def __eq__(self, other):
+ return self.id == other
+
+ def init(self, mode=IN, pull=None):
+ """Initialize the Pin"""
+ if mode is not None:
+ if mode == self.IN:
+ self._mode = self.IN
+ GPIO.setup(self.id, GPIO.IN)
+ elif mode == self.OUT:
+ self._mode = self.OUT
+ GPIO.setup(self.id, GPIO.OUT)
+ else:
+ raise RuntimeError("Invalid mode for pin: %s" % self.id)
+ if pull is not None:
+ if self._mode != self.IN:
+ raise RuntimeError("Cannot set pull resistor on output")
+ if pull == self.PULL_UP:
+ GPIO.setup(self.id, GPIO.IN, pull_up_down=GPIO.PUD_UP)
+ elif pull == self.PULL_DOWN:
+ GPIO.setup(self.id, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
+ else:
+ raise RuntimeError("Invalid pull for pin: %s" % self.id)
+
+ def value(self, val=None):
+ """Set or return the Pin Value"""
+ if val is not None:
+ if val == self.LOW:
+ self._value = val
+ GPIO.output(self.id, val)
+ elif val == self.HIGH:
+ self._value = val
+ GPIO.output(self.id, val)
+ else:
+ raise RuntimeError("Invalid value for pin")
+ return None
+ return GPIO.input(self.id)
# UART
+UART0_TX_M0 = GPIO0_C5
+UART0_RX_M0 = GPIO0_C4
+UART0_TX_M1 = GPIO0_B1
+UART0_RX_M1 = GPIO0_B0
UART0_TX_M2 = GPIO4_A3
UART0_RX_M2 = GPIO4_A4
+UART1_TX_M0 = GPIO2_B7
+UART1_RX_M0 = GPIO2_B6
UART1_TX_M1 = GPIO1_B6
UART1_RX_M1 = GPIO1_B7
+UART1_TX_M2 = GPIO0_D1
+UART1_RX_M2 = GPIO0_D2
UART2_TX_M0 = GPIO0_B5
UART2_RX_M0 = GPIO0_B6
+UART2_TX_M1 = GPIO0_D0
+UART2_RX_M1 = GPIO0_D1
UART2_TX_M2 = GPIO3_B1
UART2_RX_M2 = GPIO3_B2
UART3_TX_M0 = GPIO1_C1
UART3_RX_M0 = GPIO1_C0
UART3_TX_M1 = GPIO3_B5
UART3_RX_M1 = GPIO3_B6
+UART3_TX_M2 = GPIO4_A5
+UART3_RX_M2 = GPIO4_A6
+UART4_TX_M0 = GPIO1_D2
+UART4_RX_M0 = GPIO1_D3
+UART4_TX_M1 = GPIO3_D1
+UART4_RX_M1 = GPIO3_D0
UART4_TX_M2 = GPIO1_B3
UART4_RX_M2 = GPIO1_B2
+UART5_TX_M0 = GPIO4_D5
+UART5_RX_M0 = GPIO4_D4
+UART5_TX_M1 = GPIO3_C4
+UART5_RX_M1 = GPIO3_C5
+UART5_TX_M2 = GPIO2_D5
+UART5_RX_M2 = GPIO2_D4
+UART6_TX_M0 = GPIO2_A7
+UART6_RX_M0 = GPIO2_A6
+UART6_TX_M1 = GPIO1_A1
+UART6_RX_M1 = GPIO1_A0
+UART6_TX_M2 = GPIO1_D0
+UART6_RX_M2 = GPIO1_D1
+UART7_TX_M0 = GPIO2_B5
+UART7_RX_M0 = GPIO2_B4
UART7_TX_M1 = GPIO3_C0
UART7_RX_M1 = GPIO3_C1
UART7_TX_M2 = GPIO1_B5
UART7_RX_M2 = GPIO1_B4
+UART8_TX_M0 = GPIO4_B0
+UART8_RX_M0 = GPIO4_B1
+UART8_TX_M1 = GPIO3_A2
+UART8_RX_M1 = GPIO3_A3
+UART9_TX_M0 = GPIO2_C2
+UART9_RX_M0 = GPIO2_C4
+UART9_TX_M1 = GPIO4_B4
+UART9_RX_M1 = GPIO4_B5
+UART9_TX_M2 = GPIO3_D5
+UART9_RX_M2 = GPIO3_D4
# ordered as uartId, txId, rxId
uartPorts = (
+ (0, UART0_TX_M0, UART0_RX_M0),
+ (0, UART0_TX_M1, UART0_RX_M1),
(0, UART0_TX_M2, UART0_RX_M2),
+ (1, UART1_TX_M0, UART1_RX_M0),
(1, UART1_TX_M1, UART1_RX_M1),
+ (1, UART1_TX_M2, UART1_RX_M2),
(2, UART2_TX_M0, UART2_RX_M0),
+ (2, UART2_TX_M1, UART2_RX_M1),
(2, UART2_TX_M2, UART2_RX_M2),
(3, UART3_TX_M0, UART3_RX_M0),
(3, UART3_TX_M1, UART3_RX_M1),
+ (3, UART3_TX_M2, UART3_RX_M2),
+ (4, UART4_TX_M0, UART4_RX_M0),
+ (4, UART4_TX_M1, UART4_RX_M1),
(4, UART4_TX_M2, UART4_RX_M2),
+ (5, UART5_TX_M0, UART5_RX_M0),
+ (5, UART5_TX_M1, UART5_RX_M1),
+ (5, UART5_TX_M2, UART5_RX_M2),
+ (6, UART6_TX_M0, UART6_RX_M0),
+ (6, UART6_TX_M1, UART6_RX_M1),
+ (6, UART6_TX_M2, UART6_RX_M2),
+ (7, UART7_TX_M0, UART7_RX_M0),
(7, UART7_TX_M1, UART7_RX_M1),
(7, UART7_TX_M2, UART7_RX_M2),
+ (8, UART8_TX_M0, UART8_RX_M0),
+ (8, UART8_TX_M1, UART8_RX_M1),
+ (9, UART9_TX_M0, UART9_RX_M0),
+ (9, UART9_TX_M1, UART9_RX_M1),
+ (9, UART9_TX_M2, UART9_RX_M2),
)
# I2C
+I2C0_SCL_M0 = GPIO0_B3
+I2C0_SDA_M0 = GPIO0_A6
I2C0_SCL_M1 = GPIO4_C5
I2C0_SDA_M1 = GPIO4_C6
+I2C0_SCL_M2 = GPIO0_D1
+I2C0_SDA_M2 = GPIO0_D2
I2C1_SCL_M0 = GPIO0_B5
I2C1_SDA_M0 = GPIO0_B6
+I2C1_SCL_M1 = GPIO0_B0
+I2C1_SDA_M1 = GPIO0_B1
+I2C1_SCL_M2 = GPIO0_D4
+I2C1_SDA_M2 = GPIO0_D5
+I2C1_SCL_M3 = GPIO2_D4
+I2C1_SDA_M3 = GPIO2_D5
I2C1_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C3
+SPI4_CS1_M0 = GPIO1_C4
+
+SPI4_MOSI_M1 = GPIO3_A1
+SPI4_MISO_M1 = GPIO3_A0
+SPI4_SCK_M1 = GPIO3_A2
+SPI4_SCLK_M1 = SPI4_SCK_M1
+SPI4_CS0_M1 = GPIO3_A3
+SPI4_CS1_M1 = GPIO3_A4
+
+SPI4_MOSI_M2 = GPIO1_A1
+SPI4_MISO_M2 = GPIO1_A0
+SPI4_SCK_M2 = GPIO1_A2
+SPI4_SCLK_M2 = SPI4_SCK_M2
+SPI4_CS0_M2 = GPIO1_A3
# ordered as spiId, sckId, mosiId, misoId
spiPorts = (
- (0, SPI0_SCLK_M2, SPI0_MOSI_M2, SPI0_MISO_M2),
+ (0, SPI0_SCLK_M0, SPI0_MOSI_M0, SPI0_MISO_M0),
(0, SPI0_SCLK_M1, SPI0_MOSI_M1, SPI0_MISO_M1),
+ (0, SPI0_SCLK_M2, SPI0_MOSI_M2, SPI0_MISO_M2),
+ (0, SPI0_SCLK_M3, SPI0_MOSI_M3, SPI0_MISO_M3),
+ (1, SPI1_SCLK_M0, SPI1_MOSI_M0, SPI1_MISO_M0),
(1, SPI1_SCLK_M1, SPI1_MOSI_M1, SPI1_MISO_M1),
+ (1, SPI1_SCLK_M2, SPI1_MOSI_M2, SPI1_MISO_M2),
+ (2, SPI2_SCLK_M0, SPI2_MOSI_M0, SPI2_MISO_M0),
+ (2, SPI2_SCLK_M1, SPI2_MOSI_M1, SPI2_MISO_M1),
+ (2, SPI2_SCLK_M2, SPI2_MOSI_M2, SPI2_MISO_M2),
(3, SPI3_SCLK_M0, SPI3_MOSI_M0, SPI3_MISO_M0),
+ (3, SPI3_SCLK_M1, SPI3_MOSI_M1, SPI3_MISO_M1),
+ (3, SPI3_SCLK_M2, SPI3_MOSI_M2, SPI3_MISO_M2),
+ (3, SPI3_SCLK_M3, SPI3_MOSI_M3, SPI3_MISO_M3),
(4, SPI4_SCLK_M0, SPI4_MOSI_M0, SPI4_MISO_M0),
+ (4, S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0
+PWM11_M1 = PWM11_IR_M1
+PWM11_M2 = PWM11_IR_M2
+PWM11_M3 = PWM11_IR_M3
PWM12_M0 = GPIO3_B5
-PWM13_M1 = GPIO4_B6
+PWM12_M1 = GPIO4_B5
PWM13_M0 = GPIO3_B6
+PWM13_M1 = GPIO4_B6
PWM13_M2 = GPIO1_B7
PWM14_M0 = GPIO3_C2
PWM14_M1 = GPIO4_B2
PWM14_M2 = GPIO1_D6
PWM15_IR_M0 = GPIO3_C3
PWM15_IR_M1 = GPIO4_B3
+PWM15_IR_M2 = GPIO1_C6
PWM15_IR_M3 = GPIO1_D7
+PWM15_M0 = PWM15_IR_M0
+PWM15_M1 = PWM15_IR_M1
+PWM15_M2 = PWM15_IR_M2
+PWM15_M3 = PWM15_IR_M3
# SysFS pwm outputs, pwm channel and pin in first tuple
pwmOuts = (
+ ((0, 0), PWM0_M0),
+ ((0, 0), PWM0_M1),
((0, 0), PWM0_M2),
+ ((0, 1), PWM1_M0),
+ ((0, 1), PWM1_M1),
((0, 1), PWM1_M2),
+ ((0, 2), PWM2_M0),
((0, 2), PWM2_M1),
+ ((0, 2), PWM2_M2),
+ ((0, 3), PWM3_IR_M0),
((0, 3), PWM3_IR_M1),
+ ((0, 3), PWM3_IR_M2),
+ ((0, 3), PWM3_IR_M3),
+ ((0, 4), PWM4_M0),
+ ((0, 4), PWM4_M1),
+ ((0, 5), PWM5_M0),
+ ((0, 5), PWM5_M1),
((0, 5), PWM5_M2),
+ ((0, 6), PWM6_M0),
+ ((0, 6), PWM6_M1),
((0, 6), PWM6_M2),
+ ((0, 7), PWM7_IR_M0),
+ ((0, 7), PWM7_IR_M1),
+ ((0, 7), PWM7_IR_M2),
((0, 7), PWM7_IR_M3),
((0, 8), PWM8_M0),
+ ((0, 8), PWM8_M1),
+ ((0, 8), PWM8_M2),
+ ((0, 9), PWM9_M0),
+ ((0, 9), PWM9_M1),
+ ((0, 9), PWM9_M2),
+ ((0, 10), PWM10_M0),
+ ((0, 10), PWM10_M1),
((0, 10), PWM10_M2),
+ ((0, 11), PWM11_IR_M0),
+ ((0, 11), PWM11_IR_M1),
+ ((0, 11), PWM11_IR_M2),
((0, 11), PWM11_IR_M3),
((0, 12), PWM12_M0),
+ ((0, 12), PWM12_M1),
((0, 13), PWM13_M0),
((0, 13), PWM13_M1),
((0, 13), PWM13_M2),
# SysFS analog inputs, Ordered as analog analogInId, device, and channel
ADC_IN0 = 0
-analogIns = ((ADC_IN0, 0, 4),)
+ADC_IN1 = 1
+ADC_IN2 = 2
+ADC_IN3 = 3
+ADC_IN4 = 4
+ADC_IN5 = 5
+ADC_IN6 = 6
+ADC_IN7 = 7
+analogIns = (
+ (ADC_IN0, 0, 0),
+ (ADC_IN1, 0, 1),
+ (ADC_IN2, 0, 2),
+ (ADC_IN3, 0, 3),
+ (ADC_IN4, 0, 4),
+ (ADC_IN5, 0, 5),
+ (ADC_IN6, 0, 6),
+ (ADC_IN7, 0, 7),
+)
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_5_board:
+ from adafruit_blinka.board.raspberrypi.raspi_5 import *
+
+elif detector.board.any_raspberry_pi_4_board:
from adafruit_blinka.board.raspberrypi.raspi_4b import *
elif detector.board.any_raspberry_pi_40_pin:
elif board_id == ap_board.ORANGE_PI_3:
from adafruit_blinka.board.orangepi.orangepi3 import *
+elif board_id == ap_board.ORANGE_PI_3_LTS:
+ from adafruit_blinka.board.orangepi.orangepi3lts import *
+
elif board_id == ap_board.ORANGE_PI_3B:
from adafruit_blinka.board.orangepi.orangepi3b import *
elif board_id == ap_board.ROCK_PI_E:
from adafruit_blinka.board.radxa.rockpie import *
+elif board_id == ap_board.VAAMAN:
+ from adafruit_blinka.board.vicharak.vaaman import *
+
+elif board_id == ap_board.AXON:
+ from adafruit_blinka.board.vicharak.axon import *
+
elif board_id == ap_board.UDOO_X86:
from adafruit_blinka.board.udoo_x86ultra import *
# By Chip Class
if detector.chip.BCM2XXX:
- if board_id in (
- "RASPBERRY_PI_4B",
- "RASPBERRY_PI_400",
- "RASPBERRY_PI_CM4",
- "RASPBERRY_PI_CM4S",
- "RASPBERRY_PI_5",
- ):
- from adafruit_blinka.microcontroller.bcm2711.pin import *
+ if detector.board.any_raspberry_pi_5_board:
+ from adafruit_blinka.microcontroller.bcm2712.pin import Pin
+ elif detector.board.any_raspberry_pi_4_board:
+ from adafruit_blinka.microcontroller.bcm2711.pin import Pin
else:
from adafruit_blinka.microcontroller.bcm283x.pin import Pin
elif detector.chip.AM33XX:
"""Pins named after their chip name."""
import sys
from adafruit_platformdetect.constants import chips as ap_chip, boards as ap_boards
-from adafruit_blinka.agnostic import board_id, chip_id
+from adafruit_blinka.agnostic import board_id, chip_id, detector
# We intentionally are patching into this namespace so skip the wildcard check.
# pylint: disable=unused-wildcard-import,wildcard-import,ungrouped-imports
elif chip_id == ap_chip.RP2040:
from adafruit_blinka.microcontroller.rp2040.pin import *
elif chip_id == ap_chip.BCM2XXX:
- if board_id in (
- "RASPBERRY_PI_4B",
- "RASPBERRY_PI_400",
- "RASPBERRY_PI_CM4",
- "RASPBERRY_PI_CM4S",
- "RASPBERRY_PI_5",
- ):
+ if detector.board.any_raspberry_pi_5_board:
+ from adafruit_blinka.microcontroller.bcm2712.pin import *
+ elif detector.board.any_raspberry_pi_4_board:
from adafruit_blinka.microcontroller.bcm2711.pin import *
else:
from adafruit_blinka.microcontroller.bcm283x.pin import *
# pylint: disable=too-many-boolean-expressions, ungrouped-imports
import sys
-from adafruit_blinka.agnostic import detector, board_id
+from adafruit_blinka.agnostic import detector
if detector.board.any_raspberry_pi:
- if board_id == "RASPBERRY_PI_5":
+ 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
# pylint: disable=unused-import
-if detector.board.any_raspberry_pi:
- from adafruit_blinka.microcontroller.bcm283x.pwmio.PWMOut import PWMOut
+if detector.board.any_raspberry_pi_5_board:
+ from adafruit_blinka.microcontroller.generic_linux.lgpio_pwmout import PWMOut
+elif detector.board.any_raspberry_pi:
+ # Pi 4 or lower
+ from adafruit_blinka.microcontroller.generic_linux.rpi_gpio_pwmout import PWMOut
elif detector.board.any_bananapi:
from adafruit_blinka.microcontroller.generic_linux.sysfs_pwmout import PWMOut
elif detector.board.any_coral_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)