Code Monkey home page Code Monkey logo

rpi-backlight's Introduction

rpi-backlight

A Python module for controlling power and brightness of the official Raspberry Pi 7" touch display.

PyPI Python Version Downloads Documentation License Black Build Read the Docs Issues

Example

Note: This GIF was created using the old v1 API, so please don't use it as API reference πŸ™‚

Features

  • Set the display brightness smoothly or abrupt
  • Set the display power on or off
  • Get the current brightness
  • Get the display power state (on/off)
  • Command line interface
  • Graphical user interface

Requirements

  • A Raspberry Pi or ASUS Tinker Board including a correctly assembled 7" touch display v1.1 or higher (look on the display's circuit board to see its version), or a Surface RT running a Linux-based OS
  • Python 3.7+
  • Optional: Raspberry Pi: pygobject for the GUI, already installed on a recent Raspbian
  • Optional: Tinker Board: gir1.2-gtk-3.0 for the GUI install

Installation

Install from PyPI:

$ pip3 install rpi-backlight

Note: Create this udev rule to update permissions, otherwise you'll have to run Python code, the GUI and CLI as root when changing the power or brightness:

$ echo 'SUBSYSTEM=="backlight",RUN+="/bin/chmod 666 /sys/class/backlight/%k/brightness /sys/class/backlight/%k/bl_power"' | sudo tee -a /etc/udev/rules.d/backlight-permissions.rules

Emulator

For testing without a physical display (e.g. on your main Linux/macOS/Windows machine) you can use linusg/rpi-backlight-emulator.

Usage

API

Example in a Python shell:

>>> from rpi_backlight import Backlight
>>>
>>> backlight = Backlight()
>>> backlight.brightness
100
>>> backlight.brightness = 50
>>> backlight.brightness
50
>>>
>>> with backlight.fade(duration=1):
...     backlight.brightness = 0
...
>>> backlight.fade_duration = 0.5
>>> # subsequent `backlight.brightness = x` will fade 500ms
>>>
>>> backlight.power
True
>>> backlight.power = False
>>> backlight.power
False
>>>

For more details see docs.

CLI

Open a terminal and run rpi-backlight.

$ rpi-backlight -b 100
$ rpi-backlight --set-brightness 20 --duration 1.5
$ rpi-backlight --get-brightness
20
$ rpi-backlight --get-power
on
$ rpi-backlight -p off
$ rpi-backlight --get-power
off
$ rpi-backlight --set-power off :emulator:
$ rpi-backlight -p toggle
$ rpi-backlight -p toggle -d 1.5
$

For all available options see docs.

GUI

Open a terminal and run rpi-backlight-gui.

Graphical User Interface Graphical User Interface (2)

Adding a shortcut to the LXDE panel

Panel result

See docs.

Tests

Tests use pytest, install with pip3 install pytest.

Now, run from the repository root directory:

$ python3 -m pytest

Contributing

Please free to open an issue for bug reports and to discuss new features - pull requests for new features or bug fixes are welcome as well!

License

The source code and all other files in this repository are licensed under the MIT license, so you can easily use it in your own projects. See LICENSE for more information.

rpi-backlight's People

Contributors

archisman-panigrahi avatar filmgarage avatar hofei90 avatar j-coopz avatar linusg avatar martin-hipi avatar p1r473 avatar scouttp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

rpi-backlight's Issues

Screen power supply emitting noise

Hey everyone,

I managed to run the code to change screen brightness and every thing works fine.

However for low brightness values (<100) the screen power supply starts emitting some noise.
Any one knows why ?
Could it be related to brightness being the screen turning on et off quickly so the power supply becomes some kind of square signal ?

FileNotFoundException on Bullseye

Hi,

I made a new SD based on Bullseye today and after finally getting it to boot with the touch screen, I am now running into backlight issues because the /sys/class/backlight/rpi_backlight folder does not exist. Apart from this, the screen, including touch, works perfectly normal.

Full trace:

