linusg / rpi-backlight

🔆 A Python module for controlling power and brightness of the official Raspberry Pi 7" touch display
https://rpi-backlight.readthedocs.io
MIT License
274 stars 32 forks source link

Raspberry Pi 5 #60

Open jamesecc opened 7 months ago

jamesecc commented 7 months ago

I've just installed my official raspberry pi touchscreen onto my new raspberry pi 5. I kept getting this error:

pi@rpi5:~ $ rpi-backlight -b 100
Traceback (most recent call last):
  File "/usr/local/bin/rpi-backlight", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/rpi_backlight/cli.py", line 80, in main
    backlight = Backlight(
                ^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/rpi_backlight/__init__.py", line 82, in __init__
    self._max_brightness = self._get_value("max_brightness")  # 255
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/rpi_backlight/__init__.py", line 99, in _get_value
    raise e
  File "/usr/local/lib/python3.11/dist-packages/rpi_backlight/__init__.py", line 91, in _get_value
    return int((self._backlight_sysfs_path / name).read_text())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/pathlib.py", line 1059, in read_text
    with self.open(mode='r', encoding=encoding, errors=errors) as f:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/pathlib.py", line 1045, in open
    return io.open(self, mode, buffering, encoding, errors, newline)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: '/sys/class/backlight/rpi_backlight/max_brightness'

After some digging around a bit it looks like neither of these two folders exist on my rpi5:

/sys/class/backlight/rpi_backlight /sys/class/backlight/10-0045

Instead, there is a folder called

/sys/class/backlight/4-0045

I was able to get it working when I changed the __init__.py files as follows:

_BACKLIGHT_SYSFS_PATHS = {
    BoardType.RASPBERRY_PI: (
        "/sys/class/backlight/4-0045/"
        if Path("/sys/class/backlight/4-0045/").exists()
        else "/sys/class/backlight/rpi_backlight/"
    ),

Whilst this worked for me, I have no idea how to implement a general solution for all pi models.

c899173-1024 commented 5 months ago

I can confirm this error on the RPI5 which has 2 display/camera ports.

Plugging into Display 1 creates 4-0045 and into Display 0 creates 6-0045 - The 10-0045 seems to be a Pi4 artifact which only has 1 display port.

My messy fix to rpi_backlight-2.6.0 init.py (Tested on Pi5 but not tested on Pi4 or Pi3 with earlier versions of Raspbian) is:

_BACKLIGHT_SYSFS_PATHS = { BoardType.RASPBERRY_PI: ( "/sys/class/backlight/4-0045/" if Path("/sys/class/backlight/4-0045/").exists() else "/sys/class/backlight/6-0045/" if Path("/sys/class/backlight/6-0045/").exists() else "/sys/class/backlight/10-0045/" if Path("/sys/class/backlight/10-0045/").exists() else "/sys/class/backlight/rpi_backlight/" ), BoardType.TINKER_BOARD: "/sys/devices/platform/ff150000.i2c/i2c-3/3-0045/", BoardType.TINKER_BOARD_2: "/sys/devices/platform/ff3e0000.i2c/i2c-8/8-0045/", BoardType.MICROSOFT_SURFACE_RT: "/sys/class/backlight/backlight/", }

This library will probably not work correctly if you plug in 2 displays.

Vizzyy commented 4 months ago

Just ran into the same issue, and came to the same conclusions as the posters above.

linusg commented 1 month ago

If anyone wants to make a PR that would be most welcome :)

There already is dynamic detection of which base directory to use, this should be simple to add.