🔆 A Python module for controlling power and brightness of the official Raspberry Pi 7

Overview

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) running a Linux-based OS
  • Python 3.6+
  • 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.

Comments
  • Add support for Tinker Board

    Add support for Tinker Board

    @linusg Hi, yes, that helps very much. I've been working on this for 2 hours so far and seem to hit a roadblock. I will keep this pull request open this time and we can work in this one, as I closed the last one

    First, I'd like to check in with you and make sure I am following your plan so far.

    Second, I can't seem to get this to work

    if((self._board_type == BoardType.TINKER_BOARD)
    

    Can you see if I am doing something bad with enum? I cant seem to get it to work unless I do

    if((self._board_type.name == "TINKER_BOARD")
    

    BTW, I couldnt use auto for enum, I think it is not supported on my Tinkerboard... Was not able to import it.

    opened by p1r473 19
  • Support for Asus Tinkerboard

    Support for Asus Tinkerboard

    I have added a new function, Toggle() I have also added support for Asus Tinkerboard and Asus Tinkerboard S Maybe you want to add a switch statement so support can be offered for both OS in 1 file. This pull request is in case you like any of my changes.

    opened by p1r473 18
  • This driver is amazing...! But is there a way to control it remotely?

    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

    question 
    opened by filmgarage 12
  • rpi-backlight not working in debian bullseye.

    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.

    question 
    opened by pjmpessers 11
  • Implement automatic board detection

    Implement automatic board detection

    Hi Linus, I have taken a stab at implementation. It works!

    A few points for you to review -I am not sure if I did the enumeration to your liking. -I removed the part in the readme, now that it works out of the box. -After I blacked the files, it rearranged the whitespace on your argument parser -I didnt know if I should put the _get_board in the Backlight class. I'm not very good with OOP, so I was not able to access it with self._get_board(). My guess is because it hasn't been init'd yet.

    Thanks,

    Fixes #30

    opened by p1r473 9
  • Add support for Tinkerboard 2

    Add support for Tinkerboard 2

    Hi Linus, been a while. Hope all is well. I have recently purchased a Tinkerboard 2 and am adding support for it. I'd like you to check over the changes and make sure its implemented how youd like. If not, I am happy to make any changes.

    opened by p1r473 7
  • Screen power supply emitting noise

    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 ?

    question 
    opened by max-circlefade 7
  • Fix for Bullseye new KMS path

    Fix for Bullseye new KMS path

    The path to the backlight is now /sys/class/backlight/10-0045/brightness due to the swap to KMS in Bullseye, which resulted in a file not found error when executing a command.

    This is a quick and dirty fix to dynamically fix the control path.

    opened by tonymorello 6
  • Automatic board detection for initiation

    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

    [email protected]:~# cat /proc/device-tree/model
    ASUS Tinker Board 2 (Linux Opensource)
    

    What is the output on the Pi?

    enhancement 
    opened by p1r473 5
  • GUI Permissions

    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?

    question 
    opened by p1r473 5
  • Fix setting brightness to max value and brightness fading loop condition

    Fix setting brightness to max value and brightness fading loop condition

    • Function _denormalize_brightness does not clamp the output to the normal 0-255 int() range. Force-clamped 0-255 now.
    • Function _normalize_brightness does not clamp the output to the normal 0-100 percent range. Force-clamped 0-100 now.
    • In @brightness.setter, the while loop looks only for current_value != value. In some circumstances, it won't stop incrementing. Hard-clamped to 0-100 percent range.
    opened by Martin-HiPi 4
  • Power on with duration, power on if brightness > 0

    Power on with duration, power on if brightness > 0

    Hi Linus, On my Pi4, I am able to successfully fade with a duration with rpi-backlight -b 0 -d 3. However, I cant power on/off with a duration rpi-backlight -p off -d 3. I can, however, toggle with a duration just fine rpi-backlight -p toggle -d 3 I believe we should be able to power on/off with a duration based on this line in the code, showing the intention that power setting may indeed be set with a duration. parser.error("-p/--set-power may only be used with -d/--duration")

    After checking the code, I found that powering on/off with a duration wasn't actually implemented yet So, I added it as a feature: -Power on/off is now supported with duration

    Additionally, I found that if you set the brightness to 100 while the power is off, nothing happens. So, I added an extra 2 features: -If setting brightness >0, turn the power on first. (This will better the user experience) -If setting brightness to 0, turn the power off once brightness is 0. (Perhaps $ energy savings?)

    I also added a quick check to the code. -Do not try to power on if already on (This will prevent the brightness from setting to 100 if brightness is already turned up (i.e. 40) and you try to power on) which fixes #44

    opened by p1r473 1
  • Toggle CLI turns brightness up to 100%

    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.

    opened by filmgarage 3
Releases(v2.6.0)
Setup DevTerm to be a cool non-GUI device

DevTerm hobby project I bought this amazing device: DevTerm A-0604. It has a beefy ARM processor, runs a custom version of Armbian, embraces Open Sour

Alex Shteinikov 9 Nov 17, 2022
Create (templateable) cameras that display qr codes in homeassistant

QRCam This custom component creates cameras displaying qrcodes. The QRCodes can be static or generated from templates. If you use a template as conten

Jannes Müller 5 Oct 06, 2022
I made this so I can control my Tapo L510 light bulb and Govee H6159 light strip using the PyP100 module and the Govee public API

TAPO-And-Govee-Controller I made this so I can control my Tapo L510 light bulb and Govee H6159 light strip using the PyP100 module and the Govee publi

James Westhead 0 Nov 23, 2021
A modular sequencer based on Pi Pico & EuroPi

PicoSequencer A modular sequencer based on Pi Pico & EuroPi by Zeno Van Moerkerke / Keurslager Kurt For now it is 'only' a trigger sequencer, but I si

5 Oct 27, 2022
Code for the onshape macropad.

Onshape_Macropad Code for the onshape macropad. This is a macropad built using the Pimoroni Keybow and the KPrepublic Enclosure. pimoroni_keybow kprep

Justin Cole 1 Nov 23, 2021
This Home Assistant custom component adds support for controlling Midea dehumidiferes on local network.

This is a custom component for Home assistant that adds support for Midea dehumidifier appliances via the local area network. midea-dehumidifier-lan H

Nenad Bogojevic 97 Jan 08, 2023
Huawei Solar sensors for Home Assistant

Huawei Solar Sensors This integration splits out the various values that are fetched from your Huawei Solar inverter into separate HomeAssistant senso

Thijs Walcarius 151 Dec 31, 2022
Ansible tools for operating and managing fleets of Blinksticks in harmony using the Blinkstick Python library.

Ansible tools for operating and managing fleets of Blinksticks in harmony using the Blinkstick Python library.

Greg Robinson 3 Aug 10, 2022
CircuitPython library for the CH559 USB to Serial chip

CH559 (USB to Serial) CircuitPython Library Why? Because you might want to get keyboard/mouse/gamepad/HID input into your CircuitPython projects witho

Guy Dupont 3 Nov 19, 2022
Programming of Robotics Systems course at the University of Aveiro, Portugal, 2021-2022.

Programação de Sistemas Robóticos Miguel Riem Oliveira Universidade de Aveiro 2021-2022 Projeto AtlasCar Projecto RACE IROS 2014 AtlasCar2 ATOM IROS 2

Miguel Riem de Oliveira 22 Jul 13, 2022
How to configure IOMMU device for nested Proxmox hypervisor (PVE) VM - PCIe Passthrough

Configuring PCIe Passthrough for Nested Virtualization on Proxmox Summary: If you are running bare-metal L0 (level 0) Proxmox (PVE) hypervisor with ne

Travis Johnson 6 Aug 30, 2022
ModbusTCP2MQTT - Sungrow & SMA Solar Inverter addon for Home Assistant

ModbusTCP2MQTT Sungrow & SMA Solar Inverter addon for Home Assistant This addon will connect directly to your Inverter using Modbus TCP. Support model

Teny Smart 40 Dec 21, 2022
Like htop (CPU and memory usage), but for your case LEDs. 😄

Like htop (CPU and memory usage), but for your case LEDs. 😄

Derek Anderson 3 Dec 08, 2021
A python file which I wrote to allow the Dorna Robots API to draw an Image on a 3D plane

Dorna-Robotics-Internship Code In the directory "Code" is a python file which I wrote to allow the Dorna Robots API to draw an Image on a 3D plane. I

Stephen Otto 2 Dec 06, 2021
This Home Assistant custom component adding support for controlling Midea dehumidifiers on local network.

This custom component for Home Assistant adds support for Midea air conditioner and dehumidifier appliances via the local area network. homeassistant-

Nenad Bogojevic 92 Dec 31, 2022
circuitpython version of PyBasic for microcontrollers

cPyBasic Circuitpython version of PyBasic for microcontrollers Current version work only for Adafruit titano & CardKB for now. The origninal PyBasic w

BeBoXoS 3 Nov 14, 2021
For use with an 8-bit parallel TFT touchscreen using micropython

ILI9341-parallel-TFT-driver-for-micropython For use with an 8-bit parallel TFT touchscreen using micropython. Many thanks to prenticedavid and his MCU

3 Aug 02, 2022
Switch predictor for Home Assistant with AppDeamon

Home Assistant AppDeamon - Event predictor WORK IN PROGRESS - CURRENTLY NOT COMPLETE AND NOT WORK This is an idea under development (when I have free

37 Dec 17, 2022
This repo uses a stereo camera and gray-code-based structured light to realize dense 3D reconstruction.

Structured-light-stereo This repo uses a stereo camera and gray-code-based structured light to realize dense 3D reconstruction. . How to use: STEP 1:

FEI 20 Dec 31, 2022
Control DJI Tello with Raspberry Pi and PS4 Controller

Control-DJI-Tello-with-Raspberry-Pi-and-PS4-Controller Demo of this project see

MohammadReza Sharifi 24 Aug 11, 2022