🔆 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)
Turns a compatible Raspberry Pi device into a smart USB drive for PS4/PS5.

PSBerry A WIP project for Raspberry Pi, which turns a compatible RPI device into a smart USB drive for PS4/PS5. Allows for save management of PS4 game

Filip Tomaszewski 2 Jan 15, 2022
Monitor an EnvisaLink alarm module running Honeywell firmware, and set a Nest device to Home/Away depending on whether the alarm is Disarmed/Away.

Nestalarm Monitor an EnvisaLink alarm module running Honeywell firmware, and set a Nest device to Home/Away depending on whether the alarm is Disarmed

1 Dec 30, 2021
Simple Microservice to control 433Mhz wireless sockets over HTTP, e.g. on a RaspberryPi

REST-light is a simple microservice to control 433Mhz wireless sockets over HTTP, e.g. on a RaspberryPi. The main usage is an easy integration of 433M

Pascal Höhnel 1 Jan 09, 2022
Easyeda2kicad.py - Convert any LCSC components (including EasyEDA) to KiCad library

easyeda2kicad.py A Python script that convert any electronic components from LCSC or EasyEDA to a Kicad library Installation git clone https://github.

uPesy Electronics 150 Jan 06, 2023
Aqara Camera G3 integration for Home Assistant

Aqara Camera G3 integration for Home Assistant ATTENTION: The component only works after enabled telnet. Only supportd stream. Not support still image

14 Dec 18, 2022
This is the remake of the program PYOBD. It works on Python3 and all new libraries. It was tested on Linux, Windows, and it should work on MAC too.

This is the remake of the program PYOBD. It works on Python3 and all new libraries. It was tested on Linux, Windows, and it should work on MAC too. You just need an ELM327 USB or bluetooth device and

127 Jan 06, 2023
Raspberry Pi Power Button - Wake/Power Off/Restart(Double Press)

Control Raspberry pi with physically attached button. Wake, Power Off, and Restart (Double Press) . Python3 script runs as a service with easy installation.

Stas Yakobov 16 Oct 22, 2022
An emulated LED scoreboard for Major League Baseball ⚾

An LED scoreboard for Major League Baseball. Displays a live scoreboard for your team's game on that day.

Tyler Porter 8 Apr 08, 2022
MicroPython driver for 74HC595 shift registers

MicroPython 74HC595 A MicroPython library for 74HC595 8-bit shift registers. There's both an SPI version and a bit-bang version, each with a slightly

Mike Causer 17 Nov 29, 2022
A Fear and Greed index visualiser for Bitcoin on a SSD1351 OLED Screen

We're Doomed - A Bitcoin Fear and Greed index OLED visualiser Doom is a first-person-shooter from the 1990s. The health status monitor was one of the

VEEB 19 Dec 29, 2022
View your medication from Medisafe Cloud in Home Assistant

Medisafe View your medication from Medisafe Cloud in Home Assistant. This integration adds sensors for today's upcoming, taken, skipped, and missed do

Sam Steele 12 Dec 27, 2022
Pihole-eink-display - A simple Python script to display PiHole statistics on an eInk Display

Pihole-eink-display - A simple Python script to display PiHole statistics on an eInk Display

Mark McIntyre 64 Oct 11, 2022
Create a low powered, renewable generation forecast display with a Raspberry Pi Zero & Inky wHAT.

GB Renewable Forecast Display This Raspberry Pi powered eInk display aims to give you a quick way to time your home energy usage to help balance the g

Andy Brace 32 Jul 02, 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
Python implementation of ZMP Preview Control approach for biped robot control.

ZMP Preview Control This is the Python implementation of ZMP Preview Control app

Chaobin 24 Dec 19, 2022
Интеграция Home Assistant с ЛК "Интер РАО"

ЕЛК ЖКХ «Интер РАО» для Home Assistant Предоставление информации о текущем состоянии ваших аккаунтов в ЕЛК ЖКХ. Введение @ TODO @ Установка Посредство

Alexander Ryazanov 27 Nov 05, 2022
ArduinoWaterHeaterIOT - IoT Probe of a solar PV water heating system - Arduino, Python, MQTT, MySQL

ArduinoWaterHeaterIOT IoT Probe of a solar PV water heating system - Arduino, Raspberry Pi, Python, MQTT, MySQL The Arduino sends the AC and DC watts

Jacques Fourie 1 Jan 11, 2022
🌱 - WebhookHard◞ Fines Educativos ◟

v1.0.0 WebhookHardware ¿Que es WebhookHardware? WebhookHardware se trata de un proyecto tratado para sacar informacion sobre el hardware de tus victim

3 Jun 14, 2021
Code and build instructions for Snap, a simple Raspberry Pi and LED machine to show you how expensive the electricyty is at the moment

Code and build instructions for Snap, a simple Raspberry Pi and LED machine to show you how expensive the electricyty is at the moment. On row of LEDs shows the cost of the hour, the other row the co

Johan Jonk Stenström 3 Sep 08, 2022
OpenStickFirmware is open source software designed to handle any and all tasks required in a custom Fight Stick

OpenStickFirmware is open source software designed to handle any and all tasks required in a custom Fight Stick. It can handle being the brains of your entire stick, or just handling the bells and wh

Sleep Unit 23 Nov 24, 2022