Last active
October 20, 2019 20:29
-
-
Save incognitojam/8e042fa632beeb85399ac2b501f3db31 to your computer and use it in GitHub Desktop.
XPS 7590 brightness control script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python3 | |
"""Below is a script to watch for changes to the brightness device and change | |
the display brightness using xrandr appropriately. This is useful for | |
laptops with OLED screens, like the XPS 7590, as they don't have a physical | |
backlight. | |
Requires inotify to be installed (sudo pip install inotify). | |
Made by Cameron Clough. | |
""" | |
import inotify.adapters | |
import subprocess | |
import time | |
output='eDP-1' | |
brightness_path='/sys/class/backlight/dell_backlight/brightness' | |
current_target = 1.0 | |
last_target = 1.0 | |
min_brightness = 0.01 | |
max_brightness = 1.05 | |
brightness_range = max_brightness - min_brightness | |
steps = 5 # number of animation steps | |
duration = 0.25 # animation duration in seconds | |
# apply the given screen brightness to the display using xrandr | |
def apply(brightness_pct): | |
global output | |
brightness = min_brightness + brightness_pct * brightness_range | |
subprocess.run(['xrandr', '--output', output, '--brightness', str(brightness)]) | |
time.sleep(duration / steps) | |
# interpolate between two brightness values, start and end | |
def interpolate(start, end, step): | |
global steps | |
delta = end - start | |
return (delta / steps) * step + start | |
# animate to a given brightness value | |
def animate(target): | |
global steps | |
global last_target | |
if target == last_target: | |
return | |
for step in range(steps): | |
brightness = interpolate(last_target, target, float(step) + 1.0) | |
apply(brightness) | |
last_target = target | |
# read the target brightness from the brightness device | |
def read_target(): | |
with open(brightness_path, 'r') as brightness_file: | |
return float(next(brightness_file)) / 15.0 | |
# read the historical target value from the brightness device and apply it | |
def initialise(): | |
global last_target | |
last_target = read_target() | |
apply(last_target) | |
def _main(): | |
global brightness_path | |
# set screen brightness immediately, to match historical setting | |
initialise() | |
# watch for changes to the brightess device | |
i = inotify.adapters.Inotify() | |
i.add_watch(brightness_path) | |
for event in i.event_gen(yield_nones=False): | |
(_, type_names, path, filename) = event | |
if 'IN_MODIFY' not in type_names: | |
continue | |
# animate any changes to the brightness value | |
animate(read_target()) | |
if __name__ == '__main__': | |
_main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment