67 lines
2.1 KiB
Python
Executable file
67 lines
2.1 KiB
Python
Executable file
#!/usr/bin/python
|
|
|
|
# Simple daemon to change brightness based on luminance
|
|
# TODO: add a config
|
|
# TODO: add debug option
|
|
# TODO: add dry-run
|
|
# TODO automatically find the right brightness path and do not assume intel_backlight
|
|
|
|
import time
|
|
|
|
# Open the luminance sensor device file
|
|
try:
|
|
luminance_file = open('/sys/bus/iio/devices/iio:device0/in_illuminance_raw', 'r')
|
|
except OSError as err:
|
|
print(err)
|
|
exit(1)
|
|
|
|
# max luminance of the screen - 100 (maybe)
|
|
max_luminance = 350
|
|
|
|
# get luminance value, on fail return the max
|
|
def get_luminance():
|
|
try:
|
|
l = int(luminance_file.read())
|
|
luminance_file.seek(0)
|
|
except:
|
|
l = max_luminance
|
|
return l
|
|
|
|
# Get the max brightness
|
|
max_brightness = 100
|
|
with open('/sys/class/backlight/intel_backlight/max_brightness', 'r') as f:
|
|
max_brightness = int(f.read())
|
|
min_brightness = int(max_brightness * 0.05)
|
|
|
|
|
|
lum = [get_luminance()]*4
|
|
user_offset = 0
|
|
while True:
|
|
lum.append(lum.pop(0))
|
|
lum[0] = get_luminance()
|
|
|
|
luminance = sum(lum)/len(lum)
|
|
luminance_percent = min(luminance/max_luminance, 1.0)
|
|
brightness_target = max(int(luminance_percent * max_brightness), min_brightness)
|
|
|
|
brightness_adjusted = min(max(brightness_target+user_offset, min_brightness), max_brightness)
|
|
b = open('/sys/class/backlight/intel_backlight/brightness', 'w')
|
|
b.write(str(brightness_adjusted))
|
|
b.close()
|
|
|
|
time.sleep(3)
|
|
|
|
# if the value of the brightness changed then update the user offset
|
|
# TODO: if the brightness remains stable within a range then apply the full
|
|
# offset, else ease out the offset back to zero.
|
|
# example: full brightness but the user chooses to tune it down, when
|
|
# full brightness is no longer needed tune the offset down
|
|
a = open('/sys/class/backlight/intel_backlight/actual_brightness')
|
|
x = int(a.read()) - brightness_target
|
|
a.close()
|
|
if brightness_target != max_brightness or x < 0:
|
|
user_offset = x
|
|
|
|
print("brightness target: ", brightness_target, "user offset: ", user_offset, "brightness adjusted: ", brightness_adjusted)
|
|
#print(luminance, luminance_percent, brightness)
|
|
|