πŸ”† 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)
Homeautomation system created with Raspberry Pi 3 and Firebase.

Homeautomation System - Raspberry Pi 3 Desenvolvido com Python, Flask com AJAX e Firebase permite o controle local e remoto Itens necessΓ‘rios Raspberr

Joselino Santos 0 Mar 09, 2022
Claussoft Personal Digital Assistant

Claussoft Personal Digital Assistant Install on Linux $ sudo apt update $ sudo apt install espeak ffmpeg libespeak1 portaudio19-dev $ pip install -r r

Christian Clauss 3 Dec 14, 2022
A simple program to make MSI Modern 15 speaker and microphone mute led work.

MSI Modern 15 sound led fixup for linux A simple program to fix the MSI Modern 15 speaker and microphone mute LEDs. Installation Requirements pulsectl

Seyed Danial Movahed 4 Oct 18, 2022
This repository contains all the code and files needed to simulate the notspot quadrupedal robot using Gazebo and ROS.

Notspot robot simulation - Python version This repository contains all the files and code needed to simulate the notspot quadrupedal robot using Gazeb

50 Sep 26, 2022
An IoT Trivia app that shows you how to take a JSON web API such as the opentdb.com API and stream and display it on a FeatherS2 in an OLED display.

CircuitPython IoT Trivia ESP32-S2 OLED Version An IoT Trivia app that shows you how to take a JSON web API such as the opentdb.com API and stream and

Kevin Thomas 1 Nov 27, 2021
The software that powers the sPot: a 4th generation

This code is meant to accompany this project in which a Spotify client is built into an iPod "Classic" from 2004. Everything is meant to run on a Raspberry Pi Zero W.

Guy Dupont 683 Dec 28, 2022
Inykcal is a software written in python for selected E-Paper displays.

Inykcal is a software written in python for selected E-Paper displays. It converts these displays into useful information dashboards. It's open-source, free for personal use, fully modular and user-f

Ace 727 Jan 02, 2023
A module for cross-platform control of the mouse and keyboard in python that is simple to install and use.

PyUserInput PyUserInput is a group project so we've moved the project over to a group organization: https://github.com/PyUserInput/PyUserInput . That

Paul Barton 1k Dec 27, 2022
Terkin is a flexible data logger application for MicroPython and CPython environments.

Terkin Data logging for humans, written in MicroPython. Documentation: https://terkin.org/ Source Code: https://github.com/hiveeyes/terkin-datalogger

hiveeyes 45 Dec 15, 2022
Testing additional addon devices, and their working scripts

ESP32-addon-devices-potpurri Testing additional addon devices, and their micropython working scripts πŸ“‘ List of device addons tested so far Ethernet P

f-caro 0 Nov 26, 2022
Doughskript interpreter for converting simple command sequences into executable Arduino C++ code.

Doughskript interpreter for converting simple command sequences into executable Arduino C++ code.

Svjatoslav 2 Jan 11, 2022
Hook and simulate global mouse events in pure Python

mouse Take full control of your mouse with this small Python library. Hook global events, register hotkeys, simulate mouse movement and clicks, and mu

BoppreH 722 Dec 31, 2022
Open source home automation that puts local control and privacy first.

Home Assistant Open source home automation that puts local control and privacy first. Powered by a worldwide community of tinkerers and DIY enthusiast

Home Assistant 57k Jan 01, 2023
Intel Realsense t265 into Unreal Engine

t265_UE Intel Realsense t265 into Unreal Engine. Windows only, and Livelink plugin is 4.26.2 only at the moment. Might recompile it for different vers

Bjarke Aagaard 30 Jan 02, 2023
Self Driving Car Prototype

Package Delivery Rover πŸš€ This project is a prototype of Self Driving Car. It's based on embedded systems, to meet the current requirement of delivery

Abhishek Pawar 1 Oct 31, 2021
A 3rd party Moonraker component to create timelapse of 3D prints.

A 3rd party Moonraker component to create timelapse of 3D prints.

Mainsail-Crew 166 Dec 26, 2022
A simple Picobot project implemented in Python

Python-Picobot A simple Picobot project implemented in Python About Explanation This is my first programming project. Picobot use rules.txt file which

Shayan Shiravani 0 Apr 03, 2022
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
A refreshed Python toolbox for building complex digital hardware

A refreshed Python toolbox for building complex digital hardware

nMigen 1k Jan 05, 2023
An alternative to Demise-Assistant-Batch made entirely in Python for more capabilities.

Demise-Assistant-Python An alternative to Demise-Assistant-Batch made entirely in Python for more capabilities. IMPORTANT NOTE Demise-Assistant-Batch

SkelOrganisation 1 Nov 24, 2021