pi@retinapi:~ $ rpi-backlight -b 100
Traceback (most recent call last):
  File "/home/pi/.local/bin/rpi-backlight", line 8, in <module>
    sys.exit(main())
  File "/home/pi/.local/lib/python3.9/site-packages/rpi_backlight/cli.py", line 78, in main
    backlight = Backlight(
  File "/home/pi/.local/lib/python3.9/site-packages/rpi_backlight/__init__.py", line 75, in __init__
    self._max_brightness = self._get_value("max_brightness")  # 255
  File "/home/pi/.local/lib/python3.9/site-packages/rpi_backlight/__init__.py", line 92, in _get_value
    raise e
  File "/home/pi/.local/lib/python3.9/site-packages/rpi_backlight/__init__.py", line 84, in _get_value
    return int((self._backlight_sysfs_path / name).read_text())
  File "/usr/lib/python3.9/pathlib.py", line 1255, in read_text
    with self.open(mode='r', encoding=encoding, errors=errors) as f:
  File "/usr/lib/python3.9/pathlib.py", line 1241, in open
    return io.open(self, mode, buffering, encoding, errors, newline,
  File "/usr/lib/python3.9/pathlib.py", line 1109, in _opener
    return self._accessor.open(self, flags, mode)
FileNotFoundError: [Errno 2] No such file or directory: '/sys/class/backlight/rpi_backlight/max_brightness'

Setting Brightness using integer duration doesn't work

If you set the duration is the set brightness call using an integer, the time.sleep calculation in set brightness ends up as zero most of the time because you're dividing two integers. So duration = 10 results in immediate dimming, but duration = 10.0 results in a dimming over 10 seconds. I think the function should convert the duration to a float, or at least all the examples should use floats for the duration.

Automatic board detection for initiation

Further fleshing out our discussion of automatic board detection/initiation so hopefully we don't need to manually initiate the board, in both init and CLI files.

I suppose if there's a reliable way to detect the board type, we could have automatic detection if no board type is explicitly provided - perhaps by checking the existence of each path in _BACKLIGHT_SYSFS_PATHS?

I was thinking of using /proc/device-tree/model

root@Harbormaster:~# cat /proc/device-tree/model
ASUS Tinker Board 2 (Linux Opensource)

What is the output on the Pi?

GUI on Debian 10: unmet dependencies

I do believe we need on Debian 10 based on my Googling

$ sudo apt-get install libcairo2-dev
$ pip3 install pycairo pygoject

However, on Debian 10 I am unable to update these

root@Harbormaster:/home/linaro/rpi-backlight# sudo apt-get install gir1.2-gtk-3.0 libcairo2-dev
libcairo2-dev : Depends: libcairo2 (= 1.16.0-4+deb10u1) but 1.16.0-4 is to be installed
                 Depends: libcairo-gobject2 (= 1.16.0-4+deb10u1) but 1.16.0-4 is to be installed
E: Unable to correct problems, you have held broken packages.
root@Harbormaster:/home/linaro/rpi-backlight# pip3 install pycairo pygoject
Collecting pycairo
  Using cached https://files.pythonhosted.org/packages/bc/3f/64e6e066d163fbcf13213f9eeda0fc83376243335ea46a66cefd70d62e8f/pycairo-1.20.1.tar.gz
  Installing build dependencies ... done
Collecting pygoject
Could not install packages due to an EnvironmentError: 404 Client Error: Not Found for url: https://pypi.org/simple/pygoject/

I have not changed the readme to reflect as so as I can't get the GUI working on Debian 10 due to these missing dependencies

Without this, trying to launch the GUI has no action. (Spinning wheel cursor, then nothing)

Have you tested on a Pi4 based off Debian 10?

fading fails with float brightness values

try the following

from rpi_backlight import Backlight

backlight = Backlight()<br/>
backlight.fade_duration = 0.5  
backlight.brightness = 0     -- works as expected  
backlight.brightness = 10    -- works as expected
backlight.brightness = 4.2   -- works as expected
backlight.brightness = 4.3   -- DOES NOT WORK AS EXPECTED, takes much more than 0.5sec and brightness goes to max

I guess the problem lies here (rpi_backlight/init.py(194)brightness())

192               while (
193                     0.0 <= current_value
**194                   and current_value != value    
195                     and current_value <= 100.0
196                 ):

my environment: rpi-backlight 2.6.0 on Rabpberry Pi 3B running Linux raspi1 6.1.21-v7+ with Raspberry Original 7inch display v1.1

Bullseye Issue

Hi,
Running in the CLI rpi-backlight produces strange errors. I do not know if due to the fact that Bullseye brings changes internally rbi-backlight does not work with it. Question is if you will be able to quickly make a solution to this issue.

Suggested UDEV rule not triggering?

Running: Raspberry Pi 4 Model B Rev 1.4, Debian GNU/Linus 11 (bullseye), aarch64.
Attached: Raspberry Pi 7" Touchscreen Display

Problem: cmdline rpi-backlight -b 50 returns "Permission Error".
Reason: /sys/class/backlight/10-0045/brightness is owned by root, permissions=644.
Cause: Suggested UDEV rule (SUBSYSTEM=="backlight") is not being triggered. Permissions are not modified upon startup.

How to debug? How to fix?

Cron command?

Hello,

When I enter "rpi-backlight -p off" via ssh my screen turns off. When I enter "rpi-backlight -p on" it turns back on again.

I want to have it do this automatically every day, so I added this to my crontab:

0 23 * * * rpi-backlight -p off

0 12 * * * rpi-backlight -p on

but it's not doing anything. Have I missed something?

possibility to set time in seconds, after which the screen will turn off automatically and wakes up on touch again

i would love the feature to set a time in seconds after which the screen will turn off/fade automatically and if a touch on the screen happens it wakes up again to full or defined brightness... like it happens on every smartphone...

the tool backlight_dimmer (https://github.com/DougieLawson/backlight_dimmer) does something similar but has some bugs ... it just recognizes input from keyboard after some time and not from touchscreen and or mouse ... and it seems like the dev is gone also so no bug fixes :(

Raspbian Buster Lite

Hi.
I'm trying to use rpi-backlight on a raspberry-pi using Raspbian Buster Lite and the official 7" touchscreen.

I got the error /sys/class/backlight/rpi_backlight/bl_power: No such file or directory when I try to power off with rpi-backlight --p off

Directory /sys/class/backlight is empty.

Do I need to install some dependencies?

V2.4 Introduced Failure

I updated from V2.2 to V2.4 on my two Raspberry Pi's (a 3A+ and a 3B+, both running Python 3.7.3) yesterday, and now I get an error message when running

rpi-backlight -b 100

The error message is as follows:

Traceback (most recent call last): File "/home/pi/.local/bin/rpi-backlight", line 10, in <module> sys.exit(main()) File "/home/pi/.local/lib/python3.7/site-packages/rpi_backlight/cli.py", line 104, in main backlight.brightness = args.set_brightness File "/home/pi/.local/lib/python3.7/site-packages/rpi_backlight/__init__.py", line 212, in brightness self._set_value("tinker_mcu_bl", self._denormalize_brightness(value)) File "/home/pi/.local/lib/python3.7/site-packages/rpi_backlight/__init__.py", line 100, in _set_value raise e File "/home/pi/.local/lib/python3.7/site-packages/rpi_backlight/__init__.py", line 96, in _set_value (self._backlight_sysfs_path / name).write_text(str(value)) File "/usr/lib/python3.7/pathlib.py", line 1218, in write_text with self.open(mode='w', encoding=encoding, errors=errors) as f: File "/usr/lib/python3.7/pathlib.py", line 1186, in open opener=self._opener) File "/usr/lib/python3.7/pathlib.py", line 1039, in _opener return self._accessor.open(self, flags, mode) FileNotFoundError: [Errno 2] No such file or directory: '/sys/devices/platform/ff3e0000.i2c/i2c-8/8-0045/tinker_mcu_bl'

I have uninstalled rpi-backlight, rebooted, reinstalled v2.4, rebooted again and the error still pops up when running the above command. It appears as though it's looking for a Tinker board directory when one doesn't exist.

Thank you.

Support for Microsoft Surface RT Tablet

Hi, can you add support for the Microsoft Surface RT Tablet? It supports the RPi OS, and your software works with some tweaks.

The backlight is controlled by /sys/class/backlight/backlight (instead of /sys/class/backlight/rpi_backlight).

I edited that in __init.py__ and it works. However, it would be great if this is included in this software.

Pi3B+ OSError: [Errno 22] when "100" passed to backlight.brightness, value = 258

In Raspberry Pi OS 32-bit (Pi3B+) Python 3.7.3, if I set backlight.brightness = 100, the value passed to the OS is 258 and should be limited to 255.

(self._backlight_sysfs_path / name).write_text(str(value))

This causes an OSError: [Errno 22] Invalid argument error. Maybe it would be a good idea to sanitize the output of _denormalize_brightness?

def _denormalize_brightness(self, value: float) -> int:
return int(round(value * self._max_brightness / 100))

Thanks for your time writing this useful library!
EDIT: Forgot to mention library is 2.3.0

GUI Permissions

Hi Linus!
I have ran the following command:

echo 'SUBSYSTEM=="backlight",RUN+="/bin/chmod 666 /sys/class/backlight/%k/brightness /sys/class/backlight/%k/bl_power /sys/devices/platform/ff150000.i2c/i2c-3/3-0045/tinker_mcu_bl
"' | sudo tee -a /etc/udev/rules.d/backlight-permissions.rules

Note, I ran for both the TinkerBoard and the Pi files, as all files exist on my OS.
This does not fix the permissions for me.

Sidenote, I actually had to replace the %k with rpi_backlight:

echo 'SUBSYSTEM=="backlight",RUN+="/bin/chmod 666 /sys/class/backlight/rpi_backlight/brightness /sys/class/backlight/rpi_backlight/bl_power /sys/devices/platform/ff150000.i2c/i2c-3/3-0045/tinker_mcu_bl
> "' | sudo tee -a /etc/udev/rules.d/backlight-permissions.rules

as I got:

chmod: cannot access '/sys/class/backlight/%k/brightness': No such file or directory
chmod: cannot access '/sys/class/backlight/%k/bl_power': No such file or directory

Just wondering if you have any idea why both the udev rule wouldn't work, nor the %k in that rule?

How to update existing installation

I currently have V2.0.0 installed on 2 Raspberry Pi's. Could you explain how to update these installations to the current release version please.

I've tried the following command:

pip3 install rpi-backlight

but it just returns

Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: rpi-backlight in ./.local/lib/python3.7/site-packages (2.0.0)

Thank you.

ModuleNotFoundError: No module named 'rpi_backlight'

Pasting our email conversation. Will backfill/improve if missing critical information.


Allen

I'm instalilng rpi-backlight on an RPI 3B+ w/ the Buster release and experiencing problems. Have others reported problems, too?

After installation and update to the $SUBSYSTEM variable, I receive the follwing error message:

pi@piconsole:~/.local/lib/python3.7/site-packages/rpi_backlight $ sudo rpi-backlight -n 50
Traceback (most recent call last):
  File "/bin/rpi-backlight", line 6, in <module>
    from rpi_backlight.cli import main
ModuleNotFoundError: No module named 'rpi_backlight'

Please advise.


Linus

Have others reported problems, too?

I'm afraid not. I'll need more info what exactly have you done prior execution, please open an issue on GitHub: https://github.com/linusg/rpi-backlight/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc

For example I'm not sure what the file /bin/rpi-backlight does.


Allen

I can explain the /bin/rpi-backlight.Β  The .profile $PATH didn't (seemingly) recognize/follow theΒ  $HOME/.local/bin path (I don't have .bash_login or .bash_profile config files in ~), so I copied rpi-backlight to the /bin directory.Β Β 

The installation was conducted per the instructions on the github website, i.e., "pip3 install rpi-backlight" (or similar).Β  The SD is new with a fresh, full Raspian install.Β Β 

rpi-backlight off

What is the expected outcome of rpi-backlight off and rpi-backlight on?
I understand I am supposed to be using -p off and -p on

However I mistakenly tried just off and on and found it crashes.
On a Raspberry Pi 4B

rpi-backlight on

Traceback (most recent call last):
  File "/usr/local/bin/rpi-backlight", line 10, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.7/dist-packages/rpi_backlight/cli.py", line 80, in main
    backlight_sysfs_path=args.sysfs_path,
  File "/usr/local/lib/python3.7/dist-packages/rpi_backlight/__init__.py", line 75, in __init__
    self._max_brightness = self._get_value("max_brightness")  # 255
  File "/usr/local/lib/python3.7/dist-packages/rpi_backlight/__init__.py", line 92, in _get_value
    raise e
  File "/usr/local/lib/python3.7/dist-packages/rpi_backlight/__init__.py", line 84, in _get_value
    return int((self._backlight_sysfs_path / name).read_text())
  File "/usr/lib/python3.7/pathlib.py", line 1199, in read_text
    with self.open(mode='r', encoding=encoding, errors=errors) as f:
  File "/usr/lib/python3.7/pathlib.py", line 1186, in open
    opener=self._opener)
  File "/usr/lib/python3.7/pathlib.py", line 1039, in _opener
    return self._accessor.open(self, flags, mode)
