completed c implementation
This commit is contained in:
parent
d06794b06e
commit
810a7e29b8
5 changed files with 12 additions and 7 deletions
73
ambientd.py
Executable file
73
ambientd.py
Executable file
|
|
@ -0,0 +1,73 @@
|
|||
#!/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 = 400
|
||||
|
||||
# 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)
|
||||
|
||||
# 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)
|
||||
|
||||
# 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(delay)
|
||||
|
||||
# if the value of the brightness changed then update the user offset
|
||||
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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue