🔆 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)
Extremely simple PyBadge examples to demonstrate different aspects of CircuitPython using PyBadge hardware.

BeginnerPyBadge I purchased a PyBadge recently. I'm new to hardware. I was surprised how hard it was to find easy examples demonstrating how different

Rubini LaForest 2 Oct 21, 2021
Python Wrapper for Homeassistant's REST API

HomeassistantAPI Python Wrapper for Homeassistant's REST API Please ⭐️ the repo if you find this project useful or cool! Here is a quick example. from

Nate 29 Dec 31, 2022
KIRI - Keyboard Interception, Remapping, and Injection using Raspberry Pi as an HID Proxy.

KIRI - Keyboard Interception, Remapping and Injection using Raspberry Pi as a HID Proxy. Near limitless abilities for a keyboard warrior. Features Sim

Viggo Falster 10 Dec 23, 2022
Sleep Functionality for Adafruit MacroPad RP2040

Adafruit-MacroPad-RP2040 Sleep Functionality for Adafruit MacroPad RP2040 Details This is a modification of AdaFruit project bundle found here specifi

9 Dec 18, 2022
HomeAssistant - Polyaire AirTouch 4 Integration

HomeAssistant - Polyaire AirTouch 4 Integration Custom integration to add an AirTouch 4 AC Controller Installation: Copy contents of custom_components

7 Aug 05, 2022
Modi2-firmware-updater - MODI+ Firmware Updater With Python

MODI+ Firmware Updater 실행 준비 python3(파이썬3.9 혹은 그 이상의 버전)를 컴퓨터에 설치 python3 -m pip

LUXROBO 1 Feb 04, 2022
A Raspberry Pi Pico powered Macro board, like a Streamdeck but cheaper and simpler.

Env-MCRO A Raspberry Pi Pico powered Macro board, like a Streamdeck but cheaper and simpler. (btw this image is a bit outdated, some of the silkscreen

EnviousData 68 Oct 14, 2022
Home Assistant integration for energy consumption data from UK SMETS (Smart) meters using the Hildebrand Glow API.

Hildebrand Glow (DCC) Integration Home Assistant integration for energy consumption data from UK SMETS (Smart) meters using the Hildebrand Glow API. T

Aniket 153 Dec 30, 2022
Home solar infrastructure (with Peimar Inverter) monitoring based on Raspberry Pi 3 B+ using Grafana, InfluxDB, Custom Python Collector and Shelly EM.

raspberry-solar-mon Home solar infrastructure (with Peimar Inverter) monitoring based on Raspberry Pi 3 B+ using Grafana, InfluxDB, Custom Python Coll

cislow 10 Dec 23, 2022
Ha-rpi gpio - Home Assistant Raspberry Pi GPIO Integration

Home Assistant Raspberry Pi GPIO custom integration This is a spin-off from the

Shay Levy 98 Dec 24, 2022
Control the lights of Alienware computers under GNU/Linux systems.

Before requesting support please consider that this software is not actively developed. I created it in 2014 for managing my Alienware M14X-R1 (where

rsm 111 Dec 05, 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
A blender 2.9x addon for managing camera settings

TMG-Camera-Tools A blender 2.9x addon for managing camera settings Tutorial showcasing current features

Mainman002 12 Apr 16, 2022
A refreshed Python toolbox for building complex digital hardware

A refreshed Python toolbox for building complex digital hardware

nMigen 1k Jan 05, 2023
A python script to poll RPi GPIO pins and subscribe and publish their state via MQTT

MQTT-GPIO A python script to poll RPi GPIO pins and subscribe and publish their state via MQTT using TLS. This script is short and meant to be edited

23 Oct 12, 2021
Provide Unifi device info via api to Home Assistant that will give ap sensors

Unifi AP Device info Provide Unifi device info via api to Home Assistant that will give ap sensors

12 Jan 07, 2023
Fener ROS2 package version 2

Fener's ROS2 codes that runs on the vehicle. This node contains basic sensing and actuation nodes for vehicle control. Also example applications will be added.

Muhammed Sezer 1 Jan 18, 2022
A python library written for the raspberry pi.

A python package for using certain components on the raspberry pi.

Builder212 1 Nov 09, 2021
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
🌱 - 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