FileNotFoundError: [Errno 2] No such file or directory: 'on/max_brightness'

rpi-backlight off

Traceback (most recent call last):
  File "/usr/local/bin/rpi-backlight", line 10, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.7/dist-packages/rpi_backlight/cli.py", line 80, in main
    backlight_sysfs_path=args.sysfs_path,
  File "/usr/local/lib/python3.7/dist-packages/rpi_backlight/__init__.py", line 75, in __init__
    self._max_brightness = self._get_value("max_brightness")  # 255
  File "/usr/local/lib/python3.7/dist-packages/rpi_backlight/__init__.py", line 92, in _get_value
    raise e
  File "/usr/local/lib/python3.7/dist-packages/rpi_backlight/__init__.py", line 84, in _get_value
    return int((self._backlight_sysfs_path / name).read_text())
  File "/usr/lib/python3.7/pathlib.py", line 1199, in read_text
    with self.open(mode='r', encoding=encoding, errors=errors) as f:
  File "/usr/lib/python3.7/pathlib.py", line 1186, in open
    opener=self._opener)
  File "/usr/lib/python3.7/pathlib.py", line 1039, in _opener
    return self._accessor.open(self, flags, mode)
FileNotFoundError: [Errno 2] No such file or directory: 'off/max_brightness'

