automatic user offset reduction for large brightness changes

This commit is contained in:
Alessandro Mauri 2024-10-06 16:35:20 +02:00
parent 28fbf1ba8b
commit 86538e9e41

View file

@ -33,35 +33,41 @@ with open('/sys/class/backlight/intel_backlight/max_brightness', 'r') as f:
max_brightness = int(f.read())
min_brightness = int(max_brightness * 0.05)
# loop delay in seconds
delay = 3
lum = [get_luminance()]*4
user_offset = 0
brightness_target = min_brightness
brightness_old = min_brightness
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_old = brightness_target
brightness_target = max(int(luminance_percent * max_brightness), min_brightness)
brightness_delta = abs((brightness_target - brightness_old)/brightness_old)
brightness_adjusted = min(max(brightness_target+user_offset, min_brightness), max_brightness)
# if the brightness changes by more than 10% reduce the user offset, scaling
# with the delta, this accounts for large changes in brightness no longer
# corresponding to the adjustments that the user made to the brightness
if brightness_delta > 0.1:
user_offset = int(user_offset * max(min(1-(brightness_delta*1.2), 1), 0))
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)
time.sleep(delay)
# 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)
#print("brightness target: ", brightness_target, "user offset: ", user_offset, "brightness adjusted: ", brightness_adjusted)