From e79775a482644014e2adfd6df8507a0d2da7beaa Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Tue, 31 Oct 2023 19:20:46 +0000 Subject: [PATCH] Search for common gpiochip device names. Tested so far on a Pi 4 running Bullseye and a Pi 5 running Bookworm. --- blinkt/__init__.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/blinkt/__init__.py b/blinkt/__init__.py index b230d01..552a54c 100644 --- a/blinkt/__init__.py +++ b/blinkt/__init__.py @@ -1,5 +1,6 @@ """Library for the Pimoroni Blinkt! - 8-pixel APA102 LED display.""" import atexit +import glob import time import gpiod @@ -11,7 +12,10 @@ CLK = 24 NUM_PIXELS = 8 BRIGHTNESS = 7 -GPIOCHIP = "/dev/gpiochip4" +RPI_GPIO_LABELS = [ + "pinctrl-rp1", # Pi 5 - Bookworm, /dev/gpiochip4 maybe + "pinctrl-bcm2711" # Pi 4 - Bullseye, /dev/gpiochip1 maybe +] pixels = [[0, 0, 0, BRIGHTNESS]] * NUM_PIXELS @@ -28,6 +32,14 @@ def _exit(): gpio_lines.release() +def get_gpiochip(): + for path in glob.glob("/dev/gpiochip*"): + if gpiod.is_gpiochip_device(path): + if gpiod.Chip(path).get_info().label in RPI_GPIO_LABELS: + return path + raise RuntimeError("Compatible /dev/gpiochipN device not found!") + + def set_brightness(brightness): """Set the brightness of all pixels. @@ -83,7 +95,7 @@ def show(): if not gpio_lines: gpio_lines = gpiod.request_lines( - GPIOCHIP, + get_gpiochip(), consumer="blinkt", config={ DAT: gpiod.LineSettings(direction=Direction.OUTPUT, output_value=Value.INACTIVE),