PermissionError not available in Python lower than 3.3

Not having set up the (recommended) backlight permissions will result in the following error on Python 2.7:

File "/usr/local/lib/python2.7/dist-packages/rpi_backlight.py", line 103, in set_power
    except PermissionError:
NameError: global name 'PermissionError' is not defined

This is because it has just been introduced with Python 3.3.

A possible solution would be to create your own PermissionError class, if it is not yet there. This will however not be thrown, so you also need to catch OSError and check errno for EPERM.

rpi-backligth --power is always false

Hi,
when I run "rpi-backligth --power" as a command it always return FASLE

pi@raspberrypi:~ $ rpi-backlight --actual-brightness
255
pi@raspberrypi:~ $ rpi-backlight --off
pi@raspberrypi:~ $ rpi-backlight --power
False
pi@raspberrypi:~ $ rpi-backlight --on
pi@raspberrypi:~ $ rpi-backlight --power
False
pi@raspberrypi:~ $

Version 2

Since this project now has a few users (and watchers) I figured I'd ask for some feedback before doing this.

I'm planning a version 2 of this library with a completely new, more pythonic, less awkward API.

Support for Python 2 will be dropped, the new min. required Python version will be 3.5, which is included in the previous Raspbian Stretch (Buster ships with 3.7). This allows adding stuff like type annotations (which I already added, but never released to maintain backwards compatibility).

