From 41a35825e11240e5e98cdf5bba0b943c0dbc6d1f Mon Sep 17 00:00:00 2001 From: Alessandro Mauri Date: Sun, 6 Oct 2024 12:47:51 +0200 Subject: [PATCH] first commit --- ambient.py | 67 +++++++++++++++++++++++++++++++++++++++++++++++++ sv/ambientd/run | 3 +++ 2 files changed, 70 insertions(+) create mode 100755 ambient.py create mode 100755 sv/ambientd/run diff --git a/ambient.py b/ambient.py new file mode 100755 index 0000000..51ad8d4 --- /dev/null +++ b/ambient.py @@ -0,0 +1,67 @@ +#!/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) + diff --git a/sv/ambientd/run b/sv/ambientd/run new file mode 100755 index 0000000..39a535f --- /dev/null +++ b/sv/ambientd/run @@ -0,0 +1,3 @@ +#!/bin/sh + +exec ambient.py