The API will look like this:

>>> from rpi_backlight import Backlight
>>> 
>>> backlight = Backlight()
>>> backlight.brightness
100
>>> backlight.brightness = 50
>>> backlight.brightness
50
>>> 
>>> with backlight.fade(duration=1):
...     backlight.brightness = 0
... 
>>> backlight.fade_duration = 0.5
>>> # subsequent `backlight.brightness = x` will fade 500ms
>>> 
>>> backlight.power
True
>>> backlight.power = False
>>> backlight.power
False
>>> 
  • All brightness values will be normalized from 0-100, no more awkward 11 as min value (which I never even was sure about, 10 and below just happened to set the backlight off on mine) and 255 as max value
  • More pythonic properties instead of Java-like getters and setters
  • actual_brightness (the underlying file in the sysfs) is now simply exposed as brightness
  • max_brightness is not exposed any more since it will always be 100, even if other displays are supported in the future of the driver changes
  • Due to the property based backlight brightness setting, fading can now be enabled globally by setting the fade_duration property to a value (in seconds) greater than 0 (default) - or individually using the fade context manager

I'll add some tests as well.

I'm currently writing an Emulator for rpi_backlight, similar to https://sense-emu.readthedocs.io/en/v1.1/sense_emu_gui.html, which will either be detected automatically by the new Backlight class (not sure yet), or can be used explicitly by passing the backlight_sysfs_path argument to the constructor.

The CLI might receive some polishing as well, not sure yet either.

Any feedback is highly appreciated, I'm already implementing this but won't do the final release until the end of next week.

The 1.x version will still be available but receive no updates or bug fixes!

Doesn't work with hdmi on raspberry pi 4

Hi, I install your tool as on README on Raspberry pi 4 ( Raspbian OS)
I want to change brightness on my external monitor, connected through microhdmi
Do you have any ideas, how to fix it ? Thanks!

pi@raspberrypi:~ $ rpi-backlight --set-brightness 20
Traceback (most recent call last):
  File "/home/pi/.local/bin/rpi-backlight", line 10, in <module>
    sys.exit(main())
  File "/home/pi/.local/lib/python3.7/site-packages/rpi_backlight/cli.py", line 62, in main
    backlight = Backlight()
  File "/home/pi/.local/lib/python3.7/site-packages/rpi_backlight/__init__.py", line 39, in __init__
    self._max_brightness = self._get_value("max_brightness")  # 255
  File "/home/pi/.local/lib/python3.7/site-packages/rpi_backlight/__init__.py", line 52, in _get_value
    raise e
  File "/home/pi/.local/lib/python3.7/site-packages/rpi_backlight/__init__.py", line 44, in _get_value
    return int((self._backlight_sysfs_path / name).read_text())
  File "/usr/lib/python3.7/pathlib.py", line 1199, in read_text
    with self.open(mode='r', encoding=encoding, errors=errors) as f:
  File "/usr/lib/python3.7/pathlib.py", line 1186, in open
    opener=self._opener)
  File "/usr/lib/python3.7/pathlib.py", line 1039, in _opener
    return self._accessor.open(self, flags, mode)
FileNotFoundError: [Errno 2] No such file or directory: '/sys/class/backlight/rpi_backlight/max_brightness'

Toggle CLI turns brightness up to 100%

If the screen is on and the command power off is sent, the screen is back to the former brightness when turned on again.

For example:

// Brightness 20%
rpi-backlight --p off //screen blank
rpi-backlight --p on // screen 20%

Toggle does not remember the former brightness:

// Brightness 20%
rpi-backlight --p toggle //screen blank
rpi-backlight --p toggle // screen 100%

Is this intentional? I would prefer the previous setting to be remembered.

Other displays

Are there cheap versions of the display on which the brightness can be controlled using this library?

Hardware switch?

Hi there.

Relative newbie here. Thanks for this app, it's just what I needed, but I was wondering if there was a way of making a hardware non latching switch trigger the toggle on/off cmd? I realise this is a bit off topic for issues with this code...but I'd love any help or pointers anyone might have please. I have a hifiberry hat on, so my GPIO pins are slightly limited?

Thanks very much

gskips

-bash: rpi-backlight: command not found

When typing this:

pi@DALI:~ $ rpi-backlight --get-power

I get this error:

-bash: rpi-backlight: command not found

How can I run this from any directory ?

Allow to change fading time

It would be nice if we could set the duration of the fading process in set_brightness like so:

def set_brightness(value, smooth=True, duration=500):
    # duration in ms
    ...

It's actually the most important part on the TODO list.

Touch 7"

Hello this script work with waveshare touch 7"?

This driver is amazing...! But is there a way to control it remotely?

Thank you so much for this great piece of work!

It runs great on a small touch screen that I built as a clock for my mother who is getting old and keeps forgetting what day it is... I run AppDaemon with Home Assistant on the device. Unfortunately, Home Assistant and AppDaemon are running in dock images and cannot control the host system and hence have no control over the display. Home Assistant would have beentje perfect companion to dim the screen in the evenings... Is there a way to send the commands to the screen remotely? It is not possible to connect with ssh from docker image to host system. MQTT would be fine, for example...

IMG_3819

PermissionError: [Errno 13] Permission denied: '/sys/class/backlight/rpi_backlight/brightness'

Inside Python3 I typed:

>>> backlight.brightness = 50

Result:

Traceback (most recent call last):
File "", line 1, in
File "/home/pi/.local/lib/python3.7/site-packages/rpi_backlight/init.py", line 207, in brightness
self._set_value("brightness", self._denormalize_brightness(value))
File "/home/pi/.local/lib/python3.7/site-packages/rpi_backlight/init.py", line 100, in _set_value
raise e
File "/home/pi/.local/lib/python3.7/site-packages/rpi_backlight/init.py", line 96, in _set_value
(self._backlight_sysfs_path / name).write_text(str(value))
File "/usr/lib/python3.7/pathlib.py", line 1218, in write_text
with self.open(mode='w', encoding=encoding, errors=errors) as f:
File "/usr/lib/python3.7/pathlib.py", line 1186, in open
opener=self._opener)
File "/usr/lib/python3.7/pathlib.py", line 1039, in _opener
return self._accessor.open(self, flags, mode)
PermissionError: [Errno 13] Permission denied: '/sys/class/backlight/rpi_backlight/brightness'

Problem with installing rpi backlight on Volumio.org

Hello

I have installed the latest Volumio and am having trouble installing a button script that can control the official display backlight for Raspberry Pi.

The full tutorial I use is on this forum:
https://community.volumio.org/t/guide-for-setting-up-touchscreen-backlight-control/9666

First I install "Python GPIO", then "Python backlight library" and when installing "sudo pip install rpi_backlight" it encounters an error that I cannot solve. I would like to add that the old version of Volumio from 2 years ago installed without any problems and everything worked as it should.

Sorry for my english but this is google translation.

Below is a photo with this error.
blad

I would like to ask for your help because I care about this function. Thank you. Thomas :)

Command not found

Whenever I try to use rpi-backlight, i get command not found. I'm not too good at linux so I probably skipped a step or did something wrong. Any help would be great.

Trouble with TinkerBoard

Installed with pip3 and created udev Rule.
but get this error when I try to execute the Script:

linaro@tinkerboard:~$ rpi-backlight
Traceback (most recent call last):
File "/home/linaro/.local/bin/rpi-backlight", line 7, in
from rpi_backlight.cli import main
File "/home/linaro/.local/lib/python3.5/site-packages/rpi_backlight/init.py", line 55
f"board_type must be a member of the BoardType enum, got {type(board_type)}"

changing brightness makes rpi crash after a while.

I recently installed rpi-backlight v2.2.0 and i can change the brightness of the original 7" touch screen version v1.1 with a Raspberry pi model 4B + 4 gb. So i'm happy about that. But i noticed when i use rpi-backlight and it's not set to 100% it just crashes after a while. Doesn't matter if it's set at 50, 8, 5, etc %. It can take a few minutes or hour, but it will crash.
Also removing a USB-stick or USB-keyboard results could result in a crash, so the screen or raspberry pi are very unstable. Not when it's set at 100%. I use a 5V/3A power brick, so there would be enough current.

Could you help me?

Thanks

Improvement to gui

If the gui had on and off buttons for the display it would then be perfect.

Raspberry Pi 5

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.

Unable to init server

pi@raspberrypi:~ $ sudo rpi-backlight-gui
Unable to init server: Could not connect: Connection refused
Unable to init server: Could not connect: Connection refused
Segmentation fault

This kind of result.

Functions returning NoneType

I'm getting the following error.

Ill note that I'm not using an 'official' rpi screen. if that matters. I'm using a 7" touch via hdmi

pi@raspberrypi:~ $ sudo python3

Python 3.5.3 (default, Jan 19 2017, 14:11:04)
[GCC 6.3.0 20170124] on linux
Type "help", "copyright", "credits" or "license" for more information.`

>>> import rpi_backlight as bl
>>> bl.set_brightness(255)

Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python3.5/dist-packages/rpi_backlight-1.8.1-py3.5.egg/rpi_backlight.py", line 83, in set_brightness
File "/usr/local/lib/python3.5/dist-packages/rpi_backlight-1.8.1-py3.5.egg/rpi_backlight.py", line 62, in get_max_brightness
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

Change CLI behaviour

It would be more intuitive to have the CLI like this:

  • Set brightness to minimum:

    # rpi-backlight 11
    
  • Set brightness to maximum:

    # rpi-backlight 255
    
  • Power backlight off:

    # rpi-backlight off
    
  • Power backlight on:

    # rpi-backlight on
    
  • Invalid value:

    # rpi-backlight xy
    Usage:
    rpi-backlight [11-255|on|off]
    
  • No value:

    # rpi-backlight
    Minimum brightness: 11
    Maximum brightness: 255
    Actual brightness: 100
    Display on: yes
    

Should be not too hard to implement!

rpi-backlight not working in debian bullseye.

rpi-backlight worked in debian buster, but not on bullseye. Installing gives this notification:

Defaulting to user installation because normal site-packages is not writeable
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting rpi-backlight
Downloading https://www.piwheels.org/simple/rpi-backlight/rpi_backlight-2.4.1-py3-none-any.whl (9.8 kB)
Installing collected packages: rpi-backlight
Successfully installed rpi-backlight-2.4.1

After installation i gave the command:"echo 'SUBSYSTEM=="backlight",RUN+="/bin/chmod 666 /sys/class/backlight/%k/brightness /sys/class/backlight/%k/bl_power"' | sudo tee -a /etc/udev/rules.d/backlight-permissions.rules"
and a reboot, when i give the command backlight -b5 it is saying it doesn't excist.
With rpi-backlight -b5 it is saying:"

Traceback (most recent call last):
File "/home/pi/.local/bin/rpi-backlight", line 8, in
sys.exit(main())
File "/home/pi/.local/lib/python3.9/site-packages/rpi_backlight/cli.py", line 78, in main
backlight = Backlight(
File "/home/pi/.local/lib/python3.9/site-packages/rpi_backlight/init.py", line 75, in init
self._max_brightness = self._get_value("max_brightness") # 255
File "/home/pi/.local/lib/python3.9/site-packages/rpi_backlight/init.py", line 92, in _get_value
raise e
File "/home/pi/.local/lib/python3.9/site-packages/rpi_backlight/init.py", line 84, in _get_value
return int((self._backlight_sysfs_path / name).read_text())
File "/usr/lib/python3.9/pathlib.py", line 1255, in read_text
with self.open(mode='r', encoding=encoding, errors=errors) as f:
File "/usr/lib/python3.9/pathlib.py", line 1241, in open
return io.open(self, mode, buffering, encoding, errors, newline,
File "/usr/lib/python3.9/pathlib.py", line 1109, in _opener
return self._accessor.open(self, flags, mode)
FileNotFoundError: [Errno 2] No such file or directory: '/sys/class/backlight/rpi_backlight/max_brightness'

In /sys/class there is a directory "backlight", but no directory "rpi_backlight". Only a directory "10-0045"
Has this to do with a new video-driver in the debian bullseye?

Thanks in advance.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.